2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
|
|
|
import type { IDataObject } from 'n8n-workflow';
|
|
|
|
import { NodeApiError, NodeOperationError } 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 {
|
2021-10-06 11:14:53 -07:00
|
|
|
private credentials?: RundeckCredentials;
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2020-03-15 11:20:41 -07:00
|
|
|
private executeFunctions: IExecuteFunctions;
|
|
|
|
|
|
|
|
constructor(executeFunctions: IExecuteFunctions) {
|
2021-04-16 09:33:36 -07:00
|
|
|
this.executeFunctions = executeFunctions;
|
2020-03-13 09:02:54 -07:00
|
|
|
}
|
2020-03-12 11:57:57 -07:00
|
|
|
|
2020-03-15 11:20:41 -07:00
|
|
|
protected async request(method: string, endpoint: string, body: IDataObject, query: object) {
|
2022-10-16 06:26:31 -07:00
|
|
|
const credentialType = 'rundeckApi';
|
|
|
|
|
2020-03-15 11:20:41 -07:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
method,
|
|
|
|
qs: query,
|
2023-01-13 09:11:56 -08:00
|
|
|
uri: (this.credentials?.url as string) + endpoint,
|
2020-03-15 11:20:41 -07:00
|
|
|
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 {
|
2022-10-16 06:26:31 -07:00
|
|
|
return await this.executeFunctions.helpers.requestWithAuthentication.call(
|
|
|
|
this.executeFunctions,
|
|
|
|
credentialType,
|
|
|
|
options,
|
|
|
|
);
|
2020-03-15 11:20:41 -07:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.executeFunctions.getNode(), error);
|
2020-03-15 11:20:41 -07:00
|
|
|
}
|
2020-03-13 09:02:54 -07:00
|
|
|
}
|
|
|
|
|
2021-10-06 11:14:53 -07:00
|
|
|
async init() {
|
|
|
|
const credentials = await this.executeFunctions.getCredentials('rundeckApi');
|
|
|
|
|
|
|
|
if (credentials === undefined) {
|
|
|
|
throw new NodeOperationError(this.executeFunctions.getNode(), 'No credentials got returned!');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.credentials = credentials as unknown as RundeckCredentials;
|
|
|
|
}
|
2020-03-13 09:02:54 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
async executeJob(jobId: string, args: IDataObject[]): Promise<IDataObject> {
|
2020-03-13 09:02:54 -07:00
|
|
|
let params = '';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
if (args) {
|
|
|
|
for (const arg of args) {
|
2023-01-13 09:11:56 -08:00
|
|
|
params += '-' + (arg.name as string) + ' ' + (arg.value as string) + ' ';
|
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, {});
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
async getJobMetadata(jobId: string): Promise<IDataObject> {
|
2020-03-15 11:20:41 -07:00
|
|
|
return this.request('GET', `/api/18/job/${jobId}/info`, {}, {});
|
2020-03-13 09:02:54 -07:00
|
|
|
}
|
2020-03-15 11:20:41 -07:00
|
|
|
}
|