UNPKG

n8n-nodes-netbox

Version:

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

331 lines (330 loc) 14.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listVRFs = listVRFs; exports.getVRF = getVRF; exports.createVRF = createVRF; exports.updateVRF = updateVRF; exports.deleteVRF = deleteVRF; const apiRequest_1 = require("../../../helpers/apiRequest"); const responseFormatter_1 = require("../../../helpers/responseFormatter"); const resourceLookup_1 = require("../../../helpers/resourceLookup"); async function listVRFs() { const returnAll = this.getNodeParameter('returnAll', 0); const filters = this.getNodeParameter('filters', 0, {}); const qs = {}; Object.assign(qs, filters); if (returnAll) { const response = await apiRequest_1.apiRequestAllItems.call(this, 'GET', '/api/ipam/vrfs/', {}, qs); return responseFormatter_1.formatResponse.call(this, response); } else { const limit = this.getNodeParameter('limit', 0); qs.limit = limit; const response = await apiRequest_1.apiRequest.call(this, 'GET', '/api/ipam/vrfs/', {}, qs); return responseFormatter_1.formatResponse.call(this, response); } } async function getVRF() { const vrfIdentifier = this.getNodeParameter('vrfId', 0); try { // If numeric, assume it's an ID and fetch directly if (!isNaN(Number(vrfIdentifier))) { const endpoint = `/api/ipam/vrfs/${vrfIdentifier}/`; const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint); return responseFormatter_1.formatResponse.call(this, response); } // Try lookup by RD first (route distinguisher is unique) let endpoint = '/api/ipam/vrfs/'; let query = { rd: vrfIdentifier }; let response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); // If no results, try name lookup if (!response.results || response.results.length === 0) { query = { name: vrfIdentifier }; response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); } // If still no results, try contains search if (!response.results || response.results.length === 0) { query = { name__ic: vrfIdentifier }; response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); } // If still no results, throw error if (!response.results || response.results.length === 0) { throw new Error(`VRF with identifier "${vrfIdentifier}" not found`); } // Return the first matching result return responseFormatter_1.formatResponse.call(this, response.results[0]); } catch (error) { throw new Error(`Failed to get VRF: ${error.message}`); } } async function createVRF() { const name = this.getNodeParameter('vrfName', 0); const additionalFields = this.getNodeParameter('additionalFields', 0, {}); const body = { name, }; try { // Process tenant lookup if (additionalFields.tenant && !isNaN(Number(additionalFields.tenant))) { // Already a number, use as-is } else if (additionalFields.tenant) { try { additionalFields.tenant = await resourceLookup_1.lookupResourceByName.call(this, 'tenants', additionalFields.tenant, 'tenancy'); } catch (error) { throw new Error(`Tenant lookup failed: ${error.message}`); } } // Process import_targets if provided as comma-separated string if (additionalFields.import_targets && typeof additionalFields.import_targets === 'string') { try { const targetIds = []; const targetNames = additionalFields.import_targets .split(',') .map((target) => target.trim()); for (const targetName of targetNames) { if (!isNaN(Number(targetName))) { targetIds.push(Number(targetName)); } else { const targetId = await resourceLookup_1.lookupResourceByName.call(this, 'route-targets', targetName, 'ipam'); targetIds.push(targetId); } } additionalFields.import_targets = targetIds; } catch (error) { throw new Error(`Import targets lookup failed: ${error.message}`); } } // Process export_targets if provided as comma-separated string if (additionalFields.export_targets && typeof additionalFields.export_targets === 'string') { try { const targetIds = []; const targetNames = additionalFields.export_targets .split(',') .map((target) => target.trim()); for (const targetName of targetNames) { if (!isNaN(Number(targetName))) { targetIds.push(Number(targetName)); } else { const targetId = await resourceLookup_1.lookupResourceByName.call(this, 'route-targets', targetName, 'ipam'); targetIds.push(targetId); } } additionalFields.export_targets = targetIds; } catch (error) { throw new Error(`Export targets lookup failed: ${error.message}`); } } // Process tags if they're provided as a string if (additionalFields.tags && typeof additionalFields.tags === 'string') { try { const tagIds = []; const tagNames = additionalFields.tags.split(',').map((tag) => tag.trim()); for (const tagName of tagNames) { if (!isNaN(Number(tagName))) { tagIds.push(Number(tagName)); } else { const tagId = await resourceLookup_1.lookupResourceByName.call(this, 'tags', tagName, 'extras'); tagIds.push(tagId); } } additionalFields.tags = tagIds; } catch (error) { throw new Error(`Tag lookup failed: ${error.message}`); } } // Parse custom_fields if it's a string if (additionalFields.custom_fields && typeof additionalFields.custom_fields === 'string') { try { additionalFields.custom_fields = JSON.parse(additionalFields.custom_fields); } catch (e) { throw new Error(`Invalid JSON in custom_fields: ${e.message}`); } } // Transform prefixed field names to API field names if (additionalFields.vrfDescription) { additionalFields.description = additionalFields.vrfDescription; delete additionalFields.vrfDescription; } // Add all additional fields to the request body Object.assign(body, additionalFields); const endpoint = '/api/ipam/vrfs/'; const response = await apiRequest_1.apiRequest.call(this, 'POST', endpoint, body); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { throw new Error(`Failed to create VRF: ${error.message}`); } } async function updateVRF() { const vrfIdentifier = this.getNodeParameter('vrfId', 0); const updateFields = this.getNodeParameter('updateFields', 0, {}); try { // Get VRF ID if name or RD was provided let vrfId = vrfIdentifier; if (isNaN(Number(vrfIdentifier))) { // Try to find VRF by RD or name const endpoint = '/api/ipam/vrfs/'; // Try RD first let query = { rd: vrfIdentifier }; let response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); // If no results, try name if (!response.results || response.results.length === 0) { query = { name: vrfIdentifier }; response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); } // If still no results, try name contains if (!response.results || response.results.length === 0) { query = { name__ic: vrfIdentifier }; response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); } if (!response.results || response.results.length === 0) { throw new Error(`VRF with identifier "${vrfIdentifier}" not found`); } vrfId = response.results[0].id; } // Process tenant lookup if (updateFields.tenant && !isNaN(Number(updateFields.tenant))) { // Already a number, use as-is } else if (updateFields.tenant) { try { updateFields.tenant = await resourceLookup_1.lookupResourceByName.call(this, 'tenants', updateFields.tenant, 'tenancy'); } catch (error) { throw new Error(`Tenant lookup failed: ${error.message}`); } } // Process import_targets if provided as comma-separated string if (updateFields.import_targets && typeof updateFields.import_targets === 'string') { try { const targetIds = []; const targetNames = updateFields.import_targets .split(',') .map((target) => target.trim()); for (const targetName of targetNames) { if (!isNaN(Number(targetName))) { targetIds.push(Number(targetName)); } else { const targetId = await resourceLookup_1.lookupResourceByName.call(this, 'route-targets', targetName, 'ipam'); targetIds.push(targetId); } } updateFields.import_targets = targetIds; } catch (error) { throw new Error(`Import targets lookup failed: ${error.message}`); } } // Process export_targets if provided as comma-separated string if (updateFields.export_targets && typeof updateFields.export_targets === 'string') { try { const targetIds = []; const targetNames = updateFields.export_targets .split(',') .map((target) => target.trim()); for (const targetName of targetNames) { if (!isNaN(Number(targetName))) { targetIds.push(Number(targetName)); } else { const targetId = await resourceLookup_1.lookupResourceByName.call(this, 'route-targets', targetName, 'ipam'); targetIds.push(targetId); } } updateFields.export_targets = targetIds; } catch (error) { throw new Error(`Export targets lookup failed: ${error.message}`); } } // Process tags if they're provided as a string if (updateFields.tags && typeof updateFields.tags === 'string') { try { const tagIds = []; const tagNames = updateFields.tags.split(',').map((tag) => tag.trim()); for (const tagName of tagNames) { if (!isNaN(Number(tagName))) { tagIds.push(Number(tagName)); } else { const tagId = await resourceLookup_1.lookupResourceByName.call(this, 'tags', tagName, 'extras'); tagIds.push(tagId); } } updateFields.tags = tagIds; } catch (error) { throw new Error(`Tag lookup failed: ${error.message}`); } } // Parse custom_fields if it's a string if (updateFields.custom_fields && typeof updateFields.custom_fields === 'string') { try { updateFields.custom_fields = JSON.parse(updateFields.custom_fields); } catch (e) { throw new Error(`Invalid JSON in custom_fields: ${e.message}`); } } // Transform prefixed field names to API field names if (updateFields.vrfName) { updateFields.name = updateFields.vrfName; delete updateFields.vrfName; } if (updateFields.vrfDescription) { updateFields.description = updateFields.vrfDescription; delete updateFields.vrfDescription; } const endpoint = `/api/ipam/vrfs/${vrfId}/`; const response = await apiRequest_1.apiRequest.call(this, 'PATCH', endpoint, updateFields); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { throw new Error(`Failed to update VRF: ${error.message}`); } } async function deleteVRF() { const vrfIdentifier = this.getNodeParameter('vrfId', 0); try { // Get VRF ID if name or RD was provided let vrfId = vrfIdentifier; if (isNaN(Number(vrfIdentifier))) { // Try to find VRF by RD or name const endpoint = '/api/ipam/vrfs/'; // Try RD first let query = { rd: vrfIdentifier }; let response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); // If no results, try name if (!response.results || response.results.length === 0) { query = { name: vrfIdentifier }; response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); } // If still no results, try name contains if (!response.results || response.results.length === 0) { query = { name__ic: vrfIdentifier }; response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); } if (!response.results || response.results.length === 0) { throw new Error(`VRF with identifier "${vrfIdentifier}" not found`); } vrfId = response.results[0].id; } const endpoint = `/api/ipam/vrfs/${vrfId}/`; await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint); return [{ json: { success: true } }]; } catch (error) { throw new Error(`Failed to delete VRF: ${error.message}`); } }