UNPKG

salt-api

Version:
161 lines 6.69 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Salt = void 0; const axios_1 = __importDefault(require("axios")); const url_1 = require("url"); const eventsource_1 = __importDefault(require("eventsource")); class Salt { constructor(config, debug = false, axiosInstance = undefined) { this.config = { url: "http://localhost:8000", eauth: "pam", }; this.headers = { Accept: "application/json", }; this.config = config; this.debug = debug; if (typeof axiosInstance === "undefined") { this.axios = axiosInstance || axios_1.default.create({ headers: { Accept: "application/json", }, }); } if (this.debug) { this.axios.interceptors.request.use(function (config) { config.metadata = { startTime: new Date() }; return config; }, function (error) { return Promise.reject(error); }); this.axios.interceptors.response.use(function (response) { response.config.metadata.endTime = new Date(); response.duration = response.config.metadata.endTime - response.config.metadata.startTime; return response; }, function (error) { error.config.metadata.endTime = new Date(); error.duration = error.config.metadata.endTime - error.config.metadata.startTime; return Promise.reject(error); }); } } async login() { const form = { eauth: typeof this.config.eauth === "string" ? this.config.eauth : "pam", }; if (typeof this.config.username !== "undefined") form.username = this.config.username; if (typeof this.config.password !== "undefined") form.password = this.config.password; if (typeof this.config.token !== "undefined") form.token = this.config.token; const response = await this.axios.post(this.config.url + "/login", new url_1.URLSearchParams(form), { headers: { "Content-Type": "application/x-www-form-urlencoded", }, }); const data = response.data; this.debugLog({ url: this.config.url, path: "/login", perms: data.return[0].perms }, response); if (typeof data === "object" && typeof data.return === "object" && typeof data.return[0].token === "string") { this.token = data.return[0].token; this.expire = data.return[0].expire; } else { throw "Got no token"; } } async eventSource() { if ((this.expire <= Date.now() / 1000)) { this.debugLog({ path: "/events", log: "Token expired, logging in again" }); await this.login(); } return new eventsource_1.default(`${this.config.url}/events`, { headers: Object.assign(Object.assign({}, this.headers), { "X-Auth-Token": this.token }) }); } async fun(tgt = "*", fun = "test.ping", funOptions = undefined) { if ((this.expire <= Date.now() / 1000)) { this.debugLog({ function: "fun", log: "Token expired, logging in again" }); await this.login(); } const form = { tgt, fun, client: "local" }; if (funOptions === null || funOptions === void 0 ? void 0 : funOptions.client) form.arg = funOptions.client; if (funOptions === null || funOptions === void 0 ? void 0 : funOptions.arg) form.arg = funOptions.arg; if (funOptions === null || funOptions === void 0 ? void 0 : funOptions.kwarg) form.kwarg = funOptions.kwarg; if (funOptions === null || funOptions === void 0 ? void 0 : funOptions.tgt_type) form.tgt_type = funOptions.tgt_type; if (funOptions === null || funOptions === void 0 ? void 0 : funOptions.pillar) form.pillar = funOptions.pillar; if (funOptions === null || funOptions === void 0 ? void 0 : funOptions.timeout) form.timeout = funOptions.timeout; return this.axios .post(this.config.url, form, { headers: Object.assign(Object.assign({}, this.headers), { "X-Auth-Token": this.token }), }) .then((response) => { this.debugLog(form, response); return response.data; }) .catch((err) => { this.debugLog(form, undefined, err); throw err; }); } async minions(mid = "") { if ((this.expire <= Date.now() / 1000)) { this.debugLog({ path: "/minions", mid, log: "Token expired, logging in again" }); await this.login(); } return this.axios .get(`${this.config.url}/minions/${mid}`, { headers: Object.assign(Object.assign({}, this.headers), { "X-Auth-Token": this.token }), }) .then((response) => { this.debugLog({ path: "/minions", mid }, response); return response.data; }) .catch((err) => { this.debugLog({ path: "/minions", mid }, undefined, err); throw err; }); } async jobs(jid = "") { if ((this.expire <= Date.now() / 1000)) { this.debugLog({ path: "/jobs", jid, log: "Token expired, logging in again" }); await this.login(); } return this.axios .get(`${this.config.url}/jobs/${jid}`, { headers: Object.assign(Object.assign({}, this.headers), { "X-Auth-Token": this.token }), }) .then((response) => { this.debugLog({ path: "/jobs", jid }, response); return response.data; }) .catch((err) => { this.debugLog({ path: "/jobs", jid }, undefined, err); throw err; }); } debugLog(debugObject, response = undefined, error = undefined) { if (!this.debug) return; if (response) debugObject.duration = response.duration / 1000; if (error) { debugObject.duration = error.duration / 1000; console.log(`[NODE-SALT-API] ${error.message}`); } console.log("[NODE-SALT-API]"); console.log(debugObject); } } exports.Salt = Salt; //# sourceMappingURL=salt.js.map