n8n-nodes-netbox
Version:
n8n community node for NetBox API integration with comprehensive DCIM, IPAM, Virtualization, Circuits, Wireless, and data center management operations
185 lines (184 loc) • 7.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.listTunnelTerminations = listTunnelTerminations;
exports.getTunnelTermination = getTunnelTermination;
exports.createTunnelTermination = createTunnelTermination;
exports.updateTunnelTermination = updateTunnelTermination;
exports.deleteTunnelTermination = deleteTunnelTermination;
const apiRequest_1 = require("../../../helpers/apiRequest");
const responseFormatter_1 = require("../../../helpers/responseFormatter");
const resourceLookup_1 = require("../../../helpers/resourceLookup");
async function listTunnelTerminations() {
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/tunnel-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/tunnel-terminations/', {}, qs);
return responseFormatter_1.formatResponse.call(this, response);
}
}
async function getTunnelTermination() {
const tunnelTerminationId = this.getNodeParameter('tunnelTerminationId', 0);
try {
const endpoint = `/api/vpn/tunnel-terminations/${tunnelTerminationId}/`;
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 tunnel termination: ${error.message}`);
}
}
async function createTunnelTermination() {
const tunnel = this.getNodeParameter('tunnel', 0);
const role = this.getNodeParameter('role', 0);
const outsideIp = this.getNodeParameter('outside_ip', 0);
const additionalFields = this.getNodeParameter('additionalFields', 0);
const body = {
role,
};
try {
// Process tunnel lookup
if (tunnel && !isNaN(Number(tunnel))) {
body.tunnel = Number(tunnel);
}
else if (tunnel) {
try {
body.tunnel = await resourceLookup_1.lookupResourceByName.call(this, 'tunnels', tunnel, 'vpn');
}
catch (error) {
throw new Error(`Tunnel lookup failed: ${error.message}`);
}
}
// Process outside IP lookup
if (outsideIp && !isNaN(Number(outsideIp))) {
body.outside_ip = Number(outsideIp);
}
else if (outsideIp) {
try {
body.outside_ip = await resourceLookup_1.lookupResourceByName.call(this, 'ip-addresses', outsideIp, 'ipam');
}
catch (error) {
throw new Error(`Outside IP 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/tunnel-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 tunnel termination: ${error.message}`);
}
}
async function updateTunnelTermination() {
const tunnelTerminationId = this.getNodeParameter('tunnelTerminationId', 0);
const updateFields = this.getNodeParameter('updateFields', 0);
try {
// Process tunnel lookup
if (updateFields.tunnel && !isNaN(Number(updateFields.tunnel))) {
// Already a number, use as-is
}
else if (updateFields.tunnel) {
try {
updateFields.tunnel = await resourceLookup_1.lookupResourceByName.call(this, 'tunnels', updateFields.tunnel, 'vpn');
}
catch (error) {
throw new Error(`Tunnel lookup failed: ${error.message}`);
}
}
// Process outside IP lookup
if (updateFields.outside_ip && !isNaN(Number(updateFields.outside_ip))) {
// Already a number, use as-is
}
else if (updateFields.outside_ip) {
try {
updateFields.outside_ip = await resourceLookup_1.lookupResourceByName.call(this, 'ip-addresses', updateFields.outside_ip, 'ipam');
}
catch (error) {
throw new Error(`Outside IP 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/vpn/tunnel-terminations/${tunnelTerminationId}/`;
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 tunnel termination: ${error.message}`);
}
}
async function deleteTunnelTermination() {
const tunnelTerminationId = this.getNodeParameter('tunnelTerminationId', 0);
try {
const endpoint = `/api/vpn/tunnel-terminations/${tunnelTerminationId}/`;
await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint);
return [{ json: { success: true } }];
}
catch (error) {
throw new Error(`Failed to delete tunnel termination: ${error.message}`);
}
}