UNPKG

n8n-nodes-awx

Version:

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

212 lines 12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleHostOperations = handleHostOperations; const n8n_workflow_1 = require("n8n-workflow"); const SharedHelpers_1 = require("../utils/SharedHelpers"); function formatHostData(host, format = 'standard', baseUrl) { if (!host || typeof host !== 'object') { return { error: 'Invalid host data' }; } const { id, name, description, enabled } = host; const ui_url = `${baseUrl}/#/hosts/${id}`; if (format === 'minimal') { return { id, name, }; } let groups = []; const summaryFields = host.summary_fields; if (summaryFields && typeof summaryFields === 'object') { const groupsData = summaryFields.groups; if (Array.isArray(groupsData)) { groups = groupsData.map((group) => ({ id: group.id, name: group.name })); } } let inventoryName = ''; let inventoryId = null; if (summaryFields && typeof summaryFields === 'object' && summaryFields.inventory && typeof summaryFields.inventory === 'object') { const inventoryData = summaryFields.inventory; inventoryName = inventoryData.name || ''; inventoryId = inventoryData.id !== undefined ? inventoryData.id : null; } const friendlyData = { id, name, description: description || '', enabled: enabled !== undefined ? enabled : null, inventory_id: inventoryId, inventory_name: inventoryName, groups, ui_url, }; if (format === 'friendly') { return friendlyData; } return { ...host, ui_url, }; } async function handleHostOperations(action, itemIndex = 0) { if (!this) { throw new Error('This function must be called with a valid IExecuteFunctions context'); } const credentials = await this.getCredentials('awxApi'); const baseUrl = credentials.baseUrl; const isToolMode = this.getNode().type === 'n8n-nodes-awx.awxToolTool'; switch (action) { case 'list': { const returnAll = this.getNodeParameter('returnAll', itemIndex, false); const inventoryName = this.getNodeParameter('inventoryName', itemIndex, ''); let inventoryId = this.getNodeParameter('inventoryId', itemIndex, ''); const responseFormat = this.getNodeParameter('responseFormat', itemIndex, 'standard'); if (!inventoryId && !inventoryName) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Inventory ID or Inventory Name must be provided to list hosts.', { itemIndex }); } if (inventoryName && !inventoryId) { const inventory = await SharedHelpers_1.getAwxResource.call(this, 'inventories', { name: inventoryName }); if (!inventory || !inventory.id) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Inventory with name "${inventoryName}" not found.`, { itemIndex }); } inventoryId = inventory.id.toString(); } const endpoint = `/api/v2/inventories/${inventoryId}/hosts/`; const hosts = (await SharedHelpers_1.getAwxResources.call(this, endpoint, returnAll ? 'allPages' : 'firstPage')); const processedHosts = hosts.map(host => formatHostData(host, responseFormat, baseUrl)); if (isToolMode) { return [[{ json: { hosts: processedHosts }, pairedItem: { item: itemIndex } }]]; } const returnItems = []; for (const host of processedHosts) { returnItems.push({ json: host, pairedItem: { item: itemIndex, }, }); } return [returnItems]; } case 'get': { let inventoryId = this.getNodeParameter('inventoryId', itemIndex, ''); const inventoryName = this.getNodeParameter('inventoryName', itemIndex, ''); let hostId = this.getNodeParameter('hostId', itemIndex, ''); const hostName = this.getNodeParameter('hostName', itemIndex, ''); const responseFormat = this.getNodeParameter('responseFormat', itemIndex, 'standard'); if (!hostId && !hostName) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Host ID or Host Name must be provided.', { itemIndex }); } if (!hostId && hostName) { if (!inventoryId && !inventoryName) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Inventory ID or Name is required to find a host by name.', { itemIndex }); } if (inventoryName && !inventoryId) { const inventory = await SharedHelpers_1.getAwxResource.call(this, 'inventories', { name: inventoryName }); if (!inventory || !inventory.id) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Inventory with name "${inventoryName}" not found.`, { itemIndex }); } inventoryId = inventory.id.toString(); } const hosts = (await SharedHelpers_1.getAwxResources.call(this, `/api/v2/inventories/${inventoryId}/hosts/`, 'allPages', { name: hostName })); if (hosts.length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Host with name "${hostName}" not found in inventory "${inventoryName || inventoryId}".`, { itemIndex }); } hostId = hosts[0].id.toString(); } const host = await SharedHelpers_1.getAwxResource.call(this, 'hosts', { id: hostId }); const processedHost = formatHostData(host, responseFormat, baseUrl); return [[{ json: processedHost }]]; } case 'create': { let inventoryId = this.getNodeParameter('inventoryId', itemIndex, ''); const inventoryName = this.getNodeParameter('inventoryName', itemIndex, ''); const hostInputs = this.getNodeParameter('host', itemIndex, {}); if (!inventoryId && !inventoryName) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Inventory ID or Name is required to create a host.', { itemIndex }); } if (!hostInputs.name) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Host Name is a required field for creation.', { itemIndex }); } if (inventoryName && !inventoryId) { const inventory = await SharedHelpers_1.getAwxResource.call(this, 'inventories', { name: inventoryName }); if (!inventory || !inventory.id) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Inventory with name "${inventoryName}" not found.`, { itemIndex }); } inventoryId = inventory.id.toString(); } const newHost = await SharedHelpers_1.createAwxResource.call(this, 'hosts', { ...hostInputs, inventory: Number(inventoryId) }); return [[{ json: newHost }]]; } case 'update': { let hostId = this.getNodeParameter('hostId', itemIndex, ''); const hostName = this.getNodeParameter('hostName', itemIndex, ''); const updateFields = this.getNodeParameter('updateFields', itemIndex, {}); if (!hostId && !hostName) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Host ID or Host Name must be provided for update.', { itemIndex }); } if (!hostId && hostName) { let inventoryId = this.getNodeParameter('inventoryId', itemIndex, ''); const inventoryName = this.getNodeParameter('inventoryName', itemIndex, ''); if (!inventoryId && !inventoryName) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Inventory ID or Name is required to find a host by name for updating.', { itemIndex }); } if (inventoryName && !inventoryId) { const inventory = await SharedHelpers_1.getAwxResource.call(this, 'inventories', { name: inventoryName }); if (!inventory || !inventory.id) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Inventory with name "${inventoryName}" not found.`, { itemIndex }); } inventoryId = inventory.id.toString(); } const hosts = await SharedHelpers_1.getAwxResources.call(this, `/api/v2/inventories/${inventoryId}/hosts/`, 'allPages', { name: hostName }); if (hosts.length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Host with name "${hostName}" not found in the specified inventory.`, { itemIndex }); } hostId = hosts[0].id.toString(); } if (Object.keys(updateFields).length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No update fields provided. Please specify at least one field to update.', { itemIndex }); } const endpoint = `/hosts/${hostId}/`; const updatedHost = await SharedHelpers_1.updateAwxResource.call(this, endpoint, updateFields); const processedHost = formatHostData(updatedHost, 'standard', baseUrl); return [[{ json: processedHost }]]; } case 'delete': { let hostId = this.getNodeParameter('hostId', itemIndex, ''); const hostName = this.getNodeParameter('hostName', itemIndex, ''); if (!hostId && hostName) { let inventoryId = this.getNodeParameter('inventoryId', itemIndex, ''); const inventoryName = this.getNodeParameter('inventoryName', itemIndex, ''); if (!inventoryId && !inventoryName) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Inventory ID or Name is required to find a host by name for deletion.', { itemIndex }); } if (inventoryName && !inventoryId) { const inventory = await SharedHelpers_1.getAwxResource.call(this, 'inventories', { name: inventoryName }); if (!inventory || !inventory.id) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Inventory with name "${inventoryName}" not found.`, { itemIndex }); } inventoryId = inventory.id.toString(); } const hosts = await SharedHelpers_1.getAwxResources.call(this, `/api/v2/inventories/${inventoryId}/hosts/`, 'allPages', { name: hostName }); if (hosts.length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Host with name "${hostName}" not found in the specified inventory.`, { itemIndex }); } hostId = hosts[0].id.toString(); } else if (!hostId && !hostName) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Host ID or Host Name must be provided for deletion.', { itemIndex }); } await SharedHelpers_1.deleteAwxResourceWithLogging.call(this, `/hosts/${hostId}/`, itemIndex); const successMessage = { success: true, message: `Host ${hostId} deleted.` }; return [[{ json: successMessage }]]; } default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The action '${action}' is not supported for resource 'host'.`, { itemIndex }); } } //# sourceMappingURL=HostResources.js.map