RIFT-14989: Login fixes for Accounts and RO Config pages
[osm/UI.git] / skyquake / plugins / accounts / api / accounts.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 var request = require('request');
20 var Promise = require('bluebird');
21 var rp = require('request-promise');
22 var utils = require('../../../framework/core/api_utils/utils.js');
23 var constants = require('../../../framework/core/api_utils/constants.js');
24 var _ = require('underscore');
25 var Cloud = require('./cloud_account/cloudAccount')
26 var Sdn = require('./sdn_account/sdnAccount')
27 var ConfigAgent = require('./config_agent/configAgent')
28 var Accounts = {};
29 var nameSpace = {
30 cloud: 'cloud',
31 sdn: 'sdn',
32 'config-agent': 'config-agent'
33 };
34 Accounts.get = function(req) {
35 return new Promise(function(resolve, reject) {
36 if (req.params.type || req.params.name) {
37 getAccount(req)
38 .then(function(data) {
39 resolve({
40 statusCode: 200,
41 data: Object.assign(data, {
42 type: req.params.type
43 })
44 });
45 }, function(reason) {
46 reject(reason);
47 })
48 } else {
49 getAll(req, resolve, reject);
50 }
51 });
52
53 function getAll(req, resolve, reject) {
54 Promise.all([
55 Cloud.get(req),
56 Sdn.get(req),
57 ConfigAgent.get(req)
58 ]).then(function(result) {
59 var ReturnData = {
60 cloud: result[0],
61 sdn: result[1],
62 'config-agent': result[2]
63 };
64 ReturnData.cloud.type = 'cloud';
65 ReturnData.sdn.type = 'sdn';
66 ReturnData['config-agent'].type = 'config';
67 resolve({
68 statusCode: 200,
69 data: ReturnData
70 });
71 }, function(reason) {
72 reject(reason);
73 })
74 }
75 }
76
77 Accounts.update = updateAccount;
78 Accounts.create = updateAccount;
79 Accounts.delete = deleteAccount;
80 Accounts.refreshAccountConnectionStatus = refreshAccountConnectionStatus
81 function getAccount(req) {
82 return new Promise(function(resolve, reject) {
83 var self = this;
84 var api_server = req.query["api_server"];
85 var id = req.params.id || req.params.name;
86 var requestHeaders = {};
87 var type = nameSpace[req.params.type];
88 var url = utils.confdPort(api_server) + '/api/operational/' + type + '/account';
89 if (id) {
90 url += '/' + id;
91 }
92
93 _.extend(
94 requestHeaders,
95 id ? constants.HTTP_HEADERS.accept.data : constants.HTTP_HEADERS.accept.collection, {
96 'Authorization': req.get('Authorization')
97 }
98 );
99
100 request({
101 url: url + '?deep',
102 type: 'GET',
103 headers: requestHeaders,
104 forever: constants.FOREVER_ON,
105 rejectUnauthorized: false
106 },
107 function(error, response, body) {
108 var data;
109 var objKey = 'rw-' + type + ':account';
110 //SDN model doesn't follow convention
111 if (utils.validateResponse(type.toUpperCase() + '.get', error, response, body, resolve, reject)) {
112 try {
113 data = JSON.parse(response.body);
114 if (!id) {
115 data = data.collection;
116 }
117 data = data[objKey]
118 } catch (e) {
119 console.log('Problem with "' + type.toUpperCase() + '.get"', e);
120 var err = {};
121 err.statusCode = 500;
122 err.errorMessage = {
123 error: 'Problem with "' + type.toUpperCase() + '.get": ' + e
124 }
125 return reject(err);
126 }
127 return resolve({
128 statusCode: response.statusCode,
129 data: data
130 });
131 };
132 });
133 });
134 }
135
136 function updateAccount(req) {
137 var self = this;
138 var id = req.params.id || req.params.name;
139 var api_server = req.query["api_server"];
140 var type = nameSpace[req.params.type];
141 var data = req.body;
142 var requestHeaders = {};
143 var createData = {};
144 var url = utils.confdPort(api_server) + '/api/config/' + type;
145 var method = 'POST'
146 if (!id) {
147 createData = {
148 'account': Array.isArray(data) ? data : [data]
149 }
150 console.log('Creating ' + type + ' account: ', createData);
151 } else {
152 method = 'PUT';
153 url += '/account/' + id;
154 createData['rw-' + type + ':account'] = Array.isArray(data) ? data : [data];
155 }
156
157
158
159 return new Promise(function(resolve, reject) {
160 _.extend(requestHeaders,
161 constants.HTTP_HEADERS.accept.data,
162 constants.HTTP_HEADERS.content_type.data, {
163 'Authorization': req.get('Authorization')
164 });
165 request({
166 url: url,
167 method: method,
168 headers: requestHeaders,
169 forever: constants.FOREVER_ON,
170 rejectUnauthorized: false,
171 json: createData,
172 }, function(error, response, body) {
173 if (utils.validateResponse(type.toUpperCase() + '.' + method, error, response, body, resolve, reject)) {
174 return resolve({
175 statusCode: response.statusCode,
176 data: JSON.stringify(response.body)
177 });
178 };
179 });
180 })
181 }
182
183 function deleteAccount(req) {
184 var self = this;
185 var id = req.params.id || req.params.name;
186 var api_server = req.query["api_server"];
187 var type = nameSpace[req.params.type];
188 var data = req.body;
189 var requestHeaders = {};
190 var createData = {};
191 var url = utils.confdPort(api_server) + '/api/config/' + type;
192 url += '/account/' + id;
193 return new Promise(function(resolve, reject) {
194 _.extend(requestHeaders,
195 constants.HTTP_HEADERS.accept.data,
196 constants.HTTP_HEADERS.content_type.data, {
197 'Authorization': req.get('Authorization')
198 });
199 request({
200 url: url,
201 method: 'DELETE',
202 headers: requestHeaders,
203 forever: constants.FOREVER_ON,
204 rejectUnauthorized: false,
205 }, function(error, response, body) {
206 if (utils.validateResponse(type.toUpperCase() + '.DELETE', error, response, body, resolve, reject)) {
207 return resolve({
208 statusCode: response.statusCode,
209 data: JSON.stringify(response.body)
210 });
211 };
212 });
213 })
214 }
215
216 function refreshAccountConnectionStatus (req) {
217 var api_server = req.query['api_server'];
218 var Name = req.params.name;
219 var Type = req.params.type;
220 var jsonData = {
221 input: {}
222 };
223 var rpcInfo = {
224 sdn: {
225 label: 'sdn-account',
226 rpc: 'update-sdn-status'
227 },
228 'config': {
229 label: 'cfg-agent-account',
230 rpc: 'update-cfg-agent-status'
231 },
232 cloud: {
233 label: 'cloud-account',
234 rpc: 'update-cloud-status'
235 }
236 }
237 jsonData.input[rpcInfo[Type].label] = Name;
238 var headers = _.extend({},
239 constants.HTTP_HEADERS.accept.data,
240 constants.HTTP_HEADERS.content_type.data, {
241 'Authorization': req.get('Authorization')
242 }
243 );
244 return new Promise(function(resolve, reject) {
245
246 request({
247 uri: utils.confdPort(api_server) + '/api/operations/' + rpcInfo[Type].rpc,
248 method: 'POST',
249 headers: headers,
250 forever: constants.FOREVER_ON,
251 rejectUnauthorized: false,
252 json: jsonData
253 }, function(error, response, body) {
254 if (utils.validateResponse('RPC.refreshAccountConnectionStatus', error, response, body, resolve, reject)) {
255
256 resolve({
257 statusCode: response.statusCode,
258 data: body
259 });
260 }
261 });
262 }).catch(function(error) {
263 console.log('Error refreshing account info');
264 });
265 };
266
267 module.exports = Accounts;