299 lines
7.1 KiB
JavaScript
299 lines
7.1 KiB
JavaScript
const express = require('express')
|
|
const router = express.Router()
|
|
const db = require('../mongo.js');
|
|
const passport = require('../passport');
|
|
|
|
import { queues, TF_APPLY_QUEUE, TF_DESTROY_QUEUE } from '../queues';
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/me:
|
|
* get:
|
|
* description: Get all users (Only admin)
|
|
* summary: Get all users (Only admin)
|
|
* produces:
|
|
* - application/json
|
|
* responses:
|
|
* 200:
|
|
* description: User
|
|
*/
|
|
router.get('/', passport.ensureAuthenticatedAndAdmin, async (req, res, next) => {
|
|
try {
|
|
const result = await db.user.get();
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/me:
|
|
* get:
|
|
* description: Get profile logged-in user
|
|
* summary: Get logged-in user
|
|
* produces:
|
|
* - application/json
|
|
* responses:
|
|
* 200:
|
|
* description: User
|
|
*/
|
|
router.get('/me', passport.ensureAuthenticated, async (req, res, next) => {
|
|
try {
|
|
const result = await db.user.getSingle(req.user._id);
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}:
|
|
* get:
|
|
* description: Get profile for an user
|
|
* summary: Get profile for an user
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* produces:
|
|
* - application/json
|
|
* responses:
|
|
* 200:
|
|
* description: User
|
|
*/
|
|
router.get('/:userId', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
try {
|
|
const result = await db.user.getSingle(req.params.userId);
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}:
|
|
* put:
|
|
* description: Update profile for an user
|
|
* summary: Update profile for an user
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* - in: body
|
|
* name: body
|
|
* description: User object
|
|
* required: true
|
|
* produces:
|
|
* - application/json
|
|
* responses:
|
|
* 200:
|
|
* description: User
|
|
*/
|
|
router.put('/:userId', passport.ensureAuthenticatedAndAdmin, async (req, res, next) => {
|
|
|
|
try {
|
|
const result = await db.user.update(req.params.userId, req.body);
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/provisions:
|
|
* post:
|
|
* description: Start a new Terraform provision
|
|
* summary: Start a new Terraform provision
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* - in: body
|
|
* name: body
|
|
* description: Provision object
|
|
* required: true
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* scenario:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: Provision
|
|
*/
|
|
router.post('/:userId/provisions', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
try {
|
|
|
|
req.body.userId = req.params.userId;
|
|
|
|
const mongoJob = await db.provision.add(req.body);
|
|
|
|
queues[TF_APPLY_QUEUE].add("tf_apply_job", {
|
|
scenario: req.body.scenario,
|
|
id: mongoJob._id,
|
|
user: req.user
|
|
});
|
|
|
|
return res.status(200).json(mongoJob);
|
|
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/provisions/{id}:
|
|
* delete:
|
|
* description: Delete Provision by ID
|
|
* summary: Delete a Terraform Provision by ID
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* - name: id
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* responses:
|
|
* 200:
|
|
* description: Provision
|
|
* 404:
|
|
* description: Not found
|
|
*
|
|
*/
|
|
router.delete('/:userId/provisions/:id', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
try {
|
|
const mongoJob = await db.provision.getSingle(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);
|
|
return res.json({"provision": delProv, "destroy": delDest});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/destroyprovisions:
|
|
* post:
|
|
* description: Destroy a Terraform Provision
|
|
* summary: Destroy a Terraform Provision
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* - in: body
|
|
* name: body
|
|
* description: Provision object
|
|
* required: true
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* id:
|
|
* type: string
|
|
* responses:
|
|
* 200:
|
|
* description: Provision
|
|
*/
|
|
router.post('/:userId/destroyprovisions', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
try {
|
|
|
|
req.body.userId = req.params.userId;
|
|
|
|
const mongoJob = await db.provision.getSingle(req.body.id);
|
|
const destroyJob = await db.destroy.add({
|
|
"userId": req.body.userId,
|
|
"provId": mongoJob._id
|
|
});
|
|
|
|
queues[TF_DESTROY_QUEUE].add("tf_destroy_job", {
|
|
scenario: mongoJob.scenario,
|
|
provId: mongoJob._id,
|
|
id: destroyJob._id
|
|
});
|
|
|
|
return res.status(200).json(destroyJob);
|
|
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/provisions:
|
|
* get:
|
|
* description: Get all Provisions for an User
|
|
* summary: Get all Provisions for an User
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* responses:
|
|
* 200:
|
|
* description: JSON Array
|
|
*/
|
|
router.get('/:userId/provisions', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
|
|
try {
|
|
const result = await db.provision.get({"userId": req.params.userId});
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
/**
|
|
* @swagger
|
|
* /users/{userId}/destroyprovisions:
|
|
* get:
|
|
* description: Get all Destroy Provisions for an User
|
|
* summary: Get all Destroy Provisions for an User
|
|
* produces:
|
|
* - application/json
|
|
* parameters:
|
|
* - name: userId
|
|
* in: path
|
|
* type: string
|
|
* required: true
|
|
* responses:
|
|
* 200:
|
|
* description: JSON Array
|
|
*/
|
|
router.get('/:userId/destroyprovisions', passport.ensureAuthenticatedAndIsMe, async (req, res, next) => {
|
|
|
|
try {
|
|
const result = await db.destroy.get({"userId": req.params.userId});
|
|
return res.json(result);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
|
|
module.exports = router; |