2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
|
|
|
import type {
|
2020-04-04 07:04:25 -07:00
|
|
|
IDataObject,
|
2020-04-05 06:49:47 -07:00
|
|
|
ILoadOptionsFunctions,
|
2020-04-04 07:04:25 -07:00
|
|
|
INodeExecutionData,
|
2020-04-05 06:49:47 -07:00
|
|
|
INodePropertyOptions,
|
2020-04-04 07:04:25 -07:00
|
|
|
INodeType,
|
2020-04-05 06:49:47 -07:00
|
|
|
INodeTypeDescription,
|
2020-04-04 07:04:25 -07:00
|
|
|
} from 'n8n-workflow';
|
2022-08-01 13:47:55 -07:00
|
|
|
import { collectionFields, collectionOperations } from './CollectionDescription';
|
2020-04-04 07:04:25 -07:00
|
|
|
import {
|
2020-04-05 06:49:47 -07:00
|
|
|
createCollectionEntry,
|
|
|
|
getAllCollectionEntries,
|
|
|
|
getAllCollectionNames,
|
2020-04-04 07:04:25 -07:00
|
|
|
} from './CollectionFunctions';
|
2022-08-01 13:47:55 -07:00
|
|
|
import { formFields, formOperations } from './FormDescription';
|
2020-04-04 07:04:25 -07:00
|
|
|
import { submitForm } from './FormFunctions';
|
2020-04-11 11:26:15 -07:00
|
|
|
import { createDataFromParameters } from './GenericFunctions';
|
2022-08-01 13:47:55 -07:00
|
|
|
import { singletonFields, singletonOperations } from './SingletonDescription';
|
|
|
|
import { getAllSingletonNames, getSingleton } from './SingletonFunctions';
|
2020-04-04 07:04:25 -07:00
|
|
|
|
|
|
|
export class Cockpit implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Cockpit',
|
|
|
|
name: 'cockpit',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-04-04 07:04:25 -07:00
|
|
|
icon: 'file:cockpit.png',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
2020-04-05 06:49:47 -07:00
|
|
|
subtitle: '={{ $parameter["operation"] + ": " + $parameter["resource"] }}',
|
2020-04-04 07:04:25 -07:00
|
|
|
description: 'Consume Cockpit API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Cockpit',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'cockpitApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-04-11 08:06:57 -07:00
|
|
|
default: 'collection',
|
2020-04-04 07:04:25 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Collection',
|
2020-04-06 01:03:49 -07:00
|
|
|
value: 'collection',
|
2020-04-04 07:04:25 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Form',
|
2020-04-06 01:03:49 -07:00
|
|
|
value: 'form',
|
2020-04-04 07:04:25 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Singleton',
|
2020-04-06 01:03:49 -07:00
|
|
|
value: 'singleton',
|
2020-04-04 07:04:25 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-04-05 06:49:47 -07:00
|
|
|
|
2020-04-04 07:04:25 -07:00
|
|
|
...collectionOperations,
|
|
|
|
...collectionFields,
|
|
|
|
...formOperations,
|
|
|
|
...formFields,
|
|
|
|
...singletonOperations,
|
2020-04-05 06:49:47 -07:00
|
|
|
...singletonFields,
|
2020-04-04 07:04:25 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-04-05 06:49:47 -07:00
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getCollections(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const collections = await getAllCollectionNames.call(this);
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
return collections.map((itemName) => {
|
2020-04-05 06:49:47 -07:00
|
|
|
return {
|
|
|
|
name: itemName,
|
|
|
|
value: itemName,
|
2020-04-06 01:03:49 -07:00
|
|
|
};
|
2020-04-05 06:49:47 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
async getSingletons(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const singletons = await getAllSingletonNames.call(this);
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
return singletons.map((itemName) => {
|
2020-04-05 06:49:47 -07:00
|
|
|
return {
|
|
|
|
name: itemName,
|
|
|
|
value: itemName,
|
2020-04-06 01:03:49 -07:00
|
|
|
};
|
2020-04-05 06:49:47 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-04-04 07:04:25 -07:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-04-04 07:04:25 -07:00
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'collection') {
|
|
|
|
const collectionName = this.getNodeParameter('collection', i) as string;
|
2020-04-11 11:26:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'create') {
|
|
|
|
const data = createDataFromParameters.call(this, i);
|
2020-04-04 07:04:25 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await createCollectionEntry.call(this, collectionName, data);
|
|
|
|
} else if (operation === 'getAll') {
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-04-06 01:03:49 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
options.limit = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-04-04 07:04:25 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await getAllCollectionEntries.call(this, collectionName, options);
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
|
|
const data = createDataFromParameters.call(this, i);
|
2020-04-04 07:04:25 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await createCollectionEntry.call(this, collectionName, data, id);
|
|
|
|
}
|
|
|
|
} else if (resource === 'form') {
|
|
|
|
const formName = this.getNodeParameter('form', i) as string;
|
2020-04-11 11:26:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'submit') {
|
|
|
|
const form = createDataFromParameters.call(this, i);
|
2020-04-04 07:04:25 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await submitForm.call(this, formName, form);
|
|
|
|
}
|
|
|
|
} else if (resource === 'singleton') {
|
|
|
|
const singletonName = this.getNodeParameter('singleton', i) as string;
|
2020-04-11 11:26:15 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
responseData = await getSingleton.call(this, singletonName);
|
|
|
|
}
|
2020-04-04 07:04:25 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-04-04 07:04:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|