RIFT-16172: Project switch does not affect polling and socket pages.
[osm/UI.git] / skyquake / plugins / accounts / src / account / accountStore.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 import AccountActions from './accountActions.js';
19 import AccountSource from './accountSource.js';
20
21 var Utils = require('utils/utils.js');
22 var rw = require('utils/rw.js');
23 var altImage = rw.getSearchParams(window.location).alt_image;
24 var _ = require('lodash');
25
26 let Params = {
27 //Config Agent
28 ConfigAgent: {
29
30 }
31 }
32
33
34
35 let AccountMeta = {
36 'config-agent': {
37
38 defaultType: 'juju',
39 params: {
40 "juju": [{
41 label: "IP Address",
42 ref: 'ip-address'
43 }, {
44 label: "Port",
45 ref: 'port',
46 optional: true
47 }, {
48 label: "Username",
49 ref: 'user',
50 optional: true
51 }, {
52 label: "Secret",
53 ref: 'secret',
54 optional: true
55 }]
56 },
57 types: [{
58 "name": "JUJU",
59 "account-type": "juju",
60 }]
61 },
62 'sdn': {
63 defaultType: 'odl',
64 params: {
65 "odl": [{
66 label: "Username",
67 ref: 'username'
68 }, {
69 label: "Password",
70 ref: 'password'
71 }, {
72 label: "URL",
73 ref: 'url'
74 }]
75 },
76 types: [{
77 "name": "ODL",
78 "account-type": "odl",
79 }]
80 },
81 'cloud': {
82 defaultType: 'openstack',
83 params: {
84 "aws": [{
85 label: "Key",
86 ref: 'key'
87 }, {
88 label: "Secret",
89 ref: 'secret'
90 }, {
91 label: "Availability Zone",
92 ref: 'availability-zone'
93 }, {
94 label: "Default Subnet ID",
95 ref: 'default-subnet-id'
96 }, {
97 label: "Region",
98 ref: 'region'
99 }, {
100 label: "VPC ID",
101 ref: 'vpcid'
102 }, {
103 label: "SSH Key",
104 ref: 'ssh-key'
105 }],
106 "cloudsim_proxy": [{
107 label: "Host",
108 ref: "host"
109 }],
110 "openstack": [{
111 label: "Key",
112 ref: 'key'
113 }, {
114 label: "Secret",
115 ref: 'secret'
116 }, {
117 label: "Authentication URL",
118 ref: 'auth_url'
119 }, {
120 label: "Tenant",
121 ref: 'tenant'
122 }, {
123 label: 'Management Network',
124 ref: 'mgmt-network'
125 }, {
126 label: 'Floating IP Pool',
127 ref: 'floating-ip-pool',
128 optional: true
129 }, {
130 label: "User Domain",
131 ref: 'user-domain',
132 optional: true
133 }, {
134 label: "Project Domain",
135 ref: 'project-domain',
136 optional: true
137 }, {
138 label: "Region",
139 ref: 'region',
140 optional: true
141 }],
142 "openvim": [{
143 label: "Host",
144 ref: 'host'
145 }, {
146 label: "Tenant Name",
147 ref: 'tenant-name'
148 }, {
149 label: 'Management Network',
150 ref: 'mgmt-network'
151 }, {
152 label: "Port",
153 ref: 'port',
154 optional: true
155 }]
156 },
157 nestedParams: {
158 "openvim": {
159 "container-name": "image-management",
160 "label": "Image Management",
161 "params": [{
162 label: "Username",
163 ref: 'username'
164 }, {
165 label: "Password",
166 ref: 'password'
167 }, {
168 label: 'Image Directory Path',
169 ref: 'image-directory-path',
170 optional: true
171 }]
172 }
173 },
174 types: [{
175 "name": "OpenStack",
176 "account-type": "openstack"
177 }, {
178 "name": "Cloudsim",
179 "account-type": "cloudsim_proxy"
180 }, {
181 "name": "AWS",
182 "account-type": "aws"
183 }, {
184 "name": "Open VIM",
185 "account-type": "openvim"
186 }]
187 },
188 resources: {
189 },
190 image: {
191 "aws": require("../../images/aws.png"),
192 "openvim": require("../../images/openmano.png"),
193 "openstack": require("../../images/openstack.png"),
194 "cloudsim_proxy": require("../../images/riftio.png"),
195 "odl": require("../../images/OpenDaylight_logo.png"),
196 "juju": require("../../images/juju.svg")
197
198 },
199 labelByType: {
200 "aws": "AWS",
201 "openvim": "Open VIM",
202 "openstack": "OpenStack",
203 "cloudsim_proxy": "Cloudsim"
204 }
205 }
206
207 export default class AccountStore {
208 constructor() {
209 this.cloud = [];
210 this['config-agent'] = [];
211 this.sdn = [];
212 this.account = null;
213 this.types = [];
214 this.refreshingAll = false;
215 this.sdnOptions = [];
216 this.AccountMeta = AccountMeta;
217 this.showVIM = true;
218 this.bindActions(AccountActions(this.alt));
219 this.registerAsync(AccountSource);
220 this.exportPublicMethods({
221 closeSocket:this.closeSocket,
222 setAccountTemplate: this.setAccountTemplate,
223 handleParamChange: this.handleParamChange,
224 handleNameChange: this.handleNameChange,
225 handleAccountTypeChange: this.handleAccountTypeChange,
226 updateAccount: this.updateAccount,
227 viewAccount: this.viewAccount,
228 handleNestedParamChange: this.handleNestedParamChange,
229 getImage: this.getImage
230 })
231 }
232 refreshAllAccountsSuccess = () => {
233 this.setState({
234 refreshingAll: false
235 });
236 }
237 refreshAllAccountsLoading = () => {
238 this.setState({
239 refreshingAll: true
240 });
241 }
242 refreshAllAccountsError = () => {
243
244 }
245 refreshCloudAccountFail = () => {
246 console.log(this);
247 }
248 refreshCloudAccountSuccess = () => {
249
250 }
251 getResourceOrchestratorSuccess = (data) => {
252 this.alt.actions.global.hideScreenLoader.defer();
253 if(data['rw-launchpad:resource-orchestrator'] && (data['rw-launchpad:resource-orchestrator']['account-type'] == 'openmano')) {
254 this.setState({
255 showVIM: false
256 })
257 }
258 }
259 deleteAccountSuccess = (response) => {
260 this.setState({
261 currentAccount: false,
262 account: {}
263 });
264 }
265 openAccountSocketSuccess = (connection) => {
266 let self = this;
267 let ws = window.multiplexer.channel(connection);
268 if (!connection) return;
269 this.setState({
270 socket: ws.ws,
271 channelId: connection
272 });
273 ws.onmessage = (socket) => {
274 try {
275 var data = JSON.parse(socket.data);
276 Utils.checkAuthentication(data.statusCode, function() {
277 self.closeSocket();
278 });
279 let SdnOptions = [{
280 label: 'Select an SDN Account',
281 value: false
282 }];
283 SdnOptions = SdnOptions.concat(this.generateOptionsByName(data.sdn.data))
284 var newState = {
285 cloud: data.cloud.data,
286 'config-agent': data['config-agent'].data,
287 sdn: data.sdn.data,
288 sdnOptions: SdnOptions
289 };
290
291 //If account is selected, updated connection status only
292 if(self.currentAccount) {
293 let Account = self.getAccountFromStream(data[self.currentAccount.type].data, self.currentAccount.name);
294 newState.account = self.account;
295 newState.account['connection-status'] = Account && Account['connection-status']
296 }
297 self.setState(newState)
298 } catch(error) {
299 console.log('Hit at exception in openAccountSocketSuccess', error)
300 }
301
302 }
303 ws.onclose = () => {
304 self.closeSocket();
305 }
306 }
307 closeSocket = () => {
308 if (this.socket) {
309 window.multiplexer.channel(this.channelId).close();
310 }
311 this.setState({
312 socket: null
313 })
314 }
315 setAccountTemplate = (AccountType, type) => {
316 let account = {
317 name: '',
318 'account-type': type || AccountMeta[AccountType].defaultType,
319 params: AccountMeta[AccountType].params[AccountMeta[AccountType].defaultType],
320 nestedParams: AccountMeta[AccountType].nestedParams ? AccountMeta[AccountType].nestedParams[AccountMeta[AccountType].defaultType]:null,
321 'connection-status': {
322 status: ''
323 }
324 };
325
326 account[type || AccountMeta[AccountType].defaultType] = {}
327 this.setState({
328 account: account,
329 accountType: AccountType,
330 types: AccountMeta[AccountType].types,
331 currentAccount: null
332 })
333 }
334 getAccountFromStream(data, name) {
335 let result = null;
336 data && _.isArray(data) && data.map(function(a) {
337 if(a.name == name) {
338 result = a;
339 }
340 });
341 return result;
342 }
343 viewAccount = ({type, name}) => {
344 var data = null;
345 var accounts = null;
346 if(this && this[type] && this[type].length) {
347 accounts = this[type];
348 data = this.getAccountFromStream(accounts, name);
349 if(data) {
350 let accountParams = {
351 params: AccountMeta[type].params[data['account-type']]
352 };
353
354 let accountNestedParams = {
355 nestedParams: AccountMeta[type].nestedParams?AccountMeta[type].nestedParams[data['account-type']]:null
356 };
357
358 this.setState({
359 currentAccount: {type, name},
360 account: Object.assign(data, accountParams, accountNestedParams),
361 accountType: type
362 })
363 }
364 }
365 }
366 generateOptionsByName(data) {
367 let results = [];
368 if (data && _.isArray(data)) {
369 data.map(function(d) {
370 results.push({
371 label: d.name,
372 value: d.name
373 })
374 });
375 }
376 return results;
377 }
378 updateAccount = (account) => {
379 this.setState({account:account})
380 }
381 handleNameChange = (event) => {
382 var account = this.account;
383 account.name = event.target.value;
384 this.setState(
385 {
386 account:account
387 }
388 );
389 }
390 handleAccountTypeChange = (node, event) => {
391 var temp = {};
392 temp.name = this.account.name;
393 temp['account-type'] = event.target.value;
394 temp.params= AccountMeta[this.accountType].params[event.target.value];
395 temp.nestedParams = AccountMeta[this.accountType]?AccountMeta[this.accountType].nestedParams[event.target.value]:null;
396 temp[event.target.value] = {};
397 this.setState({
398 account: temp
399 });
400 }
401 handleParamChange(node, event) {
402 return function(event) {
403 var account = this.state.account;
404 account[account['account-type']][node.ref] = event.target.value;
405 account.params[node.ref] = event.target.value;
406 this.updateAccount(account);
407 }.bind(this);
408 }
409 handleNestedParamChange(containerName, node, event) {
410 return function(event) {
411 var account = this.state.account;
412 account[account['account-type']][containerName] = account[account['account-type']][containerName] || {};
413 account[account['account-type']][containerName][node.ref] = event.target.value;
414 account[containerName] = account[containerName] || {};
415 account[containerName].params = account[containerName].params || {};
416 account[containerName].params[node.ref] = event.target.value;
417 this.updateAccount(account);
418 }.bind(this);
419 }
420 getImage = (type) => {
421 return AccountMeta.image[type];
422 }
423 }
424