-
Notifications
You must be signed in to change notification settings - Fork 17
/
listProxiesWithTargetServers.js
executable file
·180 lines (157 loc) · 6.38 KB
/
listProxiesWithTargetServers.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
#! /usr/local/bin/node
/*jslint node:true, esversion:9 */
// listProxiesWithTargetServers.js
// ------------------------------------------------------------------
//
// Copyright 2018-2022 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// created: Mon Mar 20 09:57:02 2017
// last saved: <2022-December-05 11:43:09>
const apigeejs = require('apigee-edge-js'),
common = apigeejs.utility,
apigee = apigeejs.apigee,
util = require('util'),
sprintf = require('sprintf-js').sprintf,
AdmZip = require('adm-zip'),
DOM = require('@xmldom/xmldom').DOMParser,
xpath = require('xpath'),
Getopt = require('node-getopt'),
version = '20221205-1136',
getopt = new Getopt(common.commonOptions.concat([
['' , 'filter=ARG', 'Optional. filter the set of proxies. valid values: (deployed, deployed:envname, latest).']
])).bindHelp();
const isFilterLatestRevision = () => opt.options.filter == 'latest';
const isFilterDeployed = () => opt.options.filter == 'deployed';
const isFilterDeployedEnv = () => opt.options.filter && opt.options.filter.startsWith('deployed:') && opt.options.filter.slice(9);
const revisionMapper = (org, name) =>
revision =>
org.proxies.export({ name, revision })
.then( result => {
let zip = new AdmZip(result.buffer);
let re2 = new RegExp('^apiproxy/targets/[^/]+.xml$');
let targetEndpoints = zip
.getEntries()
.filter( entry => entry.entryName.match(re2))
.map( entry => {
let data = entry.getData().toString('utf8'),
doc = new DOM().parseFromString(data),
endpointName = xpath.select('/TargetEndpoint/@name', doc)[0].value,
httpTargetConnNodeset = xpath.select('/TargetEndpoint/HTTPTargetConnection', doc);
if (httpTargetConnNodeset && httpTargetConnNodeset[0]) {
let lbNodeset = xpath.select('/TargetEndpoint/HTTPTargetConnection/LoadBalancer', doc);
let theNode = lbNodeset && lbNodeset[0];
if (theNode) {
let serverNodeset = xpath.select('/TargetEndpoint/HTTPTargetConnection/LoadBalancer/Server/@name', doc);
let servers = serverNodeset.map(node => node.value);
return {
target: endpointName,
url: 'load-balancer: ' + servers.join(', '),
adminPath: sprintf('/apis/%s/revisions/%s/targets/%s', name, revision, endpointName)
};
}
}
return null;
});
return targetEndpoints.filter(e => !!e);
});
const revisionReducer = fn =>
(p, revision) =>
p.then( accumulator =>
fn(revision)
.then( endpoint => [...accumulator, { revision, endpoint }]));
const toRevisions = org =>
(promise, name) =>
promise .then( accumulator => {
if (isFilterDeployedEnv() || isFilterDeployed()) {
let environment = isFilterDeployedEnv();
return org.proxies.getDeployments({ name, environment })
.then( response => {
if (response.deployments) {
// GAAMBO
let deployments = response.deployments.map( d => ({name, revision:[d.revision], environment:d.environment}));
return [...accumulator, ...deployments];
}
if (response.revision) {
// Admin API
let deployments = response.revision.map( r => ({name, revision:[r.name]}));
return [...accumulator, ...deployments];
}
return accumulator;
})
.catch( e => {
if (e.code == "distribution.ApplicationNotDeployed") {
return accumulator;
}
throw e;
});
}
return org.proxies.get({ name })
.then( ({revision}) => {
if (isFilterLatestRevision()) {
revision = [revision.pop()];
}
return [ ...accumulator, {name, revision} ];
});
});
// ========================================================
// process.argv array starts with 'node' and 'scriptname.js'
var opt = getopt.parse(process.argv.slice(2));
if (opt.options.verbose) {
console.log(
`Apigee listProxiesWithTargetServers tool, version: ${version}\n` +
`Node.js ${process.version}\n`);
common.logWrite('start');
}
if (opt.options.filter && !isFilterLatestRevision() && !isFilterDeployed() && !isFilterDeployedEnv()) {
console.log("It looks like you've specified an invalid filter.");
getopt.showHelp();
process.exit(1);
}
common.verifyCommonRequiredParameters(opt.options, getopt);
apigee
.connect(common.optToOptions(opt))
.then(org =>
org.proxies.get()
.then( apiproxies => {
// for gaambo
if (Array.isArray(apiproxies.proxies)) {
apiproxies = apiproxies.proxies.map(p => p.name);
}
if (opt.options.verbose) {
common.logWrite('total count of API proxies for that org: %d', apiproxies.length);
}
return apiproxies
.sort()
.reduce( toRevisions(org), Promise.resolve([]));
})
.then( candidates => {
//console.log('candidates: ' + JSON.stringify(candidates, null, 2));
let r = (p, nameAndRevisions) =>
p.then( accumulator => {
let mapper = revisionMapper(org, nameAndRevisions.name);
return nameAndRevisions.revision
.reduce(revisionReducer(mapper), Promise.resolve([]))
.then( a => {
let mapped =
a.filter(item => item.endpoint && item.endpoint.length)
.map(e => ({environment: nameAndRevisions.environment, ...e }));
return (mapped.length)? [...accumulator, {proxyname: nameAndRevisions.name, found:mapped}]: accumulator;
});
});
return candidates.reduce(r, Promise.resolve([]));
})
)
.then( r => console.log('' + JSON.stringify(r, null, 2)) )
.catch( e => console.log('while executing, error: ' + e.stack) );