update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / framework / core / modules / api / projectManagementAPI.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 // DescriptorModelMeta API (NSD + VNFD)
19
20
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) {
30 var self = this;
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(';') : '';
35
36 return new Promise(function(resolve, reject) {
37 Promise.all([
38 rp({
39 uri: `${utils.confdPort(api_server)}/${API_VERSION}/api/operational/project` + select,
40 method: 'GET',
41 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
42 'Authorization': req.session && req.session.authorization
43 }),
44 forever: constants.FOREVER_ON,
45 rejectUnauthorized: false,
46 resolveWithFullResponse: true
47 })
48 ]).then(function(result) {
49 var response = {};
50 response['data'] = {};
51 if (result[0].body) {
52 response['data']['project'] = JSON.parse(result[0].body)['rw-project:project'];
53 }
54 response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK
55
56 resolve(response);
57 }).catch(function(error) {
58 var response = {};
59 console.log('Problem with ProjectManagement.get', error);
60 response.statusCode = error.statusCode || 500;
61 response.errorMessage = {
62 error: 'Failed to get ProjectManagement' + error
63 };
64 reject(response);
65 });
66 });
67 };
68
69 ProjectManagement.create = function(req) {
70 var self = this;
71 var api_server = req.query['api_server'];
72 var data = req.body;
73 data = {
74 "project":[data]
75 }
76 return new Promise(function(resolve, reject) {
77 Promise.all([
78 rp({
79 uri: utils.confdPort(api_server) + '/' + API_VERSION + '/api/config/project',
80 method: 'POST',
81 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
82 'Authorization': req.session && req.session.authorization
83 }),
84 forever: constants.FOREVER_ON,
85 json: data,
86 rejectUnauthorized: false,
87 resolveWithFullResponse: true
88 })
89 ]).then(function(result) {
90 var response = {};
91 response['data'] = {};
92 if (result[0].body) {
93 response['data'] = result[0].body;
94 }
95 response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK
96
97 resolve(response);
98 }).catch(function(error) {
99 var response = {};
100 console.log('Problem with ProjectManagement.create', error);
101 response.statusCode = error.statusCode || 500;
102 response.errorMessage = {
103 error: 'Failed to create user' + error
104 };
105 reject(response);
106 });
107 });
108 };
109 ProjectManagement.update = function(req) {
110 //"rw-project:project"
111 var self = this;
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
120 }
121 }
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',
126 method: 'PUT',
127 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
128 'Authorization': req.session && req.session.authorization
129 }),
130 forever: constants.FOREVER_ON,
131 json: {
132 "project-config": bodyData['project-config']
133 },
134 rejectUnauthorized: false,
135 resolveWithFullResponse: true
136 });
137 updateTasks.push(updateProjectConfig);
138
139 var updateProjectDescription = rp({
140 uri: baseUrl + '/description',
141 method: 'PATCH',
142 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
143 'Authorization': req.session && req.session.authorization
144 }),
145 forever: constants.FOREVER_ON,
146 json: {"description": bodyData.description},
147 rejectUnauthorized: false,
148 resolveWithFullResponse: true
149 });
150 updateTasks.push(updateProjectDescription)
151 return new Promise(function(resolve, reject) {
152 Promise.all(
153 updateTasks
154 ).then(function(result) {
155 var response = {};
156 response['data'] = {};
157 if (result[0].body) {
158 response['data'] = result[0].body;
159 }
160 response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK
161
162 resolve(response);
163 }).catch(function(error) {
164 var response = {};
165 console.log('Problem with ProjectManagement.update', error);
166 response.statusCode = error.statusCode || 500;
167 response.errorMessage = {
168 error: 'Failed to update project - ' + error
169 };
170 reject(response);
171 });
172 });
173 };
174
175 ProjectManagement.delete = function(req) {
176 var self = this;
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
186 });
187 rp({
188 url: url,
189 method: 'DELETE',
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)) {
195 return resolve({
196 statusCode: response.statusCode,
197 data: JSON.stringify(response.body)
198 });
199 };
200 });
201 })
202 }
203
204
205 ProjectManagement.getPlatform = function(req, userId) {
206 var self = this;
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';
211 if(user) {
212 url = url + '/user/' + encodeURIComponent(user);
213 }
214 Promise.all([
215 rp({
216 uri: url,
217 method: 'GET',
218 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
219 'Authorization': req.session && req.session.authorization
220 }),
221 forever: constants.FOREVER_ON,
222 rejectUnauthorized: false,
223 resolveWithFullResponse: true
224 })
225 ]).then(function(result) {
226 var response = {};
227 response['data'] = {};
228 if (result[0].body) {
229 if(user) {
230 response['data']['platform'] = JSON.parse(result[0].body)['rw-rbac-platform:user'];
231 } else {
232 response['data']['platform'] = JSON.parse(result[0].body)['rw-rbac-platform:rbac-platform-config'];
233 }
234 }
235 response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK
236
237 resolve(response);
238 }).catch(function(error) {
239 var response = {};
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
244 };
245 reject(response);
246 });
247 });
248 };
249
250 ProjectManagement.updatePlatform = function(req) {
251 var self = this;
252 var api_server = req.query['api_server'];
253 var bodyData = req.body;
254 data = bodyData;
255 data.user = JSON.parse(data.user)
256 var updateTasks = [];
257
258 var updatePlatform = rp({
259 uri: utils.confdPort(api_server) + '/' + API_VERSION + '/api/config/rbac-platform-config',
260 method: 'PUT',
261 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
262 'Authorization': req.session && req.session.authorization
263 }),
264 forever: constants.FOREVER_ON,
265 json: {
266 "rw-rbac-platform:rbac-platform-config": data
267 },
268 rejectUnauthorized: false,
269 resolveWithFullResponse: true
270 });
271 updateTasks.push(updatePlatform)
272 return new Promise(function(resolve, reject) {
273 Promise.all([
274 updateTasks
275 ]).then(function(result) {
276 var response = {};
277 response['data'] = {};
278 if (result[0].body) {
279 response['data'] = result[0].body;
280 }
281 response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK
282
283 resolve(response);
284 }).catch(function(error) {
285 var response = {};
286 console.log('Problem with ProjectManagement.updatePlatform', error);
287 response.statusCode = error.statusCode || 500;
288 response.errorMessage = {
289 error: 'Failed to update platform - ' + error
290 };
291 reject(response);
292 });
293 });
294 };
295
296
297 module.exports = ProjectManagement;