RIFT-14970: Display general error message when ns fails to launch
[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;
132 if(xhr.responseText) {
133 try {
134 error = JSON.stringify(xhr.responseText);
135 } catch(e){
136
137 }
138 }
139 reject(error);
140 });
141 })
142 },
143 loading: Alt.actions.global.launchNSRLoading,
144 success: Alt.actions.global.launchNSRSuccess,
145 error: Alt.actions.global.launchNSRError
146 },
147 getSshKey: {
148 remote (state) {
149 return new Promise((resolve,reject) => {
150 $.ajax({
151 url: 'api/ssh-key?api_server=' + API_SERVER,
152 type: 'GET',
153 beforeSend: Utils.addAuthorizationStub,
154 success: function (data) {
155 resolve(data);
156 }
157 }).fail(function(xhr){
158 console.log(xhr)
159 //Authentication and the handling of fail states should be wrapped up into a connection class.
160 Utils.checkAuthentication(xhr.status);
161 });
162 });
163 },
164 loading: Actions.getInstantiateSshKeyLoading,
165 success: Actions.getInstantiateSshKeySuccess,
166 error: Actions.getInstantiateSshKeyError
167 },
168 getSshKeys:{
169 remote(state) {
170 return new Promise((resolve, reject) => {
171 $.ajax({
172 url: 'api/nsd/' + NSDId + '/input-param?api_server=' + API_SERVER,
173 type: 'GET',
174 beforeSend: Utils.addAuthorizationStub,
175 success: function (data) {
176 resolve(data);
177 }
178 });
179 });
180 }
181 },
182 getConfigAgent:{
183 remote (state, cb) {
184 return new Promise((resolve, reject) => {
185 $.ajax({
186 url: 'api/config-agent-account?api_server=' +
187 API_SERVER,
188 type: 'GET',
189 beforeSend: Utils.addAuthorizationStub,
190 success: function (data) {
191 resolve(data);
192 if(cb) {
193 cb();
194 }
195 }
196 })
197 })
198 },
199 success: Alt.actions.global.getConfigAgentSuccess,
200 error: Alt.actions.global.getConfigAgentError
201 },
202 getResourceOrchestrator: {
203 remote: function() {
204 return new Promise(function(resolve, reject) {
205 $.ajax({
206 url: 'passthrough/data/api/running/resource-orchestrator' + '?api_server=' + API_SERVER,
207 type: 'GET',
208 beforeSend: Utils.addAuthorizationStub,
209 contentType: "application/json",
210 success: function(data) {
211 let returnedData;
212 if (data.hasOwnProperty("rw-launchpad:resource-orchestrator")) {
213 returnedData = data['rw-launchpad:resource-orchestrator'];
214 } else {
215 returnedData = {};
216 }
217 resolve(returnedData);
218 },
219 error: function(error) {
220 console.log("There was an error updating the account: ", arguments);
221
222 }
223 }).fail(function(xhr){
224 //Authentication and the handling of fail states should be wrapped up into a connection class.
225 Utils.checkAuthentication(xhr.status);
226 return reject('error');
227 });
228 });
229 },
230 interceptResponse: interceptResponse({
231 'error': 'There was an error retrieving the resource orchestrator information.'
232 }),
233 success: Alt.actions.global.getResourceOrchestratorSuccess,
234 loading: Alt.actions.global.showScreenLoader,
235 error: Alt.actions.global.showNotification
236 }
237
238 }
239 }
240 function interceptResponse (responses) {
241 return function(data, action, args) {
242 if(responses.hasOwnProperty(data)) {
243 return {
244 type: data,
245 msg: responses[data]
246 }
247 } else {
248 return data;
249 }
250 }
251 }