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.
18 var request
= require('request');
19 var Promise
= require('promise');
20 var _
= require('underscore');
21 var utils
= require('../../../../framework/core/api_utils/utils.js');
22 var constants
= require('../../../../framework/core/api_utils/constants.js');
23 var ConfigAgentAccount
= {};
26 // Config-Agent Account APIs
27 ConfigAgentAccount
.get = function(req
) {
30 var api_server
= req
.query
["api_server"];
31 var id
= req
.params
.id
;
34 // Get all config accounts
35 return new Promise(function(resolve
, reject
) {
37 var requestHeaders
= {};
38 _
.extend(requestHeaders
,
39 constants
.HTTP_HEADERS
.accept
.collection
, {
40 'Authorization': req
.get('Authorization')
44 url
: utils
.confdPort(api_server
) + '/api/operational/config-agent/account',
46 headers
: requestHeaders
,
47 forever
: constants
.FOREVER_ON
,
48 rejectUnauthorized
: false,
50 function(error
, response
, body
) {
53 if (utils
.validateResponse('ConfigAgentAccount.get', error
, response
, body
, resolve
, reject
)) {
55 data
= JSON
.parse(response
.body
).collection
['rw-config-agent:account'];
56 statusCode
= response
.statusCode
;
58 console
.log('Problem with "ConfigAgentAccount.get"', e
);
62 error
: 'Problem with "ConfigAgentAccount.get": ' + e
.toString()
68 statusCode
: statusCode
,
75 //Get a specific config account
76 return new Promise(function(resolve
, reject
) {
77 var requestHeaders
= {};
78 _
.extend(requestHeaders
,
79 constants
.HTTP_HEADERS
.accept
.data
, {
80 'Authorization': req
.get('Authorization')
84 url
: utils
.confdPort(api_server
) + '/api/operational/config-agent/account/' + id
,
86 headers
: requestHeaders
,
87 forever
: constants
.FOREVER_ON
,
88 rejectUnauthorized
: false,
90 function(error
, response
, body
) {
93 if (utils
.validateResponse('ConfigAgentAccount.get', error
, response
, body
, resolve
, reject
)) {
95 data
= JSON
.parse(response
.body
)['rw-config-agent:account'];
96 statusCode
= response
.statusCode
;
98 console
.log('Problem with "ConfigAgentAccount.get"', e
);
100 err
.statusCode
= 500;
102 error
: 'Problem with "ConfigAgentAccount.get": ' + e
.toString()
108 statusCode
: statusCode
,
117 ConfigAgentAccount
.create = function(req
) {
119 var api_server
= req
.query
["api_server"];
122 return new Promise(function(resolve
, reject
) {
124 "account": Array
.isArray(data
) ? data
: [data
]
127 console
.log('Creating with', JSON
.stringify(jsonData
));
129 var requestHeaders
= {};
130 _
.extend(requestHeaders
,
131 constants
.HTTP_HEADERS
.accept
.data
,
132 constants
.HTTP_HEADERS
.content_type
.data
, {
133 'Authorization': req
.get('Authorization')
137 url
: utils
.confdPort(api_server
) + '/api/config/config-agent',
139 headers
: requestHeaders
,
140 forever
: constants
.FOREVER_ON
,
141 rejectUnauthorized
: false,
143 }, function(error
, response
, body
) {
144 if (utils
.validateResponse('ConfigAgentAccount.create', error
, response
, body
, resolve
, reject
)) {
146 statusCode
: response
.statusCode
,
147 data
: JSON
.stringify(response
.body
),
148 body
:response
.body
.body
155 ConfigAgentAccount
.update = function(req
) {
157 var api_server
= req
.query
["api_server"];
158 var id
= req
.params
.id
;
161 return new Promise(function(resolve
, reject
) {
163 "rw-config-agent:account": data
166 console
.log('Updating config-agent', id
, ' with', JSON
.stringify(jsonData
));
168 var requestHeaders
= {};
169 _
.extend(requestHeaders
,
170 constants
.HTTP_HEADERS
.accept
.data
,
171 constants
.HTTP_HEADERS
.content_type
.data
, {
172 'Authorization': req
.get('Authorization')
176 url
: utils
.confdPort(api_server
) + '/api/config/config-agent/account/' + id
,
178 headers
: requestHeaders
,
179 forever
: constants
.FOREVER_ON
,
180 rejectUnauthorized
: false,
182 }, function(error
, response
, body
) {
183 if (utils
.validateResponse('ConfigAgentAccount.update', error
, response
, body
, resolve
, reject
)) {
185 statusCode
: response
.statusCode
,
186 data
: JSON
.stringify(response
.body
)
193 ConfigAgentAccount
.delete = function(req
) {
195 var api_server
= req
.query
["api_server"];
196 var id
= req
.params
.id
;
198 if (!id
|| !api_server
) {
199 return new Promise(function(resolve
, reject
) {
200 console
.log('Must specifiy api_server and id to delete config-agent account');
204 error
: 'Must specifiy api_server and id to delete config agent account'
210 return new Promise(function(resolve
, reject
) {
211 var requestHeaders
= {};
212 _
.extend(requestHeaders
,
213 constants
.HTTP_HEADERS
.accept
.data
, {
214 'Authorization': req
.get('Authorization')
217 url
: utils
.confdPort(api_server
) + '/api/config/config-agent/account/' + id
,
219 headers
: requestHeaders
,
220 forever
: constants
.FOREVER_ON
,
221 rejectUnauthorized
: false,
222 }, function(error
, response
, body
) {
223 if (utils
.validateResponse('ConfigAgentAccount.delete', error
, response
, body
, resolve
, reject
)) {
225 statusCode
: response
.statusCode
,
226 data
: JSON
.stringify(response
.body
)
235 module
.exports
= ConfigAgentAccount
;