]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
ad39b0ebae743bf8d2e18f12f86848f66e7a264a
[ceph.git] /
1 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
2 import {
3   TreeComponent,
4   ITreeOptions,
5   TreeModel,
6   TreeNode,
7   TREE_ACTIONS
8 } from '@circlon/angular-tree-component';
9 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
10 import _ from 'lodash';
11
12 import { forkJoin, Subscription, timer as observableTimer } from 'rxjs';
13 import { RgwRealmService } from '~/app/shared/api/rgw-realm.service';
14 import { RgwZoneService } from '~/app/shared/api/rgw-zone.service';
15 import { RgwZonegroupService } from '~/app/shared/api/rgw-zonegroup.service';
16 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
17 import { ActionLabelsI18n, TimerServiceInterval } from '~/app/shared/constants/app.constants';
18 import { Icons } from '~/app/shared/enum/icons.enum';
19 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
20 import { CdTableAction } from '~/app/shared/models/cd-table-action';
21 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
22 import { Permission } from '~/app/shared/models/permissions';
23 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
24 import { ModalService } from '~/app/shared/services/modal.service';
25 import { NotificationService } from '~/app/shared/services/notification.service';
26 import { TimerService } from '~/app/shared/services/timer.service';
27 import { RgwRealm, RgwZone, RgwZonegroup } from '../models/rgw-multisite';
28 import { RgwMultisiteMigrateComponent } from '../rgw-multisite-migrate/rgw-multisite-migrate.component';
29 import { RgwMultisiteZoneDeletionFormComponent } from '../models/rgw-multisite-zone-deletion-form/rgw-multisite-zone-deletion-form.component';
30 import { RgwMultisiteZonegroupDeletionFormComponent } from '../models/rgw-multisite-zonegroup-deletion-form/rgw-multisite-zonegroup-deletion-form.component';
31 import { RgwMultisiteExportComponent } from '../rgw-multisite-export/rgw-multisite-export.component';
32 import { RgwMultisiteImportComponent } from '../rgw-multisite-import/rgw-multisite-import.component';
33 import { RgwMultisiteRealmFormComponent } from '../rgw-multisite-realm-form/rgw-multisite-realm-form.component';
34 import { RgwMultisiteZoneFormComponent } from '../rgw-multisite-zone-form/rgw-multisite-zone-form.component';
35 import { RgwMultisiteZonegroupFormComponent } from '../rgw-multisite-zonegroup-form/rgw-multisite-zonegroup-form.component';
36 import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service';
37 import { MgrModuleService } from '~/app/shared/api/mgr-module.service';
38 import { BlockUI, NgBlockUI } from 'ng-block-ui';
39 import { Router } from '@angular/router';
40 import { RgwMultisiteWizardComponent } from '../rgw-multisite-wizard/rgw-multisite-wizard.component';
41 import { RgwMultisiteSyncPolicyComponent } from '../rgw-multisite-sync-policy/rgw-multisite-sync-policy.component';
42
43 const BASE_URL = 'rgw/multisite/configuration';
44
45 @Component({
46   selector: 'cd-rgw-multisite-details',
47   templateUrl: './rgw-multisite-details.component.html',
48   styleUrls: ['./rgw-multisite-details.component.scss']
49 })
50 export class RgwMultisiteDetailsComponent implements OnDestroy, OnInit {
51   private sub = new Subscription();
52
53   @ViewChild('tree') tree: TreeComponent;
54   @ViewChild(RgwMultisiteSyncPolicyComponent) syncPolicyComp: RgwMultisiteSyncPolicyComponent;
55
56   messages = {
57     noDefaultRealm: $localize`Please create a default realm first to enable this feature`,
58     noMasterZone: $localize`Please create a master zone for each zone group to enable this feature`,
59     noRealmExists: $localize`No realm exists`,
60     disableExport: $localize`Please create master zone group and master zone for each of the realms`
61   };
62
63   @BlockUI()
64   blockUI: NgBlockUI;
65
66   icons = Icons;
67   permission: Permission;
68   selection = new CdTableSelection();
69   createTableActions: CdTableAction[];
70   migrateTableAction: CdTableAction[];
71   importAction: CdTableAction[];
72   exportAction: CdTableAction[];
73   multisiteReplicationActions: CdTableAction[];
74   loadingIndicator = true;
75   nodes: object[] = [];
76   treeOptions: ITreeOptions = {
77     useVirtualScroll: true,
78     nodeHeight: 22,
79     levelPadding: 20,
80     actionMapping: {
81       mouse: {
82         click: this.onNodeSelected.bind(this)
83       }
84     }
85   };
86   modalRef: NgbModalRef;
87
88   realms: RgwRealm[] = [];
89   zonegroups: RgwZonegroup[] = [];
90   zones: RgwZone[] = [];
91   metadata: any;
92   metadataTitle: string;
93   bsModalRef: NgbModalRef;
94   realmIds: string[] = [];
95   zoneIds: string[] = [];
96   defaultRealmId = '';
97   defaultZonegroupId = '';
98   defaultZoneId = '';
99   multisiteInfo: object[] = [];
100   defaultsInfo: string[] = [];
101   showMigrateAndReplicationActions = false;
102   editTitle: string = 'Edit';
103   deleteTitle: string = 'Delete';
104   disableExport = true;
105   rgwModuleStatus: boolean;
106   restartGatewayMessage = false;
107   rgwModuleData: string | any[] = [];
108   activeId: string;
109
110   constructor(
111     private modalService: ModalService,
112     private timerService: TimerService,
113     private authStorageService: AuthStorageService,
114     public actionLabels: ActionLabelsI18n,
115     public timerServiceVariable: TimerServiceInterval,
116     public router: Router,
117     public rgwRealmService: RgwRealmService,
118     public rgwZonegroupService: RgwZonegroupService,
119     public rgwZoneService: RgwZoneService,
120     public rgwDaemonService: RgwDaemonService,
121     public mgrModuleService: MgrModuleService,
122     private notificationService: NotificationService
123   ) {
124     this.permission = this.authStorageService.getPermissions().rgw;
125   }
126
127   openModal(entity: any, edit = false) {
128     const entityName = edit ? entity.data.type : entity;
129     const action = edit ? 'edit' : 'create';
130     const initialState = {
131       resource: entityName,
132       action: action,
133       info: entity,
134       defaultsInfo: this.defaultsInfo,
135       multisiteInfo: this.multisiteInfo
136     };
137     if (entityName === 'realm') {
138       this.bsModalRef = this.modalService.show(RgwMultisiteRealmFormComponent, initialState, {
139         size: 'lg'
140       });
141     } else if (entityName === 'zonegroup') {
142       this.bsModalRef = this.modalService.show(RgwMultisiteZonegroupFormComponent, initialState, {
143         size: 'lg'
144       });
145     } else {
146       this.bsModalRef = this.modalService.show(RgwMultisiteZoneFormComponent, initialState, {
147         size: 'lg'
148       });
149     }
150   }
151
152   openMultisiteSetupWizard() {
153     this.bsModalRef = this.modalService.show(RgwMultisiteWizardComponent, {
154       size: 'lg'
155     });
156   }
157
158   openMigrateModal() {
159     const initialState = {
160       multisiteInfo: this.multisiteInfo
161     };
162     this.bsModalRef = this.modalService.show(RgwMultisiteMigrateComponent, initialState, {
163       size: 'lg'
164     });
165   }
166
167   openImportModal() {
168     const initialState = {
169       multisiteInfo: this.multisiteInfo
170     };
171     this.bsModalRef = this.modalService.show(RgwMultisiteImportComponent, initialState, {
172       size: 'lg'
173     });
174   }
175
176   openExportModal() {
177     const initialState = {
178       defaultsInfo: this.defaultsInfo,
179       multisiteInfo: this.multisiteInfo
180     };
181     this.bsModalRef = this.modalService.show(RgwMultisiteExportComponent, initialState, {
182       size: 'lg'
183     });
184   }
185
186   getDisableExport() {
187     this.realms.forEach((realm: any) => {
188       this.zonegroups.forEach((zonegroup) => {
189         if (realm.id === zonegroup.realm_id) {
190           if (zonegroup.is_master && zonegroup.master_zone !== '') {
191             this.disableExport = false;
192           }
193         }
194       });
195     });
196     if (!this.rgwModuleStatus) {
197       return true;
198     }
199     if (this.realms.length < 1) {
200       return this.messages.noRealmExists;
201     } else if (this.disableExport) {
202       return this.messages.disableExport;
203     } else {
204       return false;
205     }
206   }
207
208   getDisableImport() {
209     if (!this.rgwModuleStatus) {
210       return true;
211     } else {
212       return false;
213     }
214   }
215
216   ngOnInit() {
217     this.createTableActions = [
218       {
219         permission: 'create',
220         icon: Icons.add,
221         name: this.actionLabels.CREATE + ' Realm',
222         click: () => this.openModal('realm')
223       },
224       {
225         permission: 'create',
226         icon: Icons.add,
227         name: this.actionLabels.CREATE + ' Zone Group',
228         click: () => this.openModal('zonegroup'),
229         disable: () => this.getDisable()
230       },
231       {
232         permission: 'create',
233         icon: Icons.add,
234         name: this.actionLabels.CREATE + ' Zone',
235         click: () => this.openModal('zone')
236       }
237     ];
238     this.migrateTableAction = [
239       {
240         permission: 'create',
241         icon: Icons.wrench,
242         name: this.actionLabels.MIGRATE,
243         click: () => this.openMigrateModal()
244       }
245     ];
246     this.importAction = [
247       {
248         permission: 'create',
249         icon: Icons.download,
250         name: this.actionLabels.IMPORT,
251         click: () => this.openImportModal(),
252         disable: () => this.getDisableImport()
253       }
254     ];
255     this.exportAction = [
256       {
257         permission: 'create',
258         icon: Icons.upload,
259         name: this.actionLabels.EXPORT,
260         click: () => this.openExportModal(),
261         disable: () => this.getDisableExport()
262       }
263     ];
264     this.multisiteReplicationActions = [
265       {
266         permission: 'create',
267         icon: Icons.wrench,
268         name: this.actionLabels.SETUP_MULTISITE_REPLICATION,
269         click: () =>
270           this.router.navigate([BASE_URL, { outlets: { modal: 'setup-multisite-replication' } }])
271       }
272     ];
273
274     const observables = [
275       this.rgwRealmService.getAllRealmsInfo(),
276       this.rgwZonegroupService.getAllZonegroupsInfo(),
277       this.rgwZoneService.getAllZonesInfo()
278     ];
279     this.sub = this.timerService
280       .get(() => forkJoin(observables), this.timerServiceVariable.TIMER_SERVICE_PERIOD * 2)
281       .subscribe(
282         (multisiteInfo: [object, object, object]) => {
283           this.multisiteInfo = multisiteInfo;
284           this.loadingIndicator = false;
285           this.nodes = this.abstractTreeData(multisiteInfo);
286         },
287         (_error) => {}
288       );
289     this.mgrModuleService.list().subscribe((moduleData: any) => {
290       this.rgwModuleData = moduleData.filter((module: object) => module['name'] === 'rgw');
291       if (this.rgwModuleData.length > 0) {
292         this.rgwModuleStatus = this.rgwModuleData[0].enabled;
293       }
294     });
295   }
296   /* setConfigValues() {
297     this.rgwDaemonService
298       .setMultisiteConfig(
299         this.defaultsInfo['defaultRealmName'],
300         this.defaultsInfo['defaultZonegroupName'],
301         this.defaultsInfo['defaultZoneName']
302       )
303       .subscribe(() => {});
304   }*/
305
306   ngOnDestroy() {
307     this.sub.unsubscribe();
308   }
309
310   private abstractTreeData(multisiteInfo: [object, object, object]): any[] {
311     let allNodes: object[] = [];
312     let rootNodes = {};
313     let firstChildNodes = {};
314     let allFirstChildNodes = [];
315     let secondChildNodes = {};
316     let allSecondChildNodes: {}[] = [];
317     this.realms = multisiteInfo[0]['realms'];
318     this.zonegroups = multisiteInfo[1]['zonegroups'];
319     this.zones = multisiteInfo[2]['zones'];
320     this.defaultRealmId = multisiteInfo[0]['default_realm'];
321     this.defaultZonegroupId = multisiteInfo[1]['default_zonegroup'];
322     this.defaultZoneId = multisiteInfo[2]['default_zone'];
323     this.defaultsInfo = this.getDefaultsEntities(
324       this.defaultRealmId,
325       this.defaultZonegroupId,
326       this.defaultZoneId
327     );
328     if (this.realms.length > 0) {
329       // get tree for realm -> zonegroup -> zone
330       for (const realm of this.realms) {
331         const result = this.rgwRealmService.getRealmTree(realm, this.defaultRealmId);
332         rootNodes = result['nodes'];
333         this.realmIds = this.realmIds.concat(result['realmIds']);
334         for (const zonegroup of this.zonegroups) {
335           if (zonegroup.realm_id === realm.id) {
336             firstChildNodes = this.rgwZonegroupService.getZonegroupTree(
337               zonegroup,
338               this.defaultZonegroupId,
339               realm
340             );
341             for (const zone of zonegroup.zones) {
342               const zoneResult = this.rgwZoneService.getZoneTree(
343                 zone,
344                 this.defaultZoneId,
345                 this.zones,
346                 zonegroup,
347                 realm
348               );
349               secondChildNodes = zoneResult['nodes'];
350               this.zoneIds = this.zoneIds.concat(zoneResult['zoneIds']);
351               allSecondChildNodes.push(secondChildNodes);
352               secondChildNodes = {};
353             }
354             firstChildNodes['children'] = allSecondChildNodes;
355             allSecondChildNodes = [];
356             allFirstChildNodes.push(firstChildNodes);
357             firstChildNodes = {};
358           }
359         }
360         rootNodes['children'] = allFirstChildNodes;
361         allNodes.push(rootNodes);
362         firstChildNodes = {};
363         secondChildNodes = {};
364         rootNodes = {};
365         allFirstChildNodes = [];
366         allSecondChildNodes = [];
367       }
368     }
369     if (this.zonegroups.length > 0) {
370       // get tree for zonegroup -> zone (standalone zonegroups that don't match a realm eg(initial default))
371       for (const zonegroup of this.zonegroups) {
372         if (!this.realmIds.includes(zonegroup.realm_id)) {
373           rootNodes = this.rgwZonegroupService.getZonegroupTree(zonegroup, this.defaultZonegroupId);
374           for (const zone of zonegroup.zones) {
375             const zoneResult = this.rgwZoneService.getZoneTree(
376               zone,
377               this.defaultZoneId,
378               this.zones,
379               zonegroup
380             );
381             firstChildNodes = zoneResult['nodes'];
382             this.zoneIds = this.zoneIds.concat(zoneResult['zoneIds']);
383             allFirstChildNodes.push(firstChildNodes);
384             firstChildNodes = {};
385           }
386           rootNodes['children'] = allFirstChildNodes;
387           allNodes.push(rootNodes);
388           firstChildNodes = {};
389           rootNodes = {};
390           allFirstChildNodes = [];
391         }
392       }
393     }
394     if (this.zones.length > 0) {
395       // get tree for standalone zones(zones that do not belong to a zonegroup)
396       for (const zone of this.zones) {
397         if (this.zoneIds.length > 0 && !this.zoneIds.includes(zone.id)) {
398           const zoneResult = this.rgwZoneService.getZoneTree(zone, this.defaultZoneId, this.zones);
399           rootNodes = zoneResult['nodes'];
400           allNodes.push(rootNodes);
401           rootNodes = {};
402         }
403       }
404     }
405     if (this.realms.length < 1 && this.zonegroups.length < 1 && this.zones.length < 1) {
406       return [
407         {
408           name: 'No nodes!'
409         }
410       ];
411     }
412     this.realmIds = [];
413     this.zoneIds = [];
414     this.evaluateMigrateAndReplicationActions();
415     this.rgwDaemonService.list().subscribe((data: any) => {
416       const realmName = data.map((item: { [x: string]: any }) => item['realm_name']);
417       if (
418         this.defaultRealmId != '' &&
419         this.defaultZonegroupId != '' &&
420         this.defaultZoneId != '' &&
421         realmName.includes('')
422       ) {
423         this.restartGatewayMessage = true;
424       }
425     });
426     return allNodes;
427   }
428
429   getDefaultsEntities(
430     defaultRealmId: string,
431     defaultZonegroupId: string,
432     defaultZoneId: string
433   ): any {
434     const defaultRealm = this.realms.find((x: { id: string }) => x.id === defaultRealmId);
435     const defaultZonegroup = this.zonegroups.find(
436       (x: { id: string }) => x.id === defaultZonegroupId
437     );
438     const defaultZone = this.zones.find((x: { id: string }) => x.id === defaultZoneId);
439     const defaultRealmName = defaultRealm !== undefined ? defaultRealm.name : null;
440     const defaultZonegroupName = defaultZonegroup !== undefined ? defaultZonegroup.name : null;
441     const defaultZoneName = defaultZone !== undefined ? defaultZone.name : null;
442     return {
443       defaultRealmName: defaultRealmName,
444       defaultZonegroupName: defaultZonegroupName,
445       defaultZoneName: defaultZoneName
446     };
447   }
448
449   onNodeSelected(tree: TreeModel, node: TreeNode) {
450     TREE_ACTIONS.ACTIVATE(tree, node, true);
451     this.metadataTitle = node.data.name;
452     this.metadata = node.data.info;
453     node.data.show = true;
454   }
455
456   onUpdateData() {
457     this.tree.treeModel.expandAll();
458   }
459
460   getDisable() {
461     let isMasterZone = true;
462     if (this.defaultRealmId === '') {
463       return this.messages.noDefaultRealm;
464     } else {
465       this.zonegroups.forEach((zgp: any) => {
466         if (_.isEmpty(zgp.master_zone)) {
467           isMasterZone = false;
468         }
469       });
470       if (!isMasterZone) {
471         this.editTitle =
472           'Please create a master zone for each existing zonegroup to enable this feature';
473         return this.messages.noMasterZone;
474       } else {
475         this.editTitle = 'Edit';
476         return false;
477       }
478     }
479   }
480
481   evaluateMigrateAndReplicationActions() {
482     if (
483       this.realms.length === 0 &&
484       this.zonegroups.length === 1 &&
485       this.zonegroups[0].name === 'default' &&
486       this.zones.length === 1 &&
487       this.zones[0].name === 'default'
488     ) {
489       this.showMigrateAndReplicationActions = true;
490     } else {
491       this.showMigrateAndReplicationActions = false;
492     }
493     return this.showMigrateAndReplicationActions;
494   }
495
496   isDeleteDisabled(node: TreeNode): boolean {
497     let disable: boolean = false;
498     let masterZonegroupCount: number = 0;
499     if (node.data.type === 'realm' && node.data.is_default && this.realms.length < 2) {
500       disable = true;
501     }
502
503     if (node.data.type === 'zonegroup') {
504       if (this.zonegroups.length < 2) {
505         this.deleteTitle = 'You can not delete the only zonegroup available';
506         disable = true;
507       } else if (node.data.is_default) {
508         this.deleteTitle = 'You can not delete the default zonegroup';
509         disable = true;
510       } else if (node.data.is_master) {
511         for (let zonegroup of this.zonegroups) {
512           if (zonegroup.is_master === true) {
513             masterZonegroupCount++;
514             if (masterZonegroupCount > 1) break;
515           }
516         }
517         if (masterZonegroupCount < 2) {
518           this.deleteTitle = 'You can not delete the only master zonegroup available';
519           disable = true;
520         }
521       }
522     }
523
524     if (node.data.type === 'zone') {
525       if (this.zones.length < 2) {
526         this.deleteTitle = 'You can not delete the only zone available';
527         disable = true;
528       } else if (node.data.is_default) {
529         this.deleteTitle = 'You can not delete the default zone';
530         disable = true;
531       } else if (node.data.is_master && node.data.zone_zonegroup.zones.length < 2) {
532         this.deleteTitle =
533           'You can not delete the master zone as there are no more zones in this zonegroup';
534         disable = true;
535       }
536     }
537
538     if (!disable) {
539       this.deleteTitle = 'Delete';
540     }
541
542     return disable;
543   }
544
545   delete(node: TreeNode) {
546     if (node.data.type === 'realm') {
547       this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
548         itemDescription: $localize`${node.data.type} ${node.data.name}`,
549         itemNames: [`${node.data.name}`],
550         submitAction: () => {
551           this.rgwRealmService.delete(node.data.name).subscribe(
552             () => {
553               this.modalRef.close();
554               this.notificationService.show(
555                 NotificationType.success,
556                 $localize`Realm: '${node.data.name}' deleted successfully`
557               );
558             },
559             () => {
560               this.modalRef.componentInstance.stopLoadingSpinner();
561             }
562           );
563         }
564       });
565     } else if (node.data.type === 'zonegroup') {
566       this.modalRef = this.modalService.show(RgwMultisiteZonegroupDeletionFormComponent, {
567         zonegroup: node.data
568       });
569     } else if (node.data.type === 'zone') {
570       this.modalRef = this.modalService.show(RgwMultisiteZoneDeletionFormComponent, {
571         zone: node.data
572       });
573     }
574   }
575
576   enableRgwModule() {
577     let $obs;
578     const fnWaitUntilReconnected = () => {
579       observableTimer(2000).subscribe(() => {
580         // Trigger an API request to check if the connection is
581         // re-established.
582         this.mgrModuleService.list().subscribe(
583           () => {
584             // Resume showing the notification toasties.
585             this.notificationService.suspendToasties(false);
586             // Unblock the whole UI.
587             this.blockUI.stop();
588             // Reload the data table content.
589             this.notificationService.show(NotificationType.success, $localize`Enabled RGW Module`);
590             this.router.navigateByUrl('/', { skipLocationChange: true }).then(() => {
591               this.router.navigate(['/rgw/multisite']);
592             });
593             // Reload the data table content.
594           },
595           () => {
596             fnWaitUntilReconnected();
597           }
598         );
599       });
600     };
601
602     if (!this.rgwModuleStatus) {
603       $obs = this.mgrModuleService.enable('rgw');
604     }
605     $obs.subscribe(
606       () => undefined,
607       () => {
608         // Suspend showing the notification toasties.
609         this.notificationService.suspendToasties(true);
610         // Block the whole UI to prevent user interactions until
611         // the connection to the backend is reestablished
612         this.blockUI.start($localize`Reconnecting, please wait ...`);
613         fnWaitUntilReconnected();
614       }
615     );
616   }
617 }