-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfindApiProductForProxy.js
executable file
·138 lines (116 loc) · 4.24 KB
/
findApiProductForProxy.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
// findApiProductForProxy.js
// ------------------------------------------------------------------
//
// created: Mon Mar 20 09:57:02 2017
// last saved: <2017-August-14 17:02:09>
var request = require('request'),
readlineSync = require('readline-sync'),
Getopt = require('node-getopt'),
version = '20170814-1702',
netrc = require('netrc')(),
mgmtUrl,
getopt = new Getopt([
['M' , 'mgmtserver=ARG', 'the base path, including optional port, of the Edge mgmt server. Defaults to https://api.enterprise.apigee.com . '],
['u' , 'username=ARG', 'org user with permissions to read Edge configuration.'],
['p' , 'password=ARG', 'password for the org user.'],
['n' , 'netrc', 'retrieve the username + password from the .netrc file. In lieu of -u/-p'],
['o' , 'org=ARG', 'the Edge organization.'],
['P' , 'proxy=ARG', 'the proxy to find.'],
['v' , 'verbose'],
['h' , 'help']
]).bindHelp();
function joinUrlElements() {
var re1 = new RegExp('^\\/|\\/$', 'g'),
elts = Array.prototype.slice.call(arguments);
return elts.map(function(element){return element.replace(re1,""); }).join('/');
}
function edgeGet(url, cb) {
request.get(url,
gRequestOptions,
function (error, response, body) {
var result;
if (error) {
console.log(error);
cb(error);
}
else if (response.statusCode == 200) {
result = JSON.parse(body);
cb(null, result);
}
else {
console.log('status: ' + response.statusCode );
cb({error: 'bad status' + response.statusCode });
}
});
}
// ========================================================
console.log(
'Edge API-Product-for-proxy finder, version: ' + version + '\n' +
'Node.js ' + process.version + '\n');
// process.argv array starts with 'node' and 'scriptname.js'
var opt = getopt.parse(process.argv.slice(2));
if ( !opt.options.mgmtserver ) {
opt.options.mgmtserver = 'https://api.enterprise.apigee.com';
}
if (opt.options.netrc) {
mgmtUrl = require('url').parse(opt.options.mgmtserver);
if ( ! netrc[mgmtUrl.hostname]) {
console.log('The specified host ('+ mgmtUrl.hostname +') is not present in the .netrc file.');
getopt.showHelp();
process.exit(1);
}
opt.options.username = netrc[mgmtUrl.hostname].login;
opt.options.password = netrc[mgmtUrl.hostname].password;
}
if ( !opt.options.username) {
opt.options.username = readlineSync.question(' USER NAME : ');
}
if ( !opt.options.password) {
opt.options.password = readlineSync.question(' Password for '+opt.options.username + ' : ',
{hideEchoBack: true});
}
if ( !opt.options.username || !opt.options.password) {
console.log('You must provide some way to authenticate to the Edge Management API');
getopt.showHelp();
process.exit(1);
}
if ( !opt.options.org ) {
console.log('You must specify an Edge organization');
getopt.showHelp();
process.exit(1);
}
if ( !opt.options.proxy ) {
console.log('You must specify a proxy to find');
getopt.showHelp();
process.exit(1);
}
var gRequestOptions = {
headers : { accept: 'application/json' },
auth : {
user: opt.options.username,
pass: opt.options.password,
sendImmediately : true
}};
var gUrlBase = joinUrlElements(opt.options.mgmtserver, '/v1/o/', opt.options.org);
var url = joinUrlElements(gUrlBase, 'apiproducts?expand=true');
edgeGet(url, function(e, result) {
var found = null;
if (e) {
console.log(e.stack);
process.exit(1);
}
var apiproducts = result.apiProduct;
console.log('total count of API products for that org: %d', apiproducts.length);
var filtered = apiproducts.filter(function(product) {
return (product.proxies.indexOf(opt.options.proxy) >= 0);
});
if (filtered) {
console.log('count of API products containing %s: %d', opt.options.proxy, filtered.length);
if (filtered.length) {
console.log(JSON.stringify(filtered.map( function(item) { return item.name;}), null, 2));
}
if ( opt.options.verbose ) {
console.log(JSON.stringify(filtered, null, 2));
}
}
});