Bug 94 - SDN and Cloud/Config-Agent account data inconsistent
[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 })
46 } else {
47 getAll(req, resolve, reject);
48 }
49 });
50
51 function getAll(req, resolve, reject) {
52 Promise.all([
53 Cloud.get(req),
54 Sdn.get(req),
55 ConfigAgent.get(req)
56 ]).then(function(result) {
57 var ReturnData = {
58 cloud: result[0],
59 sdn: result[1],
60 'config-agent': result[2]
61 };
62 ReturnData.cloud.type = 'cloud';
63 ReturnData.sdn.type = 'sdn';
64 ReturnData['config-agent'].type = 'config';
65 resolve({
66 statusCode: 200,
67 data: ReturnData
68 });
69 })
70 }
71 }
72
73 Accounts.update = updateAccount;
74 Accounts.create = updateAccount;
75 Accounts.delete = deleteAccount;
76 Accounts.refreshAccountConnectionStatus = refreshAccountConnectionStatus
77 function getAccount(req) {
78 return new Promise(function(resolve, reject) {
79 var self = this;
80 var api_server = req.query["api_server"];
81 var id = req.params.id || req.params.name;
82 var requestHeaders = {};
83 var type = nameSpace[req.params.type];
84 var url = utils.confdPort(api_server) + '/api/operational/' + type + '/account';
85 if (id) {
86 url += '/' + id;
87 }
88
89 _.extend(
90 requestHeaders,
91 id ? constants.HTTP_HEADERS.accept.data : constants.HTTP_HEADERS.accept.collection, {
92 'Authorization': req.get('Authorization')
93 }
94 );
95
96 request({
97 url: url + '?deep',
98 type: 'GET',
99 headers: requestHeaders,
100 forever: constants.FOREVER_ON,
101 rejectUnauthorized: false
102 },
103 function(error, response, body) {
104 var data;
105 var objKey = 'rw-' + type + ':account';
106 //SDN model doesn't follow convention
107 if (utils.validateResponse(type.toUpperCase() + '.get', error, response, body, resolve, reject)) {
108 try {
109 data = JSON.parse(response.body);
110 if (!id) {
111 data = data.collection;
112 }
113 data = data[objKey]
114 } catch (e) {
115 console.log('Problem with "' + type.toUpperCase() + '.get"', e);
116 var err = {};
117 err.statusCode = 500;
118 err.errorMessage = {
119 error: 'Problem with "' + type.toUpperCase() + '.get": ' + e
120 }
121 return reject(err);
122 }
123 return resolve({
124 statusCode: response.statusCode,
125 data: data
126 });
127 };
128 });
129 });
130 }
131
132 function updateAccount(req) {
133 var self = this;
134 var id = req.params.id || req.params.name;
135 var api_server = req.query["api_server"];
136 var type = nameSpace[req.params.type];
137 var data = req.body;
138 var requestHeaders = {};
139 var createData = {};
140 var url = utils.confdPort(api_server) + '/api/config/' + type;
141 var method = 'POST'
142 if (!id) {
143 createData = {
144 'account': Array.isArray(data) ? data : [data]
145 }
146 console.log('Creating ' + type + ' account: ', createData);
147 } else {
148 method = 'PUT';
149 url += '/account/' + id;
150 createData['rw-' + type + ':account'] = Array.isArray(data) ? data : [data];
151 }
152
153
154
155 return new Promise(function(resolve, reject) {
156 _.extend(requestHeaders,
157 constants.HTTP_HEADERS.accept.data,
158 constants.HTTP_HEADERS.content_type.data, {
159 'Authorization': req.get('Authorization')
160 });
161 request({
162 url: url,
163 method: method,
164 headers: requestHeaders,
165 forever: constants.FOREVER_ON,
166 rejectUnauthorized: false,
167 json: createData,
168 }, function(error, response, body) {
169 if (utils.validateResponse(type.toUpperCase() + '.' + method, error, response, body, resolve, reject)) {
170 return resolve({
171 statusCode: response.statusCode,
172 data: JSON.stringify(response.body)
173 });
174 };
175 });
176 })
177 }
178
179 function deleteAccount(req) {
180 var self = this;
181 var id = req.params.id || req.params.name;
182 var api_server = req.query["api_server"];
183 var type = nameSpace[req.params.type];
184 var data = req.body;
185 var requestHeaders = {};
186 var createData = {};
187 var url = utils.confdPort(api_server) + '/api/config/' + type;
188 url += '/account/' + id;
189 return new Promise(function(resolve, reject) {
190 _.extend(requestHeaders,
191 constants.HTTP_HEADERS.accept.data,
192 constants.HTTP_HEADERS.content_type.data, {
193 'Authorization': req.get('Authorization')
194 });
195 request({
196 url: url,
197 method: 'DELETE',
198 headers: requestHeaders,
199 forever: constants.FOREVER_ON,
200 rejectUnauthorized: false,
201 }, function(error, response, body) {
202 if (utils.validateResponse(type.toUpperCase() + '.DELETE', error, response, body, resolve, reject)) {
203 return resolve({
204 statusCode: response.statusCode,
205 data: JSON.stringify(response.body)
206 });
207 };
208 });
209 })
210 }
211
212 function refreshAccountConnectionStatus (req) {
213 var api_server = req.query['api_server'];
214 var Name = req.params.name;
215 var Type = req.params.type;
216 var jsonData = {
217 input: {}
218 };
219 var rpcInfo = {
220 sdn: {
221 label: 'sdn-account',
222 rpc: 'update-sdn-status'
223 },
224 'config': {
225 label: 'cfg-agent-account',
226 rpc: 'update-cfg-agent-status'
227 },
228 cloud: {
229 label: 'cloud-account',
230 rpc: 'update-cloud-status'
231 }
232 }
233 jsonData.input[rpcInfo[Type].label] = Name;
234 var headers = _.extend({},
235 constants.HTTP_HEADERS.accept.data,
236 constants.HTTP_HEADERS.content_type.data, {
237 'Authorization': req.get('Authorization')
238 }
239 );
240 return new Promise(function(resolve, reject) {
241
242 request({
243 uri: utils.confdPort(api_server) + '/api/operations/' + rpcInfo[Type].rpc,
244 method: 'POST',
245 headers: headers,
246 forever: constants.FOREVER_ON,
247 rejectUnauthorized: false,
248 json: jsonData
249 }, function(error, response, body) {
250 if (utils.validateResponse('RPC.refreshAccountConnectionStatus', error, response, body, resolve, reject)) {
251
252 resolve({
253 statusCode: response.statusCode,
254 data: body
255 });
256 }
257 });
258 }).catch(function(error) {
259 console.log('Error refreshing account info');
260 });
261 };
262
263 module.exports = Accounts;