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

78 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-03-15 11:20:41 -07:00
import { OptionsWithUri } from 'request';
import { IExecuteFunctions } from 'n8n-core';
import { IDataObject } from 'n8n-workflow';
2020-03-12 11:57:57 -07:00
export interface RundeckCredentials {
2020-03-13 09:02:54 -07:00
url: string;
token: string;
2020-03-12 11:57:57 -07:00
}
2020-03-15 11:20:41 -07:00
export class RundeckApi {
private credentials: RundeckCredentials;
private executeFunctions: IExecuteFunctions;
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;
2020-03-13 09:02:54 -07:00
}
2020-03-12 11:57:57 -07:00
2020-03-13 09:02:54 -07:00
2020-03-15 11:20:41 -07:00
protected async request(method: string, endpoint: string, body: IDataObject, query: object) {
2020-03-13 09:02:54 -07:00
2020-03-15 11:20:41 -07:00
const options: OptionsWithUri = {
2020-03-13 09:02:54 -07:00
headers: {
'user-agent': 'n8n',
2020-03-15 11:20:41 -07:00
'X-Rundeck-Auth-Token': this.credentials.token,
},
rejectUnauthorized: false,
method,
qs: query,
uri: this.credentials.url + endpoint,
body,
2020-10-22 06:46:03 -07:00
json: true,
2020-03-13 09:02:54 -07:00
};
2020-03-15 11:20:41 -07:00
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', '');
}
2020-03-13 09:02:54 -07:00
2020-03-15 11:20:41 -07:00
throw Error(`Rundeck Error [${error.statusCode}]: ${errorMessage}`);
}
2020-03-13 09:02:54 -07:00
}
2020-03-15 11:20:41 -07:00
executeJob(jobId: string, args: IDataObject[]): Promise<IDataObject> {
2020-03-13 09:02:54 -07:00
let params = '';
if(args) {
for(const arg of args) {
2021-01-13 11:20:30 -08:00
params += '-' + arg.name + ' ' + arg.value + ' ';
2020-03-13 09:02:54 -07:00
}
}
2020-03-15 11:20:41 -07:00
const body = {
2020-10-22 06:46:03 -07:00
argString: params,
2020-03-15 11:20:41 -07:00
};
return this.request('POST', `/api/14/job/${jobId}/run`, body, {});
}
getJobMetadata(jobId: string): Promise<IDataObject> {
return this.request('GET', `/api/18/job/${jobId}/info`, {}, {});
2020-03-13 09:02:54 -07:00
}
2020-03-12 11:57:57 -07:00
2020-03-15 11:20:41 -07:00
}