Improved Rundeck-Node

This commit is contained in:
Jan Oberhauser 2020-03-15 19:20:41 +01:00
parent 07f0b95608
commit 1d43cbf3e7
6 changed files with 143 additions and 151 deletions

View file

@ -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',

View file

@ -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<T, R = AxiosResponse<T>> (config: AxiosRequestConfig): Promise<R> {
return this.api.request(config);
}
protected get<T, R = AxiosResponse<T>> (url: string, config?: AxiosRequestConfig): Promise<R> {
return this.api.get(url, config);
}
protected delete<T, R = AxiosResponse<T>> (url: string, config?: AxiosRequestConfig): Promise<R> {
return this.api.delete(url, config);
}
protected head<T, R = AxiosResponse<T>> (url: string, config?: AxiosRequestConfig): Promise<R> {
return this.api.head(url, config);
}
protected post<T, R = AxiosResponse<T>> (url: string, data?: string, config?: AxiosRequestConfig): Promise<R> {
return this.api.post(url, data, config);
}
protected put<T, R = AxiosResponse<T>> (url: string, data?: string, config?: AxiosRequestConfig): Promise<R> {
return this.api.put(url, data, config);
}
protected patch<T, R = AxiosResponse<T>> (url: string, data?: string, config?: AxiosRequestConfig): Promise<R> {
return this.api.patch(url, data, config);
}
}

View file

@ -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.',
},
],
};
@ -115,53 +160,35 @@ export class Rundeck implements INodeType {
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;
const rundeckApi = new RundeckApi(this);
try {
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);
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);
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;
}
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!`);
}
}

View file

@ -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<Xml> {
acceptVersion(this.credentials.apiVersion, 12);
executeJob(jobId: string, args: IDataObject[]): Promise<IDataObject> {
let params = '';
@ -50,14 +63,15 @@ export class RundeckApi extends Api {
}
}
return this.post<Xml>(this.credentials.url + '/api/12/job/' + jobId + '/run?argString=' + params)
.then((response: AxiosResponse<Xml>) => {
const { data } = response;
return data;
})
.catch((error: AxiosError) => {
throw error;
});
const body = {
argString: params
};
return this.request('POST', `/api/14/job/${jobId}/run`, body, {});
}
getJobMetadata(jobId: string): Promise<IDataObject> {
return this.request('GET', `/api/18/job/${jobId}/info`, {}, {});
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 825 B

After

Width:  |  Height:  |  Size: 319 B

View file

@ -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",