UNPKG

n8n-nodes-ovh

Version:

Comprehensive n8n nodes for OVH API integration - Developed by Ascenzia, experts in AI agent creation

644 lines 26.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OvhStorage = void 0; const n8n_workflow_1 = require("n8n-workflow"); const crypto_1 = require("crypto"); class OvhStorage { constructor() { this.description = { displayName: 'OVH Public Cloud Storage', name: 'ovhStorage', icon: 'file:ovh.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Manage OVH Public Cloud Object Storage', defaults: { name: 'OVH Public Cloud Storage', }, inputs: ["main"], outputs: ["main"], credentials: [ { name: 'ovhApi', required: true, }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Container', value: 'container', }, { name: 'Credential', value: 'credential', }, { name: 'Presigned URL', value: 'presignedUrl', }, { name: 'Region', value: 'region', }, { name: 'Storage', value: 'storage', }, { name: 'User', value: 'user', }, ], default: 'container', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['container'], }, }, options: [ { name: 'Create', value: 'create', description: 'Create a new storage container', action: 'Create a new storage container', }, { name: 'Delete', value: 'delete', description: 'Delete a storage container', action: 'Delete a storage container', }, { name: 'Get', value: 'get', description: 'Get storage container information', action: 'Get storage container information', }, { name: 'Get Many', value: 'getAll', description: 'Get many storage containers', action: 'Get many storage containers', }, { name: 'Update', value: 'update', description: 'Update storage container', action: 'Update storage container', }, ], default: 'get', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['credential'], }, }, options: [ { name: 'Create', value: 'create', description: 'Create storage credentials', action: 'Create storage credentials', }, { name: 'Delete', value: 'delete', description: 'Delete storage credentials', action: 'Delete storage credentials', }, { name: 'Get', value: 'get', description: 'Get storage credentials information', action: 'Get storage credentials information', }, { name: 'Get Many', value: 'getAll', description: 'Get many storage credentials', action: 'Get many storage credentials', }, ], default: 'get', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['user'], }, }, options: [ { name: 'Create', value: 'create', description: 'Create a new storage user', action: 'Create a new storage user', }, { name: 'Delete', value: 'delete', description: 'Delete a storage user', action: 'Delete a storage user', }, { name: 'Get', value: 'get', description: 'Get storage user information', action: 'Get storage user information', }, { name: 'Get Many', value: 'getAll', description: 'Get many storage users', action: 'Get many storage users', }, ], default: 'get', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['presignedUrl'], }, }, options: [ { name: 'Create', value: 'create', description: 'Create a presigned URL', action: 'Create a presigned URL', }, ], default: 'create', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['storage'], }, }, options: [ { name: 'Get Many', value: 'getAll', description: 'Get many storage services', action: 'Get many storage services', }, ], default: 'getAll', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['region'], }, }, options: [ { name: 'Get Many', value: 'getAll', description: 'Get many storage regions', action: 'Get many storage regions', }, ], default: 'getAll', }, { displayName: 'Project ID', name: 'projectId', type: 'string', default: '', required: true, placeholder: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', description: 'The cloud project ID', }, { displayName: 'Container ID', name: 'containerId', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['container', 'presignedUrl'], operation: ['get', 'delete', 'update', 'create'], }, hide: { resource: ['container'], operation: ['getAll', 'create'], }, }, }, { displayName: 'Credential ID', name: 'credentialId', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['credential'], operation: ['get', 'delete'], }, }, }, { displayName: 'User ID', name: 'userId', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['user'], operation: ['get', 'delete'], }, }, }, { displayName: 'Region', name: 'region', type: 'options', displayOptions: { show: { resource: ['container'], operation: ['create'], }, }, options: [ { name: 'BHS', value: 'BHS', }, { name: 'DE', value: 'DE', }, { name: 'GRA', value: 'GRA', }, { name: 'SBG', value: 'SBG', }, { name: 'UK', value: 'UK', }, { name: 'WAW', value: 'WAW', }, ], default: 'GRA', required: true, }, { displayName: 'Container Name', name: 'containerName', type: 'string', displayOptions: { show: { resource: ['container'], operation: ['create'], }, }, default: '', required: true, placeholder: 'my-storage-container', }, { displayName: 'User Description', name: 'userDescription', type: 'string', displayOptions: { show: { resource: ['user'], operation: ['create'], }, }, default: '', required: true, placeholder: 'Storage user for my application', }, { displayName: 'Object Name', name: 'objectName', type: 'string', displayOptions: { show: { resource: ['presignedUrl'], operation: ['create'], }, }, default: '', required: true, placeholder: 'path/to/my-file.jpg', }, { displayName: 'Expiration (Seconds)', name: 'expire', type: 'number', displayOptions: { show: { resource: ['presignedUrl'], operation: ['create'], }, }, default: 3600, required: true, description: 'URL expiration time in seconds', }, { displayName: 'Method', name: 'method', type: 'options', displayOptions: { show: { resource: ['presignedUrl'], operation: ['create'], }, }, options: [ { name: 'GET', value: 'GET', }, { name: 'PUT', value: 'PUT', }, ], default: 'GET', required: true, }, { displayName: 'Update Fields', name: 'updateFields', type: 'collection', placeholder: 'Add Field', default: {}, displayOptions: { show: { resource: ['container'], operation: ['update'], }, }, options: [ { displayName: 'Versioning', name: 'versioning', type: 'boolean', default: false, description: 'Whether to enable versioning for the container', }, { displayName: 'Container Type', name: 'containerType', type: 'options', options: [ { name: 'Private', value: 'private', }, { name: 'Public', value: 'public', }, { name: 'Static', value: 'static', }, ], default: 'private', }, ], }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); const credentials = await this.getCredentials('ovhApi'); const endpoint = credentials.endpoint; const applicationKey = credentials.applicationKey; const applicationSecret = credentials.applicationSecret; const consumerKey = credentials.consumerKey; for (let i = 0; i < items.length; i++) { try { let responseData; let method = 'GET'; let path = ''; let body = {}; const projectId = this.getNodeParameter('projectId', i); if (resource === 'container') { if (operation === 'get') { const containerId = this.getNodeParameter('containerId', i); path = `/cloud/project/${projectId}/storage/${containerId}`; } else if (operation === 'getAll') { path = `/cloud/project/${projectId}/storage`; } else if (operation === 'create') { method = 'POST'; const containerName = this.getNodeParameter('containerName', i); const region = this.getNodeParameter('region', i); path = `/cloud/project/${projectId}/storage`; body = { containerName, region, }; } else if (operation === 'delete') { method = 'DELETE'; const containerId = this.getNodeParameter('containerId', i); path = `/cloud/project/${projectId}/storage/${containerId}`; } else if (operation === 'update') { method = 'PUT'; const containerId = this.getNodeParameter('containerId', i); const updateFields = this.getNodeParameter('updateFields', i); path = `/cloud/project/${projectId}/storage/${containerId}`; if (updateFields.versioning !== undefined) body.versioning = updateFields.versioning; if (updateFields.containerType) body.containerType = updateFields.containerType; } } else if (resource === 'credential') { if (operation === 'get') { const credentialId = this.getNodeParameter('credentialId', i); path = `/cloud/project/${projectId}/storage/access/${credentialId}`; } else if (operation === 'getAll') { path = `/cloud/project/${projectId}/storage/access`; } else if (operation === 'create') { method = 'POST'; path = `/cloud/project/${projectId}/storage/access`; } else if (operation === 'delete') { method = 'DELETE'; const credentialId = this.getNodeParameter('credentialId', i); path = `/cloud/project/${projectId}/storage/access/${credentialId}`; } } else if (resource === 'user') { if (operation === 'get') { const userId = this.getNodeParameter('userId', i); path = `/cloud/project/${projectId}/user/${userId}`; } else if (operation === 'getAll') { path = `/cloud/project/${projectId}/user`; } else if (operation === 'create') { method = 'POST'; const userDescription = this.getNodeParameter('userDescription', i); path = `/cloud/project/${projectId}/user`; body = { description: userDescription }; } else if (operation === 'delete') { method = 'DELETE'; const userId = this.getNodeParameter('userId', i); path = `/cloud/project/${projectId}/user/${userId}`; } } else if (resource === 'presignedUrl') { if (operation === 'create') { method = 'POST'; const containerId = this.getNodeParameter('containerId', i); const objectName = this.getNodeParameter('objectName', i); const expire = this.getNodeParameter('expire', i); const urlMethod = this.getNodeParameter('method', i); path = `/cloud/project/${projectId}/storage/${containerId}/presign`; body = { expire, method: urlMethod, object: objectName, }; } } else if (resource === 'storage') { if (operation === 'getAll') { path = `/cloud/project/${projectId}/storage`; } } else if (resource === 'region') { if (operation === 'getAll') { path = `/cloud/project/${projectId}/region`; } } const timestamp = Math.round(Date.now() / 1000); const fullUrl = `${endpoint}${path}`; let bodyForSignature = ''; if (method === 'POST' || method === 'PUT') { if (Object.keys(body).length > 0) { bodyForSignature = JSON.stringify(body).replace(/[\u0080-\uFFFF]/g, (m) => { return '\\u' + ('0000' + m.charCodeAt(0).toString(16)).slice(-4); }); } } const signatureElements = [ applicationSecret, consumerKey, method, fullUrl, bodyForSignature, timestamp, ]; const signature = '$1$' + (0, crypto_1.createHash)('sha1').update(signatureElements.join('+')).digest('hex'); const headers = { 'X-Ovh-Application': applicationKey, 'X-Ovh-Consumer': consumerKey, 'X-Ovh-Signature': signature, 'X-Ovh-Timestamp': timestamp.toString(), }; const options = { method, url: fullUrl, headers, json: true, }; if (method === 'POST' || method === 'PUT') { if (Object.keys(body).length > 0) { options.body = body; headers['Content-Type'] = 'application/json'; } } responseData = await this.helpers.request(options); if (method === 'GET' && typeof responseData === 'string') { try { responseData = JSON.parse(responseData); } catch (error) { } } if (Array.isArray(responseData)) { responseData.forEach((item) => { if (typeof item === 'string' || typeof item === 'number') { returnData.push({ json: { value: item } }); } else { returnData.push({ json: item }); } }); } else { returnData.push(responseData); } } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message }, pairedItem: { item: i } }); } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i, }); } } } return [this.helpers.returnJsonArray(returnData)]; } } exports.OvhStorage = OvhStorage; //# sourceMappingURL=OvhStorage.node.js.map