-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfindKvmAccess.js
executable file
·111 lines (97 loc) · 3.55 KB
/
findKvmAccess.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
#! /usr/local/bin/node
/*jslint node:true */
// findKvmAccess.js
// ------------------------------------------------------------------
// in Apigee Edge, find all policies in all proxies that reference a KVM
//
// last saved: <2020-May-29 17:08:10>
/* jshint esversion:9 */
var fs = require('fs'),
async = require('async'),
edgejs = require('apigee-edge-js'),
common = edgejs.utility,
apigeeEdge = edgejs.edge,
sprintf = require('sprintf-js').sprintf,
Getopt = require('node-getopt'),
version = '20200529-1707',
getopt = new Getopt(common.commonOptions.concat([
['M' , 'kvm=ARG', 'Optional. KVM name to find.'],
['S' , 'scope=ARG', 'Optional. Scope to match. Should be one of: (organization, environment, apiproxy)']
])).bindHelp();
// ========================================================
console.log(
'Apigee Edge KVM check tool, version: ' + version + '\n' +
'Node.js ' + process.version + '\n');
common.logWrite('start');
// process.argv array starts with 'node' and 'scriptname.js'
var opt = getopt.parse(process.argv.slice(2));
function handleError(e) {
if (e) {
console.log(e);
console.log(e.stack);
process.exit(1);
}
}
function examineOnePolicy(org, options) {
return function(policyName, callback) {
org.proxies.getPoliciesForRevision({...options, policy:policyName}, function(e, result) {
handleError(e);
// return true if KVM and if the mapIdentifier is as specified
var boolResult = (result.policyType == 'KeyValueMapOperations') &&
( ! opt.options.kvm || ((opt.options.kvm == result.mapIdentifier) &&
( ! opt.options.scope || (opt.options.scope == result.scope))));
callback(null, boolResult);
});
};
}
function getOneRevision (org, proxyName) {
return function (revision, callback) {
var options = {name:proxyName, revision:revision};
org.proxies.getPoliciesForRevision(options, function(e, result){
async.filterSeries(result, examineOnePolicy(org, options), function(err, results) {
// results now equals an array of the KVM policies in this revision
callback(null, results.map(function(elt){ return sprintf('apis/%s/revisions/%s/policies/%s', proxyName, revision, elt); }));
});
});
};
}
function doneAllRevisions(proxyName, callback) {
return function(e, results) {
handleError(e);
// results is an array of arrays
var flattened = [].concat.apply([], results);
common.logWrite('proxy: '+ proxyName + ' ' + JSON.stringify(flattened));
callback(null, flattened);
};
}
function doneAllProxies(e, results) {
handleError(e);
var flattened = [].concat.apply([], results);
common.logWrite('matching KVM policies: ' + JSON.stringify(flattened));
}
function analyzeOneProxy(org) {
return function(proxyName, callback) {
org.proxies.get({ name: proxyName }, function(e, result){
handleError(e);
async.mapSeries(result.revision, getOneRevision(org, proxyName), doneAllRevisions(proxyName, callback));
});
};
}
common.verifyCommonRequiredParameters(opt.options, getopt);
var options = {
mgmtServer: opt.options.mgmtserver,
org : opt.options.org,
user: opt.options.username,
password: opt.options.password,
verbosity: opt.options.verbose || 0
};
apigeeEdge.connect(options, function(e, org){
if (e) {
common.logWrite(JSON.stringify(e, null, 2));
//console.log(e.stack);
process.exit(1);
}
org.proxies.get({}, function(e, proxies) {
async.mapSeries(proxies, analyzeOneProxy(org), doneAllProxies);
});
});