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