RIFT-14134, RIFT-14548: RO CRU and Instantiate Data Centers
[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
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
256 if (!connection) return;
257 this.setState({
258 socket: ws.ws,
259 channelId: connection
260 });
261 ws.onmessage = (socket) => {
262 try {
263 var data = JSON.parse(socket.data);
264 let SdnOptions = [{
265 label: 'Select an SDN Account',
266 value: false
267 }];
268 SdnOptions = SdnOptions.concat(this.generateOptionsByName(data.sdn.data))
269 var newState = {
270 cloud: data.cloud.data,
271 'config-agent': data['config-agent'].data,
272 sdn: data.sdn.data,
273 sdnOptions: SdnOptions
274 };
275
276 //If account is selected, updated connection status only
277 if(self.currentAccount) {
278 let Account = self.getAccountFromStream(data[self.currentAccount.type].data, self.currentAccount.name);
279 newState.account = self.account;
280 newState.account['connection-status'] = Account['connection-status']
281 }
282 self.setState(newState)
283 } catch(error) {
284 console.log('Hit at exception in openAccountSocketSuccess', error)
285 }
286
287 }
288 ws.onclose = () => {
289 self.closeSocket();
290 }
291 }
292 closeSocket = () => {
293 if (this.socket) {
294 window.multiplexer.channel(this.channelId).close();
295 }
296 this.setState({
297 socket: null
298 })
299 }
300 setAccountTemplate = (AccountType, type) => {
301 let account = {
302 name: '',
303 'account-type': type || AccountMeta[AccountType].defaultType,
304 params: AccountMeta[AccountType].params[AccountMeta[AccountType].defaultType],
305 nestedParams: AccountMeta[AccountType].nestedParams ? AccountMeta[AccountType].nestedParams[AccountMeta[AccountType].defaultType]:null,
306 'connection-status': {
307 status: ''
308 }
309 };
310
311 account[type || AccountMeta[AccountType].defaultType] = {}
312 this.setState({
313 account: account,
314 accountType: AccountType,
315 types: AccountMeta[AccountType].types,
316 currentAccount: null
317 })
318 }
319 getAccountFromStream(data, name) {
320 let result = null;
321 data.map(function(a) {
322 if(a.name == name) {
323 result = a;
324 }
325 });
326 return result;
327 }
328 viewAccount = ({type, name}) => {
329 var data = null;
330 var accounts = null;
331 if(this && this[type].length) {
332 accounts = this[type];
333 data = this.getAccountFromStream(accounts, name);
334 if(data) {
335 let accountParams = {
336 params: AccountMeta[type].params[data['account-type']]
337 };
338
339 let accountNestedParams = {
340 nestedParams: AccountMeta[type].nestedParams?AccountMeta[type].nestedParams[data['account-type']]:null
341 };
342
343 this.setState({
344 currentAccount: {type, name},
345 account: Object.assign(data, accountParams, accountNestedParams),
346 accountType: type
347 })
348 }
349 }
350 }
351 generateOptionsByName(data) {
352 let results = [];
353 if (data.constructor.name == "Array") {
354 data.map(function(d) {
355 results.push({
356 label: d.name,
357 value: d.name
358 })
359 });
360 }
361 return results;
362 }
363 updateAccount = (account) => {
364 this.setState({account:account})
365 }
366 handleNameChange = (event) => {
367 var account = this.account;
368 account.name = event.target.value;
369 this.setState(
370 {
371 account:account
372 }
373 );
374 }
375 handleAccountTypeChange = (node, event) => {
376 var temp = {};
377 temp.name = this.account.name;
378 temp['account-type'] = event.target.value;
379 temp.params= AccountMeta[this.accountType].params[event.target.value];
380 temp.nestedParams = AccountMeta[this.accountType]?AccountMeta[this.accountType].nestedParams[event.target.value]:null;
381 temp[event.target.value] = {};
382 this.setState({
383 account: temp
384 });
385 }
386 handleParamChange(node, event) {
387 return function(event) {
388 var account = this.state.account;
389 account[account['account-type']][node.ref] = event.target.value;
390 account.params[node.ref] = event.target.value;
391 this.updateAccount(account);
392 }.bind(this);
393 }
394 handleNestedParamChange(containerName, node, event) {
395 return function(event) {
396 var account = this.state.account;
397 account[account['account-type']][containerName] = account[account['account-type']][containerName] || {};
398 account[account['account-type']][containerName][node.ref] = event.target.value;
399 account[containerName] = account[containerName] || {};
400 account[containerName].params = account[containerName].params || {};
401 account[containerName].params[node.ref] = event.target.value;
402 this.updateAccount(account);
403 }.bind(this);
404 }
405 getImage = (type) => {
406 return AccountMeta.image[type];
407 }
408 }
409
410
411 /**
412 *Cloud
413 *
414 * {"name":"eng2","account-type":"openstack","openstack":{"key":"lmaultsb","secret":"mypasswd","auth_url":"http://engstack.eng.riftio.com:5000/v3/","tenant":"lmaultsb","mgmt-network":"private"}}
415 Name
416 Path
417
418
419 SDN
420
421
422 *
423 *
424 */