Revert "BUG-410 -- update RIFT platform"
[osm/UI.git] / skyquake / plugins / composer / src / src / sources / CatalogPackageManagerSource.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 'use strict';
20
21 import alt from '../alt'
22 import catalogUtils from '../libraries/utils'
23 import CatalogPackageManagerActions from '../actions/CatalogPackageManagerActions'
24 import Utils from 'utils/utils.js';
25
26 let API_SERVER = catalogUtils.getSearchParams(window.location).api_server;
27 const FILE_SERVER = window.location.hostname === 'localhost'
28 ? API_SERVER : window.location.protocol + '//' + window.location.hostname;
29
30
31 const getAuthorization = () => 'Basic ' + window.sessionStorage.getItem("auth");
32
33 const getStateApiPath = (operation, id) =>
34 FILE_SERVER + ':8008/mano/' + operation + '/' + id + '/state';
35
36 const getComposerApiPath = (api) =>
37 window.location.origin + '/composer/api/' + api + '?api_server=' + catalogUtils.getSearchParams(window.location).api_server;
38
39 const SUCCESS = {
40 pending: false,
41 success: true,
42 error: false,
43 message: "Completely successfully"
44 };
45 const FAILED = {
46 pending: false,
47 success: false,
48 error: true,
49 message: "Failed"
50 };
51 const PENDING = {
52 pending: true,
53 success: false,
54 error: false,
55 message: "In progress"
56 };
57
58 function ajaxFetch(url, operation, resolve, reject, method = 'GET', input, urlOverride) {
59 let credentials = 'same-origin';
60 let body = input ? JSON.stringify(input) : null;
61 let headers = new Headers();
62 headers.append('Authorization', getAuthorization());
63 headers.append('Accept', 'application/json');
64 if (input) {
65 headers.append('Content-Type', 'application/json');
66 }
67
68 fetch(url, {method, credentials, headers, body})
69 .then(checkStatusGetJson)
70 .then(handleSuccess)
71 .catch(handleError);
72
73 function checkStatusGetJson(response) {
74 if (response.status >= 200 && response.status < 300) {
75 return response.json();
76 } else {
77 var error = new Error(response.statusText)
78 error.status = response.status;
79 error.statusText = response.statusText;
80 throw error
81 }
82 }
83
84 function handleSuccess (data) {
85 if (typeof data == 'string') {
86 data = JSON.parse(data);
87 }
88 resolve({
89 state: operation,
90 data,
91 operation
92 });
93 }
94
95 function handleError (data) {
96 if (typeof data == 'string') {
97 data = JSON.parse(data);
98 }
99 reject({
100 state: operation,
101 data,
102 operation
103 });
104 }
105 }
106
107 const CatalogPackageManagerSource = {
108
109 requestCatalogPackageDownload: function () {
110 return {
111 remote: function (state, download, format, grammar, schema) {
112 return new Promise((resolve, reject) => {
113 // we need an initial status for UI (server does not send)
114 const setStatusBeforeResolve = (response) => {
115 response.data.status = 'download-requested';
116 resolve(response);
117 };
118 const data = {
119 "package-type": download['catalogItems'][0]['uiState']['type'].toUpperCase(),
120 "package-id": download.ids,
121 "export-format": format && format.toUpperCase() || 'YAML',
122 "export-grammar": grammar && grammar.toUpperCase() || 'OSM',
123 "export-schema": schema && schema.toUpperCase() || "RIFT"
124 }
125 const path = getComposerApiPath('package-export');
126 ajaxFetch(path, download, setStatusBeforeResolve, reject, 'POST', data, true);
127 })
128 },
129 success: CatalogPackageManagerActions.downloadCatalogPackageStatusUpdated,
130 error: CatalogPackageManagerActions.downloadCatalogPackageError
131 };
132 },
133
134 requestCatalogPackageDownloadStatus: function() {
135 return {
136 remote: function(state, download) {
137 const transactionId = download.transactionId;
138 return new Promise(function(resolve, reject) {
139 const path = getStateApiPath('export', transactionId);
140 ajaxFetch(path, download, resolve, reject);
141 });
142 },
143 success: CatalogPackageManagerActions.downloadCatalogPackageStatusUpdated,
144 error: CatalogPackageManagerActions.downloadCatalogPackageError
145 }
146 },
147
148 requestCatalogPackageCopy: function () {
149 return {
150 remote: function (state, operationInfo) {
151 return new Promise((resolve, reject) => {
152 // we need an initial status for UI (server does not send)
153 const successHandler = (response) => {
154 const status = response.data.output.status;
155 const state = status === "COMPLETED" ? SUCCESS : status === "FAILED" ? FAILED : PENDING;
156 state.progress = 25; // put something
157 let operation = Object.assign({}, operationInfo, state);
158 operation.transactionId = response.data.output['transaction-id'];
159 resolve(operation);
160 }
161 const failHandler = (response) => {
162 let operation = Object.assign({}, this, FAILED);
163 reject(operation);
164 };
165 const data = {
166 "package-type": operationInfo.args.packageType,
167 "package-id": operationInfo.args.id,
168 "package-name": operationInfo.args.name
169 }
170 const path = getComposerApiPath('package-copy');
171 ajaxFetch(path, operationInfo, successHandler, failHandler, 'POST', data, true);
172 })
173 },
174 success: CatalogPackageManagerActions.updateStatus,
175 error: CatalogPackageManagerActions.updateStatus
176 };
177 },
178
179 requestCatalogPackageCopyStatus: function() {
180 return {
181 remote: function(state, operation) {
182 return new Promise(function(resolve, reject) {
183 const successHandler = (response) => {
184 const status = response.data.status;
185 const state = status === "COMPLETED" ? SUCCESS : status === "FAILED" ? FAILED : PENDING;
186 state.progress = state.pending ? operation.progress + ((100 - operation.progress) / 2) : 100;
187 let newOp = Object.assign({}, operation, state);
188 resolve(newOp);
189 };
190 const failHandler = (response) => {
191 reject(Object.assign({}, operation, FAILED));
192 };
193 const path = getComposerApiPath('package-copy/jobs/' + operation.transactionId);
194 ajaxFetch(path, operation, successHandler, failHandler);
195 });
196 },
197 success: CatalogPackageManagerActions.updateStatus,
198 error: CatalogPackageManagerActions.updateStatus
199 }
200 },
201
202 requestCatalogPackageUploadStatus: function () {
203 return {
204 remote: function (state, upload) {
205 const transactionId = upload.transactionId;
206 return new Promise(function (resolve, reject) {
207 const action = upload.riftAction === 'onboard' ? 'import' : 'update';
208 const path = getComposerApiPath('package-'+action+'/jobs/' + transactionId);
209 ajaxFetch(path, upload, resolve, reject);
210 });
211 },
212 success: CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated,
213 error: CatalogPackageManagerActions.uploadCatalogPackageError
214 };
215 }
216
217 };
218
219 export default CatalogPackageManagerSource;