-
Notifications
You must be signed in to change notification settings - Fork 50
/
httpsoptions.api.js
218 lines (200 loc) · 6.22 KB
/
httpsoptions.api.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
var fs = require('fs');
var config = process.mainModule.exports.config;
var request = require('request');
var restler = require('restler');
/* Function: getHttpsOptionsDefValue
Get the default value for https options
*/
var apiProtocolList = {};
/* Function: getHttpsOptionsDefValue
Get the default value for HTTPS options
*/
function getHttpsOptionsDefValue (reqType)
{
switch (reqType) {
case 'authProtocol':
return 'http';
case 'strictSSL':
return false;
case 'ca':
default:
return null;
}
}
/* Function getHttpsOptionsByAPIType
Get the https request option values from config file
*/
function getHttpsOptionsByAPIType (apiType, reqType)
{
var defVal = getHttpsOptionsDefValue(reqType);
if (null == apiType) {
return defVal;
}
var orchModule = getOrchModuleByAPIType(apiType);
if (null == orchModule) {
return defVal;
}
if ((null != config) && (null != config[orchModule]) &&
(null != config[orchModule][reqType])) {
return config[orchModule][reqType];
}
return defVal;
}
/* Function: getOrchModuleByAPIType
Get the Orchestration Module name by REST Api Type
*/
function getOrchModuleByAPIType (apiType)
{
var orchModule = null;
switch (apiType) {
case global.label.IDENTITY_SERVER:
orchModule = 'identityManager';
break;
case global.label.NETWORK_SERVER:
orchModule = 'networkManager';
break;
case global.label.IMAGE_SERVER:
orchModule = 'imageManager';
break;
case global.label.STORAGE_SERVER:
orchModule = 'storageManager';
break;
case global.label.COMPUTE_SERVER:
orchModule = 'computeManager';
break;
case global.label.VNCONFIG_API_SERVER:
case global.label.API_SERVER:
orchModule = 'cnfg';
break;
case global.label.OPS_API_SERVER:
orchModule = 'analytics';
break;
case global.label.DISCOVERY_SERVER:
orchModule = 'discoveryService';
break;
default:
break;
}
return orchModule;
}
/* Function: getProtocolByAPIType
Get the protocol either http/https to connect to backend server
*/
function getProtocolByAPIType (apiType)
{
if (null != apiProtocolList[apiType]) {
return apiProtocolList[apiType];
}
var orchModule = null;
var defProtocol = global.PROTOCOL_HTTP;
if (null == apiType) {
return defProtocol;
}
orchModule = getOrchModuleByAPIType(apiType);
if (null == orchModule) {
return defProtocol;
}
if ((null != config) && (null != config[orchModule]) &&
(null != config[orchModule]['authProtocol']) &&
((global.PROTOCOL_HTTP == config[orchModule]['authProtocol']) ||
(global.PROTOCOL_HTTPS == config[orchModule]['authProtocol']))) {
return config[orchModule]['authProtocol'];
}
return defProtocol;
}
/* Function: getAppReqHeader
Add protocol field along with other header fields sent from APP
*/
function getAppReqHeader (dataObj, apiType)
{
var headers = dataObj['headers'] || {};
if (null == apiType) {
apiType = dataObj['apiType'];
}
var protocol = getProtocolByAPIType(apiType);
headers['protocol'] = protocol;
return headers;
}
/* Function: updateHttpsSecureOptions
Get the HTTPS Secure config option values
*/
function updateHttpsSecureOptions (apiType, options)
{
var proto = getProtocolByAPIType(apiType);
if (global.PROTOCOL_HTTPS == proto) {
options['headers']['protocol'] = global.PROTOCOL_HTTPS;
var caFile = getHttpsOptionsByAPIType(apiType, 'ca');
if ((null != caFile) && ('' != caFile) && ("" != caFile)) {
try {
options['ca'] = fs.readFileSync(caFile);
} catch(e) {
logutils.logger.error('readFileSync error for ca file' + e);
}
}
/* https://github.com/mscdex/node-imap/issues/181 */
options['rejectUnauthorized'] = false;
var strictSSL = getHttpsOptionsByAPIType(apiType, 'strictSSL');
if (null != strictSSL) {
options['rejectUnauthorized'] = strictSSL;
}
}
return options;
}
/* Function: makeHttpOrHttpsGetRestCall
Make a http/https GET request to backend server
*/
function makeHttpOrHttpsGetRestCall (makeCB, dataObj, callback)
{
var reqUrl = dataObj['reqUrl'];
var headers = getAppReqHeader(dataObj, makeCB.name);
makeCB.get(reqUrl, function(err, data) {
callback(err, data);
}, headers);
}
/* Function: makeHttpOrHttpsPostRestCall
Make a http/https POST request to backend server
*/
function makeHttpOrHttpsPostRestCall (makeCB, dataObj, callback)
{
var reqUrl = dataObj['reqUrl'];
var data = dataObj['data'];
var headers = getAppReqHeader(dataObj, makeCB.name);
makeCB.post(reqUrl, data, function(err, data) {
callback(err, data);
}, headers);
}
/* Function: makeHttpOrHttpsPutRestCall
Make a http/https PUT request to backend server
*/
function makeHttpOrHttpsPutRestCall (makeCB, dataObj, callback)
{
var reqUrl = dataObj['reqUrl'];
var data = dataObj['data'];
var headers = getAppReqHeader(dataObj, makeCB.name);
makeCB.post(reqUrl, data, function(err, data) {
callback(err, data);
}, headers);
}
/* Function: makeHttpOrHttpsDelRestCall
Make a http/https DELETE request to backend server
*/
function makeHttpOrHttpsDelRestCall (makeCB, dataObj, callback)
{
var reqUrl = dataObj['reqUrl'];
var headers = getAppReqHeader(dataObj, makeCB.name);
makeCB.delete(reqUrl, data, function(err, data) {
callback(err, data);
}, headers);
}
exports.getHttpsOptionsByAPIType = getHttpsOptionsByAPIType;
exports.makeHttpOrHttpsGetRestCall = makeHttpOrHttpsGetRestCall;
exports.makeHttpOrHttpsPostRestCall = makeHttpOrHttpsPostRestCall;
exports.makeHttpOrHttpsPutRestCall = makeHttpOrHttpsPutRestCall;
exports.makeHttpOrHttpsDelRestCall = makeHttpOrHttpsDelRestCall;
exports.updateHttpsSecureOptions = updateHttpsSecureOptions;
exports.apiProtocolList = apiProtocolList;
exports.getProtocolByAPIType = getProtocolByAPIType;
exports.getOrchModuleByAPIType = getOrchModuleByAPIType;