-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueryserviceregistry.js
127 lines (111 loc) · 5.03 KB
/
queryserviceregistry.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
/**
* Copyright 2016 by Avid Technology, Inc.
* User: nludwig
* Date: 2016-07-14
* Time: 12:00
* Project: CTMS
*/
/**
* This example enumerates the entries in the service registry and writes the results to stdout.
*/
var https = require('https');
var PlatformTools = require('./PlatformTools');
function onServiceRootsRequestError(e) {
console.error(e);
}
/**
* Promises delivery of a formatted string containing the results of querying the service registry.
*
* @method stringify
* @param {Object} options valid options for the next HTTP request against the platform
* @param {Array} resources the resources, which make up the result of the service registry query
* @return {Promise} promising {"options": options, "text": text} containing valid options for the next HTTP request
* against the platform and a formatted string containing the results of the service registry query
*/
var stringify = function(options, resources) {
var deferred = Promise.defer();
var text ='';
if (resources) {
for (var name in resources) {
if(resources.hasOwnProperty(name)) {
text += "Resource: '"+name+"'\n";
if(resources[name].length) {
for(var i = 0; i < resources[name].length; ++i) {
var serviceHref = resources[name][i].href;
text += '\t'+(i + 1)+'. <'+serviceHref+'>\n';
}
} else {
var serviceHref = resources[name].href;
text += '\t1. <'+serviceHref+'>\n';
}
}
}
} else {
text += "No services registered.";
}
deferred.resolve({"options": options, "text": text});
return deferred.promise;
};
/**
* Promises delivery of the results of a service registry query.
*
* @method queryRegistry
* @param {Object} options valid options for the next HTTP request against the platform
* @param {String} apiDomain address to issue the advanced search against
* @return {Promise} promising {"options": options, "resources": resources} containing valid options for the next HTTP
* request against the platform and the resources, which make up the result of the service registry query
*/
var queryRegistry = function(options, apiDomain) {
var deferred = Promise.defer();
var registryServiceType = 'avid.ctms.registry';
/// Check, whether the service registry is available:
var registryURL = 'https://'+apiDomain+'/apis/'+registryServiceType+';version=0/serviceroots';
options.path = registryURL;
if (PlatformTools.getAccessToken()) {
options.headers.Cookie = 'avidAccessToken='+PlatformTools.getAccessToken();
}
https.get(options, onServiceRootsRequestResponded)
.setTimeout(PlatformTools.getDefaultRequestTimeoutms(), PlatformTools.onRequestTimeout);
function onServiceRootsRequestResponded(serviceRootsResponse) {
if(200 === serviceRootsResponse.statusCode) {
var body = [];
serviceRootsResponse.on('data', function onDataChunk(data) {
body.push(data);
});
serviceRootsResponse.on('end', onServiceRootsResultData);
serviceRootsResponse.on('error', onServiceRootsRequestError);
function onServiceRootsResultData() {
var serviceRootsResult = JSON.parse(Buffer.concat(body).toString());
deferred.resolve({"options": options, "resources": serviceRootsResult.resources})
}
} else {
console.log("Serviceroot request failed with '" + serviceRootsResponse.statusMessage + "'");
deferred.reject();
}
}
return deferred.promise;
};
if (4 !== process.argv.length) {
console.log('Usage: ' + process.argv[0] + ' ' + process.argv[1] + " <apidomain> <httpbasicauthstring>");
} else {
var apiDomain = process.argv[2];
var httpBasicAuthString = process.argv[3];
// Enable tolerant server certificate validation:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
PlatformTools
.getAuthEndpoint(null, apiDomain)
.then(function(it) {return PlatformTools.getIdentityProviders(it);}, PlatformTools.failAndExit)
.then(function(it) {
return PlatformTools.authorize(it, apiDomain, httpBasicAuthString);
}, PlatformTools.failAndExit)
.then(function(options) {return queryRegistry(options, apiDomain);}, PlatformTools.failAndExit)
.then(function(it) {
return stringify(it.options, it.resources);
}, PlatformTools.failAndExit)
.then(function(it) {console.log(it.text); return it.options;}, PlatformTools.failAndExit)
.then(function(it) {return PlatformTools.getAuthEndpoint(it, apiDomain);}, PlatformTools.failAndExit)
.then(function(it) {return PlatformTools.getCurrentToken(it);}, PlatformTools.failAndExit)
.then(function(it) {return PlatformTools.removeToken(it);}, PlatformTools.failAndExit)
.then(function() {console.log("End"); process.exit();})
.catch(PlatformTools.failAndExit);
}