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