172 lines
4.4 KiB
JavaScript
172 lines
4.4 KiB
JavaScript
const express = require('express')
|
|
const router = express.Router()
|
|
const db = require('../mongo.js');
|
|
const passport = require('../passport');
|
|
const fs = require('fs-extra');
|
|
|
|
|
|
/**
|
|
* @swagger
|
|
* /provisions:
|
|
* get:
|
|
* description: Get all Provisions (Only admin)
|
|
* summary: Get all Provisions (Only admin)
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: filter
|
|
* in: query
|
|
* required: false
|
|
* type: object
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* - name: skip
|
|
* in: query
|
|
* required: false
|
|
* type: integer
|
|
* - name: limit
|
|
* in: query
|
|
* required: false
|
|
* type: integer
|
|
* responses:
|
|
* 200:
|
|
* description: JSON Array
|
|
*/
|
|
router.get('/', passport.ensureAuthenticatedAndAdmin, async (req, res, next) => {
|
|
|
|
|
|
try {
|
|
let filter = req.query.filter? JSON.parse(req.query.filter) : {};
|
|
if ( filter.isDeleted === undefined ) {
|
|
filter.isDeleted = false;
|
|
}
|
|
|
|
const result = await db.provision.get(filter, req.query.skip, req.query.limit);
|
|
|
|
var out = {
|
|
total: result.total,
|
|
count: result.count
|
|
};
|
|
if ( result.nextSkip && result.nextLimit ) {
|
|
out.nextUrl = new URL(req.protocol + '://' + req.get('Host') + req.baseUrl);
|
|
if ( req.query.filter ) {
|
|
out.nextUrl.searchParams.append("filter", req.query.filter);
|
|
}
|
|
out.nextUrl.searchParams.append("skip", result.nextSkip);
|
|
out.nextUrl.searchParams.append("limit", result.nextLimit);
|
|
}
|
|
out.results = result.results;
|
|
|
|
return res.json(out);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /provisions/{id}:
|
|
* get:
|
|
* description: Get Provision by ID (Only admin)
|
|
* summary: Get a Terraform Provision details by ID (Only admin)
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: id
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* responses:
|
|
* 200:
|
|
* description: Provision
|
|
* 404:
|
|
* description: Not found
|
|
*
|
|
*/
|
|
router.get('/:id', passport.ensureAuthenticatedAndAdmin, async (req, res, next) => {
|
|
try {
|
|
const mongoJob = await db.provision.getById(req.params.id);
|
|
if (!mongoJob){
|
|
return res.status(404).json({"msg": "Not found"});
|
|
}
|
|
return res.json(mongoJob);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
/**
|
|
* @swagger
|
|
* /provisions/{id}:
|
|
* delete:
|
|
* description: Delete Provision by ID (Only admin)
|
|
* summary: Delete a Terraform Provision by ID (Only admin)
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: id
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* responses:
|
|
* 200:
|
|
* description: Provision
|
|
* 404:
|
|
* description: Not found
|
|
*
|
|
*/
|
|
router.delete('/:id', passport.ensureAuthenticatedAndAdmin, async (req, res, next) => {
|
|
try {
|
|
const mongoJob = await db.provision.getById(req.params.id);
|
|
if (!mongoJob){
|
|
return res.status(404).json({"msg": "Not found"});
|
|
}
|
|
const toDestroy = await db.destroy.get({"provId": req.params.id});
|
|
const delDest = await db.destroy.del(toDestroy[0]._id.toString());
|
|
const delProv = await db.provision.del(req.params.id);
|
|
|
|
//Move folder
|
|
fs.moveSync(`/provisions/${mongoJob.scenario}_${req.params.id}`, `/provisions/deleted/${mongoJob.scenario}_${req.params.id}`, { overwrite: true })
|
|
|
|
return res.json({"provision": delProv, "destroy": delDest});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /provisions/{id}/logs:
|
|
* get:
|
|
* description: Get Logs for Provision by ID
|
|
* summary: Get Logs for a Provision by ID
|
|
* produces:
|
|
* - text/plain
|
|
* parameters:
|
|
* - name: id
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* responses:
|
|
* 200:
|
|
* description: Log file
|
|
* 404:
|
|
* description: Not found
|
|
*/
|
|
router.get('/:id/logs', passport.ensureAuthenticated, async (req, res, next) => {
|
|
try {
|
|
const mongoJob = await db.provision.getById(req.params.id);
|
|
if (!mongoJob){
|
|
return res.status(404).json({"msg": "Not found"});
|
|
}
|
|
return res.sendFile(mongoJob.logFile);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
module.exports = router; |