2 ChangeDetectionStrategy,
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';
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';
48 const BASE_URL = 'rgw/multisite/configuration';
51 selector: 'cd-rgw-multisite-details',
52 templateUrl: './rgw-multisite-details.component.html',
53 styleUrls: ['./rgw-multisite-details.component.scss'],
54 changeDetection: ChangeDetectionStrategy.OnPush
56 export class RgwMultisiteDetailsComponent implements OnDestroy, OnInit {
57 private sub = new Subscription();
58 @ViewChild('treeNodeTemplate') labelTpl: TemplateRef<any>;
59 @ViewChild(RgwMultisiteSyncPolicyComponent) syncPolicyComp: RgwMultisiteSyncPolicyComponent;
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`
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;
78 toNode(values: any): Node[] {
79 return values.map((value: any) => ({
88 children: value?.children ? this.toNode(value.children) : []
92 set nodes(values: any) {
93 this._nodes = this.toNode(values);
94 this.changeDetectionRef.detectChanges();
101 private _nodes: Node[] = [];
103 modalRef: NgbModalRef;
105 realms: RgwRealm[] = [];
106 zonegroups: RgwZonegroup[] = [];
107 zones: RgwZone[] = [];
109 metadataTitle: string;
110 bsModalRef: NgbModalRef;
111 realmIds: string[] = [];
112 zoneIds: string[] = [];
114 defaultZonegroupId = '';
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[] = [];
126 activeNodeId?: string;
128 NAVIGATE_TO = '/rgw/multisite';
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
147 this.permissions = this.authStorageService.getPermissions();
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,
157 defaultsInfo: this.defaultsInfo,
158 multisiteInfo: this.multisiteInfo
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, {
167 this.bsModalRef = this.modalService.show(RgwMultisiteZoneFormComponent, initialState, {
173 openMultisiteSetupWizard() {
174 this.bsModalRef = this.cdsModalService.show(RgwMultisiteWizardComponent);
178 const initialState = {
179 multisiteInfo: this.multisiteInfo
181 this.bsModalRef = this.modalService.show(RgwMultisiteMigrateComponent, initialState, {
187 const initialState = {
188 multisiteInfo: this.multisiteInfo
190 this.bsModalRef = this.modalService.show(RgwMultisiteImportComponent, initialState, {
196 const initialState = {
197 defaultsInfo: this.defaultsInfo,
198 multisiteInfo: this.multisiteInfo
200 this.bsModalRef = this.modalService.show(RgwMultisiteExportComponent, initialState, {
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;
215 if (!this.rgwModuleStatus) {
218 if (this.realms.length < 1) {
219 return this.messages.noRealmExists;
220 } else if (this.disableExport) {
221 return this.messages.disableExport;
228 if (!this.rgwModuleStatus) {
236 this.createTableActions = [
238 permission: 'create',
240 name: this.actionLabels.CREATE + ' Realm',
241 click: () => this.openModal('realm')
244 permission: 'create',
246 name: this.actionLabels.CREATE + ' Zone Group',
247 click: () => this.openModal('zonegroup'),
248 disable: () => this.getDisable()
251 permission: 'create',
253 name: this.actionLabels.CREATE + ' Zone',
254 click: () => this.openModal('zone')
257 this.migrateTableAction = [
259 permission: 'create',
261 name: this.actionLabels.MIGRATE,
262 click: () => this.openMigrateModal()
265 this.importAction = [
267 permission: 'create',
268 icon: Icons.download,
269 name: this.actionLabels.IMPORT,
270 click: () => this.openImportModal(),
271 disable: () => this.getDisableImport()
274 this.exportAction = [
276 permission: 'create',
278 name: this.actionLabels.EXPORT,
279 click: () => this.openExportModal(),
280 disable: () => this.getDisableExport()
283 this.multisiteReplicationActions = [
285 permission: 'create',
287 name: this.actionLabels.SETUP_MULTISITE_REPLICATION,
289 this.router.navigate([BASE_URL, { outlets: { modal: 'setup-multisite-replication' } }])
293 const observables = [
294 this.rgwRealmService.getAllRealmsInfo(),
295 this.rgwZonegroupService.getAllZonegroupsInfo(),
296 this.rgwZoneService.getAllZonesInfo()
298 this.sub = this.timerService
299 .get(() => forkJoin(observables), this.timerServiceVariable.TIMER_SERVICE_PERIOD * 2)
301 (multisiteInfo: [object, object, object]) => {
302 this.multisiteInfo = multisiteInfo;
303 this.loadingIndicator = false;
304 this.nodes = this.abstractTreeData(multisiteInfo);
309 // Only get the module status if you can read from configOpt
310 if (this.permissions.configOpt.read) this.getRgwModuleStatus();
314 this.sub.unsubscribe();
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;
326 private abstractTreeData(multisiteInfo: [object, object, object]): any[] {
327 let allNodes: object[] = [];
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(
341 this.defaultZonegroupId,
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(
354 this.defaultZonegroupId,
357 for (const zone of zonegroup.zones) {
358 const zoneResult = this.rgwZoneService.getZoneTree(
365 secondChildNodes = zoneResult['nodes'];
366 this.zoneIds = this.zoneIds.concat(zoneResult['zoneIds']);
367 allSecondChildNodes.push(secondChildNodes);
368 secondChildNodes = {};
370 allSecondChildNodes = allSecondChildNodes.map((x) => ({
372 parentNode: firstChildNodes
374 firstChildNodes['children'] = allSecondChildNodes;
375 allSecondChildNodes = [];
376 allFirstChildNodes.push(firstChildNodes);
377 firstChildNodes = {};
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 = {};
386 allFirstChildNodes = [];
387 allSecondChildNodes = [];
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(
402 firstChildNodes = zoneResult['nodes'];
403 this.zoneIds = this.zoneIds.concat(zoneResult['zoneIds']);
404 allFirstChildNodes.push(firstChildNodes);
405 firstChildNodes = {};
407 allFirstChildNodes = allFirstChildNodes.map((x) => ({ ...x, parentNode: rootNodes }));
408 rootNodes['children'] = allFirstChildNodes;
409 allNodes.push({ ...rootNodes, label: rootNodes?.['name'] || rootNodes?.['id'] });
410 firstChildNodes = {};
412 allFirstChildNodes = [];
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'] });
427 if (this.realms.length < 1 && this.zonegroups.length < 1 && this.zones.length < 1) {
437 this.evaluateMigrateAndReplicationActions();
438 this.rgwMultisiteService.restartGatewayMessage$.subscribe((value) => {
439 if (value !== null) {
440 this.restartGatewayMessage = value;
442 this.checkRestartGatewayMessage();
448 checkRestartGatewayMessage() {
449 this.rgwDaemonService.list().subscribe((data: any) => {
450 const realmName = data.map((item: { [x: string]: any }) => item['realm_name']);
452 this.defaultRealmId !== '' &&
453 this.defaultZonegroupId !== '' &&
454 this.defaultZoneId !== '' &&
455 realmName.includes('')
457 this.restartGatewayMessage = true;
459 this.restartGatewayMessage = false;
465 defaultRealmId: string,
466 defaultZonegroupId: string,
467 defaultZoneId: string
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
473 const defaultZone = this.zones?.find((x: { id: string }) => x.id === defaultZoneId);
476 defaultRealmName: defaultRealm?.name,
477 defaultZonegroupName: defaultZonegroup?.name,
478 defaultZoneName: defaultZone?.name
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;
490 let isMasterZone = true;
491 if (this.defaultRealmId === '') {
492 return this.messages.noDefaultRealm;
494 this.zonegroups.forEach((zgp: any) => {
495 if (_.isEmpty(zgp.master_zone)) {
496 isMasterZone = false;
502 'Please create a master zone for each existing zonegroup to enable this feature';
504 return this.messages.noMasterZone;
507 this.editTitle = 'Edit';
514 evaluateMigrateAndReplicationActions() {
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'
522 this.showMigrateAndReplicationActions = true;
524 this.showMigrateAndReplicationActions = false;
526 return this.showMigrateAndReplicationActions;
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) {
537 if (node?.data?.type === 'zonegroup') {
538 if (this.zonegroups.length < 2) {
539 deleteTitle = 'You can not delete the only zonegroup available';
541 } else if (node?.data?.is_default) {
542 deleteTitle = 'You can not delete the default zonegroup';
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;
551 if (masterZonegroupCount < 2) {
552 deleteTitle = 'You can not delete the only master zonegroup available';
558 if (node?.data?.type === 'zone') {
559 if (this.zones.length < 2) {
560 deleteTitle = 'You can not delete the only zone available';
562 } else if (node?.data?.is_default) {
563 deleteTitle = 'You can not delete the default zone';
565 } else if (node?.data?.is_master && node?.data?.zone_zonegroup.zones.length < 2) {
567 'You can not delete the master zone as there are no more zones in this zonegroup';
573 this.deleteTitle = 'Delete';
576 return { isDisabled, deleteTitle };
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(
587 this.notificationService.show(
588 NotificationType.success,
589 $localize`Realm: '${node?.data?.name}' deleted successfully`
591 this.cdsModalService.dismissAll();
594 this.cdsModalService.stopLoadingSpinner(modalRef.deletionForm);
599 } else if (node?.data?.type === 'zonegroup') {
600 this.modalRef = this.modalService.show(RgwMultisiteZonegroupDeletionFormComponent, {
603 } else if (node?.data?.type === 'zone') {
604 this.modalRef = this.modalService.show(RgwMultisiteZoneDeletionFormComponent, {
611 this.mgrModuleService.updateModuleState(
616 'Enabled RGW Module',