UNPKG

n8n-nodes-ionos

Version:

n8n nodes for IONOS DNS, Domain, SSL/Certificate management, Cloud AI, Cloud Infrastructure, Container Registry, Database as a Service, CDN, VPN Gateway, Activity Log, Billing, Logging, Monitoring, Object Storage Management, Network File Storage, Identity

314 lines (313 loc) 14.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IonosCloudActivityLog = void 0; const baseUrl = 'https://api.ionos.com/activitylog/v1'; class IonosCloudActivityLog { description = { displayName: 'IONOS Cloud Activity Log', name: 'ionosCloudActivityLog', icon: 'file:ionos.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Retrieve audit logs and activity information from IONOS Cloud contracts. Developped with Love by Ascenzia (ascenzia.fr)', defaults: { name: 'IONOS Cloud Activity Log', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'ionosCloud', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Get API Info', value: 'getInfo', description: 'Display API information', action: 'Get API information', }, { name: 'Get Contracts', value: 'getContracts', description: 'List all accessible contracts', action: 'Get all accessible contracts', }, { name: 'Get Activity Logs', value: 'getActivityLogs', description: 'Download activity log entries for a contract', action: 'Get activity log entries', }, ], default: 'getActivityLogs', }, { displayName: 'Contract Number', name: 'contractNumber', type: 'string', required: true, displayOptions: { show: { operation: ['getActivityLogs'], }, }, default: '', placeholder: '12345678', description: 'Contract number whose activity log entries should be downloaded', }, { displayName: 'Date Start', name: 'dateStart', type: 'string', displayOptions: { show: { operation: ['getActivityLogs'], }, }, default: '', placeholder: '2024-01-01 or 2024-01-01T00:00:00Z', description: 'Start date for the Activity Log entries (inclusive). Format: YYYY-MM-DD or ISO 8601 (e.g., 2024-01-01T00:00:00Z)', }, { displayName: 'Date End', name: 'dateEnd', type: 'string', displayOptions: { show: { operation: ['getActivityLogs'], }, }, default: '', placeholder: '2024-12-31 or 2024-12-31T23:59:59Z', description: 'End date for the Activity Log entries (exclusive). Format: YYYY-MM-DD or ISO 8601 (e.g., 2024-12-31T23:59:59Z)', }, { displayName: 'Return All', name: 'returnAll', type: 'boolean', displayOptions: { show: { operation: ['getActivityLogs'], }, }, default: false, description: 'Whether to return all results or only up to a given limit', }, { displayName: 'Limit', name: 'limit', type: 'number', displayOptions: { show: { operation: ['getActivityLogs'], returnAll: [false], }, }, typeOptions: { minValue: 1, maxValue: 1000, }, default: 20, description: 'Max number of results to return', }, { displayName: 'Offset', name: 'offset', type: 'number', displayOptions: { show: { operation: ['getActivityLogs'], }, }, typeOptions: { minValue: 0, }, default: 0, description: 'Number of results to skip (page index)', }, { displayName: 'Additional Options', name: 'additionalOptions', type: 'collection', placeholder: 'Add Option', displayOptions: { show: { operation: ['getActivityLogs'], }, }, default: {}, options: [ { displayName: 'Filter by Action', name: 'filterAction', type: 'string', default: '', placeholder: 'sec.user.create', description: 'Filter logs by specific action type (e.g., sec.user.create, datacenters.create)', }, { displayName: 'Filter by User', name: 'filterUser', type: 'string', default: '', placeholder: 'user@example.com', description: 'Filter logs by username', }, { displayName: 'Filter by Resource Type', name: 'filterResourceType', type: 'string', default: '', placeholder: 'datacenter, server, volume', description: 'Filter logs by resource type', }, { displayName: 'Filter by Event Type', name: 'filterEventType', type: 'string', default: '', placeholder: 'RequestAccepted, RequestFailed', description: 'Filter logs by event type (RequestAccepted, RequestFailed, etc.)', }, ], }, ], }; async execute() { const items = this.getInputData(); const returnData = []; const operation = this.getNodeParameter('operation', 0); for (let i = 0; i < items.length; i++) { try { let responseData = {}; if (operation === 'getInfo') { responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'ionosCloud', { method: 'GET', url: baseUrl, }); } else if (operation === 'getContracts') { responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'ionosCloud', { method: 'GET', url: `${baseUrl}/contracts`, }); } else if (operation === 'getActivityLogs') { const contractNumber = this.getNodeParameter('contractNumber', i); const dateStart = this.getNodeParameter('dateStart', i, ''); const dateEnd = this.getNodeParameter('dateEnd', i, ''); const returnAll = this.getNodeParameter('returnAll', i); const limit = this.getNodeParameter('limit', i, 20); const offset = this.getNodeParameter('offset', i, 0); const additionalOptions = this.getNodeParameter('additionalOptions', i, {}); const qs = { offset: offset.toString(), }; if (dateStart) { qs.dateStart = dateStart; } if (dateEnd) { qs.dateEnd = dateEnd; } if (!returnAll) { qs.limit = limit.toString(); } responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'ionosCloud', { method: 'GET', url: `${baseUrl}/contracts/${contractNumber}`, qs, }); if (responseData && typeof responseData === 'object' && 'hits' in responseData) { const hitsData = responseData.hits; if (hitsData && typeof hitsData === 'object' && 'hits' in hitsData) { let activities = hitsData.hits; if (additionalOptions.filterAction) { const filterAction = additionalOptions.filterAction; activities = activities.filter((activity) => { const source = activity._source; if (source && source.event) { const event = source.event; if (event.resources && Array.isArray(event.resources)) { return event.resources.some((resource) => { const actions = resource.action; return actions && actions.includes(filterAction); }); } } return false; }); } if (additionalOptions.filterUser) { const filterUser = additionalOptions.filterUser; activities = activities.filter((activity) => { const source = activity._source; if (source && source.principal) { const principal = source.principal; if (principal.identity) { const identity = principal.identity; return identity.username === filterUser; } } return false; }); } if (additionalOptions.filterResourceType) { const filterResourceType = additionalOptions.filterResourceType; activities = activities.filter((activity) => { const source = activity._source; if (source && source.event) { const event = source.event; if (event.resources && Array.isArray(event.resources)) { return event.resources.some((resource) => { return resource.type === filterResourceType; }); } } return false; }); } if (additionalOptions.filterEventType) { const filterEventType = additionalOptions.filterEventType; activities = activities.filter((activity) => { const source = activity._source; if (source && source.event) { const event = source.event; return event.type === filterEventType; } return false; }); } responseData = { total: hitsData.total, offset, limit: returnAll ? activities.length : limit, activities: activities.map((activity) => activity._source), }; } } } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } }); returnData.push(...executionData); continue; } throw error; } } return [returnData]; } } exports.IonosCloudActivityLog = IonosCloudActivityLog;