2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2023-03-09 09:13:15 -08:00
|
|
|
import type { IDataObject, IExecuteFunctions, JsonObject } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
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) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.executeFunctions.getNode(), error as JsonObject);
|
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
|
|
|
|
2023-07-24 07:01:49 -07:00
|
|
|
async executeJob(jobId: string, args: IDataObject[], filter?: string): 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
|
|
|
};
|
|
|
|
|
2023-07-24 07:01:49 -07:00
|
|
|
const query: IDataObject = {};
|
|
|
|
if (filter) {
|
|
|
|
query.filter = filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.request('POST', `/api/14/job/${jobId}/run`, body, query);
|
2020-03-15 11:20:41 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|