UNPKG

n8n-nodes-netbox

Version:

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

201 lines (200 loc) 8.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listWirelessLinks = listWirelessLinks; exports.getWirelessLink = getWirelessLink; exports.createWirelessLink = createWirelessLink; exports.updateWirelessLink = updateWirelessLink; exports.deleteWirelessLink = deleteWirelessLink; const apiRequest_1 = require("../../../helpers/apiRequest"); const responseFormatter_1 = require("../../../helpers/responseFormatter"); const resourceLookup_1 = require("../../../helpers/resourceLookup"); async function listWirelessLinks() { 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/wireless/wireless-links/', {}, 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/wireless/wireless-links/', {}, qs); return responseFormatter_1.formatResponse.call(this, response); } } async function getWirelessLink() { const wirelessLinkId = this.getNodeParameter('wirelessLinkId', 0); try { const endpoint = `/api/wireless/wireless-links/${wirelessLinkId}/`; 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 wireless link: ${error.message}`); } } async function lookupInterface(interfaceValue) { if (!isNaN(Number(interfaceValue))) { return Number(interfaceValue); } // Try to parse device:interface format if (interfaceValue.includes(':')) { const [deviceName, interfaceName] = interfaceValue.split(':'); try { // First, look up the device const deviceId = await resourceLookup_1.lookupResourceByName.call(this, 'devices', deviceName.trim(), 'dcim'); // Then look up the interface on that device const interfaceEndpoint = '/api/dcim/interfaces/'; const query = { device_id: deviceId, name: interfaceName.trim() }; const response = await apiRequest_1.apiRequest.call(this, 'GET', interfaceEndpoint, {}, query); if (response.results && response.results.length > 0) { return response.results[0].id; } else { throw new Error(`Interface ${interfaceName} not found on device ${deviceName}`); } } catch (error) { throw new Error(`Interface lookup failed: ${error.message}`); } } // Try to look up interface by name directly try { return await resourceLookup_1.lookupResourceByName.call(this, 'interfaces', interfaceValue, 'dcim'); } catch (error) { throw new Error(`Interface lookup failed: ${error.message}`); } } async function createWirelessLink() { const interface_a = this.getNodeParameter('interface_a', 0); const interface_b = this.getNodeParameter('interface_b', 0); const additionalFields = this.getNodeParameter('additionalFields', 0); const body = { interface_a, interface_b, }; try { // Handle interface lookups for main parameters body.interface_a = await lookupInterface.call(this, interface_a); body.interface_b = await lookupInterface.call(this, interface_b); // Process additional fields that need lookup if (additionalFields.tenant && isNaN(Number(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 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/wireless/wireless-links/'; 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 wireless link: ${error.message}`); } } async function updateWirelessLink() { const wirelessLinkId = this.getNodeParameter('wirelessLinkId', 0); const updateFields = this.getNodeParameter('updateFields', 0); try { // Process fields that need lookup if (updateFields.interface_a && typeof updateFields.interface_a === 'string') { updateFields.interface_a = await lookupInterface.call(this, updateFields.interface_a); } if (updateFields.interface_b && typeof updateFields.interface_b === 'string') { updateFields.interface_b = await lookupInterface.call(this, updateFields.interface_b); } if (updateFields.tenant && isNaN(Number(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 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/wireless/wireless-links/${wirelessLinkId}/`; 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 wireless link: ${error.message}`); } } async function deleteWirelessLink() { const wirelessLinkId = this.getNodeParameter('wirelessLinkId', 0); try { const endpoint = `/api/wireless/wireless-links/${wirelessLinkId}/`; await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint); return [{ json: { success: true } }]; } catch (error) { throw new Error(`Failed to delete wireless link: ${error.message}`); } }