n8n-nodes-netbox
Version:
n8n community node for NetBox API integration with comprehensive DCIM, IPAM, and data center management operations
201 lines (200 loc) • 8.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.listPlatforms = listPlatforms;
exports.getPlatform = getPlatform;
exports.createPlatform = createPlatform;
exports.updatePlatform = updatePlatform;
exports.deletePlatform = deletePlatform;
const apiRequest_1 = require("../../../helpers/apiRequest");
const responseFormatter_1 = require("../../../helpers/responseFormatter");
async function listPlatforms() {
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/platforms/', {}, 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/platforms/', {}, queryParams);
return responseFormatter_1.formatResponse.call(this, responseData);
}
}
catch (error) {
console.log('ERROR in listPlatforms:', error);
if (error.response?.body?.detail) {
throw new Error(`NetBox API error: ${error.response.body.detail}`);
}
else {
throw error;
}
}
}
async function getPlatform() {
try {
const platformId = this.getNodeParameter('platformId', 0);
// Fix the endpoint path to include /api/
const endpoint = `/api/dcim/platforms/${platformId}/`;
const response = await apiRequest_1.apiRequest.call(this, 'GET', endpoint, {}, {});
return responseFormatter_1.formatResponse.call(this, response);
}
catch (error) {
console.log('ERROR in getPlatform:', error);
if (error.response?.body?.detail) {
throw new Error(`NetBox API error: ${error.response.body.detail}`);
}
else {
throw error;
}
}
}
async function createPlatform() {
try {
// Get required fields
const name = this.getNodeParameter('name', 0);
// Get additional fields
const additionalFields = this.getNodeParameter('additionalFields', 0, {});
// Build the platform data
const platformData = {
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 !== '');
platformData.tags = tagsArray;
delete additionalFields.tags;
}
// Handle NAPALM arguments (parse JSON string to object)
if (additionalFields.napalm_args && typeof additionalFields.napalm_args === 'string') {
try {
platformData.napalm_args = JSON.parse(additionalFields.napalm_args);
}
catch (e) {
throw new Error(`Invalid JSON in napalm_args: ${e.message}`);
}
delete additionalFields.napalm_args;
}
// Handle custom fields (parse JSON string to object)
if (additionalFields.custom_fields && typeof additionalFields.custom_fields === 'string') {
try {
platformData.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/platforms/';
const response = await apiRequest_1.apiRequest.call(this, 'POST', endpoint, platformData, {});
return responseFormatter_1.formatResponse.call(this, response);
}
catch (error) {
console.log('ERROR in createPlatform:', 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?.manufacturer) {
throw new Error(`Manufacturer error: ${error.response.body.manufacturer[0]}`);
}
else if (error.response?.body?.napalm_driver) {
throw new Error(`NAPALM driver error: ${error.response.body.napalm_driver[0]}`);
}
else {
throw error;
}
}
}
async function updatePlatform() {
try {
const platformId = this.getNodeParameter('platformId', 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 NAPALM arguments (parse JSON string to object)
if (updateFields.napalm_args && typeof updateFields.napalm_args === 'string') {
try {
updateFields.napalm_args = JSON.parse(updateFields.napalm_args);
}
catch (e) {
throw new Error(`Invalid JSON in napalm_args: ${e.message}`);
}
}
// 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/platforms/${platformId}/`;
const response = await apiRequest_1.apiRequest.call(this, 'PATCH', endpoint, updateFields, {});
return responseFormatter_1.formatResponse.call(this, response);
}
catch (error) {
console.log('ERROR in updatePlatform:', 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?.manufacturer) {
throw new Error(`Manufacturer error: ${error.response.body.manufacturer[0]}`);
}
else if (error.response?.body?.napalm_driver) {
throw new Error(`NAPALM driver error: ${error.response.body.napalm_driver[0]}`);
}
else {
throw error;
}
}
}
async function deletePlatform() {
try {
const platformId = this.getNodeParameter('platformId', 0);
// Fix the endpoint path to include /api/
const endpoint = `/api/dcim/platforms/${platformId}/`;
await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint, {}, {});
return [
{ json: { success: true, message: `Platform with ID ${platformId} successfully deleted` } },
];
}
catch (error) {
console.log('ERROR in deletePlatform:', error);
if (error.response?.body?.detail) {
throw new Error(`NetBox API error: ${error.response.body.detail}`);
}
else {
throw error;
}
}
}