91 lines
3.1 KiB
JavaScript
91 lines
3.1 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const db = require('qmi-cloud-common/mongo');
|
|
const passport = require('../passport');
|
|
|
|
/**
|
|
* @swagger
|
|
* /divvy/updates:
|
|
* post:
|
|
* description: DivvyCloud updates webhook
|
|
* tags:
|
|
* - admin
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* produces:
|
|
* - application/json
|
|
* responses:
|
|
* 200:
|
|
* description: Notifications
|
|
*/
|
|
router.post('/updates', passport.ensureAuthenticatedAndAdmin, async (req, res, next) => {
|
|
const now = new Date().toISOString();
|
|
try {
|
|
|
|
let event = req.body;
|
|
|
|
|
|
if ( event.cloudName === 'QMI Automation' ) {
|
|
|
|
console.log(`DivvyCloud (${now}): event received for subscription (${event.cloudName}) --> provId (${event.provID}) - new status (${event.instanceState})`);
|
|
|
|
if ( event.provID && event.provID !== 'None' ) {
|
|
|
|
let provision = await db.provision.getById(event.provID);
|
|
let id = provision._id.toString();
|
|
|
|
if ( provision ) {
|
|
|
|
console.log(`DivvyCloud (${now}): event for scenario (${provision.scenario} - v${provision.scenarioVersion})`);
|
|
|
|
if ( provision.status === 'provisioned' ) {
|
|
|
|
if ( event.instanceState === 'Stopped' ) {
|
|
|
|
if ( provision.statusVms !== 'Stopped' ) {
|
|
let timeRunning = db.utils.getNewTimeRunning(provision);
|
|
await db.provision.update(id, {"statusVms": "Stopped", "timeRunning": timeRunning, "stoppedFrom": new Date(), "pendingNextAction": undefined});
|
|
console.log(`DivvyCloud (${now}): VMs for provision (${id}) are changed to Stopped!`);
|
|
} else {
|
|
console.log(`DivvyCloud (${now}): VMs for provision (${id}) were already Stopped!`);
|
|
}
|
|
|
|
} else if ( event.instanceState === 'Running' ) {
|
|
|
|
if ( provision.statusVms !== 'Running' ) {
|
|
await db.provision.update(id, {"statusVms": "Running", "runningFrom": new Date(), "pendingNextAction": undefined});
|
|
console.log(`DivvyCloud (${now}): VMs for provision (${id}) are changed to Running!`);
|
|
} else {
|
|
console.log(`DivvyCloud (${now}): VMs for provision (${id}) were already Running!`);
|
|
}
|
|
}
|
|
|
|
} else {
|
|
console.log(`DivvyCloud (${now}): provision (${id}) not yet 'provisioned'`);
|
|
}
|
|
|
|
} else {
|
|
console.log(`DivvyCloud (${now}): provision (${event.provID}) not found.`);
|
|
}
|
|
|
|
} else {
|
|
console.log(`DivvyCloud (${now}): 'provID' attribute is missing.`);
|
|
}
|
|
|
|
} else {
|
|
console.log(`DivvyCloud (${now}): event received for subscription (${event.cloudName}) --> won't be processed`);
|
|
}
|
|
|
|
return res.json(req.body);
|
|
|
|
} catch (error) {
|
|
console.log(`DivvyCloud (${now}) error!!!!`, error);
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
module.exports = router; |