Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / accounts / api / config_agent / configAgent.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 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 = {};
24
25
26 // Config-Agent Account APIs
27 ConfigAgentAccount.get = function(req) {
28 var self = this;
29
30 var api_server = req.query["api_server"];
31 var id = req.params.id;
32
33 if (!id) {
34 // Get all config accounts
35 return new Promise(function(resolve, reject) {
36
37 var requestHeaders = {};
38 _.extend(requestHeaders,
39 constants.HTTP_HEADERS.accept.collection, {
40 'Authorization': req.get('Authorization')
41 });
42
43 request({
44 url: utils.confdPort(api_server) + '/api/operational/config-agent/account',
45 type: 'GET',
46 headers: requestHeaders,
47 forever: constants.FOREVER_ON,
48 rejectUnauthorized: false,
49 },
50 function(error, response, body) {
51 var data;
52 var statusCode;
53 if (utils.validateResponse('ConfigAgentAccount.get', error, response, body, resolve, reject)) {
54 try {
55 data = JSON.parse(response.body).collection['rw-config-agent:account'];
56 statusCode = response.statusCode;
57 } catch (e) {
58 console.log('Problem with "ConfigAgentAccount.get"', e);
59 var err = {};
60 err.statusCode = 500;
61 err.errorMessage = {
62 error: 'Problem with "ConfigAgentAccount.get": ' + e.toString()
63 }
64 return reject(err);
65 }
66
67 return resolve({
68 statusCode: statusCode,
69 data: data
70 });
71 };
72 });
73 });
74 } else {
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')
81 });
82
83 request({
84 url: utils.confdPort(api_server) + '/api/operational/config-agent/account/' + id,
85 type: 'GET',
86 headers: requestHeaders,
87 forever: constants.FOREVER_ON,
88 rejectUnauthorized: false,
89 },
90 function(error, response, body) {
91 var data;
92 var statusCode;
93 if (utils.validateResponse('ConfigAgentAccount.get', error, response, body, resolve, reject)) {
94 try {
95 data = JSON.parse(response.body)['rw-config-agent:account'];
96 statusCode = response.statusCode;
97 } catch (e) {
98 console.log('Problem with "ConfigAgentAccount.get"', e);
99 var err = {};
100 err.statusCode = 500;
101 err.errorMessage = {
102 error: 'Problem with "ConfigAgentAccount.get": ' + e.toString()
103 }
104 return reject(err);
105 }
106
107 return resolve({
108 statusCode: statusCode,
109 data: data
110 });
111 }
112 });
113 });
114 }
115 };
116
117 ConfigAgentAccount.create = function(req) {
118
119 var api_server = req.query["api_server"];
120 var data = req.body;
121
122 return new Promise(function(resolve, reject) {
123 var jsonData = {
124 "account": Array.isArray(data) ? data : [data]
125 };
126
127 console.log('Creating with', JSON.stringify(jsonData));
128
129 var requestHeaders = {};
130 _.extend(requestHeaders,
131 constants.HTTP_HEADERS.accept.data,
132 constants.HTTP_HEADERS.content_type.data, {
133 'Authorization': req.get('Authorization')
134 });
135
136 request({
137 url: utils.confdPort(api_server) + '/api/config/config-agent',
138 method: 'POST',
139 headers: requestHeaders,
140 forever: constants.FOREVER_ON,
141 rejectUnauthorized: false,
142 json: jsonData,
143 }, function(error, response, body) {
144 if (utils.validateResponse('ConfigAgentAccount.create', error, response, body, resolve, reject)) {
145 return resolve({
146 statusCode: response.statusCode,
147 data: JSON.stringify(response.body),
148 body:response.body.body
149 });
150 };
151 });
152 });
153 };
154
155 ConfigAgentAccount.update = function(req) {
156
157 var api_server = req.query["api_server"];
158 var id = req.params.id;
159 var data = req.body;
160
161 return new Promise(function(resolve, reject) {
162 var jsonData = {
163 "rw-config-agent:account": data
164 };
165
166 console.log('Updating config-agent', id, ' with', JSON.stringify(jsonData));
167
168 var requestHeaders = {};
169 _.extend(requestHeaders,
170 constants.HTTP_HEADERS.accept.data,
171 constants.HTTP_HEADERS.content_type.data, {
172 'Authorization': req.get('Authorization')
173 });
174
175 request({
176 url: utils.confdPort(api_server) + '/api/config/config-agent/account/' + id,
177 method: 'PUT',
178 headers: requestHeaders,
179 forever: constants.FOREVER_ON,
180 rejectUnauthorized: false,
181 json: jsonData,
182 }, function(error, response, body) {
183 if (utils.validateResponse('ConfigAgentAccount.update', error, response, body, resolve, reject)) {
184 return resolve({
185 statusCode: response.statusCode,
186 data: JSON.stringify(response.body)
187 });
188 };
189 });
190 });
191 };
192
193 ConfigAgentAccount.delete = function(req) {
194
195 var api_server = req.query["api_server"];
196 var id = req.params.id;
197
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');
201 return reject({
202 statusCode: 500,
203 errorMessage: {
204 error: 'Must specifiy api_server and id to delete config agent account'
205 }
206 });
207 });
208 };
209
210 return new Promise(function(resolve, reject) {
211 var requestHeaders = {};
212 _.extend(requestHeaders,
213 constants.HTTP_HEADERS.accept.data, {
214 'Authorization': req.get('Authorization')
215 });
216 request({
217 url: utils.confdPort(api_server) + '/api/config/config-agent/account/' + id,
218 method: 'DELETE',
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)) {
224 return resolve({
225 statusCode: response.statusCode,
226 data: JSON.stringify(response.body)
227 });
228 };
229 });
230 });
231 };
232
233
234
235 module.exports = ConfigAgentAccount;