71e3d7d5dda7f491e8573f727d89782b8ec6e0bb
[osm/UI.git] / skyquake / plugins / launchpad / src / launchpadFleetSource.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
19 let API_SERVER = require('utils/rw.js').getSearchParams(window.location).api_server;
20 let HOST = API_SERVER;
21 let NODE_PORT = require('utils/rw.js').getSearchParams(window.location).api_port || 8000;
22 let DEV_MODE = require('utils/rw.js').getSearchParams(window.location).dev_mode || false;
23
24 if (DEV_MODE) {
25 HOST = window.location.protocol + '//' + window.location.hostname;
26 }
27
28 var isSocketOff = true;
29 var FleetActions = require('./launchpadFleetActions.js');
30 var Utils = require('utils/utils.js');
31 import $ from 'jquery';
32
33
34 module.exports = function(Alt) {
35 //NEW SOURCE
36 return {
37 nsrControl: {
38 remote: function(state, method, url, value) {
39 return new Promise(function(resolve, reject) {
40 // $.ajax({});
41 // console.log(method + 'ing: "' + value + '" to "' + url + '"');
42 resolve(method + 'ing: "' + value + '" to "' + url + '"')
43 });
44 },
45 success: FleetActions.nsrControlSuccess,
46 error: FleetActions.nsrControlError
47 },
48 getNsrInstances: {
49 remote: function() {
50 return new Promise(function(resolve, reject) {
51 $.ajax({
52 url: 'api/nsr?api_server=' + API_SERVER,
53 type: 'GET',
54 beforeSend: Utils.addAuthorizationStub,
55 success: function(data) {
56 resolve(data);
57 }
58 });
59 });
60 },
61 success: FleetActions.getNsrInstancesSuccess,
62 error: FleetActions.getNsrInstancesError
63 },
64 deleteNsrInstance: {
65 remote: function(d, id) {
66 console.log(id)
67 return new Promise(function(resolve, reject) {
68 $.ajax({
69 url: 'api/nsr/' + id + '?api_server=' + API_SERVER,
70 type: 'DELETE',
71 beforeSend: Utils.addAuthorizationStub,
72 success: function(data) {
73 resolve(data);
74 }
75 });
76 });
77 },
78 success: FleetActions.deleteNsrInstanceSuccess,
79 error: FleetActions.deleteNsrInstancesError
80 } ,
81 openNSRSocket: {
82 remote: function(state) {
83 return new Promise(function(resolve, reject) {
84 //If socket connection already exists, eat the request.
85 if(state.socket) {
86 return resolve(false);
87 }
88 $.ajax({
89 url: '/socket-polling?api_server=' + API_SERVER ,
90 type: 'POST',
91 beforeSend: Utils.addAuthorizationStub,
92 data: {
93 url: 'launchpad/api/nsr?api_server=' + API_SERVER
94 },
95 success: function(data, textStatus, jqXHR) {
96 Utils.checkAndResolveSocketRequest(data, resolve, reject);
97 }
98 }).fail(function(xhr){
99 //Authentication and the handling of fail states should be wrapped up into a connection class.
100 Utils.checkAuthentication(xhr.status);
101 });;
102 });
103 },
104 loading: Alt.actions.global.openNSRSocketLoading,
105 success: FleetActions.openNSRSocketSuccess,
106 error: FleetActions.openNSRError
107 },
108 setNSRStatus: {
109 remote: function(state, id, status) {
110 return new Promise(function(resolve, reject) {
111 $.ajax({
112 url: 'api/nsr/' + id + '/admin-status?api_server=' + API_SERVER ,
113 type:'PUT',
114 beforeSend: Utils.addAuthorizationStub,
115 data: {
116 status: status
117 },
118 success: function(data, textStatus, jqXHR) {
119
120 }
121 });
122 });
123 },
124 success: FleetActions.setNSRStatusSuccess,
125 error: FleetActions.setNSRStatusError
126 }
127 }
128 };
129
130 Object.size = function(obj) {
131 var size = 0,
132 key;
133 for (key in obj) {
134 if (obj.hasOwnProperty(key)) size++;
135 }
136 return size;
137 };