UNPKG

n8n-nodes-netbox

Version:

n8n community node for NetBox API integration with comprehensive DCIM, IPAM, Virtualization, Circuits, Wireless, and data center management operations

168 lines (167 loc) 6.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listJobs = listJobs; exports.getJob = getJob; exports.createJob = createJob; exports.updateJob = updateJob; exports.deleteJob = deleteJob; exports.executeJob = executeJob; exports.terminateJob = terminateJob; const apiRequest_1 = require("../../../helpers/apiRequest"); const responseFormatter_1 = require("../../../helpers/responseFormatter"); async function listJobs() { 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/core/jobs/', {}, 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/core/jobs/', {}, qs); return responseFormatter_1.formatResponse.call(this, response); } } async function getJob() { const jobId = this.getNodeParameter('jobId', 0); try { const endpoint = `/api/core/jobs/${jobId}/`; 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 job: ${error.message}`); } } async function createJob() { const name = this.getNodeParameter('jobName', 0); const objectType = this.getNodeParameter('object_type', 0); const additionalFields = this.getNodeParameter('additionalFields', 0); const body = { name, object_type: objectType, }; try { // Parse JSON fields if they are strings if (additionalFields.data && typeof additionalFields.data === 'string') { try { additionalFields.data = JSON.parse(additionalFields.data); } catch (e) { throw new Error(`Invalid JSON in data field: ${e.message}`); } } if (additionalFields.args && typeof additionalFields.args === 'string') { try { additionalFields.args = JSON.parse(additionalFields.args); } catch (e) { throw new Error(`Invalid JSON in args field: ${e.message}`); } } if (additionalFields.kwargs && typeof additionalFields.kwargs === 'string') { try { additionalFields.kwargs = JSON.parse(additionalFields.kwargs); } catch (e) { throw new Error(`Invalid JSON in kwargs field: ${e.message}`); } } 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/core/jobs/'; 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 job: ${error.message}`); } } async function updateJob() { const jobId = this.getNodeParameter('jobId', 0); const updateFields = this.getNodeParameter('updateFields', 0); try { // Parse JSON fields if they are strings if (updateFields.data && typeof updateFields.data === 'string') { try { updateFields.data = JSON.parse(updateFields.data); } catch (e) { throw new Error(`Invalid JSON in data field: ${e.message}`); } } if (updateFields.args && typeof updateFields.args === 'string') { try { updateFields.args = JSON.parse(updateFields.args); } catch (e) { throw new Error(`Invalid JSON in args field: ${e.message}`); } } if (updateFields.kwargs && typeof updateFields.kwargs === 'string') { try { updateFields.kwargs = JSON.parse(updateFields.kwargs); } catch (e) { throw new Error(`Invalid JSON in kwargs field: ${e.message}`); } } 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/core/jobs/${jobId}/`; 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 job: ${error.message}`); } } async function deleteJob() { const jobId = this.getNodeParameter('jobId', 0); try { const endpoint = `/api/core/jobs/${jobId}/`; await apiRequest_1.apiRequest.call(this, 'DELETE', endpoint); return [{ json: { success: true } }]; } catch (error) { throw new Error(`Failed to delete job: ${error.message}`); } } async function executeJob() { const jobId = this.getNodeParameter('jobId', 0); try { const endpoint = `/api/core/jobs/${jobId}/execute/`; const response = await apiRequest_1.apiRequest.call(this, 'POST', endpoint); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { throw new Error(`Failed to execute job: ${error.message}`); } } async function terminateJob() { const jobId = this.getNodeParameter('jobId', 0); try { const endpoint = `/api/core/jobs/${jobId}/terminate/`; const response = await apiRequest_1.apiRequest.call(this, 'POST', endpoint); return responseFormatter_1.formatResponse.call(this, response); } catch (error) { throw new Error(`Failed to terminate job: ${error.message}`); } }