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