|
| 1 | +// findApiProductForProxy.js |
| 2 | +// ------------------------------------------------------------------ |
| 3 | +// |
| 4 | +// created: Mon Mar 20 09:57:02 2017 |
| 5 | +// last saved: <2017-December-07 18:19:37> |
| 6 | + |
| 7 | +var edgejs = require('apigee-edge-js'), |
| 8 | + common = edgejs.utility, |
| 9 | + apigeeEdge = edgejs.edge, |
| 10 | + Getopt = require('node-getopt'), |
| 11 | + version = '20171207-1807', |
| 12 | + getopt = new Getopt(common.commonOptions.concat([ |
| 13 | + ['P' , 'proxy=ARG', 'Required. the proxy to find.'], |
| 14 | + ['T' , 'notoken', 'Optional. do not try to obtain a login token.'] |
| 15 | + ])).bindHelp(); |
| 16 | + |
| 17 | +console.log( |
| 18 | + 'Apigee Edge findApiProductForProxy.js tool, version: ' + version + '\n' + |
| 19 | + 'Node.js ' + process.version + '\n'); |
| 20 | + |
| 21 | +common.logWrite('start'); |
| 22 | + |
| 23 | +// process.argv array starts with 'node' and 'scriptname.js' |
| 24 | +var opt = getopt.parse(process.argv.slice(2)); |
| 25 | + |
| 26 | +function handleError(e) { |
| 27 | + if (e) { |
| 28 | + console.log(e); |
| 29 | + console.log(e.stack); |
| 30 | + process.exit(1); |
| 31 | + } |
| 32 | +} |
| 33 | +// ======================================================== |
| 34 | + |
| 35 | +common.verifyCommonRequiredParameters(opt.options, getopt); |
| 36 | + |
| 37 | +if ( !opt.options.proxy ) { |
| 38 | + console.log('You must specify a proxy to find'); |
| 39 | + getopt.showHelp(); |
| 40 | + process.exit(1); |
| 41 | +} |
| 42 | + |
| 43 | +var options = { |
| 44 | + mgmtServer: opt.options.mgmtserver, |
| 45 | + org : opt.options.org, |
| 46 | + user: opt.options.username, |
| 47 | + password: opt.options.password, |
| 48 | + no_token: opt.options.notoken, |
| 49 | + verbosity: opt.options.verbose || 0 |
| 50 | + }; |
| 51 | + |
| 52 | +apigeeEdge.connect(options, function(e, org) { |
| 53 | + handleError(e); |
| 54 | + org.products.get({expand:true}, function(e, result) { |
| 55 | + handleError(e); |
| 56 | + var apiproducts = result.apiProduct; |
| 57 | + common.logWrite('total count of API products for that org: %d', apiproducts.length); |
| 58 | + var filtered = apiproducts.filter(function(product) { |
| 59 | + return (product.proxies.indexOf(opt.options.proxy) >= 0); |
| 60 | + }); |
| 61 | + |
| 62 | + if (filtered) { |
| 63 | + common.logWrite('count of API products containing %s: %d', opt.options.proxy, filtered.length); |
| 64 | + if (filtered.length) { |
| 65 | + common.logWrite(JSON.stringify(filtered.map( function(item) { return item.name;}), null, 2)); |
| 66 | + |
| 67 | + } |
| 68 | + if ( opt.options.verbose ) { |
| 69 | + common.logWrite(JSON.stringify(filtered, null, 2)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + }); |
| 74 | +}); |
0 commit comments