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

63 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-03-12 11:57:57 -07:00
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";
export interface RundeckCredentials {
2020-03-13 09:02:54 -07:00
url: string;
apiVersion: number;
token: string;
2020-03-12 11:57:57 -07:00
}
function acceptVersion(credentialsApiVersion: number, apiVersion: number) {
2020-03-13 09:02:54 -07:00
if(apiVersion > credentialsApiVersion) {
throw Error('This endpoint is not supported for this version!');
}
2020-03-12 11:57:57 -07:00
}
export class RundeckApi extends Api {
2020-03-13 09:02:54 -07:00
private credentials: RundeckCredentials;
constructor (credentials: RundeckCredentials) {
const config: AxiosRequestConfig = {
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
headers: {
'Accept': 'application/xml',
'user-agent': 'n8n',
'X-Rundeck-Auth-Token': credentials.token,
}
};
super(config);
this.credentials = credentials;
}
executeJob(jobId: string, args: IDataObject[]): Promise<Xml> {
acceptVersion(this.credentials.apiVersion, 12);
let params = '';
if(args) {
for(const arg of args) {
params += "-" + arg.name + " " + arg.value + " ";
}
}
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;
});
}
2020-03-12 11:57:57 -07:00
}