update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / framework / core / modules / api / appConfigAPI.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 Schema = {};
22 var request = require('request');
23 var Promise = require('promise');
24 var constants = require('../../api_utils/constants');
25 var utils = require('../../api_utils/utils');
26 var _ = require('lodash');
27 var cors = require('cors');
28 var bodyParser = require('body-parser');
29 var utils = require('../../api_utils/utils');
30 var sessionAPI = require('./sessions.js');
31 var configuration = require('./configuration');
32
33 var router = require('express').Router();
34
35 router.use(bodyParser.json());
36 router.use(cors());
37 router.use(bodyParser.urlencoded({
38 extended: true
39 }));
40
41 router.get('/app-config', cors(), function (req, res) {
42 getConfig(req).then(function (response) {
43 utils.sendSuccessResponse(response, res);
44 }, function (error) {
45 utils.sendErrorResponse(error, res);
46 });
47 });
48
49 var inactivityTimeout = process.env.UI_TIMEOUT_SECS || 600000;
50
51 var versionPromise = null;
52
53 var init = function () {
54 versionPromise = new Promise(
55 function (resolve, reject) {
56 sessionAPI.sessionPromise.then(
57 function (session) {
58 request({
59 url: configuration.getBackendURL() + '/api/operational/version',
60 type: 'GET',
61 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
62 'Authorization': session.authorization
63 }),
64 forever: constants.FOREVER_ON,
65 rejectUnauthorized: false
66 },
67 function (error, response, body) {
68 var data;
69 if (utils.validateResponse('schema/version.get', error, response, body, resolve, reject)) {
70 try {
71 data = JSON.parse(response.body)['rw-base:version'];
72 resolve(data.version);
73 } catch (e) {
74 return reject({});
75 }
76 } else {
77 console.log(error);
78 }
79 });
80 });
81 });
82 }
83
84 var getConfig = function (req) {
85 var api_server = req.query['api_server'];
86
87 var requests = [versionPromise];
88
89 return new Promise(function (resolve, reject) {
90 Promise.all(requests).then(
91 function (results) {
92 var data = {
93 version: results[0],
94 'api-server': configuration.getBackendURL,
95 'inactivity-timeout': process.env.UI_TIMEOUT_SECS || 600000
96 }
97 resolve({
98 data: data,
99 statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK
100 });
101 }).catch(
102 function (error) {
103 var response = {};
104 console.log('Problem with config.get', error);
105 response.statusCode = error.statusCode || 500;
106 response.errorMessage = {
107 error: 'Failed to get config' + error
108 };
109 reject(response);
110 });
111 });
112 };
113
114 module.exports = {
115 getRouter: function () {
116 return router;
117 },
118 init: init
119 };