update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / plugins / accounts / api / sdn_account / sdnAccount.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 // SDN-Account APIs
19
20
21 var Sdn = {};
22 var request = require('request');
23 var Promise = require('promise');
24 var utils = require('../../../../framework/core/api_utils/utils.js');
25 var constants = require('../../../../framework/core/api_utils/constants.js');
26 var _ = require('underscore');
27
28 //Sdn Account APIs
29 Sdn.get = function(req) {
30
31 var api_server = req.query["api_server"];
32 var id = req.params.id;
33
34 var self = this;
35 if (!id) {
36 // Get all sdn accounts
37 return new Promise(function(resolve, reject) {
38 var requestHeaders = {};
39 _.extend(requestHeaders,
40 constants.HTTP_HEADERS.accept.collection, {
41 'Authorization': req.session && req.session.authorization
42 });
43 request({
44 url: utils.projectContextUrl(req, utils.confdPort(api_server) + '/api/operational/sdn/account?deep'),
45 type: 'GET',
46 headers: requestHeaders,
47 forever: constants.FOREVER_ON,
48 rejectUnauthorized: false
49 },
50 function(error, response, body) {
51 var data;
52 if (utils.validateResponse('Sdn.get', error, response, body, resolve, reject)) {
53 try {
54 data = JSON.parse(response.body).collection['rw-sdn:account']
55 } catch (e) {
56 console.log('Problem with "Sdn.get"', e);
57
58 var err = {};
59 err.statusCode = 500;
60 err.errorMessage = {
61 error: 'Problem with "Sdn.get": ' + e.toString()
62 }
63
64 return reject(err);
65 }
66
67 return resolve({
68 data: data,
69 statusCode: response.statusCode
70 });
71 }
72 });
73 });
74 } else {
75 //Get a specific sdn account
76 return new Promise(function(resolve, reject) {
77 var requestHeaders = {};
78 _.extend(requestHeaders,
79 constants.HTTP_HEADERS.accept.data, {
80 'Authorization': req.session && req.session.authorization
81 });
82
83 request({
84 url: utils.projectContextUrl(req, utils.confdPort(api_server) + '/api/operational/sdn/account/' + id + '?deep'),
85 type: 'GET',
86 headers: requestHeaders,
87 forever: constants.FOREVER_ON,
88 rejectUnauthorized: false
89 },
90 function(error, response, body) {
91 var data;
92 if (utils.validateResponse('Sdn.get', error, response, body, resolve, reject)) {
93 try {
94 data = JSON.parse(response.body)['rw-sdn:sdn'];
95 } catch (e) {
96 console.log('Problem with "Sdn.get"', e);
97
98 var err = {};
99 err.statusCode = 500;
100 err.errorMessage = {
101 error: 'Problem with "Sdn.get": ' + e.toString()
102 }
103
104 return reject(err);
105 }
106
107 return resolve({
108 data: data,
109 statusCode: response.statusCode
110 });
111 }
112 });
113 });
114 }
115 };
116
117 Sdn.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 "sdn-account": Array.isArray(data) ? data : [data]
125 };
126
127 console.log('Creating SDN account 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.session && req.session.authorization
134 });
135
136 request({
137 url: utils.projectContextUrl(req, utils.confdPort(api_server) + '/api/config/sdn/account'),
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('Sdn.create', error, response, body, resolve, reject)) {
145 return resolve({
146 data: JSON.stringify(response.body),
147 statusCode: response.statusCode
148 });
149 }
150 });
151 });
152 };
153
154 Sdn.update = function(req) {
155
156 var api_server = req.query["api_server"];
157 var id = req.params.id;
158 var data = req.body;
159
160 return new Promise(function(resolve, reject) {
161 var jsonData = {
162 "rw-sdn:sdn": data
163 };
164
165 console.log('Updating SDN account ', id, ' with', JSON.stringify(jsonData));
166
167 var requestHeaders = {};
168 _.extend(requestHeaders,
169 constants.HTTP_HEADERS.accept.data,
170 constants.HTTP_HEADERS.content_type.data, {
171 'Authorization': req.session && req.session.authorization
172 });
173
174 request({
175 url: utils.projectContextUrl(req, utils.confdPort(api_server) + '/api/config/sdn/account/' + id),
176 method: 'PUT',
177 headers: requestHeaders,
178 forever: constants.FOREVER_ON,
179 rejectUnauthorized: false,
180 json: jsonData,
181 }, function(error, response, body) {
182 if (utils.validateResponse('Sdn.update', error, response, body, resolve, reject)) {
183 return resolve({
184 data: JSON.stringify(response.body),
185 statusCode: response.statusCode
186 });
187 }
188 });
189 });
190 };
191
192 Sdn.delete = function(req) {
193
194 var api_server = req.query["api_server"];
195 var id = req.params.id;
196
197 if (!id || !api_server) {
198 return new Promise(function(resolve, reject) {
199 console.log('Must specifiy api_server and id to delete sdn account');
200 reject({
201 statusCode: 500,
202 errorMessage: {
203 error: 'Must specifiy api_server and id to delete sdn account'
204 }
205 });
206 });
207 };
208
209 return new Promise(function(resolve, reject) {
210
211 var requestHeaders = {};
212 _.extend(requestHeaders,
213 constants.HTTP_HEADERS.accept.data, {
214 'Authorization': req.session && req.session.authorization
215 });
216
217 request({
218 url: utils.projectContextUrl(req, utils.confdPort(api_server) + '/api/config/sdn/account/' + id),
219 method: 'DELETE',
220 headers: requestHeaders,
221 forever: constants.FOREVER_ON,
222 rejectUnauthorized: false
223 }, function(error, response, body) {
224 if (utils.validateResponse('Sdn.delete', error, response, body, resolve, reject)) {
225 return resolve({
226 data: JSON.stringify(response.body),
227 statusCode: response.statusCode
228 });
229 }
230 });
231 });
232 };
233
234 module.exports = Sdn