'use strict';
const axios = require('axios');
const https = require("https");
const SMTP_EMAIL_SENDER = process.env.SMTP_EMAIL_SENDER;
const HOSTNAME_URL = process.env.HOSTNAME_URL || "https://qmicloud.qliktech.com";
const FOOTER = `
`;
async function _doSend(to, subject, htmlText) {
try {
var body = {
"to": to,
"subject": subject,
"html": htmlText
};
await axios({
url: SMTP_EMAIL_SENDER,
method: "post",
data: body,
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
console.log('SendEmail (Qlik SMTP)# message sent to: ' + to);
} catch (err) {
// Handle Error Here
console.log("SendEmail (Qlik SMTP) _doSend error: could not send the email to: " +to);
}
}
function _getCommonDetails(provision, scenario){
var description = decodeURI(scenario.description);
var externalAccess = provision.isExternalAccess? 'Yes' : 'No';
var schedule = "";
if ( !provision.schedule || provision.schedule.is24x7 ) {
schedule = "24x7";
} else if ( provision.schedule && !provision.schedule.is24x7 ) {
schedule = `from ${provision.schedule.localeStartupTime}h until ${provision.schedule.localeShutdownTime}h (${provision.schedule.localTimezone})`;
}
return `
ID: ${provision._id}
VMs Running schedule: ${schedule}
Purpose: ${provision.description}
Scenario: ${scenario.title}
With external access: ${externalAccess}
Description: ${description}
`;
}
function getHtmlScenarioDestroyIn24( provision, scenario, period, warningDays) {
var common = _getCommonDetails(provision,scenario);
return`
Provision '${scenario.title}' inactive more than ${period} days
This scenario will be automatically DESTROYED in ${(warningDays*24)} hours.
If you don't want this to happen, you've got ${(warningDays*24)} hours (from when this email was sent) as a grace period to get back at 'Running' status this provision.
${common}
${FOOTER}
`;
}
function getHtmlScenarioVMsStopped( provision, scenario) {
var common = _getCommonDetails(provision,scenario);
return `
Provision '${scenario.title}'
All VMs for this provision stopped automatically.
${common}
${FOOTER}
`;
}
function getHtmlScenarioWillStopIn24( provision, scenario, period, warningDays ) {
var common = _getCommonDetails(provision,scenario);
return`
Provision '${scenario.title}' - VMs running for ${period} days
This scenario will automatically stop its VMs in ${warningDays*24} hours.
Take action and extend the period ${(period+warningDays)} extra days.
If you don't want the VMs to automatically stop, you've got ${warningDays*24} hours (from when this email was sent) as a grace period to extend this scenario's Running VMs for ${(period+warningDays)} extra days.
${common}
${FOOTER}
`;
}
function getHtmlNewProvision(provision, scenario) {
var htmlint;
if ( provision && provision.outputs ) {
htmlint = `Connection resources
`;
} else {
htmlint = "";
}
for (let key in provision.outputs) {
htmlint += `
${key}
${provision.outputs[key]}
`;
}
var common = _getCommonDetails(provision, scenario);
return `
Scenario '${scenario.title}' successfully provisioned!
${common}
${htmlint}
${FOOTER}
`;
}
function getHtmlErrorProvision(provision, scenario) {
var common = _getCommonDetails(provision, scenario);
return`
Oops! Something didn't work.
Scenario '${scenario.title}' failed during provision.
Please, follow these steps:
- Reach out the person responsible for this scenario for support.
- As soon as it's possible, consider destroy this provision since it's taking valuable cloud resources which might imply relevant cost.
${common}
${FOOTER}
`;
}
function getHtmlDestroyProvision(provision, scenario) {
var common = _getCommonDetails(provision, scenario);
return`
Scenario '${scenario.title}' successfully destroyed!
${common}
${FOOTER}
`;
}
// async..await is not allowed in global scope, must use a wrapper
async function sendProvisionSuccess( provision, scenario ) {
const htmlText = getHtmlNewProvision(provision, scenario);
await _doSend(provision.user.upn, 'QMI Cloud - Provision finished successfully', htmlText);
}
async function sendProvisionError(provision, scenario ) {
const htmlText = getHtmlErrorProvision(provision, scenario);
await _doSend(provision.user.upn, 'QMI Cloud - Provision with errors', htmlText);
}
async function sendDestroyedSuccess(provision, scenario ) {
const htmlText = getHtmlDestroyProvision(provision, scenario);
await _doSend(provision.user.upn, 'QMI Cloud - Provision destroyed successfully', htmlText);
}
async function sendWillStopIn24( provision, scenario, period, warningDays ) {
const htmlText = getHtmlScenarioWillStopIn24( provision, scenario, period, warningDays);
await _doSend(provision.user.upn, `QMI Cloud - VMs will stop in ${warningDays*24} hours`, htmlText);
}
async function sendWillDestroyIn24( provision, scenario, period, warningDays ) {
const htmlText = getHtmlScenarioDestroyIn24( provision, scenario, period, warningDays);
await _doSend(provision.user.upn, `QMI Cloud - Provision will destroy in ${(warningDays*24)} hours`, htmlText);
}
async function sendVMsStopped( provision, scenario ) {
const htmlText = getHtmlScenarioVMsStopped( provision, scenario);
await _doSend(provision.user.upn, 'QMI Cloud - VMs stopped automatically', htmlText);
}
module.exports.sendProvisionSuccess = sendProvisionSuccess;
module.exports.sendProvisionError = sendProvisionError;
module.exports.sendDestroyedSuccess = sendDestroyedSuccess;
module.exports.sendWillStopIn24 = sendWillStopIn24;
module.exports.sendVMsStopped = sendVMsStopped;
module.exports.sendWillDestroyIn24 = sendWillDestroyIn24;
module.exports._doSend = _doSend;