-
Notifications
You must be signed in to change notification settings - Fork 50
/
plugins.api.js
212 lines (200 loc) · 7.39 KB
/
plugins.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
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
/**
* This file contains the util functions for all plugins
*/
var config = process.mainModule.exports['config'];
var configMainServer = require('../../web/api/configServer.main.api');
var configJobServer = require('../../jobs/api/configServer.jobs.api');
var vCenterMainServer = require('../../web/api/vCenterServer.main.api');
//var vCenterJobServer = require('../../web/api/vCenterServer.jobs.api');
var assert = require('assert');
var authApi = require('../../common/auth.api');
var logutils = require('../../utils/log.utils');
var orch = require('../orchestration.api');
var configUtils = require('../../common/configServer.utils');
var commonUtils = require('../../utils/common.utils');
var orchModels = orch.getOrchestrationModels();
function getApiServerRequestedByData (appData,reqBy)
{
assert(appData);
var defproject = null;
//Set loggedInOrchestrionMode
var loggedInOrchestrationMode = 'openstack';
if ((null != appData) && (null != appData['authObj']) &&
(null != appData['authObj']['req']) &&
(null != appData['authObj']['req'].session) &&
(null != appData['authObj']['req'].session.loggedInOrchestrationMode)) {
loggedInOrchestrationMode =
appData['authObj']['req'].session.loggedInOrchestrationMode;
} else {
if ((null != appData) && (null != appData.taskData) &&
(null != appData.taskData.loggedInOrchestrationMode)) {
loggedInOrchestrationMode =
appData.taskData.loggedInOrchestrationMode;
}
}
return getApiServerRequestedByApp(loggedInOrchestrationMode, appData,reqBy);
}
function getApiServerRequestedByApp (loggedInOrchestrationMode, appData, reqBy)
{
// console.log("reqBy a:S", reqBy, loggedInOrchestrationMode);
switch (reqBy) {
case global.label.API_SERVER:
return getApiServerRequestedByApiServer(loggedInOrchestrationMode,
appData);
case global.label.VCENTER_SERVER:
return getApiServerRequestedByvCenter(loggedInOrchestrationMode,
appData);
default:
assert(0);
}
}
function getApiServerRequestedByApiServer (loggedInOrchestrationMode, appData)
{
switch (orchModel) {
case 'openstack':
case 'cloudstack':
case 'vcenter':
case 'none':
var genBy = appData['genBy'];
if (null == genBy) {
genBy = appData['taskData']['genBy'];
}
if (global.service.MAINSEREVR == genBy) {
return configMainServer;
} else if (global.service.MIDDLEWARE == genBy) {
return configJobServer;
} else {
logutils.logger.error("We did not get info of generator");
return configMainServer;
}
break
default:
if (null != appData['taskData']) {
if ((global.REQ_AT_SYS_INIT == appData['taskData']['reqBy']) ||
(null != appData['taskData']['authObj'])) {
return configJobServer;
}
}
return configMainServer;
}
}
function getApiServerRequestedByvCenter (loggedInOrchestrationMode, appData)
{
/* Openstack auth is keystone based, as config Server does not do
* authentication using cloudstack, so for now add check
*/
var genBy = appData['genBy'];
if (null == genBy) {
genBy = appData['taskData']['genBy'];
}
if (global.service.MAINSEREVR == genBy) {
return vCenterMainServer;
} else {
return vCenterJobServer;
}
}
function getOrchestrationPluginModel ()
{
return {'orchestrationModel' : orchModels}
}
function doDomainExist (domain, domainList)
{
var data = domainList['tenants'];
var cnt = data.length;
for (var i = 0; i < cnt; i++) {
if (domain == data[i]['domain_id']) {
return true;
}
}
return false;
}
function formatDomainList (tenantList)
{
var domainObjs = {};
var data = tenantList['tenants'];
var len = data.length;
for (var i = 0; i < len; i++) {
if (null == domainObjs[data[i]['domain_id']]) {
domainObjs[data[i]['domain_id']] = [];
}
domainObjs[data[i]['domain_id']].push(data[i]['name']);
}
return domainObjs;
}
var adminProjects = ['admin'];
function getAdminProjectList (req)
{
var adminProjectList = [];
var adminProjectsCnt = adminProjects.length;
var userRoles = req.session.userRoles;
for (key in userRoles) {
for (var i = 0; i < adminProjectsCnt; i++) {
if (-1 !=
req.session.userRoles[key].indexOf(adminProjects[i])) {
adminProjectList.push(key);
}
}
}
return adminProjectList;
}
function setAllCookies (req, res, appData, cookieObj, callback)
{
var loginErrFile = 'webroot/html/login-error.html';
var multiTenancyEnabled = commonUtils.isMultiTenancyEnabled();
if (null == appData['authObj']['defTokenObj']) {
/* We have not got defTokenObj filled yet while sending to Auth
* Module, so fill it up here
*/
var adminProjectList = getAdminProjectList(req);
/* adminProjectList must not empty array */
if (adminProjectList.length) {
appData['authObj']['defTokenObj'] =
req.session.tokenObjs[adminProjectList[0]]['token'];
} else {
/* Check if multi_tenancy enabled */
if (true == multiTenancyEnabled) {
/* We should not come here, multi_tenancy enabled, why we came
* here still
*/
logutils.logger.error("No Admin Projects!!!");
errStr = "No admin projects";
commonUtils.changeFileContentAndSend(res, loginErrFile,
global.CONTRAIL_LOGIN_ERROR,
errStr, function() {
});
return;
}
var tokenObjs = req.session.tokenObjs;
for (key in tokenObjs) {
appData['authObj']['defTokenObj'] =
req.session.tokenObjs[key]['token'];
break;
}
}
}
var defDomainId;
res.setHeader('Set-Cookie', 'username=' + cookieObj.username +
'; expires=' +
new Date(new Date().getTime() +
global.MAX_AGE_SESSION_ID).toUTCString());
authApi.getCookieObjs(req, appData, function(cookieObjs) {
if (null != cookieObjs['domain']) {
res.setHeader('Set-Cookie', 'domain=' + cookieObjs['domain'] +
'; expires=' + new Date(new Date().getTime() +
global.MAX_AGE_SESSION_ID).toUTCString());
}
if (null != cookieObjs['project']) {
res.setHeader('Set-Cookie', 'project=' + cookieObjs['project'] +
'; expires=' + new Date(new Date().getTime() +
global.MAX_AGE_SESSION_ID).toUTCString());
}
callback();
});
}
exports.getApiServerRequestedByData = getApiServerRequestedByData;
exports.getOrchestrationPluginModel = getOrchestrationPluginModel;
exports.setAllCookies = setAllCookies;
exports.doDomainExist = doDomainExist;