Files
qmi-cloud/server/routes/api-provisions.js
2020-05-03 21:53:36 +02:00

156 lines
4.0 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: extras
* in: query
* required: false
* type: object
* content:
* application/json:
* schema:
* type: object
* 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;
}
let extras = req.query.extras? JSON.parse(req.query.extras) : {};
const result = await db.provision.get(filter, extras);
return res.json(result);
} 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;