UNPKG

n8n-nodes-netbox

Version:

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

268 lines (267 loc) 10.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listSites = listSites; exports.getSite = getSite; exports.createSite = createSite; exports.updateSite = updateSite; exports.deleteSite = deleteSite; exports.getDevices = getDevices; exports.getRacks = getRacks; const apiRequest_1 = require("../../../helpers/apiRequest"); const responseFormatter_1 = require("../../../helpers/responseFormatter"); const resourceLookup_1 = require("../../../helpers/resourceLookup"); async function listSites() { try { // Get pagination parameters const returnAll = this.getNodeParameter('returnAll', 0); // Get filters const filters = this.getNodeParameter('filters', 0, {}); // Prepare query parameters const queryParams = { ...filters }; let responseData; if (returnAll === true) { responseData = await apiRequest_1.apiRequestAllItems.call(this, 'GET', '/api/dcim/sites/', {}, queryParams); return responseFormatter_1.formatResponse.call(this, responseData); } else { const limit = this.getNodeParameter('limit', 0); queryParams.limit = limit; responseData = await apiRequest_1.apiRequest.call(this, 'GET', '/api/dcim/sites/', {}, queryParams); return responseFormatter_1.formatResponse.call(this, responseData); } } catch (error) { console.log('ERROR in listSites:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else { throw error; } } } async function getSite() { try { const siteId = this.getNodeParameter('siteId', 0); // Fix the endpoint path to include /api/ const endpoint = `/api/dcim/sites/${siteId}/`; const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, {}); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { console.log('ERROR in getSite:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else { throw error; } } } async function createSite() { try { // Get required fields const name = this.getNodeParameter('name', 0); const status = this.getNodeParameter('status', 0); // Get additional fields const additionalFields = this.getNodeParameter('additionalFields', 0, {}); // Build the site data const siteData = { name, status, ...additionalFields, }; // Handle tenant resolution (name/slug to ID) if (additionalFields.tenant) { const tenantId = await resourceLookup_1.resolveTenantId.call(this, additionalFields.tenant); if (tenantId) { siteData.tenant = tenantId; } else { delete siteData.tenant; } } // Handle tags (convert from comma-separated to array) if (additionalFields.tags && typeof additionalFields.tags === 'string') { const tagsArray = additionalFields.tags .split(',') .map((tag) => tag.trim()) .filter((tag) => tag !== ''); siteData.tags = tagsArray; } // Handle custom fields (parse JSON string to object) if (additionalFields.custom_fields && typeof additionalFields.custom_fields === 'string') { try { siteData.custom_fields = JSON.parse(additionalFields.custom_fields); } catch (e) { throw new Error(`Invalid JSON in custom_fields: ${e.message}`); } } const endpoint = '/api/dcim/sites/'; const response = await apiRequest_1.apiRequest.call(this, 'POST', endpoint, siteData, {}); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { console.log('ERROR in createSite:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else if (error.response?.body?.name) { throw new Error(`Name error: ${error.response.body.name[0]}`); } else if (error.response?.body?.slug) { throw new Error(`Slug error: ${error.response.body.slug[0]}`); } else if (error.response?.body?.status) { throw new Error(`Status error: ${error.response.body.status[0]}`); } else if (error.response?.body?.region) { throw new Error(`Region error: ${error.response.body.region[0]}`); } else if (error.response?.body?.group) { throw new Error(`Group error: ${error.response.body.group[0]}`); } else if (error.response?.body?.tenant) { throw new Error(`Tenant error: ${error.response.body.tenant[0]}`); } else { throw error; } } } async function updateSite() { try { const siteId = this.getNodeParameter('siteId', 0); const updateFields = this.getNodeParameter('updateFields', 0, {}); // Handle tenant resolution (name/slug to ID) if (updateFields.tenant) { const tenantId = await resourceLookup_1.resolveTenantId.call(this, updateFields.tenant); if (tenantId) { updateFields.tenant = tenantId; } else { delete updateFields.tenant; } } // Handle tags (convert from comma-separated to array) if (updateFields.tags && typeof updateFields.tags === 'string') { const tagsArray = updateFields.tags .split(',') .map((tag) => tag.trim()) .filter((tag) => tag !== ''); updateFields.tags = tagsArray; } // Handle custom fields (parse JSON string to object) 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/dcim/sites/${siteId}/`; const response = await apiRequest_1.apiRequest.call(this, 'PATCH', endpoint, updateFields, {}); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { console.log('ERROR in updateSite:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else if (error.response?.body?.name) { throw new Error(`Name error: ${error.response.body.name[0]}`); } else if (error.response?.body?.slug) { throw new Error(`Slug error: ${error.response.body.slug[0]}`); } else if (error.response?.body?.status) { throw new Error(`Status error: ${error.response.body.status[0]}`); } else if (error.response?.body?.tenant) { throw new Error(`Tenant error: ${error.response.body.tenant[0]}`); } else { throw error; } } } async function deleteSite() { try { const siteId = this.getNodeParameter('siteId', 0); // Fix the endpoint path to include /api/ const endpoint = `/api/dcim/sites/${siteId}/`; await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint, {}, {}); return [{ json: { success: true, message: `Site with ID ${siteId} successfully deleted` } }]; } catch (error) { console.log('ERROR in deleteSite:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else { throw error; } } } async function getDevices() { try { const siteId = this.getNodeParameter('siteId', 0); const returnAll = this.getNodeParameter('returnAll', 0); // Query devices filtering by the site ID const queryParams = { site_id: siteId }; let responseData; if (returnAll === true) { // Get all devices at the site responseData = await apiRequest_1.apiRequestAllItems.call(this, 'GET', '/api/dcim/devices/', {}, queryParams); return responseFormatter_1.formatResponse.call(this, responseData); } else { // Get limited number of devices at the site const limit = this.getNodeParameter('limit', 0); queryParams.limit = limit; responseData = await apiRequest_1.apiRequest.call(this, 'GET', '/api/dcim/devices/', {}, queryParams); return responseFormatter_1.formatResponse.call(this, responseData); } } catch (error) { console.log('ERROR in getDevices:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else { throw error; } } } async function getRacks() { try { const siteId = this.getNodeParameter('siteId', 0); const returnAll = this.getNodeParameter('returnAll', 0); // Query racks filtering by the site ID const queryParams = { site_id: siteId }; let responseData; if (returnAll === true) { // Get all racks at the site responseData = await apiRequest_1.apiRequestAllItems.call(this, 'GET', '/api/dcim/racks/', {}, queryParams); return responseFormatter_1.formatResponse.call(this, responseData); } else { // Get limited number of racks at the site const limit = this.getNodeParameter('limit', 0); queryParams.limit = limit; responseData = await apiRequest_1.apiRequest.call(this, 'GET', '/api/dcim/racks/', {}, queryParams); return responseFormatter_1.formatResponse.call(this, responseData); } } catch (error) { console.log('ERROR in getRacks:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else { throw error; } } }