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.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.listRegions = listRegions;
exports.getRegion = getRegion;
exports.createRegion = createRegion;
exports.updateRegion = updateRegion;
exports.deleteRegion = deleteRegion;
exports.getSites = getSites;
const apiRequest_1 = require("../../../helpers/apiRequest");
const responseFormatter_1 = require("../../../helpers/responseFormatter");
async function listRegions() {
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/regions/', {}, 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/regions/', {}, queryParams);
return responseFormatter_1.formatResponse.call(this, responseData);
}
}
catch (error) {
console.log('ERROR in listRegions:', error);
if (error.response?.body?.detail) {
throw new Error(`NetBox API error: ${error.response.body.detail}`);
}
else {
throw error;
}
}
}
async function getRegion() {
try {
const regionId = this.getNodeParameter('regionId', 0);
// Fix the endpoint path to include /api/
const endpoint = `/api/dcim/regions/${regionId}/`;
const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, {});
return responseFormatter_1.formatResponse.call(this, response);
}
catch (error) {
console.log('ERROR in getRegion:', error);
if (error.response?.body?.detail) {
throw new Error(`NetBox API error: ${error.response.body.detail}`);
}
else {
throw error;
}
}
}
async function createRegion() {
try {
// Get required fields
const name = this.getNodeParameter('regionName', 0);
// Get additional fields
const additionalFields = this.getNodeParameter('additionalFields', 0, {});
// Build the region data
const regionData = {
name,
...additionalFields,
};
// 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 !== '');
regionData.tags = tagsArray;
delete additionalFields.tags;
}
// Handle custom fields (parse JSON string to object)
if (additionalFields.custom_fields && typeof additionalFields.custom_fields === 'string') {
try {
regionData.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/regions/';
const response = await apiRequest_1.apiRequest.call(this, 'POST', endpoint, regionData, {});
return responseFormatter_1.formatResponse.call(this, response);
}
catch (error) {
console.log('ERROR in createRegion:', 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?.parent) {
throw new Error(`Parent error: ${error.response.body.parent[0]}`);
}
else {
throw error;
}
}
}
async function updateRegion() {
try {
const regionId = this.getNodeParameter('regionId', 0);
const updateFields = this.getNodeParameter('updateFields', 0, {});
// 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/regions/${regionId}/`;
const response = await apiRequest_1.apiRequest.call(this, 'PATCH', endpoint, updateFields, {});
return responseFormatter_1.formatResponse.call(this, response);
}
catch (error) {
console.log('ERROR in updateRegion:', 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?.parent) {
throw new Error(`Parent error: ${error.response.body.parent[0]}`);
}
else {
throw error;
}
}
}
async function deleteRegion() {
try {
const regionId = this.getNodeParameter('regionId', 0);
// Fix the endpoint path to include /api/
const endpoint = `/api/dcim/regions/${regionId}/`;
await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint, {}, {});
return [
{ json: { success: true, message: `Region with ID ${regionId} successfully deleted` } },
];
}
catch (error) {
console.log('ERROR in deleteRegion:', error);
if (error.response?.body?.detail) {
throw new Error(`NetBox API error: ${error.response.body.detail}`);
}
else {
throw error;
}
}
}
async function getSites() {
try {
const regionId = this.getNodeParameter('regionId', 0);
const returnAll = this.getNodeParameter('returnAll', 0);
// Query sites filtering by the region ID
const queryParams = { region_id: regionId };
let responseData;
if (returnAll === true) {
// Get all sites belonging to the region
responseData = await apiRequest_1.apiRequestAllItems.call(this, 'GET', '/api/dcim/sites/', {}, queryParams);
return responseFormatter_1.formatResponse.call(this, responseData);
}
else {
// Get limited number of sites belonging to the region
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 getSites:', error);
if (error.response?.body?.detail) {
throw new Error(`NetBox API error: ${error.response.body.detail}`);
}
else {
throw error;
}
}
}