n8n-nodes-netbox
Version:
n8n community node for NetBox API integration with comprehensive DCIM, IPAM, Virtualization, Circuits, Wireless, and data center management operations
148 lines (147 loc) • 6.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.listContacts = listContacts;
exports.getContact = getContact;
exports.createContact = createContact;
exports.updateContact = updateContact;
exports.deleteContact = deleteContact;
const apiRequest_1 = require("../../../helpers/apiRequest");
const responseFormatter_1 = require("../../../helpers/responseFormatter");
const resourceLookup_1 = require("../../../helpers/resourceLookup");
async function listContacts() {
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/tenancy/contacts/', {}, 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/tenancy/contacts/', {}, qs);
return responseFormatter_1.formatResponse.call(this, response);
}
}
async function getContact() {
const contactId = this.getNodeParameter('id', 0);
try {
const endpoint = `/api/tenancy/contacts/${contactId}/`;
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 contact: ${error.message}`);
}
}
async function createContact() {
const name = this.getNodeParameter('contactName', 0);
const additionalFields = this.getNodeParameter('additionalFields', 0);
const body = {
name,
};
try {
// Process additional fields that need lookup
if (additionalFields.group && !isNaN(Number(additionalFields.group))) {
// Already a number, use as-is
}
else if (additionalFields.group) {
try {
additionalFields.group = await resourceLookup_1.lookupResourceByName.call(this, 'contact-groups', additionalFields.group, 'tenancy');
}
catch (error) {
throw new Error(`Contact group 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}`);
}
}
// Add all additional fields to the request body
Object.assign(body, additionalFields);
const endpoint = '/api/tenancy/contacts/';
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 contact: ${error.message}`);
}
}
async function updateContact() {
const contactId = this.getNodeParameter('id', 0);
const name = this.getNodeParameter('contactName', 0);
const additionalFields = this.getNodeParameter('additionalFields', 0);
const body = {
name,
};
try {
// Process additional fields that need lookup
if (additionalFields.group && !isNaN(Number(additionalFields.group))) {
// Already a number, use as-is
}
else if (additionalFields.group) {
try {
additionalFields.group = await resourceLookup_1.lookupResourceByName.call(this, 'contact-groups', additionalFields.group, 'tenancy');
}
catch (error) {
throw new Error(`Contact group 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}`);
}
}
// Add all additional fields to the request body
Object.assign(body, additionalFields);
const endpoint = `/api/tenancy/contacts/${contactId}/`;
const response = await apiRequest_1.apiRequest.call(this, 'PATCH', endpoint, body);
return responseFormatter_1.formatResponse.call(this, response);
}
catch (error) {
throw new Error(`Failed to update contact: ${error.message}`);
}
}
async function deleteContact() {
const contactId = this.getNodeParameter('id', 0);
try {
const endpoint = `/api/tenancy/contacts/${contactId}/`;
await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint);
return [{ json: { success: true } }];
}
catch (error) {
throw new Error(`Failed to delete contact: ${error.message}`);
}
}