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 // DescriptorModelMeta API (NSD + VNFD)
21 var ProjectManagement
= {};
22 var Promise
= require('bluebird');
23 var rp
= require('request-promise');
24 var Promise
= require('promise');
25 var constants
= require('../../api_utils/constants');
26 var utils
= require('../../api_utils/utils');
27 var _
= require('lodash');
28 var API_VERSION
= 'v2';
29 ProjectManagement
.get = function(req
, fields
) {
31 var api_server
= req
.query
['api_server'];
32 // by default just load basic info as this request is expensive
33 fields
= fields
|| ['name', 'description', 'project-config'];
34 var select
= fields
.length
? '?fields=' + fields
.join(';') : '';
36 return new Promise(function(resolve
, reject
) {
39 uri
: `${utils.confdPort(api_server)}/${API_VERSION}/api/operational/project` + select
,
41 headers
: _
.extend({}, constants
.HTTP_HEADERS
.accept
.data
, {
42 'Authorization': req
.session
&& req
.session
.authorization
44 forever
: constants
.FOREVER_ON
,
45 rejectUnauthorized
: false,
46 resolveWithFullResponse
: true
48 ]).then(function(result
) {
50 response
['data'] = {};
52 response
['data']['project'] = JSON
.parse(result
[0].body
)['rw-project:project'];
54 response
.statusCode
= constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
57 }).catch(function(error
) {
59 console
.log('Problem with ProjectManagement.get', error
);
60 response
.statusCode
= error
.statusCode
|| 500;
61 response
.errorMessage
= {
62 error
: 'Failed to get ProjectManagement' + error
69 ProjectManagement
.create = function(req
) {
71 var api_server
= req
.query
['api_server'];
76 return new Promise(function(resolve
, reject
) {
79 uri
: utils
.confdPort(api_server
) + '/' + API_VERSION
+ '/api/config/project',
81 headers
: _
.extend({}, constants
.HTTP_HEADERS
.accept
.data
, {
82 'Authorization': req
.session
&& req
.session
.authorization
84 forever
: constants
.FOREVER_ON
,
86 rejectUnauthorized
: false,
87 resolveWithFullResponse
: true
89 ]).then(function(result
) {
91 response
['data'] = {};
93 response
['data'] = result
[0].body
;
95 response
.statusCode
= constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
98 }).catch(function(error
) {
100 console
.log('Problem with ProjectManagement.create', error
);
101 response
.statusCode
= error
.statusCode
|| 500;
102 response
.errorMessage
= {
103 error
: 'Failed to create user' + error
109 ProjectManagement
.update = function(req
) {
110 //"rw-project:project"
112 var api_server
= req
.query
['api_server'];
113 var bodyData
= req
.body
;
114 // oddly enough, if we do not encode this here letting the request below does so incorrectly
115 var projectName
= encodeURIComponent(bodyData
.name
);
116 var descriptionData
= {
117 "rw-project:project" : {
118 "name": bodyData
.name
,
119 "description": bodyData
.description
122 var updateTasks
= [];
123 var baseUrl
= utils
.confdPort(api_server
) + '/' + API_VERSION
+ '/api/config/project/' + projectName
124 var updateProjectConfig
= rp({
125 uri
: baseUrl
+ '/project-config',
127 headers
: _
.extend({}, constants
.HTTP_HEADERS
.accept
.data
, {
128 'Authorization': req
.session
&& req
.session
.authorization
130 forever
: constants
.FOREVER_ON
,
132 "project-config": bodyData
['project-config']
134 rejectUnauthorized
: false,
135 resolveWithFullResponse
: true
137 updateTasks
.push(updateProjectConfig
);
139 var updateProjectDescription
= rp({
140 uri
: baseUrl
+ '/description',
142 headers
: _
.extend({}, constants
.HTTP_HEADERS
.accept
.data
, {
143 'Authorization': req
.session
&& req
.session
.authorization
145 forever
: constants
.FOREVER_ON
,
146 json
: {"description": bodyData
.description
},
147 rejectUnauthorized
: false,
148 resolveWithFullResponse
: true
150 updateTasks
.push(updateProjectDescription
)
151 return new Promise(function(resolve
, reject
) {
154 ).then(function(result
) {
156 response
['data'] = {};
157 if (result
[0].body
) {
158 response
['data'] = result
[0].body
;
160 response
.statusCode
= constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
163 }).catch(function(error
) {
165 console
.log('Problem with ProjectManagement.update', error
);
166 response
.statusCode
= error
.statusCode
|| 500;
167 response
.errorMessage
= {
168 error
: 'Failed to update project - ' + error
175 ProjectManagement
.delete = function(req
) {
177 var projectname
= encodeURIComponent(req
.params
.projectname
);
178 var api_server
= req
.query
["api_server"];
179 var requestHeaders
= {};
180 var url
= utils
.confdPort(api_server
) + '/' + API_VERSION
+ '/api/config/project/' + projectname
181 return new Promise(function(resolve
, reject
) {
182 _
.extend(requestHeaders
,
183 constants
.HTTP_HEADERS
.accept
.data
,
184 constants
.HTTP_HEADERS
.content_type
.data
, {
185 'Authorization': req
.session
&& req
.session
.authorization
190 headers
: requestHeaders
,
191 forever
: constants
.FOREVER_ON
,
192 rejectUnauthorized
: false,
193 }, function(error
, response
, body
) {
194 if (utils
.validateResponse('ProjectManagement.DELETE', error
, response
, body
, resolve
, reject
)) {
196 statusCode
: response
.statusCode
,
197 data
: JSON
.stringify(response
.body
)
205 ProjectManagement
.getPlatform = function(req
, userId
) {
207 var api_server
= req
.query
['api_server'];
208 var user
= req
.params
['userId'] || userId
;
209 return new Promise(function(resolve
, reject
) {
210 var url
= utils
.confdPort(api_server
) + '/' + API_VERSION
+ '/api/operational/rbac-platform-config';
212 url
= url
+ '/user/' + encodeURIComponent(user
);
218 headers
: _
.extend({}, constants
.HTTP_HEADERS
.accept
.data
, {
219 'Authorization': req
.session
&& req
.session
.authorization
221 forever
: constants
.FOREVER_ON
,
222 rejectUnauthorized
: false,
223 resolveWithFullResponse
: true
225 ]).then(function(result
) {
227 response
['data'] = {};
228 if (result
[0].body
) {
230 response
['data']['platform'] = JSON
.parse(result
[0].body
)['rw-rbac-platform:user'];
232 response
['data']['platform'] = JSON
.parse(result
[0].body
)['rw-rbac-platform:rbac-platform-config'];
235 response
.statusCode
= constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
238 }).catch(function(error
) {
240 console
.log('Problem with ProjectManagement.getPlatform', error
);
241 response
.statusCode
= error
.statusCode
|| 500;
242 response
.errorMessage
= {
243 error
: 'Failed to get ProjectManagement.getPlatform' + error
250 ProjectManagement
.updatePlatform = function(req
) {
252 var api_server
= req
.query
['api_server'];
253 var bodyData
= req
.body
;
255 data
.user
= JSON
.parse(data
.user
)
256 var updateTasks
= [];
258 var updatePlatform
= rp({
259 uri
: utils
.confdPort(api_server
) + '/' + API_VERSION
+ '/api/config/rbac-platform-config',
261 headers
: _
.extend({}, constants
.HTTP_HEADERS
.accept
.data
, {
262 'Authorization': req
.session
&& req
.session
.authorization
264 forever
: constants
.FOREVER_ON
,
266 "rw-rbac-platform:rbac-platform-config": data
268 rejectUnauthorized
: false,
269 resolveWithFullResponse
: true
271 updateTasks
.push(updatePlatform
)
272 return new Promise(function(resolve
, reject
) {
275 ]).then(function(result
) {
277 response
['data'] = {};
278 if (result
[0].body
) {
279 response
['data'] = result
[0].body
;
281 response
.statusCode
= constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
284 }).catch(function(error
) {
286 console
.log('Problem with ProjectManagement.updatePlatform', error
);
287 response
.statusCode
= error
.statusCode
|| 500;
288 response
.errorMessage
= {
289 error
: 'Failed to update platform - ' + error
297 module
.exports
= ProjectManagement
;