2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2023-01-27 03:22:44 -08:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2021-05-29 17:42:25 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { uptimeRobotApiRequest } from './GenericFunctions';
|
2021-05-29 17:42:25 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { monitorFields, monitorOperations } from './MonitorDescription';
|
2021-05-29 17:42:25 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { alertContactFields, alertContactOperations } from './AlertContactDescription';
|
2021-05-29 17:42:25 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
maintenanceWindowFields,
|
|
|
|
maintenanceWindowOperations,
|
|
|
|
} from './MaintenanceWindowDescription';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { publicStatusPageFields, publicStatusPageOperations } from './PublicStatusPageDescription';
|
2021-05-29 17:42:25 -07:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment-timezone';
|
2021-05-29 17:42:25 -07:00
|
|
|
|
|
|
|
export class UptimeRobot implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'UptimeRobot',
|
|
|
|
name: 'uptimeRobot',
|
|
|
|
icon: 'file:uptimerobot.svg',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume UptimeRobot API',
|
|
|
|
defaults: {
|
|
|
|
name: 'UptimeRobot',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'uptimeRobotApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-05-29 17:42:25 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Account',
|
|
|
|
value: 'account',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Alert Contact',
|
|
|
|
value: 'alertContact',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Maintenance Window',
|
|
|
|
value: 'maintenanceWindow',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Monitor',
|
|
|
|
value: 'monitor',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Public Status Page',
|
|
|
|
value: 'publicStatusPage',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'account',
|
|
|
|
},
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
/* account:getAccountDetails */
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-05-29 17:42:25 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['account'],
|
2021-05-29 17:42:25 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Get',
|
|
|
|
value: 'get',
|
|
|
|
description: 'Get account details',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Get an account',
|
2021-05-29 17:42:25 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'get',
|
|
|
|
},
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
/* Monitor */
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
...monitorOperations,
|
|
|
|
...monitorFields,
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
/* Alert Contact */
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
...alertContactOperations,
|
|
|
|
...alertContactFields,
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
/* Maintenance Window */
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
...maintenanceWindowOperations,
|
|
|
|
...maintenanceWindowFields,
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
/* Public Status Page */
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
...publicStatusPageOperations,
|
|
|
|
...publicStatusPageFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData = [];
|
|
|
|
const length = items.length;
|
|
|
|
let responseData;
|
|
|
|
const timezone = this.getTimezone();
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
try {
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-05-29 17:42:25 -07:00
|
|
|
let body: IDataObject = {};
|
|
|
|
//https://uptimerobot.com/#methods
|
|
|
|
if (resource === 'account') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/getAccountDetails');
|
|
|
|
responseData = responseData.account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resource === 'monitor') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
body = {
|
|
|
|
friendly_name: this.getNodeParameter('friendlyName', i) as string,
|
|
|
|
url: this.getNodeParameter('url', i) as string,
|
|
|
|
type: this.getNodeParameter('type', i) as number,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/newMonitor', body);
|
|
|
|
responseData = responseData.monitor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'delete') {
|
|
|
|
body = {
|
|
|
|
id: this.getNodeParameter('id', i) as string,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/deleteMonitor', body);
|
|
|
|
responseData = responseData.monitor;
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
const monitors = this.getNodeParameter('id', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/getMonitors', {
|
|
|
|
monitors,
|
|
|
|
});
|
2021-05-29 17:42:25 -07:00
|
|
|
responseData = responseData.monitors;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2021-05-29 17:42:25 -07:00
|
|
|
|
|
|
|
body = {
|
|
|
|
...filters,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (body.statuses) {
|
|
|
|
body.statuses = (body.statuses as string[]).join('-');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.types) {
|
|
|
|
body.types = (body.types as string[]).join('-');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.alert_contacts) {
|
|
|
|
body.alert_contacts = 1;
|
|
|
|
}
|
|
|
|
if (body.logs) {
|
|
|
|
body.logs = 1;
|
|
|
|
}
|
|
|
|
if (body.mwindow) {
|
|
|
|
body.mwindows = 1;
|
|
|
|
}
|
|
|
|
if (body.response_times) {
|
|
|
|
body.response_times = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
body.limit = this.getNodeParameter('limit', i);
|
2021-05-29 17:42:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/getMonitors', body);
|
|
|
|
responseData = responseData.monitors;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'reset') {
|
|
|
|
body = {
|
|
|
|
id: this.getNodeParameter('id', i) as string,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/resetMonitor', body);
|
|
|
|
responseData = responseData.monitor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'update') {
|
|
|
|
body = {
|
|
|
|
id: this.getNodeParameter('id', i) as string,
|
2022-12-02 12:54:28 -08:00
|
|
|
...this.getNodeParameter('updateFields', i),
|
2021-05-29 17:42:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/editMonitor', body);
|
|
|
|
responseData = responseData.monitor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resource === 'alertContact') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
body = {
|
|
|
|
friendly_name: this.getNodeParameter('friendlyName', i) as string,
|
|
|
|
value: this.getNodeParameter('value', i) as string,
|
|
|
|
type: this.getNodeParameter('type', i) as number,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/newAlertContact', body);
|
|
|
|
responseData = responseData.alertcontact;
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
body = {
|
|
|
|
id: this.getNodeParameter('id', i) as string,
|
|
|
|
};
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await uptimeRobotApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/deleteAlertContact',
|
|
|
|
body,
|
|
|
|
);
|
2021-05-29 17:42:25 -07:00
|
|
|
responseData = responseData.alert_contact;
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/getAlertContacts', {
|
|
|
|
alert_contacts: id,
|
|
|
|
});
|
2021-05-29 17:42:25 -07:00
|
|
|
responseData = responseData.alert_contacts;
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-05-29 17:42:25 -07:00
|
|
|
|
|
|
|
body = {
|
2022-12-02 12:54:28 -08:00
|
|
|
...this.getNodeParameter('filters', i),
|
2021-05-29 17:42:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
body.limit = this.getNodeParameter('limit', i);
|
2021-05-29 17:42:25 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await uptimeRobotApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/getAlertContacts',
|
|
|
|
body,
|
|
|
|
);
|
2021-05-29 17:42:25 -07:00
|
|
|
responseData = responseData.alert_contacts;
|
|
|
|
}
|
|
|
|
if (operation === 'update') {
|
|
|
|
body = {
|
|
|
|
id: this.getNodeParameter('id', i) as string,
|
2022-12-02 12:54:28 -08:00
|
|
|
...this.getNodeParameter('updateFields', i),
|
2021-05-29 17:42:25 -07:00
|
|
|
};
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await uptimeRobotApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/editAlertContact',
|
|
|
|
body,
|
|
|
|
);
|
2021-05-29 17:42:25 -07:00
|
|
|
responseData = responseData.alert_contact;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resource === 'maintenanceWindow') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const startTime = this.getNodeParameter('start_time', i) as string;
|
|
|
|
const type = this.getNodeParameter('type', i) as number;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const parsedStartTime =
|
|
|
|
type === 1
|
|
|
|
? moment.tz(startTime, timezone).unix()
|
|
|
|
: moment.tz(startTime, timezone).format('HH:mm');
|
2021-05-29 17:42:25 -07:00
|
|
|
|
|
|
|
body = {
|
|
|
|
duration: this.getNodeParameter('duration', i) as number,
|
|
|
|
friendly_name: this.getNodeParameter('friendlyName', i) as string,
|
|
|
|
start_time: parsedStartTime,
|
|
|
|
type,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (type === 3) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.value = this.getNodeParameter('weekDay', i) as number;
|
2021-05-29 17:42:25 -07:00
|
|
|
}
|
|
|
|
if (type === 4) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.value = this.getNodeParameter('monthDay', i) as number;
|
2021-05-29 17:42:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/newMWindow', body);
|
|
|
|
responseData = responseData.mwindow;
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
body = {
|
|
|
|
id: this.getNodeParameter('id', i) as string,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/deleteMWindow', body);
|
|
|
|
responseData = { status: responseData.message };
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
const mwindows = this.getNodeParameter('id', i) as string;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/getMWindows', {
|
|
|
|
mwindows,
|
|
|
|
});
|
2021-05-29 17:42:25 -07:00
|
|
|
responseData = responseData.mwindows;
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-05-29 17:42:25 -07:00
|
|
|
|
|
|
|
body = {
|
2022-12-02 12:54:28 -08:00
|
|
|
...this.getNodeParameter('filters', i),
|
2021-05-29 17:42:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
body.limit = this.getNodeParameter('limit', i);
|
2021-05-29 17:42:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/getMWindows', body);
|
|
|
|
responseData = responseData.mwindows;
|
|
|
|
}
|
|
|
|
if (operation === 'update') {
|
|
|
|
body = {
|
|
|
|
id: this.getNodeParameter('id', i) as string,
|
|
|
|
duration: this.getNodeParameter('duration', i) as string,
|
2022-12-02 12:54:28 -08:00
|
|
|
...this.getNodeParameter('updateFields', i),
|
2021-05-29 17:42:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (body.type === 1 && body.start_time) {
|
|
|
|
body.start_time = moment.tz(body.start_time, timezone).unix();
|
|
|
|
} else {
|
|
|
|
body.start_time = moment.tz(body.start_time, timezone).format('HH:mm');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.type === 3) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.value = body.weekDay;
|
2021-05-29 17:42:25 -07:00
|
|
|
delete body.weekDay;
|
|
|
|
}
|
|
|
|
if (body.type === 4) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.value = body.monthDay;
|
2021-05-29 17:42:25 -07:00
|
|
|
delete body.monthDay;
|
|
|
|
}
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/editMWindow', body);
|
|
|
|
responseData = responseData.mwindow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resource === 'publicStatusPage') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
body = {
|
|
|
|
friendly_name: this.getNodeParameter('friendlyName', i) as string,
|
|
|
|
monitors: this.getNodeParameter('monitors', i) as string,
|
2022-12-02 12:54:28 -08:00
|
|
|
...this.getNodeParameter('additionalFields', i),
|
2021-05-29 17:42:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/newPSP', body);
|
|
|
|
responseData = responseData.psp;
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
body = {
|
|
|
|
id: this.getNodeParameter('id', i) as string,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/deletePSP', body);
|
|
|
|
responseData = responseData.psp;
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
const psps = this.getNodeParameter('id', i) as string;
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/getPSPs', { psps });
|
|
|
|
responseData = responseData.psps;
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-05-29 17:42:25 -07:00
|
|
|
|
|
|
|
body = {
|
2022-12-02 12:54:28 -08:00
|
|
|
...this.getNodeParameter('filters', i),
|
2021-05-29 17:42:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
body.limit = this.getNodeParameter('limit', i);
|
2021-05-29 17:42:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/getPSPs', body);
|
|
|
|
responseData = responseData.psps;
|
|
|
|
}
|
|
|
|
if (operation === 'update') {
|
|
|
|
body = {
|
|
|
|
id: this.getNodeParameter('id', i) as string,
|
2022-12-02 12:54:28 -08:00
|
|
|
...this.getNodeParameter('updateFields', i),
|
2021-05-29 17:42:25 -07:00
|
|
|
};
|
|
|
|
responseData = await uptimeRobotApiRequest.call(this, 'POST', '/editPSP', body);
|
|
|
|
responseData = responseData.psp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Array.isArray(responseData)
|
2023-02-27 19:39:43 -08:00
|
|
|
? returnData.push(...(responseData as IDataObject[]))
|
|
|
|
: returnData.push(responseData as IDataObject);
|
2021-05-29 17:42:25 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|