NOTICKET: Refactor with sessions. Now holds auth on server
[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
29 ProjectManagement.get = function(req) {
30 var self = this;
31 var api_server = req.query['api_server'];
32
33 return new Promise(function(resolve, reject) {
34 Promise.all([
35 rp({
36 uri: utils.confdPort(api_server) + '/api/operational/project',
37 method: 'GET',
38 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
39 'Authorization': req.session && req.session.authorization
40 }),
41 forever: constants.FOREVER_ON,
42 rejectUnauthorized: false,
43 resolveWithFullResponse: true
44 })
45 ]).then(function(result) {
46 var response = {};
47 response['data'] = {};
48 if (result[0].body) {
49 response['data']['project'] = JSON.parse(result[0].body)['rw-project:project'];
50 }
51 response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK
52
53 resolve(response);
54 }).catch(function(error) {
55 var response = {};
56 console.log('Problem with ProjectManagement.get', error);
57 response.statusCode = error.statusCode || 500;
58 response.errorMessage = {
59 error: 'Failed to get ProjectManagement' + error
60 };
61 reject(response);
62 });
63 });
64 };
65 ProjectManagement.create = function(req) {
66 var self = this;
67 var api_server = req.query['api_server'];
68 var data = req.body;
69 data = {
70 "project":[data]
71 }
72 return new Promise(function(resolve, reject) {
73 Promise.all([
74 rp({
75 uri: utils.confdPort(api_server) + '/api/config/project',
76 method: 'POST',
77 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
78 'Authorization': req.session && req.session.authorization
79 }),
80 forever: constants.FOREVER_ON,
81 json: data,
82 rejectUnauthorized: false,
83 resolveWithFullResponse: true
84 })
85 ]).then(function(result) {
86 var response = {};
87 response['data'] = {};
88 if (result[0].body) {
89 response['data'] = result[0].body;
90 }
91 response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK
92
93 resolve(response);
94 }).catch(function(error) {
95 var response = {};
96 console.log('Problem with ProjectManagement.create', error);
97 response.statusCode = error.statusCode || 500;
98 response.errorMessage = {
99 error: 'Failed to create user' + error
100 };
101 reject(response);
102 });
103 });
104 };
105 ProjectManagement.update = function(req) {
106 var self = this;
107 var api_server = req.query['api_server'];
108 var bodyData = req.body;
109 data = {
110 "project":[bodyData]
111 }
112 var updateTasks = [];
113
114 var updateUser = rp({
115 uri: utils.confdPort(api_server) + '/api/config/project',
116 method: 'PUT',
117 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
118 'Authorization': req.session && req.session.authorization
119 }),
120 forever: constants.FOREVER_ON,
121 json: data,
122 rejectUnauthorized: false,
123 resolveWithFullResponse: true
124 });
125 updateTasks.push(updateUser)
126 return new Promise(function(resolve, reject) {
127 Promise.all([
128 updateTasks
129 ]).then(function(result) {
130 var response = {};
131 response['data'] = {};
132 if (result[0].body) {
133 response['data'] = result[0].body;
134 }
135 response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK
136
137 resolve(response);
138 }).catch(function(error) {
139 var response = {};
140 console.log('Problem with ProjectManagement.update', error);
141 response.statusCode = error.statusCode || 500;
142 response.errorMessage = {
143 error: 'Failed to passwordChange user' + error
144 };
145 reject(response);
146 });
147 });
148 };
149
150 ProjectManagement.delete = function(req) {
151 var self = this;
152 var projectname = req.params.projectname;
153 var api_server = req.query["api_server"];
154 var requestHeaders = {};
155 var url = `${utils.confdPort(api_server)}/api/config/project/${projectname}`
156 return new Promise(function(resolve, reject) {
157 _.extend(requestHeaders,
158 constants.HTTP_HEADERS.accept.data,
159 constants.HTTP_HEADERS.content_type.data, {
160 'Authorization': req.session && req.session.authorization
161 });
162 rp({
163 url: url,
164 method: 'DELETE',
165 headers: requestHeaders,
166 forever: constants.FOREVER_ON,
167 rejectUnauthorized: false,
168 }, function(error, response, body) {
169 if (utils.validateResponse('ProjectManagement.DELETE', error, response, body, resolve, reject)) {
170 return resolve({
171 statusCode: response.statusCode,
172 data: JSON.stringify(response.body)
173 });
174 };
175 });
176 })
177 }
178 module.exports = ProjectManagement;