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.
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');
29 Sdn
.get = function(req
) {
31 var api_server
= req
.query
["api_server"];
32 var id
= req
.params
.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
44 url
: utils
.projectContextUrl(req
, utils
.confdPort(api_server
) + '/api/operational/sdn/account?deep'),
46 headers
: requestHeaders
,
47 forever
: constants
.FOREVER_ON
,
48 rejectUnauthorized
: false
50 function(error
, response
, body
) {
52 if (utils
.validateResponse('Sdn.get', error
, response
, body
, resolve
, reject
)) {
54 data
= JSON
.parse(response
.body
).collection
['rw-sdn:account']
56 console
.log('Problem with "Sdn.get"', e
);
61 error
: 'Problem with "Sdn.get": ' + e
.toString()
69 statusCode
: response
.statusCode
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
84 url
: utils
.projectContextUrl(req
, utils
.confdPort(api_server
) + '/api/operational/sdn/account/' + id
+ '?deep'),
86 headers
: requestHeaders
,
87 forever
: constants
.FOREVER_ON
,
88 rejectUnauthorized
: false
90 function(error
, response
, body
) {
92 if (utils
.validateResponse('Sdn.get', error
, response
, body
, resolve
, reject
)) {
94 data
= JSON
.parse(response
.body
)['rw-sdn:sdn'];
96 console
.log('Problem with "Sdn.get"', e
);
101 error
: 'Problem with "Sdn.get": ' + e
.toString()
109 statusCode
: response
.statusCode
117 Sdn
.create = function(req
) {
119 var api_server
= req
.query
["api_server"];
122 return new Promise(function(resolve
, reject
) {
124 "sdn-account": Array
.isArray(data
) ? data
: [data
]
127 console
.log('Creating SDN account 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
.session
&& req
.session
.authorization
137 url
: utils
.projectContextUrl(req
, utils
.confdPort(api_server
) + '/api/config/sdn/account'),
139 headers
: requestHeaders
,
140 forever
: constants
.FOREVER_ON
,
141 rejectUnauthorized
: false,
143 }, function(error
, response
, body
) {
144 if (utils
.validateResponse('Sdn.create', error
, response
, body
, resolve
, reject
)) {
146 data
: JSON
.stringify(response
.body
),
147 statusCode
: response
.statusCode
154 Sdn
.update = function(req
) {
156 var api_server
= req
.query
["api_server"];
157 var id
= req
.params
.id
;
160 return new Promise(function(resolve
, reject
) {
165 console
.log('Updating SDN account ', id
, ' with', JSON
.stringify(jsonData
));
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
175 url
: utils
.projectContextUrl(req
, utils
.confdPort(api_server
) + '/api/config/sdn/account/' + id
),
177 headers
: requestHeaders
,
178 forever
: constants
.FOREVER_ON
,
179 rejectUnauthorized
: false,
181 }, function(error
, response
, body
) {
182 if (utils
.validateResponse('Sdn.update', error
, response
, body
, resolve
, reject
)) {
184 data
: JSON
.stringify(response
.body
),
185 statusCode
: response
.statusCode
192 Sdn
.delete = function(req
) {
194 var api_server
= req
.query
["api_server"];
195 var id
= req
.params
.id
;
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');
203 error
: 'Must specifiy api_server and id to delete sdn account'
209 return new Promise(function(resolve
, reject
) {
211 var requestHeaders
= {};
212 _
.extend(requestHeaders
,
213 constants
.HTTP_HEADERS
.accept
.data
, {
214 'Authorization': req
.session
&& req
.session
.authorization
218 url
: utils
.projectContextUrl(req
, utils
.confdPort(api_server
) + '/api/config/sdn/account/' + id
),
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
)) {
226 data
: JSON
.stringify(response
.body
),
227 statusCode
: response
.statusCode