Merge branch 'v1.1'
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / PackageManagerApi.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 import guid from '../libraries/guid'
20 import DropZone from 'dropzone'
21 import Utils from '../libraries/utils'
22 import CatalogPackageManagerActions from '../actions/CatalogPackageManagerActions'
23 import ReactDOM from 'react-dom'
24 import $ from 'jquery'
25
26 const API_SERVER = Utils.getSearchParams(window.location).api_server;
27
28
29
30
31 export default class PackageManager {
32 constructor(element, button, action) {
33 this.stagingArea = {
34 packages: {
35 ids: []
36 }
37 }
38 this.stagingAreaMonitor = null;
39 }
40 createStagingArea(type, name) {
41 return $.ajax({
42 url: Utils.getSearchParams(window.location).api_server + ':8008/api/operations/create-staging-area',
43 type: 'POST',
44 data: {
45 "input" : {
46 // Package type not important for package upload.
47 "package-type": type || "NSD",
48 "name": name || "Package Staging Area"
49 }
50 },
51 error: function() {
52 console.log('Something went wrong creating the staging area: ', arguments)
53 }
54 }).then(function(data) {
55 /*
56 {
57 "output": {
58 "endpoint": "api/upload/85f8e2dc-638b-46e7-89cb-ee8de322066f",
59 "port": "4568"
60 }
61 }
62 */
63 const id = data.output.endpoint.split('/')[2];
64 const port = data.output.port;
65 this.stagingArea.packages.ids.push(id);
66 this.stagingArea.packages[id] = {
67 port: port
68 };
69 return data
70 })
71 }
72 monitoringStagingAreaSocket() {
73 let self = this;
74 if(self.stagingAreaMonitor) {
75 return self.stagingAreaMonitor;
76 }
77 new Promise(function(resolve, reject) {
78 $.ajax({
79 url: '/socket-polling',
80 type: 'POST',
81 beforeSend: Utils.addAuthorizationStub,
82 data: {
83 url: 'launchpad/api/nsr?api_server=' + API_SERVER
84 },
85 success: function(data, textStatus, jqXHR) {
86 Utils.checkAndResolveSocketRequest(data, resolve, reject, self.monitoringStagingAreaSocketHandler);
87 }
88 })
89 })
90
91 return undefined;
92 }
93 monitoringStagingAreaSocketHandler(connection) {
94 let self = this;
95 let ws = window.multiplexer.channel(connection);
96 if (!connection) return;
97 self.stagingAreaMonitor = connection;
98 ws.onmessage = function(socket) {
99 try {
100 Utils.checkAuthentication(data.statusCode, function() {
101 ws.close();
102 });
103
104 } catch(e) {
105 console.log('An exception occurred in monitoringStagingAreaSocketHandler', e)
106 }
107 }
108 }
109
110 }
111
112
113