5d5bd2f922845062a6df0f887b66677c91667b88
[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: "Tenant",
120 ref: 'tenant'
121 }, {
122 label: 'Management Network',
123 ref: 'mgmt-network'
124 }, {
125 label: 'Floating IP Pool',
126 ref: 'floating-ip-pool',
127 optional: true
128 }, {
129 label: "User Domain",
130 ref: 'user-domain',
131 optional: true
132 }, {
133 label: "Project Domain",
134 ref: 'project-domain',
135 optional: true
136 }, {
137 label: "Region",
138 ref: 'region',
139 optional: true
140 }],
141 "openvim": [{
142 label: "Host",
143 ref: 'host'
144 }, {
145 label: "Tenant Name",
146 ref: 'tenant-name'
147 }, {
148 label: 'Management Network',
149 ref: 'mgmt-network'
150 }, {
151 label: "Port",
152 ref: 'port',
153 optional: true
154 }]
155 },
156 nestedParams: {
157 "openvim": {
158 "container-name": "image-management",
159 "label": "Image Management",
160 "params": [{
161 label: "Username",
162 ref: 'username'
163 }, {
164 label: "Password",
165 ref: 'password'
166 }, {
167 label: 'Image Directory Path',
168 ref: 'image-directory-path',
169 optional: true
170 }]
171 }
172 },
173 types: [{
174 "name": "OpenStack",
175 "account-type": "openstack"
176 }, {
177 "name": "Cloudsim",
178 "account-type": "cloudsim_proxy"
179 }, {
180 "name": "AWS",
181 "account-type": "aws"
182 }, {
183 "name": "Open VIM",
184 "account-type": "openvim"
185 }]
186 },
187 resources: {
188 },
189 image: {
190 "aws": require("../../images/aws.png"),
191 "openvim": require("../../images/openmano.png"),
192 "openstack": require("../../images/openstack.png"),
193 "cloudsim_proxy": require("../../images/riftio.png"),
194 "odl": require("../../images/OpenDaylight_logo.png"),
195 "juju": require("../../images/juju.svg")
196
197 },
198 labelByType: {
199 "aws": "AWS",
200 "openvim": "Open VIM",
201 "openstack": "OpenStack",
202 "cloudsim_proxy": "Cloudsim"
203 }
204 }
205
206 export default class AccountStore {
207 constructor() {
208 this.cloud = [];
209 this['config-agent'] = [];
210 this.sdn = [];
211 this.account = null;
212 this.types = [];
213 this.refreshingAll = false;
214 this.sdnOptions = [];
215 this.AccountMeta = AccountMeta;
216 this.showVIM = true;
217 this.bindActions(AccountActions(this.alt));
218 this.registerAsync(AccountSource);
219 this.exportPublicMethods({
220 closeSocket:this.closeSocket,
221 setAccountTemplate: this.setAccountTemplate,
222 handleParamChange: this.handleParamChange,
223 handleNameChange: this.handleNameChange,
224 handleAccountTypeChange: this.handleAccountTypeChange,
225 updateAccount: this.updateAccount,
226 viewAccount: this.viewAccount,
227 handleNestedParamChange: this.handleNestedParamChange,
228 getImage: this.getImage
229 })
230 }
231 refreshAllAccountsSuccess = () => {
232 this.setState({
233 refreshingAll: false
234 });
235 }
236 refreshAllAccountsLoading = () => {
237 this.setState({
238 refreshingAll: true
239 });
240 }
241 refreshAllAccountsError = () => {
242
243 }
244 refreshCloudAccountFail = () => {
245 console.log(this);
246 }
247 refreshCloudAccountSuccess = () => {
248
249 }
250 getResourceOrchestratorSuccess = (data) => {
251 this.alt.actions.global.hideScreenLoader.defer();
252 if(data['account-type'] == 'openmano') {
253 this.setState({
254 showVIM: false
255 })
256 }
257 }
258 deleteAccountSuccess = (response) => {
259 this.setState({
260 currentAccount: false,
261 account: {}
262 });
263 }
264 openAccountSocketSuccess = (connection) => {
265 let self = this;
266 let ws = window.multiplexer.channel(connection);
267 if (!connection) return;
268 this.setState({
269 socket: ws.ws,
270 channelId: connection
271 });
272 ws.onmessage = (socket) => {
273 try {
274 var data = JSON.parse(socket.data);
275 Utils.checkAuthentication(data.statusCode, function() {
276 self.closeSocket();
277 });
278 let SdnOptions = [{
279 label: 'Select an SDN Account',
280 value: false
281 }];
282 SdnOptions = SdnOptions.concat(this.generateOptionsByName(data.sdn.data))
283 var newState = {
284 cloud: data.cloud.data,
285 'config-agent': data['config-agent'].data,
286 sdn: data.sdn.data,
287 sdnOptions: SdnOptions
288 };
289
290 //If account is selected, updated connection status only
291 if(self.currentAccount) {
292 let Account = self.getAccountFromStream(data[self.currentAccount.type].data, self.currentAccount.name);
293 newState.account = self.account;
294 newState.account['connection-status'] = Account['connection-status']
295 }
296 self.setState(newState)
297 } catch(error) {
298 console.log('Hit at exception in openAccountSocketSuccess', error)
299 }
300
301 }
302 ws.onclose = () => {
303 self.closeSocket();
304 }
305 }
306 closeSocket = () => {
307 if (this.socket) {
308 window.multiplexer.channel(this.channelId).close();
309 }
310 this.setState({
311 socket: null
312 })
313 }
314 setAccountTemplate = (AccountType, type) => {
315 let account = {
316 name: '',
317 'account-type': type || AccountMeta[AccountType].defaultType,
318 params: AccountMeta[AccountType].params[AccountMeta[AccountType].defaultType],
319 nestedParams: AccountMeta[AccountType].nestedParams ? AccountMeta[AccountType].nestedParams[AccountMeta[AccountType].defaultType]:null,
320 'connection-status': {
321 status: ''
322 }
323 };
324
325 account[type || AccountMeta[AccountType].defaultType] = {}
326 this.setState({
327 account: account,
328 accountType: AccountType,
329 types: AccountMeta[AccountType].types,
330 currentAccount: null
331 })
332 }
333 getAccountFromStream(data, name) {
334 let result = null;
335 data.map(function(a) {
336 if(a.name == name) {
337 result = a;
338 }
339 });
340 return result;
341 }
342 viewAccount = ({type, name}) => {
343 var data = null;
344 var accounts = null;
345 if(this && this[type].length) {
346 accounts = this[type];
347 data = this.getAccountFromStream(accounts, name);
348 if(data) {
349 let accountParams = {
350 params: AccountMeta[type].params[data['account-type']]
351 };
352
353 let accountNestedParams = {
354 nestedParams: AccountMeta[type].nestedParams?AccountMeta[type].nestedParams[data['account-type']]:null
355 };
356
357 this.setState({
358 currentAccount: {type, name},
359 account: Object.assign(data, accountParams, accountNestedParams),
360 accountType: type
361 })
362 }
363 }
364 }
365 generateOptionsByName(data) {
366 let results = [];
367 if (data && data.constructor.name == "Array") {
368 data.map(function(d) {
369 results.push({
370 label: d.name,
371 value: d.name
372 })
373 });
374 }
375 return results;
376 }
377 updateAccount = (account) => {
378 this.setState({account:account})
379 }
380 handleNameChange = (event) => {
381 var account = this.account;
382 account.name = event.target.value;
383 this.setState(
384 {
385 account:account
386 }
387 );
388 }
389 handleAccountTypeChange = (node, event) => {
390 var temp = {};
391 temp.name = this.account.name;
392 temp['account-type'] = event.target.value;
393 temp.params= AccountMeta[this.accountType].params[event.target.value];
394 temp.nestedParams = AccountMeta[this.accountType]?AccountMeta[this.accountType].nestedParams[event.target.value]:null;
395 temp[event.target.value] = {};
396 this.setState({
397 account: temp
398 });
399 }
400 handleParamChange(node, event) {
401 return function(event) {
402 var account = this.state.account;
403 account[account['account-type']][node.ref] = event.target.value;
404 account.params[node.ref] = event.target.value;
405 this.updateAccount(account);
406 }.bind(this);
407 }
408 handleNestedParamChange(containerName, node, event) {
409 return function(event) {
410 var account = this.state.account;
411 account[account['account-type']][containerName] = account[account['account-type']][containerName] || {};
412 account[account['account-type']][containerName][node.ref] = event.target.value;
413 account[containerName] = account[containerName] || {};
414 account[containerName].params = account[containerName].params || {};
415 account[containerName].params[node.ref] = event.target.value;
416 this.updateAccount(account);
417 }.bind(this);
418 }
419 getImage = (type) => {
420 return AccountMeta.image[type];
421 }
422 }
423