UNPKG

@panoptic-it-solutions/n8n-nodes-datto-rmm

Version:

n8n node for Datto RMM integration

410 lines 24.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.executeAccountOperation = executeAccountOperation; const api_helper_1 = require("../../helpers/api.helper"); const errorHandler_1 = require("../../helpers/errorHandler"); const n8n_workflow_1 = require("n8n-workflow"); async function executeAccountOperation() { const items = this.getInputData(); const operation = this.getNodeParameter('operation', 0); const returnData = []; return (0, errorHandler_1.handleErrors)(this, async () => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2; const singleExecutionOperations = [ 'get', 'getUsers', 'getComponents', 'getOpenAlerts', 'getResolvedAlerts', 'getSites', 'getVariables', ]; const executeOnce = singleExecutionOperations.includes(operation); const itemsToProcess = executeOnce ? [items[0]] : items; if (operation === 'getDevices' && items.length > 1) { const hostnames = new Set(); let allDevices = []; for (let i = 0; i < items.length; i++) { const hostname = this.getNodeParameter('hostname', i, ''); const safeHostname = ((_c = (_b = (_a = hostname === null || hostname === void 0 ? void 0 : hostname.toString) === null || _a === void 0 ? void 0 : _a.call(hostname)) === null || _b === void 0 ? void 0 : _b.trim) === null || _c === void 0 ? void 0 : _c.call(_b)) || ''; if (safeHostname !== '') { hostnames.add(safeHostname); } } const useAdvancedFilters = this.getNodeParameter('useAdvancedFilters', 0, false); const filterConditions = this.getNodeParameter('filterConditions', 0, {}); const filterLogic = this.getNodeParameter('filterLogic', 0, 'AND'); if (hostnames.size > 0) { for (const hostname of hostnames) { const retrieveAll = this.getNodeParameter('retrieveAll', 0, true); const filterId = this.getNodeParameter('filterId', 0, 0); const deviceType = this.getNodeParameter('deviceType', 0, ''); const operatingSystem = this.getNodeParameter('operatingSystem', 0, ''); const siteName = this.getNodeParameter('siteName', 0, ''); const queryParams = {}; if (filterId > 0) { queryParams.filterId = filterId; } queryParams.hostname = hostname; const safeDeviceType = ((_f = (_e = (_d = deviceType === null || deviceType === void 0 ? void 0 : deviceType.toString) === null || _d === void 0 ? void 0 : _d.call(deviceType)) === null || _e === void 0 ? void 0 : _e.trim) === null || _f === void 0 ? void 0 : _f.call(_e)) || ''; if (safeDeviceType !== '') { queryParams.deviceType = safeDeviceType; } const safeOperatingSystem = ((_j = (_h = (_g = operatingSystem === null || operatingSystem === void 0 ? void 0 : operatingSystem.toString) === null || _g === void 0 ? void 0 : _g.call(operatingSystem)) === null || _h === void 0 ? void 0 : _h.trim) === null || _j === void 0 ? void 0 : _j.call(_h)) || ''; if (safeOperatingSystem !== '') { queryParams.operatingSystem = safeOperatingSystem; } const safeSiteName = ((_m = (_l = (_k = siteName === null || siteName === void 0 ? void 0 : siteName.toString) === null || _k === void 0 ? void 0 : _k.call(siteName)) === null || _l === void 0 ? void 0 : _l.trim) === null || _m === void 0 ? void 0 : _m.call(_l)) || ''; if (safeSiteName !== '') { queryParams.siteName = safeSiteName; } let responseData; if (retrieveAll) { const devices = await api_helper_1.dattoRmmApiRequestAllItems.call(this, 'GET', '/api/v2/account/devices', {}, queryParams); allDevices.push(...devices); } else { const page = this.getNodeParameter('page', 0, 0); const max = this.getNodeParameter('max', 0, 100); queryParams.page = page; queryParams.max = max; responseData = await api_helper_1.dattoRmmApiRequest.call(this, 'GET', '/api/v2/account/devices', {}, queryParams); const devices = responseData.devices || []; allDevices.push(...devices); } } } if (useAdvancedFilters && (filterConditions === null || filterConditions === void 0 ? void 0 : filterConditions.condition) && Array.isArray(filterConditions.condition)) { const { applyAdvancedFilters, validateFilterConditions } = await Promise.resolve().then(() => __importStar(require('../../helpers/deviceFilter'))); const validatedConditions = validateFilterConditions(filterConditions.condition); if (validatedConditions.length > 0) { allDevices = applyAdvancedFilters(allDevices, validatedConditions, filterLogic); } } const resourceMapper = this.getNodeParameter('resourceMapper', 0, {}); if (resourceMapper && resourceMapper.value) { const { applyFieldSelection } = await Promise.resolve().then(() => __importStar(require('../../helpers/deviceFilter'))); allDevices = applyFieldSelection(allDevices, resourceMapper); } if (allDevices.length > 0) { allDevices.forEach((device) => { returnData.push({ json: device, pairedItem: { item: 0 }, }); }); } else { returnData.push({ json: { message: 'No devices found for the specified hostnames', searchedHostnames: Array.from(hostnames), operation: operation, }, pairedItem: { item: 0 }, }); } return [returnData]; } for (let i = 0; i < itemsToProcess.length; i++) { const itemIndex = executeOnce ? 0 : i; try { let responseData; switch (operation) { case 'get': { responseData = await api_helper_1.dattoRmmApiRequest.call(this, 'GET', '/api/v2/account'); } break; case 'getVariables': { const retrieveAll = this.getNodeParameter('retrieveAll', itemIndex, true); if (retrieveAll) { const allVariables = await api_helper_1.dattoRmmApiRequestAllItems.call(this, 'GET', '/api/v2/account/variables', {}, {}); responseData = { variables: allVariables }; } else { const page = this.getNodeParameter('page', itemIndex, 0); const max = this.getNodeParameter('max', itemIndex, 100); const queryParams = { page, max, }; responseData = await api_helper_1.dattoRmmApiRequest.call(this, 'GET', '/api/v2/account/variables', {}, queryParams); } } break; case 'getDevices': { const retrieveAll = this.getNodeParameter('retrieveAll', itemIndex, true); const filterId = this.getNodeParameter('filterId', itemIndex, 0); const hostname = this.getNodeParameter('hostname', itemIndex, ''); const deviceType = this.getNodeParameter('deviceType', itemIndex, ''); const operatingSystem = this.getNodeParameter('operatingSystem', itemIndex, ''); const siteName = this.getNodeParameter('siteName', itemIndex, ''); const useAdvancedFilters = this.getNodeParameter('useAdvancedFilters', itemIndex, false); const filterConditions = this.getNodeParameter('filterConditions', itemIndex, {}); const filterLogic = this.getNodeParameter('filterLogic', itemIndex, 'AND'); const queryParams = {}; if (filterId > 0) { queryParams.filterId = filterId; } const safeHostname = ((_q = (_p = (_o = hostname === null || hostname === void 0 ? void 0 : hostname.toString) === null || _o === void 0 ? void 0 : _o.call(hostname)) === null || _p === void 0 ? void 0 : _p.trim) === null || _q === void 0 ? void 0 : _q.call(_p)) || ''; if (safeHostname !== '') { queryParams.hostname = safeHostname; } const safeDeviceType = ((_t = (_s = (_r = deviceType === null || deviceType === void 0 ? void 0 : deviceType.toString) === null || _r === void 0 ? void 0 : _r.call(deviceType)) === null || _s === void 0 ? void 0 : _s.trim) === null || _t === void 0 ? void 0 : _t.call(_s)) || ''; if (safeDeviceType !== '') { queryParams.deviceType = safeDeviceType; } const safeOperatingSystem = ((_w = (_v = (_u = operatingSystem === null || operatingSystem === void 0 ? void 0 : operatingSystem.toString) === null || _u === void 0 ? void 0 : _u.call(operatingSystem)) === null || _v === void 0 ? void 0 : _v.trim) === null || _w === void 0 ? void 0 : _w.call(_v)) || ''; if (safeOperatingSystem !== '') { queryParams.operatingSystem = safeOperatingSystem; } const safeSiteName = ((_z = (_y = (_x = siteName === null || siteName === void 0 ? void 0 : siteName.toString) === null || _x === void 0 ? void 0 : _x.call(siteName)) === null || _y === void 0 ? void 0 : _y.trim) === null || _z === void 0 ? void 0 : _z.call(_y)) || ''; if (safeSiteName !== '') { queryParams.siteName = safeSiteName; } let allDevices = []; if (retrieveAll) { allDevices = await api_helper_1.dattoRmmApiRequestAllItems.call(this, 'GET', '/api/v2/account/devices', {}, queryParams); } else { const page = this.getNodeParameter('page', itemIndex, 0); const max = this.getNodeParameter('max', itemIndex, 100); queryParams.page = page; queryParams.max = max; const paginatedResponse = await api_helper_1.dattoRmmApiRequest.call(this, 'GET', '/api/v2/account/devices', {}, queryParams); allDevices = paginatedResponse.devices || []; } if (useAdvancedFilters && (filterConditions === null || filterConditions === void 0 ? void 0 : filterConditions.condition) && Array.isArray(filterConditions.condition)) { const { applyAdvancedFilters, validateFilterConditions } = await Promise.resolve().then(() => __importStar(require('../../helpers/deviceFilter'))); const validatedConditions = validateFilterConditions(filterConditions.condition); if (validatedConditions.length > 0) { allDevices = applyAdvancedFilters(allDevices, validatedConditions, filterLogic); } } const resourceMapper = this.getNodeParameter('resourceMapper', itemIndex, {}); if (resourceMapper && resourceMapper.value) { const { applyFieldSelection } = await Promise.resolve().then(() => __importStar(require('../../helpers/deviceFilter'))); allDevices = applyFieldSelection(allDevices, resourceMapper); } responseData = { devices: allDevices }; } break; case 'getUsers': { const retrieveAll = this.getNodeParameter('retrieveAll', itemIndex, true); if (retrieveAll) { const allUsers = await api_helper_1.dattoRmmApiRequestAllItems.call(this, 'GET', '/api/v2/account/users', {}, {}); responseData = { users: allUsers }; } else { const page = this.getNodeParameter('page', itemIndex, 0); const max = this.getNodeParameter('max', itemIndex, 100); const queryParams = { page, max, }; responseData = await api_helper_1.dattoRmmApiRequest.call(this, 'GET', '/api/v2/account/users', {}, queryParams); } } break; case 'getComponents': { const retrieveAll = this.getNodeParameter('retrieveAll', itemIndex, true); if (retrieveAll) { const allComponents = await api_helper_1.dattoRmmApiRequestAllItems.call(this, 'GET', '/api/v2/account/components', {}, {}); responseData = { components: allComponents }; } else { const page = this.getNodeParameter('page', itemIndex, 0); const max = this.getNodeParameter('max', itemIndex, 100); const queryParams = { page, max, }; responseData = await api_helper_1.dattoRmmApiRequest.call(this, 'GET', '/api/v2/account/components', {}, queryParams); } } break; case 'getOpenAlerts': { const retrieveAll = this.getNodeParameter('retrieveAll', itemIndex, true); const muted = this.getNodeParameter('muted', itemIndex, false); const queryParams = {}; if (muted !== undefined) { queryParams.muted = muted; } if (retrieveAll) { const allAlerts = await api_helper_1.dattoRmmApiRequestAllItems.call(this, 'GET', '/api/v2/account/alerts/open', {}, queryParams); responseData = { alerts: allAlerts }; } else { const page = this.getNodeParameter('page', itemIndex, 0); const max = this.getNodeParameter('max', itemIndex, 100); queryParams.page = page; queryParams.max = max; responseData = await api_helper_1.dattoRmmApiRequest.call(this, 'GET', '/api/v2/account/alerts/open', {}, queryParams); } } break; case 'getResolvedAlerts': { const retrieveAll = this.getNodeParameter('retrieveAll', itemIndex, true); const muted = this.getNodeParameter('muted', itemIndex, false); const queryParams = {}; if (muted !== undefined) { queryParams.muted = muted; } if (retrieveAll) { const allAlerts = await api_helper_1.dattoRmmApiRequestAllItems.call(this, 'GET', '/api/v2/account/alerts/resolved', {}, queryParams); responseData = { alerts: allAlerts }; } else { const page = this.getNodeParameter('page', itemIndex, 0); const max = this.getNodeParameter('max', itemIndex, 100); queryParams.page = page; queryParams.max = max; responseData = await api_helper_1.dattoRmmApiRequest.call(this, 'GET', '/api/v2/account/alerts/resolved', {}, queryParams); } } break; case 'getSites': { const retrieveAll = this.getNodeParameter('retrieveAll', itemIndex, true); const siteName = this.getNodeParameter('siteName', itemIndex, ''); const queryParams = {}; const safeSiteName = ((_2 = (_1 = (_0 = siteName === null || siteName === void 0 ? void 0 : siteName.toString) === null || _0 === void 0 ? void 0 : _0.call(siteName)) === null || _1 === void 0 ? void 0 : _1.trim) === null || _2 === void 0 ? void 0 : _2.call(_1)) || ''; if (safeSiteName !== '') { queryParams.siteName = safeSiteName; } if (retrieveAll) { const allSites = await api_helper_1.dattoRmmApiRequestAllItems.call(this, 'GET', '/api/v2/account/sites', {}, queryParams); responseData = { sites: allSites }; } else { const page = this.getNodeParameter('page', itemIndex, 0); const max = this.getNodeParameter('max', itemIndex, 100); queryParams.page = page; queryParams.max = max; responseData = await api_helper_1.dattoRmmApiRequest.call(this, 'GET', '/api/v2/account/sites', {}, queryParams); } } break; default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation: ${operation}`, { itemIndex: itemIndex, }); } if (Array.isArray(responseData)) { responseData.forEach((item) => { returnData.push({ json: item, pairedItem: { item: itemIndex }, }); }); } else if (responseData && typeof responseData === 'object') { let dataArray = []; switch (operation) { case 'getSites': dataArray = responseData.sites || []; break; case 'getDevices': dataArray = responseData.devices || []; break; case 'getUsers': dataArray = responseData.users || []; break; case 'getComponents': dataArray = responseData.components || []; break; case 'getOpenAlerts': case 'getResolvedAlerts': dataArray = responseData.alerts || []; break; default: if (responseData.data && Array.isArray(responseData.data)) { dataArray = responseData.data; } else { returnData.push({ json: responseData, pairedItem: { item: itemIndex }, }); continue; } } if (dataArray.length > 0) { dataArray.forEach((item) => { returnData.push({ json: item, pairedItem: { item: itemIndex }, }); }); } else { returnData.push({ json: { message: 'No data found', pageDetails: responseData.pageDetails || null, operation: operation, }, pairedItem: { item: itemIndex }, }); } } } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message }, pairedItem: { item: itemIndex }, }); continue; } throw error; } } return [returnData]; }); } //# sourceMappingURL=execute.js.map