UNPKG

n8n-nodes-netbox

Version:

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

222 lines (221 loc) 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listAggregates = listAggregates; exports.getAggregate = getAggregate; exports.createAggregate = createAggregate; exports.updateAggregate = updateAggregate; exports.deleteAggregate = deleteAggregate; const apiRequest_1 = require("../../../helpers/apiRequest"); const responseFormatter_1 = require("../../../helpers/responseFormatter"); const resourceLookup_1 = require("../../../helpers/resourceLookup"); async function listAggregates() { 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/aggregates/', {}, 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/aggregates/', {}, qs); return responseFormatter_1.formatResponse.call(this, response); } } async function getAggregate() { const aggregateIdentifier = this.getNodeParameter('aggregateId', 0); try { // If looks like CIDR notation (contains a '/'), query by prefix if (aggregateIdentifier.includes('/')) { const endpoint = '/api/ipam/aggregates/'; const query = { prefix: aggregateIdentifier }; const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); if (!response.results || response.results.length === 0) { throw new Error(`Aggregate "${aggregateIdentifier}" not found`); } return responseFormatter_1.formatResponse.call(this, response.results[0]); } // Otherwise assume it's an ID and fetch directly const endpoint = `/api/ipam/aggregates/${aggregateIdentifier}/`; const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { throw new Error(`Failed to get Aggregate: ${error.message}`); } } async function createAggregate() { const prefix = this.getNodeParameter('prefix', 0); const rirIdentifier = this.getNodeParameter('rir', 0); const additionalFields = this.getNodeParameter('additionalFields', 0); try { // Lookup RIR by name/slug if not numeric let rirId = rirIdentifier; if (isNaN(Number(rirIdentifier))) { // Look up RIR by name/slug const endpoint = '/api/ipam/rirs/'; let query = { name: rirIdentifier }; let response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); // If no results, try slug if (!response.results || response.results.length === 0) { query = { slug: rirIdentifier }; response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); } if (!response.results || response.results.length === 0) { throw new Error(`RIR "${rirIdentifier}" not found`); } rirId = response.results[0].id; } const body = { prefix, rir: rirId, }; // Process tenant lookup if provided if (additionalFields.tenant && isNaN(Number(additionalFields.tenant))) { try { const tenantId = await resourceLookup_1.lookupResourceByName.call(this, 'tenants', additionalFields.tenant, 'tenancy'); additionalFields.tenant = tenantId; } catch (error) { throw new Error(`Tenant 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}`); } } // Add all additional fields to the request body Object.assign(body, additionalFields); const endpoint = '/api/ipam/aggregates/'; 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 Aggregate: ${error.message}`); } } async function updateAggregate() { const aggregateIdentifier = this.getNodeParameter('aggregateId', 0); const updateFields = this.getNodeParameter('updateFields', 0); try { // Get Aggregate ID if CIDR notation was provided let aggregateId = aggregateIdentifier; if (aggregateIdentifier.includes('/')) { const endpoint = '/api/ipam/aggregates/'; const query = { prefix: aggregateIdentifier }; const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); if (!response.results || response.results.length === 0) { throw new Error(`Aggregate "${aggregateIdentifier}" not found`); } aggregateId = response.results[0].id; } // Lookup RIR by name/slug if provided and not numeric if (updateFields.rir && isNaN(Number(updateFields.rir))) { const endpoint = '/api/ipam/rirs/'; let query = { name: updateFields.rir }; let response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); if (!response.results || response.results.length === 0) { query = { slug: updateFields.rir }; response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); } if (!response.results || response.results.length === 0) { throw new Error(`RIR "${updateFields.rir}" not found`); } updateFields.rir = response.results[0].id; } // Process tenant lookup if provided if (updateFields.tenant && isNaN(Number(updateFields.tenant))) { try { const tenantId = await resourceLookup_1.lookupResourceByName.call(this, 'tenants', updateFields.tenant, 'tenancy'); updateFields.tenant = tenantId; } catch (error) { throw new Error(`Tenant 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}`); } } const endpoint = `/api/ipam/aggregates/${aggregateId}/`; 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 Aggregate: ${error.message}`); } } async function deleteAggregate() { const aggregateIdentifier = this.getNodeParameter('aggregateId', 0); try { // Get Aggregate ID if CIDR notation was provided let aggregateId = aggregateIdentifier; if (aggregateIdentifier.includes('/')) { const endpoint = '/api/ipam/aggregates/'; const query = { prefix: aggregateIdentifier }; const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, query); if (!response.results || response.results.length === 0) { throw new Error(`Aggregate "${aggregateIdentifier}" not found`); } aggregateId = response.results[0].id; } const endpoint = `/api/ipam/aggregates/${aggregateId}/`; await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint); return [{ json: { success: true } }]; } catch (error) { throw new Error(`Failed to delete Aggregate: ${error.message}`); } }