@cn-shell/netbox
Version:
A Cloud Native extension for Netbox
129 lines (128 loc) • 4.04 kB
JavaScript
;
var __importDefault =
(this && this.__importDefault) ||
function(mod) {
return mod && mod.__esModule ? mod : { default: mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CNNetbox = void 0;
// Imports here
const cn_shell_1 = __importDefault(require("cn-shell"));
const netbox_api_1 = require("./netbox-api");
const fs_1 = __importDefault(require("fs"));
// Netbox config consts here
const CFG_NETBOX_SERVER = "NETBOX_SERVER";
const CFG_NETBOX_API_KEY = "NETBOX_API_KEY";
const CFG_DUMP_NETBOX_DATA = "DUMP_NETBOX_DATA";
const CFG_LOAD_NETBOX_DATA = "LOAD_NETBOX_DATA";
const CFG_NETBOX_DATA_DIR = "NETBOX_DATA_DIR";
const DEFAULT_DUMP_NETBOX_DATA = "N"; // Y/y or N/n
const DEFAULT_LOAD_NETBOX_DATA = "N"; // Y/y or N/n
const DEFAULT_NETBOX_DATA_DIR = "/tmp";
// HTTP Prop Patterns here
process.on("unhandledRejection", error => {
// Will print "unhandledRejection err is not defined"
console.log("unhandledRejection", error);
});
// CNNetbox class here
class CNNetbox extends cn_shell_1.default {
// Constructor here
constructor(name, master) {
super(name, master);
this._netboxServer = this.getRequiredCfg(CFG_NETBOX_SERVER);
this._netboxApiKey = this.getRequiredCfg(CFG_NETBOX_API_KEY, false, true);
let dumpNetboxData = this.getCfg(
CFG_DUMP_NETBOX_DATA,
DEFAULT_DUMP_NETBOX_DATA,
);
if (dumpNetboxData.toUpperCase() === "Y") {
this._dumpNetboxData = true;
} else {
this._dumpNetboxData = false;
}
let loadNetboxData = this.getCfg(
CFG_LOAD_NETBOX_DATA,
DEFAULT_LOAD_NETBOX_DATA,
);
if (loadNetboxData.toUpperCase() === "Y") {
this._loadNetboxData = true;
} else {
this._loadNetboxData = false;
}
this._netboxDataDir = this.getCfg(
CFG_NETBOX_DATA_DIR,
DEFAULT_NETBOX_DATA_DIR,
);
}
// Abstract method implementations here
async start() {
return true;
}
async stop() {
return;
}
async healthCheck() {
return true;
}
// Public methods here
async get(group, resource, params, id) {
if (netbox_api_1.netboxApi[group] === undefined) {
throw this.error(`'${group}' is not a valid group`);
}
if (netbox_api_1.netboxApi[group][resource] === undefined) {
throw this.error(
`'${resource}' is not a valid resource in group '${group}'`,
);
}
let path = `${this._netboxDataDir}/netbox-${group}-${resource}.json`;
let results = [];
if (this._loadNetboxData) {
this.info("Loading netbox data from (%s)", path);
try {
results = require(path);
} catch (e) {
this.error("Path (%s) does not exist!", path);
}
return results;
}
let url = `${this._netboxServer}${netbox_api_1.netboxApi[group][resource]}`;
if (id !== undefined) {
url = `${url}/${id}`;
}
// Loop until there are no more pages of results
while (true) {
let res = await this.httpReq
.get(url, {
headers: {
Authorization: `Token ${this._netboxApiKey}`,
},
params,
})
.catch(e => {
let error = {
status: e.response.status,
message: e.response.data,
};
throw error;
});
results = results.concat(res.data.results);
// Check if there are more pages of results
if (res.data.next !== null) {
// The next field is a URL but the it is http:// instead of https:// so fix this
let parts = res.data.next.split("/api/");
url = `${this._netboxServer}/api/${parts[1]}`;
// The next field also contains the query paramters so lets clear params
params = undefined;
} else {
// No more data so break out of loop
break;
}
}
if (this._dumpNetboxData) {
this.info("Dumping netbox region data to (%s)", path);
fs_1.default.writeFileSync(path, JSON.stringify(results));
}
return results;
}
}
exports.CNNetbox = CNNetbox;