-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeraki-service.1.js
275 lines (243 loc) · 7.64 KB
/
meraki-service.1.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
---
It is easy to duplicate any of the methods to and modify them for new API endpoints.
The service requires a few dependencies, which must be installed.
Install:
npm install axios json-bigint --save
// index.js
const Meraki = require('meraki-service');
const meraki = new Meraki('YourAPIKey','https://api.meraki.com/api/v0');
meraki.getOrganizations().then(res => {
console.log('Organizations: ', res.data);
});
$ Organizations: [ { id: 549236, name: 'Meraki DevNet Sandbox' } ]
*/
//const admins = require('./endpoints/admins');
const axios = require("axios");
const JSONbig = require("json-bigint")({ storeAsString: true });
// Meraki Error Handler (parses the error message within responses)
function _handleError(e) {
console.log("error in Meraki API call: ", e);
if (e.message) {
e = e.message;
}
if (e.response) {
if (e.response.data) {
// Meraki specific error message
if (e.response.data.errors) {
console.log(e.response.data.errors[0]);
e = e.response.data.errors[0];
}
} else {
//console.log(e.response)
e = e.response;
}
} else {
console.log(e);
}
return e;
}
/**
* # Meraki Service for the Cisco Meraki Dashboard API
*
* A collection of functions and helpers to interact with the API.
* -- For use with NodeJS or frontend JavaScript applications.
* Features:
* Collection of common Dashboard API calls
* Handles URL redirects
* Handles Meraki error messages
* Custom scripts for common API traversals
*
* @class
* @module Meraki
*/
class merakiService {
/**
* Initialize a Meraki API Service
* @constructor
* @param {string} apiKey - The Meraki API key
* @param {string} baseUrl - The base Meraki API URL. Uses default:`https://api.meraki.com/api/v0`
* @returns {}
*/
constructor(apiKey, baseUrl) {
this._apiKey = process.env.API_KEY || apiKey;
this._baseUrl =
process.env.BASE_URL || baseUrl || "https://api.meraki.com/api/v0";
this._originalConfig;
this.initMeraki();
}
// *************
// Intialize API
// *************
/**
* @private
*/
initMeraki() {
//require("axios-debug-log");
const debugHttp = require("debug-http");
debugHttp();
this.meraki = axios.create({
baseURL: this._baseUrl,
//maxRedirects: 0,
validateStatus: function(status) {
return status >= 200 && status <= 308;
},
headers: {
"X-Cisco-Meraki-API-Key": this._apiKey,
"Content-Type": "application/json"
},
transformResponse: [JSONbig.parse]
});
// Log content type
this.meraki.interceptors.request.use(
config => {
if (config.method.toUpperCase() != "GET") {
this._originalConfig = config; //cache config
}
return config;
},
error => {
console.log("request intercep error", error);
return error;
}
);
this.meraki.interceptors.response.use(
res => {
console.log("Meraki Service res status:", res.status);
console.log("intercept res.request.method", res.request.method);
console.log(
"Meraki Service res location:",
res.request.res.responseUrl
);
console.log(
"this._originalConfig.method.toUpperCase()",
this._originalConfig.method.toUpperCase()
);
if (
//(res.status >= 300 && res.status <= 308) ||
res.status == 200 &&
this._originalConfig.method.toUpperCase() != "GET"
) {
console.log("REDIRECT");
// Copy original config options, then re-run API request with new target location
let options = {};
options.method = this._originalConfig.method;
options.headers = this._originalConfig.headers;
options.data = this._originalConfig.data;
options.url = res.request.res.responseUrl; // SET NEW LOCATION
console.log("redirect options", options);
return this.meraki(options).then(res => {
//console.log('redirect res', res);
return res.data;
});
} else {
return res;
}
},
error => {
//console.log("err in res intercept", error);
return _handleError(error);
}
);
}
/**
* Getters & Setters for Global API Options
* @module Meraki/Settings
*/
/* disabled. is this a security concern?
get apiKey() {
return this._apiKey;
}
*/
/**
* set API key
* @name set:apiKey
* @memberof module:Meraki/Settings
* @param {string} apiKey
* @example <caption>Example Assignment</caption>
const NEW_KEY = '2f301bccd61b6c642d250cd3f76e5eb66ebd170f';
meraki.apiKey = NEW_KEY;
meraki.getOrganizations().then(res => {
console.log('Organizations: ', res.data);
});
* @example <caption>Example Response</caption>
* Organizations: [{"id":549236,"name":"Meraki DevNet Sandbox"}]
*/
set apiKey(apiKey) {
this._apiKey = apiKey;
this.initMeraki();
}
/**
* get current API base URL
* @name get:baseUrl
* @memberof module:Meraki/Settings
* @return {string} - Meraki API FQDN `https://api.meraki.com/api/v0`
* @example <caption>Example Request</caption>
const meraki = new Meraki(API_KEY);
var url = meraki.baseUrl;
console.log('API Base URL: ', url);
@example <caption>Example Response</caption>
API Base URL: https://api.meraki.com/api/v0
*/
get baseUrl() {
return this._baseUrl;
}
/**
* set API base URL
* @name set:baseUrl
* @memberof module:Meraki/Settings
* @param {string} baseUrl - Meraki API FQDN `https://api.meraki.com/api/v0` or `https://myProxyServer/api`
* @example <caption>Example Assignment</caption>
*
const NEW_KEY = '2f301bccd61b6c642d250cd3f76e5eb66ebd170f';
meraki.apiKey = NEW_KEY;
meraki.getOrganizations().then(res => {
console.log('Organizations: ', res.data);
});
* @example <caption>Example Response</caption>
* Organizations: [{"id":549236,"name":"Meraki DevNet Sandbox"}]
*
*/
set baseUrl(baseUrl) {
this._baseUrl = baseUrl;
this.initMeraki();
}
}
// ****************
// ~~~~~~~~~~~~~~~~
// API Endpoints
// ~~~~~~~~~~~~~~~~
// ****************
Object.assign(merakiService.prototype, require("./endpoints/admins"));
Object.assign(merakiService.prototype, require("./endpoints/clients"));
Object.assign(merakiService.prototype, require("./endpoints/configTemplates"));
Object.assign(merakiService.prototype, require("./endpoints/devices"));
Object.assign(merakiService.prototype, require("./endpoints/groupPolicies"));
Object.assign(
merakiService.prototype,
require("./endpoints/mxCellularFirewallRules")
);
Object.assign(
merakiService.prototype,
require("./endpoints/mxL3FirewallRules")
);
Object.assign(
merakiService.prototype,
require("./endpoints/mxVPNFirewallRules")
);
Object.assign(
merakiService.prototype,
require("./endpoints/mrL3FirewallRules")
);
Object.assign(merakiService.prototype, require("./endpoints/networks"));
Object.assign(merakiService.prototype, require("./endpoints/organizations"));
Object.assign(merakiService.prototype, require("./endpoints/pii"));
Object.assign(merakiService.prototype, require("./endpoints/proxy"));
Object.assign(merakiService.prototype, require("./endpoints/saml"));
Object.assign(merakiService.prototype, require("./endpoints/ssids"));
Object.assign(merakiService.prototype, require("./endpoints/staticRoutes"));
Object.assign(merakiService.prototype, require("./endpoints/switchPorts"));
Object.assign(merakiService.prototype, require("./endpoints/vlans"));
// Custom API Scripts
Object.assign(merakiService.prototype, require("./endpoints/custom"));
module.exports = merakiService;