-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDNAC.js
176 lines (152 loc) · 4.95 KB
/
DNAC.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var exports = module.exports = {};
//Sample ping method
exports.ping = function() {
return "Hello from DNAC SDK";
};
//Package version
var VERSION = require('./package.json').version;
var URL_LOGIN = '/api/system/v1/auth/token';
var URL_SITES = '/api/v1/group';
var URL_NETWORK_DEVICES = '/api/v1/network-device';
var URL_NETWORK_DEVICES_COUNT = '/api/v1/network-device/count';
var URL_NETWORK_SUMMARY = '/api/assurance/v1/network-device/healthSummary?measureBy=global&windowInMin=5&startTime=1526594700000&endTime=1526681100000&limit=300&offset=1';
var URL_SITES_COUNT = '/api/v1/group/count?groupType=SITE';
var request = require('request');
function DNAC(options) {
if (!(this instanceof DNAC)) {
return new DNAC(options);
}
this.VERSION = VERSION;
// Merge the default options with the client submitted options
this.options = Object.assign({
host: null,
username: null,
password: null
}, options);
this.login();
}
DNAC.prototype.login = function(callback) {
var that = this;
var auth = 'Basic ' + Buffer.from(this.options.username + ':' + this.options.password).toString('base64');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var options = {
url: this.options.host+URL_LOGIN,
method: 'POST',
headers : {
"Authorization" : auth
}
};
request(options, function(error, response, body) {
// console.log(body);
var bodyJSON = JSON.parse(body);
that.AUTH_TOKEN = bodyJSON["Token"];
if(callback) {
return callback({
'token': that.AUTH_TOKEN
});
}
});
};
DNAC.prototype._getRequest = function(url, params, callback) {
var that = this;
if(this.AUTH_TOKEN) {
var options = {
url: that.options.host+url,
qs: params,
headers : {
"X-Auth-Token" : this.AUTH_TOKEN
}
};
request(options, function(error, response, body) {
var bodyJSON = JSON.parse(body);
if(callback) {
callback(bodyJSON);
}
});
} else {
that.login(function() {
var options = {
url: that.options.host+url,
qs: params,
headers : {
"X-Auth-Token" : that.AUTH_TOKEN
}
};
request(options, function(error, response, body) {
var bodyJSON = JSON.parse(body);
if(callback) {
callback(bodyJSON.response);
}
});
});
}
};
DNAC.prototype._postRequest = function(url, params, callback) {
var that = this;
if(this.AUTH_TOKEN) {
var options = {
url: that.options.host+url,
qs: params,
method: 'POST',
headers : {
"X-Auth-Token" : this.AUTH_TOKEN
}
};
request(options, function(error, response, body) {
var bodyJSON = JSON.parse(body);
if(callback) {
callback(bodyJSON);
}
});
} else {
that.login(function() {
var options = {
url: that.options.host+url,
qs: params,
method: 'POST',
headers : {
"X-Auth-Token" : that.AUTH_TOKEN
}
};
request(options, function(error, response, body) {
var bodyJSON = JSON.parse(body);
if(callback) {
callback(bodyJSON);
}
});
});
}
};
//offset=1&size=1000&field=name%2Cid%2CparentId%2CadditionalInfo.attributes.latitude%2CadditionalInfo.attributes.longitude%2CadditionalInfo.attributes.type
DNAC.prototype.getSites = function(callback) {
var that = this;
var params = {
"groupType": "SITE",
"offset": 1,
"size": 1000,
"field": "id,additionalInfo.attributes.type"
// "field": "name,id,parentId,additionalInfo.attributes.latitude,additionalInfo.attributes.longitude,additionalInfo.attributes.type"
};
return that._getRequest(URL_SITES, params, callback);
};
DNAC.prototype.getSitesCount = function(callback) {
var that = this;
var params = {};
return that._getRequest(URL_SITES_COUNT, params, callback);
};
DNAC.prototype.getNetworkDevices = function(callback) {
var that = this;
var params = {};
return that._getRequest(URL_NETWORK_DEVICES, params, callback);
};
DNAC.prototype.getNetworkDevicesCount = function(callback) {
var that = this;
var params = {};
return that._getRequest(URL_NETWORK_DEVICES_COUNT, params, callback);
};
DNAC.prototype.getNetworkSummary = function(callback) {
var that = this;
var params = {};
return that._postRequest(URL_NETWORK_SUMMARY, params, callback);
};
module.exports = DNAC;