diff --git a/packages/nodes-base/credentials/Rundeck.credentials.ts b/packages/nodes-base/credentials/RundeckApi.credentials.ts similarity index 58% rename from packages/nodes-base/credentials/Rundeck.credentials.ts rename to packages/nodes-base/credentials/RundeckApi.credentials.ts index 5285e59c5a..5f6c7422f7 100644 --- a/packages/nodes-base/credentials/Rundeck.credentials.ts +++ b/packages/nodes-base/credentials/RundeckApi.credentials.ts @@ -4,21 +4,16 @@ import { } from 'n8n-workflow'; -export class Rundeck implements ICredentialType { - name = 'rundeck'; - displayName = 'Rundeck'; +export class RundeckApi implements ICredentialType { + name = 'rundeckApi'; + displayName = 'Rundeck API'; properties = [ { displayName: 'Url', name: 'url', type: 'string' as NodePropertyTypes, default: '', - }, - { - displayName: 'Api Version', - name: 'apiVersion', - type: 'number' as NodePropertyTypes, - default: '', + placeholder: 'http://127.0.0.1:4440', }, { displayName: 'Token', diff --git a/packages/nodes-base/nodes/Rundeck/Api.ts b/packages/nodes-base/nodes/Rundeck/Api.ts deleted file mode 100644 index deac944ec1..0000000000 --- a/packages/nodes-base/nodes/Rundeck/Api.ts +++ /dev/null @@ -1,44 +0,0 @@ -import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios"; - -export class Api { - private api: AxiosInstance; - - constructor (config: AxiosRequestConfig) { - this.api = axios.create(config); - this.api.interceptors.request.use((param: AxiosRequestConfig) => ({ - ...param - })); - } - - protected getUri (config?: AxiosRequestConfig): string { - return this.api.getUri(config); - } - - protected request> (config: AxiosRequestConfig): Promise { - return this.api.request(config); - } - - protected get> (url: string, config?: AxiosRequestConfig): Promise { - return this.api.get(url, config); - } - - protected delete> (url: string, config?: AxiosRequestConfig): Promise { - return this.api.delete(url, config); - } - - protected head> (url: string, config?: AxiosRequestConfig): Promise { - return this.api.head(url, config); - } - - protected post> (url: string, data?: string, config?: AxiosRequestConfig): Promise { - return this.api.post(url, data, config); - } - - protected put> (url: string, data?: string, config?: AxiosRequestConfig): Promise { - return this.api.put(url, data, config); - } - - protected patch> (url: string, data?: string, config?: AxiosRequestConfig): Promise { - return this.api.patch(url, data, config); - } -} \ No newline at end of file diff --git a/packages/nodes-base/nodes/Rundeck/Rundeck.node.ts b/packages/nodes-base/nodes/Rundeck/Rundeck.node.ts index d9cb059cea..bae6b9a07f 100644 --- a/packages/nodes-base/nodes/Rundeck/Rundeck.node.ts +++ b/packages/nodes-base/nodes/Rundeck/Rundeck.node.ts @@ -5,8 +5,7 @@ import { INodeType, INodeTypeDescription, } from 'n8n-workflow'; -import { Parser } from 'xml2js'; -import { RundeckApi, RundeckCredentials } from "./RundeckApi"; +import { RundeckApi } from './RundeckApi'; export class Rundeck implements INodeType { description: INodeTypeDescription = { @@ -15,6 +14,7 @@ export class Rundeck implements INodeType { icon: 'file:rundeck.png', group: ['transform'], version: 1, + subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Manage Rundeck API', defaults: { name: 'Rundeck', @@ -24,28 +24,46 @@ export class Rundeck implements INodeType { outputs: ['main'], credentials: [ { - name: 'rundeck', + name: 'rundeckApi', required: true, } ], properties: [ + { + displayName: 'Resource', + name: 'resource', + type: 'options', + options: [ + { + name: 'Job', + value: 'job', + }, + ], + default: 'job', + description: 'The resource to operate on.', + }, { displayName: 'Operation', name: 'operation', type: 'options', options: [ { - name: 'Execute Job', - value: 'executeJob', - description: 'Executes Job.', + name: 'Execute', + value: 'execute', + description: 'Executes job', + }, + { + name: 'Get Metadata', + value: 'getMetadata', + description: 'Get metadata of a job', }, ], - default: 'executeJob', + default: 'execute', description: 'The operation to perform.', }, // ---------------------------------- - // JobId + // job:execute // ---------------------------------- { displayName: 'Job Id', @@ -54,7 +72,10 @@ export class Rundeck implements INodeType { displayOptions: { show: { operation: [ - 'executeJob' + 'execute', + ], + resource: [ + 'job', ], }, }, @@ -63,14 +84,10 @@ export class Rundeck implements INodeType { required: true, description: 'The job Id to execute.', }, - - // ---------------------------------- - // Arguments - // ---------------------------------- { displayName: 'Arguments', name: 'arguments', - placeholder: 'Arguments', + placeholder: 'Add Argument', type: 'fixedCollection', typeOptions: { multipleValues: true, @@ -78,7 +95,10 @@ export class Rundeck implements INodeType { displayOptions: { show: { operation: [ - 'executeJob' + 'execute', + ], + resource: [ + 'job', ], }, }, @@ -86,7 +106,7 @@ export class Rundeck implements INodeType { options: [ { name: 'arguments', - displayName: 'Add argument', + displayName: 'Arguments', values: [ { displayName: 'Name', @@ -104,7 +124,32 @@ export class Rundeck implements INodeType { }, ], }, - ] + + + // ---------------------------------- + // 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.', + }, + ], + }; @@ -114,58 +159,40 @@ export class Rundeck implements INodeType { const items = this.getInputData(); const returnData: IDataObject[] = []; const length = items.length as unknown as number; - - const credentials = this.getCredentials('rundeck'); - - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } - - const rundeckCredentials: RundeckCredentials = { - url: credentials.url as string, - apiVersion: credentials.apiVersion as number, - token: credentials.token as string - }; const operation = this.getNodeParameter('operation', 0) as string; + const resource = this.getNodeParameter('resource', 0) as string; for (let i = 0; i < length; i++) { - if (operation === 'executeJob') { - // ---------------------------------- - // executeJob - // ---------------------------------- - const rundeckApi = new RundeckApi(rundeckCredentials); - const jobid = this.getNodeParameter('jobid', i) as string; - const rundeckArguments = (this.getNodeParameter('arguments', i) as IDataObject).arguments as IDataObject[]; - let response; - - try { - - response = await rundeckApi.executeJob(jobid, rundeckArguments); - - const parser = new Parser({ - mergeAttrs: true, - explicitArray: false, - }); - - // @ts-ignore - const json = await parser.parseStringPromise(response); - returnData.push({ json }); - - } catch(error) { - if(error.response && error.response.data && error.response.status) { - throw Error(`status: ${error.response.status}, response: ${(error.response.data).replace('\n', '')}`); - } else { - throw error; - } + 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!`); } - } else { - throw new Error(`The operation "${operation}" is not supported!`); + throw new Error(`The resource "${resource}" is not supported!`); } } - + return [this.helpers.returnJsonArray(returnData)]; - + } } diff --git a/packages/nodes-base/nodes/Rundeck/RundeckApi.ts b/packages/nodes-base/nodes/Rundeck/RundeckApi.ts index 23edf71f7e..6b5fdfb202 100644 --- a/packages/nodes-base/nodes/Rundeck/RundeckApi.ts +++ b/packages/nodes-base/nodes/Rundeck/RundeckApi.ts @@ -1,46 +1,59 @@ -import { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios"; -import { Api } from "./Api"; -import * as https from 'https'; -import { Xml } from "../Xml.node"; -import { IDataObject } from "n8n-workflow"; +import { OptionsWithUri } from 'request'; +import { IExecuteFunctions } from 'n8n-core'; +import { IDataObject } from 'n8n-workflow'; export interface RundeckCredentials { url: string; - apiVersion: number; token: string; } -function acceptVersion(credentialsApiVersion: number, apiVersion: number) { - if(apiVersion > credentialsApiVersion) { - throw Error('This endpoint is not supported for this version!'); - } -} - -export class RundeckApi extends Api { - +export class RundeckApi { private credentials: RundeckCredentials; + private executeFunctions: IExecuteFunctions; - constructor (credentials: RundeckCredentials) { - const config: AxiosRequestConfig = { - httpsAgent: new https.Agent({ - rejectUnauthorized: false - }), + constructor(executeFunctions: IExecuteFunctions) { + + const credentials = executeFunctions.getCredentials('rundeckApi'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + this.credentials = credentials as unknown as RundeckCredentials; + this.executeFunctions = executeFunctions; + } + + + protected async request(method: string, endpoint: string, body: IDataObject, query: object) { + + const options: OptionsWithUri = { headers: { - 'Accept': 'application/xml', 'user-agent': 'n8n', - 'X-Rundeck-Auth-Token': credentials.token, - } + 'X-Rundeck-Auth-Token': this.credentials.token, + }, + rejectUnauthorized: false, + method, + qs: query, + uri: this.credentials.url + endpoint, + body, + json: true }; - super(config); - this.credentials = credentials; + try { + return await this.executeFunctions.helpers.request!(options); + } catch (error) { + let errorMessage = error.message; + if (error.response && error.response.body && error.response.body.message) { + errorMessage = error.response.body.message.replace('\n', ''); + } + throw Error(`Rundeck Error [${error.statusCode}]: ${errorMessage}`); + } } - executeJob(jobId: string, args: IDataObject[]): Promise { - acceptVersion(this.credentials.apiVersion, 12); + executeJob(jobId: string, args: IDataObject[]): Promise { let params = ''; @@ -49,15 +62,16 @@ export class RundeckApi extends Api { params += "-" + arg.name + " " + arg.value + " "; } } - - return this.post(this.credentials.url + '/api/12/job/' + jobId + '/run?argString=' + params) - .then((response: AxiosResponse) => { - const { data } = response; - return data; - }) - .catch((error: AxiosError) => { - throw error; - }); + + const body = { + argString: params + }; + + return this.request('POST', `/api/14/job/${jobId}/run`, body, {}); } -} \ No newline at end of file + getJobMetadata(jobId: string): Promise { + return this.request('GET', `/api/18/job/${jobId}/info`, {}, {}); + } + +} diff --git a/packages/nodes-base/nodes/Rundeck/rundeck.png b/packages/nodes-base/nodes/Rundeck/rundeck.png index b55f1973ae..45084aea60 100644 Binary files a/packages/nodes-base/nodes/Rundeck/rundeck.png and b/packages/nodes-base/nodes/Rundeck/rundeck.png differ diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 62a6a3b4db..f60f638497 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -83,7 +83,7 @@ "dist/credentials/Postgres.credentials.js", "dist/credentials/Redis.credentials.js", "dist/credentials/RocketchatApi.credentials.js", - "dist/credentials/Rundeck.credentials.js", + "dist/credentials/RundeckApi.credentials.js", "dist/credentials/ShopifyApi.credentials.js", "dist/credentials/SlackApi.credentials.js", "dist/credentials/Smtp.credentials.js",