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