UNPKG

n8n-nodes-netbox

Version:

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

207 lines (206 loc) 8.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listInterfaces = listInterfaces; exports.getInterface = getInterface; exports.createInterface = createInterface; exports.updateInterface = updateInterface; exports.deleteInterface = deleteInterface; exports.getConnections = getConnections; const apiRequest_1 = require("../../../helpers/apiRequest"); const responseFormatter_1 = require("../../../helpers/responseFormatter"); async function listInterfaces() { 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/interfaces/', {}, 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/interfaces/', {}, queryParams); return responseFormatter_1.formatResponse.call(this, responseData); } } catch (error) { console.log('ERROR in listInterfaces:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else { throw error; } } } async function getInterface() { try { const interfaceId = this.getNodeParameter('interfaceId', 0); // Fix the endpoint path to include /api/ const endpoint = `/api/dcim/interfaces/${interfaceId}/`; const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, {}); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { console.log('ERROR in getInterface:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else { throw error; } } } async function createInterface() { try { // Get required fields const deviceId = this.getNodeParameter('device_id', 0); const name = this.getNodeParameter('name', 0); const type = this.getNodeParameter('type', 0); // Get additional fields const additionalFields = this.getNodeParameter('additionalFields', 0, {}); // Build the interface data const interfaceData = { device: deviceId, name, type, ...additionalFields, }; // Handle tagged VLANs (convert from comma-separated to array) if (additionalFields.tagged_vlans && typeof additionalFields.tagged_vlans === 'string') { const vlanIdsArray = additionalFields.tagged_vlans .split(',') .map((id) => parseInt(id.trim(), 10)) .filter((id) => !isNaN(id)); interfaceData.tagged_vlans = vlanIdsArray; delete additionalFields.tagged_vlans; } // 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 !== ''); interfaceData.tags = tagsArray; delete additionalFields.tags; } // Handle custom fields (parse JSON string to object) if (additionalFields.custom_fields && typeof additionalFields.custom_fields === 'string') { try { interfaceData.custom_fields = JSON.parse(additionalFields.custom_fields); } catch (e) { throw new Error(`Invalid JSON in custom_fields: ${e.message}`); } delete additionalFields.custom_fields; } const endpoint = '/api/dcim/interfaces/'; const response = await apiRequest_1.apiRequest.call(this, 'POST', endpoint, interfaceData, {}); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { console.log('ERROR in createInterface:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else if (error.response?.body?.device) { throw new Error(`Device error: ${error.response.body.device[0]}`); } else if (error.response?.body?.name) { throw new Error(`Name error: ${error.response.body.name[0]}`); } else if (error.response?.body?.type) { throw new Error(`Type error: ${error.response.body.type[0]}`); } else { throw error; } } } async function updateInterface() { try { const interfaceId = this.getNodeParameter('interfaceId', 0); const updateFields = this.getNodeParameter('updateFields', 0, {}); // Handle tagged VLANs (convert from comma-separated to array) if (updateFields.tagged_vlans && typeof updateFields.tagged_vlans === 'string') { const vlanIdsArray = updateFields.tagged_vlans .split(',') .map((id) => parseInt(id.trim(), 10)) .filter((id) => !isNaN(id)); updateFields.tagged_vlans = vlanIdsArray; } // 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}`); } } // Fix the endpoint path to include /api/ const endpoint = `/api/dcim/interfaces/${interfaceId}/`; const response = await apiRequest_1.apiRequest.call(this, 'PATCH', endpoint, updateFields, {}); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { console.log('ERROR in updateInterface:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else { throw error; } } } async function deleteInterface() { try { const interfaceId = this.getNodeParameter('interfaceId', 0); // Fix the endpoint path to include /api/ const endpoint = `/api/dcim/interfaces/${interfaceId}/`; await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint, {}, {}); return [ { json: { success: true, message: `Interface with ID ${interfaceId} successfully deleted` } }, ]; } catch (error) { console.log('ERROR in deleteInterface:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else { throw error; } } } async function getConnections() { try { const interfaceId = this.getNodeParameter('interfaceId', 0); // Use the cable-paths endpoint to get the connections const endpoint = `/api/dcim/interfaces/${interfaceId}/trace/`; const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, {}); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { console.log('ERROR in getConnections:', error); if (error.response?.body?.detail) { throw new Error(`NetBox API error: ${error.response.body.detail}`); } else { throw error; } } }