n8n-nodes-awx
Version:
n8n node to interact with Ansible AWX/Tower with improved type safety
85 lines • 5.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleGroupOperations = handleGroupOperations;
const n8n_workflow_1 = require("n8n-workflow");
const SharedHelpers_1 = require("../utils/SharedHelpers");
const resourceHelpers_1 = require("../utils/resourceHelpers");
async function handleGroupOperations(action, itemIndex = 0) {
try {
if (action === 'list') {
const filter = this.getNodeParameter('filter', itemIndex, '');
const returnAll = this.getNodeParameter('returnAll', itemIndex, false);
const endpoint = '/groups/';
const queryParams = {};
const searchFilter = (0, resourceHelpers_1.normalizeSearchFilter)(filter);
if (searchFilter) {
queryParams.search = searchFilter;
}
const fetchMode = returnAll ? 'allPages' : 'firstPage';
const groups = await SharedHelpers_1.getAwxResources.call(this, endpoint, fetchMode, queryParams);
const responseFormat = this.getNodeParameter('responseFormat', itemIndex, 'friendly');
const formattedGroups = groups.map(group => (0, resourceHelpers_1.formatResourceResponse)(group, responseFormat, 'group'));
return [this.helpers.returnJsonArray(formattedGroups)];
}
else if (action === 'get') {
const { id, name } = resourceHelpers_1.resolveResourceParameters.call(this, ['groupId'], ['groupName'], 'group', itemIndex);
const group = await SharedHelpers_1.getAwxResource.call(this, 'groups', { id, name });
const responseFormat = this.getNodeParameter('responseFormat', itemIndex, 'friendly');
const formattedGroup = (0, resourceHelpers_1.formatResourceResponse)(group, responseFormat, 'group');
return [[{ json: formattedGroup }]];
}
else if (action === 'create') {
const name = this.getNodeParameter('name', itemIndex);
const description = this.getNodeParameter('description', itemIndex, '');
const inventoryId = this.getNodeParameter('inventoryId', itemIndex);
const groupData = {
name,
description,
inventory: inventoryId,
};
const group = await SharedHelpers_1.createAwxResource.call(this, 'groups', groupData);
const responseFormat = this.getNodeParameter('responseFormat', itemIndex, 'friendly');
const formattedGroup = (0, resourceHelpers_1.formatResourceResponse)(group, responseFormat, 'group');
return [[{ json: formattedGroup }]];
}
else if (action === 'update') {
const { id, name } = resourceHelpers_1.resolveResourceParameters.call(this, ['groupId'], ['groupName'], 'group', itemIndex);
let resolvedGroupId = id;
if (!resolvedGroupId && name) {
const group = await SharedHelpers_1.getAwxResource.call(this, 'groups', { name });
resolvedGroupId = group.id;
}
if (!resolvedGroupId) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Group ID could not be determined for update.');
}
const updateFields = this.getNodeParameter('updateFields', itemIndex, {});
const group = await SharedHelpers_1.updateAwxResource.call(this, `/groups/${resolvedGroupId}/`, updateFields);
const responseFormat = this.getNodeParameter('responseFormat', itemIndex, 'friendly');
const formattedGroup = (0, resourceHelpers_1.formatResourceResponse)(group, responseFormat, 'group');
return [[{ json: formattedGroup }]];
}
else if (action === 'delete') {
const { id, name } = resourceHelpers_1.resolveResourceParameters.call(this, ['groupId'], ['groupName'], 'group', itemIndex);
let resolvedGroupId = id;
if (!resolvedGroupId && name) {
const group = await SharedHelpers_1.getAwxResource.call(this, 'groups', { name });
resolvedGroupId = group.id;
}
if (!resolvedGroupId) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Group ID could not be determined for deletion.');
}
await SharedHelpers_1.deleteAwxResourceWithLogging.call(this, `/groups/${resolvedGroupId}/`, itemIndex);
return [
this.helpers.returnJsonArray([{
success: true,
message: `Group ${name || resolvedGroupId} deleted successfully.`,
}]),
];
}
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The action "${action}" is not supported for groups`);
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to execute group operation: ${error.message}`, { itemIndex });
}
}
//# sourceMappingURL=GroupResources.js.map