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