forked from lahdekorpi/node-salt-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalt.js
51 lines (45 loc) · 1.2 KB
/
salt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const request = require("request-promise-native");
class Salt {
constructor(config) {
this.config = config;
this.headers = "";
this.init();
}
init() {
this.ready = request({
url: this.config.url + "/login",
method: "POST",
json: true,
form: {
username: this.config.username,
password: this.config.password,
eauth: (typeof this.config.eauth === "string") ? this.config.eauth : "pam"
}
}).then(data => {
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";
}
}).catch(e => console.error(e));
}
async fun(tgt="*", fun="test.ping", arg=false, kwarg=false, client="local", pillar=false, timeout=false) {
if(this.expire <= new Date() / 1000) {
this.init();
await this.ready;
}
let form = { tgt, fun, client }
if(arg) form.arg = arg;
if(kwarg) form.kwarg = kwarg;
if(pillar) form.pillar = pillar;
if(timeout) form.timeout = timeout;
return request({
url: this.config.url,
method: "POST",
json: form,
headers: {"X-Auth-Token": this.token},
});
}
}
module.exports = Salt;