n8n/packages/nodes-base/nodes/Rundeck/Rundeck.node.ts

199 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-03-12 11:57:57 -07:00
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
2020-03-15 11:20:41 -07:00
import { RundeckApi } from './RundeckApi';
2020-03-12 11:57:57 -07:00
export class Rundeck implements INodeType {
description: INodeTypeDescription = {
displayName: 'Rundeck',
name: 'rundeck',
icon: 'file:rundeck.png',
group: ['transform'],
version: 1,
2020-03-15 11:20:41 -07:00
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
2020-03-12 11:57:57 -07:00
description: 'Manage Rundeck API',
defaults: {
name: 'Rundeck',
color: '#F73F39',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
2020-03-15 11:20:41 -07:00
name: 'rundeckApi',
2020-03-12 11:57:57 -07:00
required: true,
}
],
properties: [
2020-03-15 11:20:41 -07:00
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Job',
value: 'job',
},
],
default: 'job',
description: 'The resource to operate on.',
},
2020-03-12 11:57:57 -07:00
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
2020-03-15 11:20:41 -07:00
name: 'Execute',
value: 'execute',
description: 'Execute a job',
2020-03-15 11:20:41 -07:00
},
{
name: 'Get Metadata',
value: 'getMetadata',
description: 'Get metadata of a job',
2020-03-12 11:57:57 -07:00
},
],
2020-03-15 11:20:41 -07:00
default: 'execute',
2020-03-12 11:57:57 -07:00
description: 'The operation to perform.',
},
// ----------------------------------
2020-03-15 11:20:41 -07:00
// job:execute
2020-03-12 11:57:57 -07:00
// ----------------------------------
{
displayName: 'Job Id',
name: 'jobid',
type: 'string',
displayOptions: {
show: {
operation: [
2020-03-15 11:20:41 -07:00
'execute',
],
resource: [
'job',
2020-03-12 11:57:57 -07:00
],
},
},
default: '',
placeholder: 'Rundeck Job Id',
required: true,
description: 'The job Id to execute.',
},
{
displayName: 'Arguments',
name: 'arguments',
2020-03-15 11:20:41 -07:00
placeholder: 'Add Argument',
2020-03-12 11:57:57 -07:00
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
displayOptions: {
show: {
operation: [
2020-03-15 11:20:41 -07:00
'execute',
],
resource: [
'job',
2020-03-12 11:57:57 -07:00
],
},
},
default: {},
options: [
{
name: 'arguments',
2020-03-15 11:20:41 -07:00
displayName: 'Arguments',
2020-03-12 11:57:57 -07:00
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
},
]
},
],
},
2020-03-15 11:20:41 -07:00
// ----------------------------------
// job:getMetadata
// ----------------------------------
{
displayName: 'Job Id',
name: 'jobid',
type: 'string',
displayOptions: {
show: {
operation: [
'getMetadata',
],
resource: [
'job',
],
},
},
default: '',
placeholder: 'Rundeck Job Id',
required: true,
description: 'The job Id to get metadata off.',
},
],
2020-03-12 11:57:57 -07:00
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
// Input data
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = items.length as unknown as number;
const operation = this.getNodeParameter('operation', 0) as string;
2020-03-15 11:20:41 -07:00
const resource = this.getNodeParameter('resource', 0) as string;
2020-03-12 11:57:57 -07:00
for (let i = 0; i < length; i++) {
2020-03-15 11:20:41 -07:00
const rundeckApi = new RundeckApi(this);
if (resource === 'job') {
if (operation === 'execute') {
// ----------------------------------
// job: execute
// ----------------------------------
const jobid = this.getNodeParameter('jobid', i) as string;
const rundeckArguments = (this.getNodeParameter('arguments', i) as IDataObject).arguments as IDataObject[];
const response = await rundeckApi.executeJob(jobid, rundeckArguments);
returnData.push(response);
} else if (operation === 'getMetadata') {
// ----------------------------------
// job: getMetadata
// ----------------------------------
const jobid = this.getNodeParameter('jobid', i) as string;
const response = await rundeckApi.getJobMetadata(jobid);
returnData.push(response);
} else {
throw new Error(`The operation "${operation}" is not supported!`);
2020-03-12 11:57:57 -07:00
}
} else {
2020-03-15 11:20:41 -07:00
throw new Error(`The resource "${resource}" is not supported!`);
2020-03-12 11:57:57 -07:00
}
}
2020-03-15 11:20:41 -07:00
2020-03-12 11:57:57 -07:00
return [this.helpers.returnJsonArray(returnData)];
2020-03-15 11:20:41 -07:00
2020-03-12 11:57:57 -07:00
}
}