n8n-nodes-netbox
Version:
n8n community node for NetBox API integration with comprehensive DCIM, IPAM, and data center management operations
132 lines (131 loc) • 4.47 kB
JavaScript
;
// methods/tenancy/tenants/tenants.methods.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.listTenants = listTenants;
exports.getTenant = getTenant;
exports.createTenant = createTenant;
exports.updateTenant = updateTenant;
exports.deleteTenant = deleteTenant;
const apiRequest_1 = require("../../../helpers/apiRequest");
const responseFormatter_1 = require("../../../helpers/responseFormatter");
/**
* List all tenants
*/
async function listTenants() {
try {
const returnAll = this.getNodeParameter('returnAll', 0);
const filters = this.getNodeParameter('filters', 0, {});
const qs = {};
if (filters.name) {
qs.name = filters.name;
}
if (filters.group_id) {
qs.group_id = filters.group_id;
}
let responseData;
if (returnAll) {
responseData = await apiRequest_1.apiRequestAllItems.call(this, 'GET', '/api/tenancy/tenants/', {}, qs);
}
else {
const limit = this.getNodeParameter('limit', 0);
qs.limit = limit;
responseData = await apiRequest_1.apiRequest.call(this, 'GET', '/api/tenancy/tenants/', {}, qs);
}
return responseFormatter_1.formatResponse.call(this, responseData);
}
catch (error) {
console.log('ERROR in listTenants:', error);
throw error;
}
}
/**
* Get a single tenant
*/
async function getTenant() {
try {
const id = this.getNodeParameter('id', 0);
const responseData = await apiRequest_1.apiRequest.call(this, 'GET', `/api/tenancy/tenants/${id}/`, {}, {});
return responseFormatter_1.formatResponse.call(this, responseData);
}
catch (error) {
console.log('ERROR in getTenant:', error);
throw error;
}
}
/**
* Create a new tenant
*/
async function createTenant() {
try {
const name = this.getNodeParameter('name', 0);
const slug = this.getNodeParameter('slug', 0);
const additionalFields = this.getNodeParameter('additionalFields', 0, {});
const body = {
name,
slug,
};
if (additionalFields.description) {
body.description = additionalFields.description;
}
if (additionalFields.group !== undefined) {
body.group = additionalFields.group;
}
if (additionalFields.tags) {
// Convert comma-separated tags to array of tag objects if needed
const tagNames = additionalFields.tags.split(',').map((tag) => tag.trim());
body.tags = tagNames.map((name) => ({ name }));
}
const responseData = await apiRequest_1.apiRequest.call(this, 'POST', '/api/tenancy/tenants/', body, {});
return responseFormatter_1.formatResponse.call(this, responseData);
}
catch (error) {
console.log('ERROR in createTenant:', error);
throw error;
}
}
/**
* Update an existing tenant
*/
async function updateTenant() {
try {
const id = this.getNodeParameter('id', 0);
const updateFields = this.getNodeParameter('additionalFields', 0, {});
const body = {};
if (updateFields.name) {
body.name = updateFields.name;
}
if (updateFields.slug) {
body.slug = updateFields.slug;
}
if (updateFields.description !== undefined) {
body.description = updateFields.description;
}
if (updateFields.group !== undefined) {
body.group = updateFields.group;
}
if (updateFields.tags !== undefined) {
const tagNames = updateFields.tags.split(',').map((tag) => tag.trim());
body.tags = tagNames.map((name) => ({ name }));
}
const responseData = await apiRequest_1.apiRequest.call(this, 'PATCH', `/api/tenancy/tenants/${id}/`, body, {});
return responseFormatter_1.formatResponse.call(this, responseData);
}
catch (error) {
console.log('ERROR in updateTenant:', error);
throw error;
}
}
/**
* Delete a tenant
*/
async function deleteTenant() {
try {
const id = this.getNodeParameter('id', 0);
await apiRequest_1.apiRequest.call(this, 'DELETE', `/api/tenancy/tenants/${id}/`, {}, {});
return [{ json: { success: true } }];
}
catch (error) {
console.log('ERROR in deleteTenant:', error);
throw error;
}
}