This repository has been archived on 2025-12-25. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
qmi-cloud/server/routes/api-divvy.js
Manuel Romero d5d7a128d2 OKTA login
2024-02-19 15:37:32 +01:00

152 lines
5.4 KiB
JavaScript

const express = require('express');
const router = express.Router();
const db = require('qmi-cloud-common/mongo');
const passport = require('../passport-okta');
import { queues, SYNAPSE_QUEUE } from 'qmi-cloud-common/queues';
/**
* @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 dateNow = new Date();
const now = dateNow.toISOString();
try {
const event = req.body;
let logEvent = event.logEvent || 'DivvyCloud';
if ( event.cloudName === 'QMI Automation' ) {
console.log(`${logEvent} (${now})# event received for subscription (${event.cloudName}) - provision (${event.provID}) -> new status (${event.instanceState})`);
if ( event.provID && event.provID !== 'None' ) {
let provision = await db.provision.getById(event.provID);
if ( provision ) {
var type = "vms";
if ( logEvent.indexOf("RDS")!== -1 ){
type = "db";
}
let id = provision._id.toString();
console.log(`${logEvent} (${now})# provision (${id}) - scenario is (${provision.scenario} - v${provision.scenarioVersion})`);
if ( provision.status === 'provisioned' || provision.status === 'error' ) {
if ( event.instanceState === 'Stopped' ) {
//Check active children provisions
let children = await db.provision.get({ "parent": provision._id, "isDestroyed": false, "isDeleted": false });
if (children.results.length > 0 ) {
children.results.forEach(function(child) {
if ( child.scenario === 'azqmi-synapse') {
queues[SYNAPSE_QUEUE].add("synapse_job", {
provId: child._id,
user: req.user,
tasktype: 'pause'
});
}
});
}
if ( provision.statusVms === 'Stopped' ) {
console.log(`${logEvent} (${now})# provision (${id}) - were already Stopped!`);
} else {
let timeRunning = db.utils.getNewTimeRunning(provision);
let patch = {
"statusVms": "Stopped",
"timeRunning": timeRunning,
"stoppedFrom": dateNow,
"pendingNextAction": null
};
if ( provision.schedule && !provision.schedule.is24x7 ) {
patch["endDateOnSchedule"] = dateNow;
//This is temporary, only to make sure there is value
if ( !provision["startDateOnSchedule"] ) {
patch["startDateOnSchedule"] = dateNow;
}
}
await db.provision.update(id, patch);
console.log(`${logEvent} (${now})# provision (${id}) - changed to Stopped!`);
db.event.add({ provision: provision._id, type: `${type}.stop`, message: `[Divvy] TotalTimeRunning: ${timeRunning} mins` });
}
} else if ( event.instanceState === 'Running' ) {
if ( provision.statusVms === 'Running' ) {
console.log(`${logEvent} (${now})# provision (${id}) - were already Running!`);
} else {
let patch = {
"statusVms": "Running",
"runningFrom": dateNow,
"pendingNextAction": null
};
// This is temporary, only to make sure there are values soon
if ( provision.schedule && !provision.schedule.is24x7 ) {
if ( !provision["startDateOnSchedule"] ) {
patch["startDateOnSchedule"] = dateNow;
patch["endDateOnSchedule"] = dateNow;
}
}
await db.provision.update(id, patch);
console.log(`${logEvent} (${now})# provision (${id}) - changed to Running!`);
db.event.add({ provision: provision._id, type: `${type}.start`, message: `[Divvy] TotalTimeRunning: ${provision.timeRunning} mins` });
}
}
} else {
console.log(`${logEvent} (${now})# provision (${event.provID}) - Scenario not yet 'provisioned'`);
}
} else {
console.log(`${logEvent} (${now})# provision (${event.provID}) - Provision not found.`);
}
} else {
console.log(`${logEvent} (${now})# 'provID' attribute is missing.`);
}
} else {
//console.log(`${logEvent} (${now}): event received for subscription (${event.cloudName}) --> won't be processed`);
}
return res.json(req.body);
} catch (error) {
console.log(`${logEvent} (${now})# error!!!!`, error);
next(error);
}
});
module.exports = router;