n8n-nodes-netbox
Version:
n8n community node for NetBox API integration with comprehensive DCIM, IPAM, Virtualization, Circuits, Wireless, and data center management operations
166 lines (165 loc) • 7.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.listL2VPNTerminations = listL2VPNTerminations;
exports.getL2VPNTermination = getL2VPNTermination;
exports.createL2VPNTermination = createL2VPNTermination;
exports.updateL2VPNTermination = updateL2VPNTermination;
exports.deleteL2VPNTermination = deleteL2VPNTermination;
const apiRequest_1 = require("../../../helpers/apiRequest");
const responseFormatter_1 = require("../../../helpers/responseFormatter");
const resourceLookup_1 = require("../../../helpers/resourceLookup");
async function listL2VPNTerminations() {
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/vpn/l2vpn-terminations/', {}, 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/vpn/l2vpn-terminations/', {}, qs);
return responseFormatter_1.formatResponse.call(this, response);
}
}
async function getL2VPNTermination() {
const l2vpnTerminationId = this.getNodeParameter('l2vpnTerminationId', 0);
try {
const endpoint = `/api/vpn/l2vpn-terminations/${l2vpnTerminationId}/`;
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 L2VPN termination: ${error.message}`);
}
}
async function createL2VPNTermination() {
const l2vpn = this.getNodeParameter('l2vpn', 0);
const assignedObjectType = this.getNodeParameter('assigned_object_type', 0);
const assignedObjectId = this.getNodeParameter('assigned_object_id', 0);
const additionalFields = this.getNodeParameter('additionalFields', 0);
const body = {
assigned_object_type: assignedObjectType,
assigned_object_id: parseInt(assignedObjectId, 10),
};
try {
// Process L2VPN lookup
if (l2vpn && !isNaN(Number(l2vpn))) {
body.l2vpn = Number(l2vpn);
}
else if (l2vpn) {
try {
body.l2vpn = await resourceLookup_1.lookupResourceByName.call(this, 'l2vpns', l2vpn, 'vpn');
}
catch (error) {
throw new Error(`L2VPN 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}`);
}
}
Object.assign(body, additionalFields);
const endpoint = '/api/vpn/l2vpn-terminations/';
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 L2VPN termination: ${error.message}`);
}
}
async function updateL2VPNTermination() {
const l2vpnTerminationId = this.getNodeParameter('l2vpnTerminationId', 0);
const updateFields = this.getNodeParameter('updateFields', 0);
try {
// Process L2VPN lookup
if (updateFields.l2vpn && !isNaN(Number(updateFields.l2vpn))) {
// Already a number, use as-is
}
else if (updateFields.l2vpn) {
try {
updateFields.l2vpn = await resourceLookup_1.lookupResourceByName.call(this, 'l2vpns', updateFields.l2vpn, 'vpn');
}
catch (error) {
throw new Error(`L2VPN lookup failed: ${error.message}`);
}
}
// Process assigned_object_id to ensure it's a number
if (updateFields.assigned_object_id) {
updateFields.assigned_object_id = parseInt(updateFields.assigned_object_id, 10);
}
// 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/vpn/l2vpn-terminations/${l2vpnTerminationId}/`;
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 L2VPN termination: ${error.message}`);
}
}
async function deleteL2VPNTermination() {
const l2vpnTerminationId = this.getNodeParameter('l2vpnTerminationId', 0);
try {
const endpoint = `/api/vpn/l2vpn-terminations/${l2vpnTerminationId}/`;
await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint);
return [{ json: { success: true } }];
}
catch (error) {
throw new Error(`Failed to delete L2VPN termination: ${error.message}`);
}
}