n8n-nodes-ovh
Version:
Comprehensive n8n nodes for OVH API integration - Developed by Ascenzia, experts in AI agent creation
829 lines • 34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OvhCompute = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const crypto_1 = require("crypto");
class OvhCompute {
constructor() {
this.description = {
displayName: 'OVH Public Cloud Compute',
name: 'ovhCompute',
icon: 'file:ovh.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Manage OVH Public Cloud Compute instances',
defaults: {
name: 'OVH Public Cloud Compute',
},
inputs: ["main"],
outputs: ["main"],
credentials: [
{
name: 'ovhApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Flavor',
value: 'flavor',
},
{
name: 'Image',
value: 'image',
},
{
name: 'Instance',
value: 'instance',
},
{
name: 'Instance Backup',
value: 'instanceBackup',
},
{
name: 'Region',
value: 'region',
},
{
name: 'SSH Key',
value: 'sshkey',
},
],
default: 'instance',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['instance'],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a new compute instance',
action: 'Create a new compute instance',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a compute instance',
action: 'Delete a compute instance',
},
{
name: 'Get',
value: 'get',
description: 'Get compute instance information',
action: 'Get compute instance information',
},
{
name: 'Get Many',
value: 'getAll',
description: 'Get many compute instances',
action: 'Get many compute instances',
},
{
name: 'Reboot',
value: 'reboot',
description: 'Reboot a compute instance',
action: 'Reboot a compute instance',
},
{
name: 'Reinstall',
value: 'reinstall',
description: 'Reinstall a compute instance',
action: 'Reinstall a compute instance',
},
{
name: 'Resize',
value: 'resize',
description: 'Resize a compute instance',
action: 'Resize a compute instance',
},
{
name: 'Start',
value: 'start',
description: 'Start a compute instance',
action: 'Start a compute instance',
},
{
name: 'Stop',
value: 'stop',
description: 'Stop a compute instance',
action: 'Stop a compute instance',
},
{
name: 'Update',
value: 'update',
description: 'Update compute instance',
action: 'Update compute instance',
},
],
default: 'get',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['instanceBackup'],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create an instance backup',
action: 'Create an instance backup',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete an instance backup',
action: 'Delete an instance backup',
},
{
name: 'Get',
value: 'get',
description: 'Get instance backup information',
action: 'Get instance backup information',
},
{
name: 'Get Many',
value: 'getAll',
description: 'Get many instance backups',
action: 'Get many instance backups',
},
{
name: 'Restore',
value: 'restore',
description: 'Restore from instance backup',
action: 'Restore from instance backup',
},
],
default: 'get',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['sshkey'],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a new SSH key',
action: 'Create a new SSH key',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete an SSH key',
action: 'Delete an SSH key',
},
{
name: 'Get',
value: 'get',
description: 'Get SSH key information',
action: 'Get SSH key information',
},
{
name: 'Get Many',
value: 'getAll',
description: 'Get many SSH keys',
action: 'Get many SSH keys',
},
],
default: 'get',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['flavor'],
},
},
options: [
{
name: 'Get',
value: 'get',
description: 'Get flavor information',
action: 'Get flavor information',
},
{
name: 'Get Many',
value: 'getAll',
description: 'Get many flavors',
action: 'Get many flavors',
},
],
default: 'getAll',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['image'],
},
},
options: [
{
name: 'Get',
value: 'get',
description: 'Get image information',
action: 'Get image information',
},
{
name: 'Get Many',
value: 'getAll',
description: 'Get many images',
action: 'Get many images',
},
],
default: 'getAll',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['region'],
},
},
options: [
{
name: 'Get Many',
value: 'getAll',
description: 'Get many regions',
action: 'Get many regions',
},
],
default: 'getAll',
},
{
displayName: 'Project ID',
name: 'projectId',
type: 'string',
default: '',
required: true,
placeholder: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
description: 'The cloud project ID',
},
{
displayName: 'Instance ID',
name: 'instanceId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: ['instance'],
operation: [
'get',
'delete',
'update',
'start',
'stop',
'reboot',
'reinstall',
'resize',
],
},
},
},
{
displayName: 'Instance ID',
name: 'instanceId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: ['instanceBackup'],
operation: ['getAll', 'create', 'get', 'delete', 'restore'],
},
},
},
{
displayName: 'Backup ID',
name: 'backupId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: ['instanceBackup'],
operation: ['get', 'delete', 'restore'],
},
},
},
{
displayName: 'SSH Key ID',
name: 'sshKeyId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: ['sshkey'],
operation: ['get', 'delete'],
},
},
},
{
displayName: 'Flavor ID',
name: 'flavorId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: ['flavor'],
operation: ['get'],
},
},
},
{
displayName: 'Image ID',
name: 'imageId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: ['image'],
operation: ['get'],
},
},
},
{
displayName: 'Name',
name: 'name',
type: 'string',
displayOptions: {
show: {
resource: ['instance'],
operation: ['create'],
},
},
default: '',
required: true,
placeholder: 'my-instance',
},
{
displayName: 'Flavor ID',
name: 'flavorId',
type: 'string',
displayOptions: {
show: {
resource: ['instance'],
operation: ['create'],
},
},
default: '',
required: true,
placeholder: 'b2-7',
},
{
displayName: 'Image ID',
name: 'imageId',
type: 'string',
displayOptions: {
show: {
resource: ['instance'],
operation: ['create'],
},
},
default: '',
required: true,
placeholder: 'ubuntu-22.04',
},
{
displayName: 'Region',
name: 'region',
type: 'options',
displayOptions: {
show: {
resource: ['instance'],
operation: ['create'],
},
},
options: [
{
name: 'BHS5',
value: 'BHS5',
},
{
name: 'DE1',
value: 'DE1',
},
{
name: 'GRA7',
value: 'GRA7',
},
{
name: 'SBG5',
value: 'SBG5',
},
{
name: 'UK1',
value: 'UK1',
},
{
name: 'WAW1',
value: 'WAW1',
},
],
default: 'GRA7',
required: true,
},
{
displayName: 'SSH Key ID',
name: 'sshKeyId',
type: 'string',
displayOptions: {
show: {
resource: ['instance'],
operation: ['create'],
},
},
default: '',
placeholder: 'my-ssh-key',
},
{
displayName: 'SSH Key Name',
name: 'sshKeyName',
type: 'string',
displayOptions: {
show: {
resource: ['sshkey'],
operation: ['create'],
},
},
default: '',
required: true,
placeholder: 'my-ssh-key',
},
{
displayName: 'Public Key',
name: 'publicKey',
type: 'string',
displayOptions: {
show: {
resource: ['sshkey'],
operation: ['create'],
},
},
default: '',
required: true,
placeholder: 'ssh-rsa AAAAB3NzaC1yc2E...',
},
{
displayName: 'Backup Name',
name: 'backupName',
type: 'string',
displayOptions: {
show: {
resource: ['instanceBackup'],
operation: ['create'],
},
},
default: '',
required: true,
placeholder: 'my-backup',
},
{
displayName: 'New Flavor ID',
name: 'newFlavorId',
type: 'string',
displayOptions: {
show: {
resource: ['instance'],
operation: ['resize'],
},
},
default: '',
required: true,
placeholder: 'b2-15',
},
{
displayName: 'New Image ID',
name: 'newImageId',
type: 'string',
displayOptions: {
show: {
resource: ['instance'],
operation: ['reinstall'],
},
},
default: '',
required: true,
placeholder: 'ubuntu-22.04',
},
{
displayName: 'Update Fields',
name: 'updateFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: ['instance'],
operation: ['update'],
},
},
options: [
{
displayName: 'Instance Name',
name: 'instanceName',
type: 'string',
default: '',
},
],
},
{
displayName: 'Reboot Type',
name: 'rebootType',
type: 'options',
displayOptions: {
show: {
resource: ['instance'],
operation: ['reboot'],
},
},
options: [
{
name: 'Soft',
value: 'soft',
},
{
name: 'Hard',
value: 'hard',
},
],
default: 'soft',
},
],
};
}
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 === 'instance') {
if (operation === 'get') {
const instanceId = this.getNodeParameter('instanceId', i);
path = `/cloud/project/${projectId}/instance/${instanceId}`;
}
else if (operation === 'getAll') {
path = `/cloud/project/${projectId}/instance`;
}
else if (operation === 'create') {
method = 'POST';
const name = this.getNodeParameter('name', i);
const flavorId = this.getNodeParameter('flavorId', i);
const imageId = this.getNodeParameter('imageId', i);
const region = this.getNodeParameter('region', i);
const sshKeyId = this.getNodeParameter('sshKeyId', i);
path = `/cloud/project/${projectId}/instance`;
body = {
name,
flavorId,
imageId,
region,
};
if (sshKeyId)
body.sshKeyId = sshKeyId;
}
else if (operation === 'delete') {
method = 'DELETE';
const instanceId = this.getNodeParameter('instanceId', i);
path = `/cloud/project/${projectId}/instance/${instanceId}`;
}
else if (operation === 'update') {
method = 'PUT';
const instanceId = this.getNodeParameter('instanceId', i);
const updateFields = this.getNodeParameter('updateFields', i);
path = `/cloud/project/${projectId}/instance/${instanceId}`;
if (updateFields.instanceName)
body.instanceName = updateFields.instanceName;
}
else if (operation === 'start') {
method = 'POST';
const instanceId = this.getNodeParameter('instanceId', i);
path = `/cloud/project/${projectId}/instance/${instanceId}/start`;
}
else if (operation === 'stop') {
method = 'POST';
const instanceId = this.getNodeParameter('instanceId', i);
path = `/cloud/project/${projectId}/instance/${instanceId}/stop`;
}
else if (operation === 'reboot') {
method = 'POST';
const instanceId = this.getNodeParameter('instanceId', i);
const rebootType = this.getNodeParameter('rebootType', i);
path = `/cloud/project/${projectId}/instance/${instanceId}/reboot`;
body = { type: rebootType };
}
else if (operation === 'reinstall') {
method = 'POST';
const instanceId = this.getNodeParameter('instanceId', i);
const newImageId = this.getNodeParameter('newImageId', i);
path = `/cloud/project/${projectId}/instance/${instanceId}/reinstall`;
body = { imageId: newImageId };
}
else if (operation === 'resize') {
method = 'POST';
const instanceId = this.getNodeParameter('instanceId', i);
const newFlavorId = this.getNodeParameter('newFlavorId', i);
path = `/cloud/project/${projectId}/instance/${instanceId}/resize`;
body = { flavorId: newFlavorId };
}
}
else if (resource === 'instanceBackup') {
const instanceId = this.getNodeParameter('instanceId', i);
if (operation === 'get') {
const backupId = this.getNodeParameter('backupId', i);
path = `/cloud/project/${projectId}/instance/${instanceId}/backup/${backupId}`;
}
else if (operation === 'getAll') {
path = `/cloud/project/${projectId}/instance/${instanceId}/backup`;
}
else if (operation === 'create') {
method = 'POST';
const backupName = this.getNodeParameter('backupName', i);
path = `/cloud/project/${projectId}/instance/${instanceId}/backup`;
body = { snapshotName: backupName };
}
else if (operation === 'delete') {
method = 'DELETE';
const backupId = this.getNodeParameter('backupId', i);
path = `/cloud/project/${projectId}/instance/${instanceId}/backup/${backupId}`;
}
else if (operation === 'restore') {
method = 'POST';
const backupId = this.getNodeParameter('backupId', i);
path = `/cloud/project/${projectId}/instance/${instanceId}/backup/${backupId}/restore`;
}
}
else if (resource === 'sshkey') {
if (operation === 'get') {
const sshKeyId = this.getNodeParameter('sshKeyId', i);
path = `/cloud/project/${projectId}/sshkey/${sshKeyId}`;
}
else if (operation === 'getAll') {
path = `/cloud/project/${projectId}/sshkey`;
}
else if (operation === 'create') {
method = 'POST';
const sshKeyName = this.getNodeParameter('sshKeyName', i);
const publicKey = this.getNodeParameter('publicKey', i);
path = `/cloud/project/${projectId}/sshkey`;
body = {
name: sshKeyName,
publicKey,
};
}
else if (operation === 'delete') {
method = 'DELETE';
const sshKeyId = this.getNodeParameter('sshKeyId', i);
path = `/cloud/project/${projectId}/sshkey/${sshKeyId}`;
}
}
else if (resource === 'flavor') {
if (operation === 'get') {
const flavorId = this.getNodeParameter('flavorId', i);
path = `/cloud/project/${projectId}/flavor/${flavorId}`;
}
else if (operation === 'getAll') {
path = `/cloud/project/${projectId}/flavor`;
}
}
else if (resource === 'image') {
if (operation === 'get') {
const imageId = this.getNodeParameter('imageId', i);
path = `/cloud/project/${projectId}/image/${imageId}`;
}
else if (operation === 'getAll') {
path = `/cloud/project/${projectId}/image`;
}
}
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.OvhCompute = OvhCompute;
//# sourceMappingURL=OvhCompute.node.js.map