UNPKG

n8n-nodes-awx

Version:

n8n node to interact with Ansible AWX/Tower with improved type safety

150 lines 7.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleInventoryOperations = handleInventoryOperations; const n8n_workflow_1 = require("n8n-workflow"); const SharedHelpers_1 = require("../utils/SharedHelpers"); const resourceHelpers_1 = require("../utils/resourceHelpers"); async function handleInventoryOperations(action, itemIndex = 0) { if (action === 'list') { try { const filter = this.getNodeParameter('filter', itemIndex, ''); const returnAll = this.getNodeParameter('returnAll', itemIndex, false); const endpoint = '/inventories/'; const queryParams = {}; if (filter) { queryParams.search = filter; } const fetchMode = returnAll ? 'allPages' : 'firstPage'; const inventories = await SharedHelpers_1.getAwxResources.call(this, endpoint, fetchMode, queryParams); const responseFormat = this.getNodeParameter('responseFormat', itemIndex, 'friendly'); const formattedInventories = inventories.map(inventory => (0, resourceHelpers_1.formatResourceResponse)(inventory, responseFormat, 'inventory')); return [this.helpers.returnJsonArray(formattedInventories)]; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to list inventories: ${error.message}`, { itemIndex }); } } else if (action === 'get') { try { const idKeys = ['inventoryId', 'Inventory_ID', 'id']; const nameKeys = ['inventoryName', 'Inventory_Name', 'name']; const { id, name } = resourceHelpers_1.resolveResourceParameters.call(this, idKeys, nameKeys, 'inventory', itemIndex); const inventory = await SharedHelpers_1.getAwxResource.call(this, 'inventories', { id, name }); const responseFormat = this.getNodeParameter('responseFormat', itemIndex, 'friendly'); const formattedInventory = (0, resourceHelpers_1.formatResourceResponse)(inventory, responseFormat, 'inventory'); return [[{ json: formattedInventory }]]; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to get inventory: ${error.message}`, { itemIndex }); } } else if (action === 'create') { try { const name = this.getNodeParameter('name', itemIndex); const description = this.getNodeParameter('description', itemIndex, ''); const organization = this.getNodeParameter('organization', itemIndex); const variables = this.getNodeParameter('variables', itemIndex, ''); const inventoryData = { name, description, organization, }; if (variables) { try { inventoryData.variables = JSON.parse(variables); } catch (e) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid JSON in variables field'); } } const response = await SharedHelpers_1.createAwxResource.call(this, '/inventories/', inventoryData); return [[{ json: response }]]; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to create inventory: ${error.message}`, { itemIndex }); } } else if (action === 'update') { try { const idKeys = ['inventoryId', 'Inventory_ID', 'id']; const nameKeys = ['inventoryName', 'Inventory_Name', 'name']; const { id, name } = resourceHelpers_1.resolveResourceParameters.call(this, idKeys, nameKeys, 'inventory', itemIndex); let resolvedInventoryId = id; if (!resolvedInventoryId && name) { const inventory = await SharedHelpers_1.getAwxResource.call(this, 'inventories', { name }); resolvedInventoryId = inventory.id; } if (!resolvedInventoryId) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Inventory ID could not be determined.'); } const updateData = {}; try { const newName = this.getNodeParameter('newName', itemIndex, ''); if (newName) { updateData.name = newName; } } catch (error) { } try { const description = this.getNodeParameter('description', itemIndex, ''); if (description) { updateData.description = description; } } catch (error) { } try { const variablesRaw = this.getNodeParameter('variables', itemIndex, ''); if (variablesRaw) { try { updateData.variables = JSON.parse(variablesRaw); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid JSON in variables field'); } } } catch (error) { } try { const organization = this.getNodeParameter('organization', itemIndex, null); if (typeof organization === 'number') { updateData.organization = organization; } } catch (error) { } const response = await SharedHelpers_1.updateAwxResource.call(this, `/inventories/${resolvedInventoryId}/`, updateData); const responseFormat = this.getNodeParameter('responseFormat', itemIndex, 'friendly'); const formattedResponse = (0, resourceHelpers_1.formatResourceResponse)(response, responseFormat, 'inventory'); return [[{ json: formattedResponse }]]; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to update inventory: ${error.message}`, { itemIndex }); } } else if (action === 'delete') { try { const idKeys = ['inventoryId', 'Inventory_ID', 'id']; const nameKeys = ['inventoryName', 'Inventory_Name', 'name']; const { id, name } = resourceHelpers_1.resolveResourceParameters.call(this, idKeys, nameKeys, 'inventory', itemIndex); let resolvedInventoryId = id; if (!resolvedInventoryId && name) { const inventory = await SharedHelpers_1.getAwxResource.call(this, 'inventories', { name }); resolvedInventoryId = inventory.id; } if (!resolvedInventoryId) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Inventory ID could not be determined for deletion.'); } await SharedHelpers_1.deleteAwxResourceWithLogging.call(this, `/inventories/${resolvedInventoryId}/`, itemIndex); return [ this.helpers.returnJsonArray([{ success: true, message: `Inventory ${name || resolvedInventoryId} deleted successfully.`, }]), ]; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to delete inventory: ${error.message}`, { itemIndex }); } } throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The action "${action}" is not supported for inventories`); } //# sourceMappingURL=InventoryResources.js.map