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

541 lines (540 loc) 24.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IonosCloudCdn = void 0; const baseUrl = 'https://cdn.de-fra.ionos.com'; class IonosCloudCdn { description = { displayName: 'IONOS Cloud CDN', name: 'ionosCloudCdn', icon: 'file:ionos.svg', group: ['transform'], version: 1, subtitle: '={{ $parameter["operation"] + ": " + $parameter["resource"] }}', description: 'Manage IONOS Cloud CDN distributions and edge server IPs. Developped with Love by Ascenzia (ascenzia.fr)', defaults: { name: 'IONOS Cloud CDN', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'ionosCloud', required: true, }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Distribution', value: 'distribution', description: 'Manage CDN distributions', }, { name: 'Edge Server IP', value: 'edgeServerIp', description: 'Get CDN edge server IP information', }, ], default: 'distribution', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['distribution'], }, }, options: [ { name: 'Create', value: 'create', description: 'Create a CDN distribution', action: 'Create a CDN distribution', }, { name: 'Get', value: 'get', description: 'Get a CDN distribution', action: 'Get a CDN distribution', }, { name: 'Get Many', value: 'getMany', description: 'Get many CDN distributions', action: 'Get many CDN distributions', }, { name: 'Update', value: 'update', description: 'Update a CDN distribution', action: 'Update a CDN distribution', }, { name: 'Delete', value: 'delete', description: 'Delete a CDN distribution', action: 'Delete a CDN distribution', }, ], default: 'create', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['edgeServerIp'], }, }, options: [ { name: 'Get', value: 'get', description: 'Get edge server IP information', action: 'Get edge server IP information', }, { name: 'Get Many', value: 'getMany', description: 'Get many edge server IPs', action: 'Get many edge server IPs', }, ], default: 'getMany', }, { displayName: 'Distribution ID', name: 'distributionId', type: 'string', required: true, displayOptions: { show: { resource: ['distribution'], operation: ['get', 'update', 'delete'], }, }, default: '', description: 'The unique ID (UUID) of the distribution', }, { displayName: 'Domain', name: 'domain', type: 'string', required: true, displayOptions: { show: { resource: ['distribution'], operation: ['create', 'update'], }, }, default: '', placeholder: 'example.com', description: 'The domain for the CDN distribution', }, { displayName: 'Certificate ID', name: 'certificateId', type: 'string', displayOptions: { show: { resource: ['distribution'], operation: ['create', 'update'], }, }, default: '', description: 'The ID of the SSL certificate to use (optional)', }, { displayName: 'Routing Rules', name: 'routingRules', type: 'fixedCollection', typeOptions: { multipleValues: true, }, displayOptions: { show: { resource: ['distribution'], operation: ['create', 'update'], }, }, default: {}, placeholder: 'Add Routing Rule', description: 'Routing rules for the distribution (minimum 1, maximum 25)', options: [ { displayName: 'Rule', name: 'ruleValues', values: [ { displayName: 'Scheme', name: 'scheme', type: 'options', options: [ { name: 'HTTP', value: 'http', }, { name: 'HTTPS', value: 'https', }, { name: 'HTTP/HTTPS', value: 'http/https', }, ], default: 'http/https', description: 'The protocol scheme for this routing rule', }, { displayName: 'Prefix', name: 'prefix', type: 'string', default: '/', placeholder: '/api', description: 'The URL path prefix to match (e.g., /, /api, /static)', }, { displayName: 'Upstream Host', name: 'upstreamHost', type: 'string', default: '', placeholder: 'origin.example.com', description: 'The upstream/origin server hostname', }, { displayName: 'Upstream Geo Restrictions', name: 'upstreamGeoRestrictions', type: 'fixedCollection', typeOptions: { multipleValues: false, }, default: {}, placeholder: 'Add Geo Restrictions', options: [ { displayName: 'Restrictions', name: 'restrictionValues', values: [ { displayName: 'Allow List', name: 'allowList', type: 'string', default: '', placeholder: 'US,GB,DE', description: 'Comma-separated list of allowed country codes (ISO 3166-1 alpha-2)', }, { displayName: 'Block List', name: 'blockList', type: 'string', default: '', placeholder: 'CN,RU', description: 'Comma-separated list of blocked country codes (ISO 3166-1 alpha-2)', }, ], }, ], }, ], }, ], }, { displayName: 'IP ID', name: 'ipId', type: 'string', required: true, displayOptions: { show: { resource: ['edgeServerIp'], operation: ['get'], }, }, default: '', description: 'The unique ID (UUID) of the edge server IP', }, { displayName: 'Return All', name: 'returnAll', type: 'boolean', displayOptions: { show: { operation: ['getMany'], }, }, default: false, description: 'Whether to return all results or only up to a given limit', }, { displayName: 'Limit', name: 'limit', type: 'number', displayOptions: { show: { operation: ['getMany'], returnAll: [false], }, }, typeOptions: { minValue: 1, maxValue: 1000, }, default: 50, description: 'Max number of results to return', }, { displayName: 'Filters', name: 'filters', type: 'collection', placeholder: 'Add Filter', default: {}, displayOptions: { show: { resource: ['distribution'], operation: ['getMany'], }, }, options: [ { displayName: 'Domain', name: 'domain', type: 'string', default: '', description: 'Filter by domain', }, { displayName: 'State', name: 'state', type: 'options', options: [ { name: 'Available', value: 'AVAILABLE', }, { name: 'Busy', value: 'BUSY', }, { name: 'Updating', value: 'UPDATING', }, { name: 'Failed', value: 'FAILED', }, { name: 'Destroying', value: 'DESTROYING', }, ], default: 'AVAILABLE', description: 'Filter by state', }, ], }, ], }; async execute() { const items = this.getInputData(); const returnData = []; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); for (let i = 0; i < items.length; i++) { try { let responseData = {}; if (resource === 'distribution') { if (operation === 'create') { const domain = this.getNodeParameter('domain', i); const certificateId = this.getNodeParameter('certificateId', i); const routingRules = this.getNodeParameter('routingRules', i); const rules = []; if (routingRules.ruleValues && Array.isArray(routingRules.ruleValues)) { for (const rule of routingRules.ruleValues) { const routingRule = { scheme: rule.scheme, prefix: rule.prefix, upstream: { host: rule.upstreamHost, }, }; if (rule.upstreamGeoRestrictions) { const geoRestrictions = rule.upstreamGeoRestrictions; if (geoRestrictions.restrictionValues) { const restrictions = geoRestrictions.restrictionValues; const geoRestrictionsObj = {}; if (restrictions.allowList) { geoRestrictionsObj.allowList = restrictions.allowList .split(',') .map((c) => c.trim()); } if (restrictions.blockList) { geoRestrictionsObj.blockList = restrictions.blockList .split(',') .map((c) => c.trim()); } if (Object.keys(geoRestrictionsObj).length > 0) { routingRule.upstream.geoRestrictions = geoRestrictionsObj; } } } rules.push(routingRule); } } const body = { properties: { domain, routingRules: rules, }, }; if (certificateId) { body.properties.certificateId = certificateId; } responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'ionosCloud', { method: 'POST', url: `${baseUrl}/distributions`, body, headers: { 'Content-Type': 'application/json' }, }); } else if (operation === 'get') { const distributionId = this.getNodeParameter('distributionId', i); responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'ionosCloud', { method: 'GET', url: `${baseUrl}/distributions/${distributionId}`, }); } else if (operation === 'getMany') { const returnAll = this.getNodeParameter('returnAll', i); const limit = this.getNodeParameter('limit', i, 50); const filters = this.getNodeParameter('filters', i, {}); const qs = {}; if (!returnAll) { qs.limit = limit; } if (filters.domain) { qs.domain = filters.domain; } if (filters.state) { qs.state = filters.state; } responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'ionosCloud', { method: 'GET', url: `${baseUrl}/distributions`, qs, }); responseData = responseData.items; } else if (operation === 'update') { const distributionId = this.getNodeParameter('distributionId', i); const domain = this.getNodeParameter('domain', i); const certificateId = this.getNodeParameter('certificateId', i); const routingRules = this.getNodeParameter('routingRules', i); const rules = []; if (routingRules.ruleValues && Array.isArray(routingRules.ruleValues)) { for (const rule of routingRules.ruleValues) { const routingRule = { scheme: rule.scheme, prefix: rule.prefix, upstream: { host: rule.upstreamHost, }, }; if (rule.upstreamGeoRestrictions) { const geoRestrictions = rule.upstreamGeoRestrictions; if (geoRestrictions.restrictionValues) { const restrictions = geoRestrictions.restrictionValues; const geoRestrictionsObj = {}; if (restrictions.allowList) { geoRestrictionsObj.allowList = restrictions.allowList .split(',') .map((c) => c.trim()); } if (restrictions.blockList) { geoRestrictionsObj.blockList = restrictions.blockList .split(',') .map((c) => c.trim()); } if (Object.keys(geoRestrictionsObj).length > 0) { routingRule.upstream.geoRestrictions = geoRestrictionsObj; } } } rules.push(routingRule); } } const body = { properties: { domain, routingRules: rules, }, }; if (certificateId) { body.properties.certificateId = certificateId; } responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'ionosCloud', { method: 'PUT', url: `${baseUrl}/distributions/${distributionId}`, body, headers: { 'Content-Type': 'application/json' }, }); } else if (operation === 'delete') { const distributionId = this.getNodeParameter('distributionId', i); await this.helpers.httpRequestWithAuthentication.call(this, 'ionosCloud', { method: 'DELETE', url: `${baseUrl}/distributions/${distributionId}`, }); responseData = { success: true }; } } else if (resource === 'edgeServerIp') { if (operation === 'get') { const ipId = this.getNodeParameter('ipId', i); responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'ionosCloud', { method: 'GET', url: `${baseUrl}/ips/${ipId}`, }); } else if (operation === 'getMany') { const returnAll = this.getNodeParameter('returnAll', i); const limit = this.getNodeParameter('limit', i, 50); const qs = {}; if (!returnAll) { qs.limit = limit; } responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'ionosCloud', { method: 'GET', url: `${baseUrl}/ips`, qs, }); responseData = responseData.items; } } 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.IonosCloudCdn = IonosCloudCdn;