n8n-nodes-netbox
Version:
n8n community node for NetBox API integration with comprehensive DCIM, IPAM, Virtualization, Circuits, Wireless, and data center management operations
158 lines (157 loc) • 6.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.listIKEProposals = listIKEProposals;
exports.getIKEProposal = getIKEProposal;
exports.createIKEProposal = createIKEProposal;
exports.updateIKEProposal = updateIKEProposal;
exports.deleteIKEProposal = deleteIKEProposal;
const apiRequest_1 = require("../../../helpers/apiRequest");
const responseFormatter_1 = require("../../../helpers/responseFormatter");
const resourceLookup_1 = require("../../../helpers/resourceLookup");
async function listIKEProposals() {
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/ike-proposals/', {}, 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/ike-proposals/', {}, qs);
return responseFormatter_1.formatResponse.call(this, response);
}
}
async function getIKEProposal() {
const ikeProposalId = this.getNodeParameter('ikeProposalId', 0);
try {
const endpoint = `/api/vpn/ike-proposals/${ikeProposalId}/`;
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 IKE proposal: ${error.message}`);
}
}
async function createIKEProposal() {
const name = this.getNodeParameter('ikeProposalName', 0);
const authenticationMethod = this.getNodeParameter('authentication_method', 0);
const encryptionAlgorithm = this.getNodeParameter('encryption_algorithm', 0);
const authenticationAlgorithm = this.getNodeParameter('authentication_algorithm', 0);
const group = this.getNodeParameter('group', 0);
const additionalFields = this.getNodeParameter('additionalFields', 0);
const body = {
name,
authentication_method: authenticationMethod,
encryption_algorithm: encryptionAlgorithm,
authentication_algorithm: authenticationAlgorithm,
group,
};
try {
// 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}`);
}
}
// Map description field to API field name
if (additionalFields.ikeProposalDescription) {
additionalFields.description = additionalFields.ikeProposalDescription;
delete additionalFields.ikeProposalDescription;
}
Object.assign(body, additionalFields);
const endpoint = '/api/vpn/ike-proposals/';
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 IKE proposal: ${error.message}`);
}
}
async function updateIKEProposal() {
const ikeProposalId = this.getNodeParameter('ikeProposalId', 0);
const updateFields = this.getNodeParameter('updateFields', 0);
try {
// 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}`);
}
}
// Map description field to API field name
if (updateFields.ikeProposalDescription) {
updateFields.description = updateFields.ikeProposalDescription;
delete updateFields.ikeProposalDescription;
}
// Map name field to API field name
if (updateFields.ikeProposalName) {
updateFields.name = updateFields.ikeProposalName;
delete updateFields.ikeProposalName;
}
const endpoint = `/api/vpn/ike-proposals/${ikeProposalId}/`;
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 IKE proposal: ${error.message}`);
}
}
async function deleteIKEProposal() {
const ikeProposalId = this.getNodeParameter('ikeProposalId', 0);
try {
const endpoint = `/api/vpn/ike-proposals/${ikeProposalId}/`;
await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint);
return [{ json: { success: true } }];
}
catch (error) {
throw new Error(`Failed to delete IKE proposal: ${error.message}`);
}
}