be11dfcf1ad44ca7d534001f62b3d6a36568df96
[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 $ from 'jquery'
22 import alt from '../alt'
23 import utils from '../libraries/utils'
24 import CatalogPackageManagerActions from '../actions/CatalogPackageManagerActions'
25 let Utils = require('utils/utils.js');
26 function getApiServerOrigin() {
27 return utils.getSearchParams(window.location).upload_server + ':4567';
28 }
29
30 function ajaxRequest(path, catalogPackage, resolve, reject, method = 'GET') {
31 $.ajax({
32 url: getApiServerOrigin() + path,
33 type: method,
34 beforeSend: Utils.addAuthorizationStub,
35 dataType: 'json',
36 success: function(data) {
37 if (typeof data == 'string') {
38 data = JSON.parse(data);
39 }
40 resolve({
41 data: data,
42 state: catalogPackage
43 });
44 },
45 error: function(error) {
46 if (typeof error == 'string') {
47 error = JSON.parse(error);
48 }
49 reject({
50 data: error,
51 state: catalogPackage
52 });
53 }
54 }).fail(function(xhr){
55 //Authentication and the handling of fail states should be wrapped up into a connection class.
56 Utils.checkAuthentication(xhr.status);
57 });
58 }
59
60 const CatalogPackageManagerSource = {
61
62 requestCatalogPackageDownload: function () {
63 return {
64 remote: function (state, download, format, grammar) {
65 return new Promise((resolve, reject) => {
66 // the server does not add a status in the payload
67 // so we add one so that the success handler will
68 // be able to follow the flow of this download
69 const setStatusBeforeResolve = (response = {}) => {
70 response.data.status = 'download-requested';
71 resolve(response);
72 };
73 // RIFT-13485 requires to send type (nsd/vnfd) as a path element.
74 // Backend no longer supports mixed multi-package download.
75 // Probably does not even support multi-package download of same type.
76 // Hence, pick the type from the first element.
77 const path = '/api/export/' + download['catalogItems'][0]['uiState']['type'] + '?schema=' + format + '&grammar=' + grammar + '&format=yaml&ids=' + download.ids;
78 ajaxRequest(path, download, setStatusBeforeResolve, reject);
79 });
80 },
81 success: CatalogPackageManagerActions.downloadCatalogPackageStatusUpdated,
82 error: CatalogPackageManagerActions.downloadCatalogPackageError
83 };
84 },
85
86 requestCatalogPackageDownloadStatus: function() {
87 return {
88 remote: function(state, download) {
89 const transactionId = download.transactionId;
90 return new Promise(function(resolve, reject) {
91 const path = '/api/export/' + transactionId + '/state';
92 ajaxRequest(path, download, resolve, reject);
93 });
94 },
95 success: CatalogPackageManagerActions.downloadCatalogPackageStatusUpdated,
96 error: CatalogPackageManagerActions.downloadCatalogPackageError
97 }
98 },
99
100 requestCatalogPackageUploadStatus: function () {
101 return {
102 remote: function (state, upload) {
103 const transactionId = upload.transactionId;
104 return new Promise(function (resolve, reject) {
105 const action = upload.riftAction === 'onboard' ? 'upload' : 'update';
106 const path = '/api/' + action + '/' + transactionId + '/state';
107 ajaxRequest(path, upload, resolve, reject);
108 });
109 },
110 success: CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated,
111 error: CatalogPackageManagerActions.uploadCatalogPackageError
112 };
113 }
114
115 };
116
117 export default CatalogPackageManagerSource;