-
Notifications
You must be signed in to change notification settings - Fork 1
/
ah_service_discoverer.js
122 lines (115 loc) · 4.9 KB
/
ah_service_discoverer.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
module.exports = function (RED) {
const helpers = require("./ah_helpers");
const axios = require("axios");
// const qs = require("qs");
function ArrowheadServiceDiscovererNode(config) {
RED.nodes.createNode(this, config);
let node = this;
node.on('input', function(msg, send, done){
done = done || function() { if (arguments.length>0) node.error.apply(node, arguments) };
send = send || function() { node.send.apply(node, arguments) };
askServiceURL(config, msg, done, send, node);
});
}
RED.nodes.registerType("ah service discoverer", ArrowheadServiceDiscovererNode);
function askServiceURL(config, msg, done, send, node){
let ahOrchestrator = RED.nodes.getNode(config.orchestrator);
let orchestrationURL = ahOrchestrator.url+"/orchestration" || "/orchestration";
helpers.updateStatus(node, "info", "Asking Service URL to orchestrator...");
body = prepareOrchestrationBody(config, msg);
opts = {
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
}
}
opts = helpers.addTLSToOptions(RED, ahOrchestrator, opts);
axios.post(
orchestrationURL,
body,
opts
).then(function (response) {
if(response.data.response.length == 0){
var error = "Could not find requested service: "+body["requestedService"]["serviceDefinitionRequirement"];
helpers.updateStatus(node, "error", "Could not find service.");
done(error);
}else{
var serviceDefinition = response.data.response[0].provider.systemName;
var address = response.data.response[0].provider.address;
var port = response.data.response[0].provider.port;
var serviceUri = response.data.response[0].serviceUri;
var fullURL = address + ":" + port + serviceUri;
helpers.updateStatus(node, "success", serviceDefinition+" => "+fullURL);
msg.ahOrchestratorResponse = response;
msg.payload = [];
for (let item of response.data.response){
msg.payload.push({
"url": item.provider.address+":"+item.provider.port+item.serviceUri,
"authorizationTokens": item.authorizationTokens
});
}
response.data.response;
send(msg)
done();
}
}).catch(function (error) {
helpers.updateStatus(node, "error", "Orchestrator error.");
done(error);
});
}
function prepareOrchestrationBody(config, msg) {
let system = RED.nodes.getNode(config.requesterSystem);
if (msg.body !== undefined) {
body = msg.body;
}else{
// Set Authentication Info
if(msg.authenticationInfo !== undefined){
authenticationInfo = msg.authenticationInfo;
}else if(config.authinfo !== undefined){
let authinfo = RED.nodes.getNode(config.authinfo);
authenticationInfo = authinfo.authinfo;
}else {
authenticationInfo = null;
}
// Set Service Definition
if(msg.serviceDefinition !== undefined){
serviceDefinition = msg.serviceDefinition;
}else{
serviceDefinition = config.serviceDefinition;
}
body = {
"requesterSystem": {
"systemName": system.name,
"address": system.address,
"port": parseInt(system.port),
"authenticationInfo": authenticationInfo
},
"requesterCloud": null,
"requestedService": {
"serviceDefinitionRequirement": serviceDefinition,
"interfaceRequirements": [],
"securityRequirements": null,
"metadataRequirements": null,
"versionRequirement": null,
"minVersionRequirement": null,
"maxVersionRequirement": null,
"pingProviders": false
},
"orchestrationFlags": {
"onlyPreferred": false,
"overrideStore": true,
"externalServiceRequest": false,
"enableInterCloud": false,
"enableQoS": false,
"matchmaking": true,
"metadataSearch": false,
"triggerInterCloud": false,
"pingProviders": false
},
"preferredProviders": [],
"commands": {}
};
}
return body;
}
}