Better error reporting on instantiate page
[osm/UI.git] / skyquake / plugins / launchpad / src / instantiate / launchNetworkServiceSource.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 import $ from 'jquery';
20 let Utils = require('utils/utils.js');
21 let rw = require('utils/rw.js');
22 const API_SERVER = require('utils/rw.js').getSearchParams(window.location).api_server;
23 const API_PORT = require('utils/rw.js').getSearchParams(window.location).api_port;
24 export default function(Alt){
25 const Actions = Alt.actions.global;
26 return {
27 getCatalog: {
28 remote (state) {
29 return new Promise((resolve,reject) => {
30 $.ajax({
31 url: 'api/decorated-catalog?api_server=' + API_SERVER,
32 type: 'GET',
33 beforeSend: Utils.addAuthorizationStub,
34 success: function (data) {
35 resolve(
36 typeof(data) == "string" ? JSON.parse(data):data
37 );
38 }
39 }).fail(function(xhr){
40 console.log(xhr)
41 //Authentication and the handling of fail states should be wrapped up into a connection class.
42 Utils.checkAuthentication(xhr.status);
43 });
44 });
45 },
46 success: Alt.actions.global.getCatalogSuccess,
47 error: Alt.actions.global.getCatalogError
48 },
49 getCloudAccount:{
50 remote (state, cb) {
51 return new Promise((resolve, reject) => {
52 $.ajax({
53 url: 'api/cloud-account?api_server=' +
54 API_SERVER,
55 type: 'GET',
56 beforeSend: Utils.addAuthorizationStub,
57 success: function (data) {
58 resolve(data);
59 if(cb) {
60 cb();
61 }
62 }
63 })
64 })
65 },
66 success: Alt.actions.global.getLaunchCloudAccountSuccess,
67 error: Alt.actions.global.getLaunchCloudAccountError
68 },
69 getDataCenters:{
70 remote () {
71 return new Promise((resolve, reject) => {
72 $.ajax({
73 url: 'api/data-centers?api_server=' +
74 API_SERVER,
75 type: 'GET',
76 beforeSend: Utils.addAuthorizationStub,
77 success: function (data) {
78 resolve(data);
79 }
80 })
81 })
82 },
83 success: Alt.actions.global.getDataCentersSuccess,
84 error: Alt.actions.global.getDataCentersError
85 },
86 getVDU: {
87 remote (state, VNFDid) {
88 return new Promise((resolve,reject) => {
89 $.ajax({
90 url: 'api/vnfd?api_server=' + API_SERVER,
91 type: 'POST',
92 beforeSend: Utils.addAuthorizationStub,
93 dataType:'json',
94 data: {
95 data: VNFDid
96 },
97 success: function (data) {
98 resolve(
99 typeof(data) == "string" ? JSON.parse(data):data
100 );
101 }
102 })
103 });
104 },
105 success: Alt.actions.global.getVDUSuccess,
106 error: Alt.actions.global.getVDUError
107 },
108 launchNSR: {
109 remote (state, NSR) {
110 return new Promise((resolve, reject) => {
111 console.log('Attempting to instantiate NSR:', NSR)
112 $.ajax({
113 url: 'api/nsr?api_server=' + API_SERVER,
114 type: 'POST',
115 beforeSend: Utils.addAuthorizationStub,
116 dataType:'json',
117 data: {
118 data: NSR
119 },
120 success: function (data) {
121 resolve(
122 typeof(data) == "string" ? JSON.parse(data):data
123 );
124 },
125 error: function (err) {
126 console.log('There was an error launching')
127 }
128 }).fail(function(xhr){
129 //Authentication and the handling of fail states should be wrapped up into a connection class.
130 Utils.checkAuthentication(xhr.status);
131 var error = null;
132 if(xhr.responseText) {
133 try {
134 error = JSON.parse(xhr.responseText);
135 error = JSON.parse(error.error)['rpc-reply']['rpc-error']['error-message'];
136 } catch(e){
137 console.log(e);
138 }
139 }
140 reject(error);
141 });
142 })
143 },
144 loading: Alt.actions.global.launchNSRLoading,
145 success: Alt.actions.global.launchNSRSuccess,
146 error: Alt.actions.global.launchNSRError
147 },
148 getSshKey: {
149 remote (state) {
150 return new Promise((resolve,reject) => {
151 $.ajax({
152 url: 'api/ssh-key?api_server=' + API_SERVER,
153 type: 'GET',
154 beforeSend: Utils.addAuthorizationStub,
155 success: function (data) {
156 resolve(data);
157 }
158 }).fail(function(xhr){
159 console.log(xhr)
160 //Authentication and the handling of fail states should be wrapped up into a connection class.
161 Utils.checkAuthentication(xhr.status);
162 });
163 });
164 },
165 loading: Actions.getInstantiateSshKeyLoading,
166 success: Actions.getInstantiateSshKeySuccess,
167 error: Actions.getInstantiateSshKeyError
168 },
169 getSshKeys:{
170 remote(state) {
171 return new Promise((resolve, reject) => {
172 $.ajax({
173 url: 'api/nsd/' + NSDId + '/input-param?api_server=' + API_SERVER,
174 type: 'GET',
175 beforeSend: Utils.addAuthorizationStub,
176 success: function (data) {
177 resolve(data);
178 }
179 });
180 });
181 }
182 },
183 getConfigAgent:{
184 remote (state, cb) {
185 return new Promise((resolve, reject) => {
186 $.ajax({
187 url: 'api/config-agent-account?api_server=' +
188 API_SERVER,
189 type: 'GET',
190 beforeSend: Utils.addAuthorizationStub,
191 success: function (data) {
192 resolve(data);
193 if(cb) {
194 cb();
195 }
196 }
197 })
198 })
199 },
200 success: Alt.actions.global.getConfigAgentSuccess,
201 error: Alt.actions.global.getConfigAgentError
202 },
203 getResourceOrchestrator: {
204 remote: function() {
205 return new Promise(function(resolve, reject) {
206 $.ajax({
207 url: 'passthrough/data/api/running/resource-orchestrator' + '?api_server=' + API_SERVER,
208 type: 'GET',
209 beforeSend: Utils.addAuthorizationStub,
210 contentType: "application/json",
211 success: function(data) {
212 let returnedData;
213 if (data.hasOwnProperty("rw-launchpad:resource-orchestrator")) {
214 returnedData = data['rw-launchpad:resource-orchestrator'];
215 } else {
216 returnedData = {};
217 }
218 resolve(returnedData);
219 },
220 error: function(error) {
221 console.log("There was an error updating the account: ", arguments);
222
223 }
224 }).fail(function(xhr){
225 //Authentication and the handling of fail states should be wrapped up into a connection class.
226 Utils.checkAuthentication(xhr.status);
227 return reject('error');
228 });
229 });
230 },
231 interceptResponse: interceptResponse({
232 'error': 'There was an error retrieving the resource orchestrator information.'
233 }),
234 success: Alt.actions.global.getResourceOrchestratorSuccess,
235 loading: Alt.actions.global.showScreenLoader,
236 error: Alt.actions.global.showNotification
237 }
238
239 }
240 }
241 function interceptResponse (responses) {
242 return function(data, action, args) {
243 if(responses.hasOwnProperty(data)) {
244 return {
245 type: data,
246 msg: responses[data]
247 }
248 } else {
249 return data;
250 }
251 }
252 }