3 * Copyright 2016 RIFT.IO Inc
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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')
32 'config-agent': 'config-agent'
34 Accounts
.get = function(req
) {
35 return new Promise(function(resolve
, reject
) {
36 if (req
.params
.type
|| req
.params
.name
) {
38 .then(function(data
) {
41 data
: Object
.assign(data
, {
49 getAll(req
, resolve
, reject
);
53 function getAll(req
, resolve
, reject
) {
58 ]).then(function(result
) {
62 'config-agent': result
[2]
64 ReturnData
.cloud
.type
= 'cloud';
65 ReturnData
.sdn
.type
= 'sdn';
66 ReturnData
['config-agent'].type
= 'config';
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
) {
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';
95 id
? constants
.HTTP_HEADERS
.accept
.data
: constants
.HTTP_HEADERS
.accept
.collection
, {
96 'Authorization': req
.get('Authorization')
103 headers
: requestHeaders
,
104 forever
: constants
.FOREVER_ON
,
105 rejectUnauthorized
: false
107 function(error
, response
, body
) {
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
)) {
113 data
= JSON
.parse(response
.body
);
115 data
= data
.collection
;
119 console
.log('Problem with "' + type
.toUpperCase() + '.get"', e
);
121 err
.statusCode
= 500;
123 error
: 'Problem with "' + type
.toUpperCase() + '.get": ' + e
128 statusCode
: response
.statusCode
,
136 function updateAccount(req
) {
138 var id
= req
.params
.id
|| req
.params
.name
;
139 var api_server
= req
.query
["api_server"];
140 var type
= nameSpace
[req
.params
.type
];
142 var requestHeaders
= {};
144 var url
= utils
.confdPort(api_server
) + '/api/config/' + type
;
148 'account': Array
.isArray(data
) ? data
: [data
]
150 console
.log('Creating ' + type
+ ' account: ', createData
);
153 url
+= '/account/' + id
;
154 createData
['rw-' + type
+ ':account'] = Array
.isArray(data
) ? data
: [data
];
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')
168 headers
: requestHeaders
,
169 forever
: constants
.FOREVER_ON
,
170 rejectUnauthorized
: false,
172 }, function(error
, response
, body
) {
173 if (utils
.validateResponse(type
.toUpperCase() + '.' + method
, error
, response
, body
, resolve
, reject
)) {
175 statusCode
: response
.statusCode
,
176 data
: JSON
.stringify(response
.body
)
183 function deleteAccount(req
) {
185 var id
= req
.params
.id
|| req
.params
.name
;
186 var api_server
= req
.query
["api_server"];
187 var type
= nameSpace
[req
.params
.type
];
189 var requestHeaders
= {};
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')
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
)) {
208 statusCode
: response
.statusCode
,
209 data
: JSON
.stringify(response
.body
)
216 function refreshAccountConnectionStatus (req
) {
217 var api_server
= req
.query
['api_server'];
218 var Name
= req
.params
.name
;
219 var Type
= req
.params
.type
;
225 label
: 'sdn-account',
226 rpc
: 'update-sdn-status'
229 label
: 'cfg-agent-account',
230 rpc
: 'update-cfg-agent-status'
233 label
: 'cloud-account',
234 rpc
: 'update-cloud-status'
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')
244 return new Promise(function(resolve
, reject
) {
247 uri
: utils
.confdPort(api_server
) + '/api/operations/' + rpcInfo
[Type
].rpc
,
250 forever
: constants
.FOREVER_ON
,
251 rejectUnauthorized
: false,
253 }, function(error
, response
, body
) {
254 if (utils
.validateResponse('RPC.refreshAccountConnectionStatus', error
, response
, body
, resolve
, reject
)) {
257 statusCode
: response
.statusCode
,
262 }).catch(function(error
) {
263 console
.log('Error refreshing account info');
267 module
.exports
= Accounts
;