UNPKG

n8n-nodes-netbox

Version:

n8n community node for NetBox API integration with comprehensive DCIM, IPAM, Virtualization, Circuits, Wireless, and data center management operations

107 lines (106 loc) 4.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listConfigTemplates = listConfigTemplates; exports.getConfigTemplate = getConfigTemplate; exports.createConfigTemplate = createConfigTemplate; exports.updateConfigTemplate = updateConfigTemplate; exports.deleteConfigTemplate = deleteConfigTemplate; exports.renderConfigTemplate = renderConfigTemplate; const apiRequest_1 = require("../../../helpers/apiRequest"); const responseFormatter_1 = require("../../../helpers/responseFormatter"); async function listConfigTemplates() { const filters = this.getNodeParameter('filter', 0, {}); const endpoint = '/api/extras/config-templates/'; const response = await apiRequest_1.apiRequestAllItems.call(this, 'GET', endpoint, {}, filters); return responseFormatter_1.formatResponse.call(this, response); } async function getConfigTemplate() { const configTemplateId = this.getNodeParameter('configTemplateId', 0); const endpoint = `/api/extras/config-templates/${configTemplateId}/`; const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint); return responseFormatter_1.formatResponse.call(this, response); } async function createConfigTemplate() { const name = this.getNodeParameter('configTemplateName', 0); const templateCode = this.getNodeParameter('template_code', 0); const description = this.getNodeParameter('configTemplateDescription', 0, ''); const envParamsJson = this.getNodeParameter('environment_params', 0, '{}'); let environmentParams; try { environmentParams = JSON.parse(envParamsJson); } catch (error) { throw new Error('Invalid JSON in environment parameters field'); } const body = { name, template_code: templateCode, }; if (description) body.description = description; if (Object.keys(environmentParams).length > 0) body.environment_params = environmentParams; const endpoint = '/api/extras/config-templates/'; const response = await apiRequest_1.apiRequest.call(this, 'POST', endpoint, body); return responseFormatter_1.formatResponse.call(this, response); } async function updateConfigTemplate() { const configTemplateId = this.getNodeParameter('configTemplateId', 0); const name = this.getNodeParameter('configTemplateName', 0); const templateCode = this.getNodeParameter('template_code', 0); const description = this.getNodeParameter('configTemplateDescription', 0, ''); const envParamsJson = this.getNodeParameter('environment_params', 0, '{}'); let environmentParams; try { environmentParams = JSON.parse(envParamsJson); } catch (error) { throw new Error('Invalid JSON in environment parameters field'); } const body = { name, template_code: templateCode, }; if (description) body.description = description; if (Object.keys(environmentParams).length > 0) body.environment_params = environmentParams; const endpoint = `/api/extras/config-templates/${configTemplateId}/`; const response = await apiRequest_1.apiRequest.call(this, 'PATCH', endpoint, body); return responseFormatter_1.formatResponse.call(this, response); } async function deleteConfigTemplate() { const configTemplateId = this.getNodeParameter('configTemplateId', 0); const endpoint = `/api/extras/config-templates/${configTemplateId}/`; await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint); return [{ json: { success: true } }]; } async function renderConfigTemplate() { const configTemplateId = this.getNodeParameter('configTemplateId', 0); const contextData = this.getNodeParameter('context', 0, {}); // Handle both string and object inputs for the context field let context; if (typeof contextData === 'string') { try { context = JSON.parse(contextData); } catch (error) { throw new Error('Invalid JSON in context field'); } } else { context = contextData; } console.log('🔍 Render Config Template Debug:'); console.log(' contextData type:', typeof contextData); console.log(' contextData value:', JSON.stringify(contextData, null, 2)); console.log(' context (parsed):', JSON.stringify(context, null, 2)); // Send context data directly as the request body (not wrapped in a "context" key) // The NetBox API expects: POST /api/extras/config-templates/{id}/render/?format=json with body: { "foo": "abc", "bar": 123 } const endpoint = `/api/extras/config-templates/${configTemplateId}/render/`; const query = { format: 'json', }; const response = await apiRequest_1.apiRequest.call(this, 'POST', endpoint, context, query); return responseFormatter_1.formatResponse.call(this, response); }