1 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
8 } from '@circlon/angular-tree-component';
9 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
10 import _ from 'lodash';
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';
42 selector: 'cd-rgw-multisite-details',
43 templateUrl: './rgw-multisite-details.component.html',
44 styleUrls: ['./rgw-multisite-details.component.scss']
46 export class RgwMultisiteDetailsComponent implements OnDestroy, OnInit {
47 private sub = new Subscription();
49 @ViewChild('tree') tree: TreeComponent;
52 noDefaultRealm: $localize`Please create a default realm first to enable this feature`,
53 noMasterZone: $localize`Please create a master zone for each zonegroups to enable this feature`,
54 noRealmExists: $localize`No realm exists`,
55 disableExport: $localize`Please create master zonegroup and master zone for each of the realms`
62 permission: Permission;
63 selection = new CdTableSelection();
64 createTableActions: CdTableAction[];
65 migrateTableAction: CdTableAction[];
66 importAction: CdTableAction[];
67 exportAction: CdTableAction[];
68 loadingIndicator = true;
70 treeOptions: ITreeOptions = {
71 useVirtualScroll: true,
76 click: this.onNodeSelected.bind(this)
80 modalRef: NgbModalRef;
82 realms: RgwRealm[] = [];
83 zonegroups: RgwZonegroup[] = [];
84 zones: RgwZone[] = [];
86 metadataTitle: string;
87 bsModalRef: NgbModalRef;
88 realmIds: string[] = [];
89 zoneIds: string[] = [];
91 defaultZonegroupId = '';
93 multisiteInfo: object[] = [];
94 defaultsInfo: string[] = [];
95 showMigrateAction: boolean = false;
96 editTitle: string = 'Edit';
97 deleteTitle: string = 'Delete';
99 rgwModuleStatus: boolean;
100 rgwModuleData: string | any[] = [];
103 private modalService: ModalService,
104 private timerService: TimerService,
105 private authStorageService: AuthStorageService,
106 public actionLabels: ActionLabelsI18n,
107 public timerServiceVariable: TimerServiceInterval,
108 public router: Router,
109 public rgwRealmService: RgwRealmService,
110 public rgwZonegroupService: RgwZonegroupService,
111 public rgwZoneService: RgwZoneService,
112 public rgwDaemonService: RgwDaemonService,
113 public mgrModuleService: MgrModuleService,
114 private notificationService: NotificationService
116 this.permission = this.authStorageService.getPermissions().rgw;
119 openModal(entity: any, edit = false) {
120 const entityName = edit ? entity.data.type : entity;
121 const action = edit ? 'edit' : 'create';
122 const initialState = {
123 resource: entityName,
126 defaultsInfo: this.defaultsInfo,
127 multisiteInfo: this.multisiteInfo
129 if (entityName === 'realm') {
130 this.bsModalRef = this.modalService.show(RgwMultisiteRealmFormComponent, initialState, {
133 } else if (entityName === 'zonegroup') {
134 this.bsModalRef = this.modalService.show(RgwMultisiteZonegroupFormComponent, initialState, {
138 this.bsModalRef = this.modalService.show(RgwMultisiteZoneFormComponent, initialState, {
145 const initialState = {
146 multisiteInfo: this.multisiteInfo
148 this.bsModalRef = this.modalService.show(RgwMultisiteMigrateComponent, initialState, {
154 const initialState = {
155 multisiteInfo: this.multisiteInfo
157 this.bsModalRef = this.modalService.show(RgwMultisiteImportComponent, initialState, {
163 const initialState = {
164 defaultsInfo: this.defaultsInfo,
165 multisiteInfo: this.multisiteInfo
167 this.bsModalRef = this.modalService.show(RgwMultisiteExportComponent, initialState, {
173 this.realms.forEach((realm: any) => {
174 this.zonegroups.forEach((zonegroup) => {
175 if (realm.id === zonegroup.realm_id) {
176 if (zonegroup.is_master && zonegroup.master_zone !== '') {
177 this.disableExport = false;
182 if (!this.rgwModuleStatus) {
185 if (this.realms.length < 1) {
186 return this.messages.noRealmExists;
187 } else if (this.disableExport) {
188 return this.messages.disableExport;
195 if (!this.rgwModuleStatus) {
203 const createRealmAction: CdTableAction = {
204 permission: 'create',
206 name: this.actionLabels.CREATE + ' Realm',
207 click: () => this.openModal('realm')
209 const createZonegroupAction: CdTableAction = {
210 permission: 'create',
212 name: this.actionLabels.CREATE + ' Zonegroup',
213 click: () => this.openModal('zonegroup'),
214 disable: () => this.getDisable()
216 const createZoneAction: CdTableAction = {
217 permission: 'create',
219 name: this.actionLabels.CREATE + ' Zone',
220 click: () => this.openModal('zone')
222 const migrateMultsiteAction: CdTableAction = {
224 icon: Icons.exchange,
225 name: this.actionLabels.MIGRATE,
226 click: () => this.openMigrateModal()
228 const importMultsiteAction: CdTableAction = {
230 icon: Icons.download,
231 name: this.actionLabels.IMPORT,
232 click: () => this.openImportModal(),
233 disable: () => this.getDisableImport()
235 const exportMultsiteAction: CdTableAction = {
238 name: this.actionLabels.EXPORT,
239 click: () => this.openExportModal(),
240 disable: () => this.getDisableExport()
242 this.createTableActions = [createRealmAction, createZonegroupAction, createZoneAction];
243 this.migrateTableAction = [migrateMultsiteAction];
244 this.importAction = [importMultsiteAction];
245 this.exportAction = [exportMultsiteAction];
247 const observables = [
248 this.rgwRealmService.getAllRealmsInfo(),
249 this.rgwZonegroupService.getAllZonegroupsInfo(),
250 this.rgwZoneService.getAllZonesInfo()
252 this.sub = this.timerService
253 .get(() => forkJoin(observables), this.timerServiceVariable.TIMER_SERVICE_PERIOD * 2)
255 (multisiteInfo: [object, object, object]) => {
256 this.multisiteInfo = multisiteInfo;
257 this.loadingIndicator = false;
258 this.nodes = this.abstractTreeData(multisiteInfo);
262 this.mgrModuleService.list().subscribe((moduleData: any) => {
263 this.rgwModuleData = moduleData.filter((module: object) => module['name'] === 'rgw');
264 if (this.rgwModuleData.length > 0) {
265 this.rgwModuleStatus = this.rgwModuleData[0].enabled;
270 /* setConfigValues() {
271 this.rgwDaemonService
273 this.defaultsInfo['defaultRealmName'],
274 this.defaultsInfo['defaultZonegroupName'],
275 this.defaultsInfo['defaultZoneName']
277 .subscribe(() => {});
281 this.sub.unsubscribe();
284 private abstractTreeData(multisiteInfo: [object, object, object]): any[] {
285 let allNodes: object[] = [];
287 let firstChildNodes = {};
288 let allFirstChildNodes = [];
289 let secondChildNodes = {};
290 let allSecondChildNodes: {}[] = [];
291 this.realms = multisiteInfo[0]['realms'];
292 this.zonegroups = multisiteInfo[1]['zonegroups'];
293 this.zones = multisiteInfo[2]['zones'];
294 this.defaultRealmId = multisiteInfo[0]['default_realm'];
295 this.defaultZonegroupId = multisiteInfo[1]['default_zonegroup'];
296 this.defaultZoneId = multisiteInfo[2]['default_zone'];
297 this.defaultsInfo = this.getDefaultsEntities(
299 this.defaultZonegroupId,
302 if (this.realms.length > 0) {
303 // get tree for realm -> zonegroup -> zone
304 for (const realm of this.realms) {
305 const result = this.rgwRealmService.getRealmTree(realm, this.defaultRealmId);
306 rootNodes = result['nodes'];
307 this.realmIds = this.realmIds.concat(result['realmIds']);
308 for (const zonegroup of this.zonegroups) {
309 if (zonegroup.realm_id === realm.id) {
310 firstChildNodes = this.rgwZonegroupService.getZonegroupTree(
312 this.defaultZonegroupId,
315 for (const zone of zonegroup.zones) {
316 const zoneResult = this.rgwZoneService.getZoneTree(
323 secondChildNodes = zoneResult['nodes'];
324 this.zoneIds = this.zoneIds.concat(zoneResult['zoneIds']);
325 allSecondChildNodes.push(secondChildNodes);
326 secondChildNodes = {};
328 firstChildNodes['children'] = allSecondChildNodes;
329 allSecondChildNodes = [];
330 allFirstChildNodes.push(firstChildNodes);
331 firstChildNodes = {};
334 rootNodes['children'] = allFirstChildNodes;
335 allNodes.push(rootNodes);
336 firstChildNodes = {};
337 secondChildNodes = {};
339 allFirstChildNodes = [];
340 allSecondChildNodes = [];
343 if (this.zonegroups.length > 0) {
344 // get tree for zonegroup -> zone (standalone zonegroups that don't match a realm eg(initial default))
345 for (const zonegroup of this.zonegroups) {
346 if (!this.realmIds.includes(zonegroup.realm_id)) {
347 rootNodes = this.rgwZonegroupService.getZonegroupTree(zonegroup, this.defaultZonegroupId);
348 for (const zone of zonegroup.zones) {
349 const zoneResult = this.rgwZoneService.getZoneTree(
355 firstChildNodes = zoneResult['nodes'];
356 this.zoneIds = this.zoneIds.concat(zoneResult['zoneIds']);
357 allFirstChildNodes.push(firstChildNodes);
358 firstChildNodes = {};
360 rootNodes['children'] = allFirstChildNodes;
361 allNodes.push(rootNodes);
362 firstChildNodes = {};
364 allFirstChildNodes = [];
368 if (this.zones.length > 0) {
369 // get tree for standalone zones(zones that do not belong to a zonegroup)
370 for (const zone of this.zones) {
371 if (this.zoneIds.length > 0 && !this.zoneIds.includes(zone.id)) {
372 const zoneResult = this.rgwZoneService.getZoneTree(zone, this.defaultZoneId, this.zones);
373 rootNodes = zoneResult['nodes'];
374 allNodes.push(rootNodes);
379 if (this.realms.length < 1 && this.zonegroups.length < 1 && this.zones.length < 1) {
388 this.getDisableMigrate();
393 defaultRealmId: string,
394 defaultZonegroupId: string,
395 defaultZoneId: string
397 const defaultRealm = this.realms.find((x: { id: string }) => x.id === defaultRealmId);
398 const defaultZonegroup = this.zonegroups.find(
399 (x: { id: string }) => x.id === defaultZonegroupId
401 const defaultZone = this.zones.find((x: { id: string }) => x.id === defaultZoneId);
402 const defaultRealmName = defaultRealm !== undefined ? defaultRealm.name : null;
403 const defaultZonegroupName = defaultZonegroup !== undefined ? defaultZonegroup.name : null;
404 const defaultZoneName = defaultZone !== undefined ? defaultZone.name : null;
406 defaultRealmName: defaultRealmName,
407 defaultZonegroupName: defaultZonegroupName,
408 defaultZoneName: defaultZoneName
412 onNodeSelected(tree: TreeModel, node: TreeNode) {
413 TREE_ACTIONS.ACTIVATE(tree, node, true);
414 this.metadataTitle = node.data.name;
415 this.metadata = node.data.info;
416 node.data.show = true;
420 this.tree.treeModel.expandAll();
424 let isMasterZone = true;
425 if (this.defaultRealmId === '') {
426 return this.messages.noDefaultRealm;
428 this.zonegroups.forEach((zgp: any) => {
429 if (_.isEmpty(zgp.master_zone)) {
430 isMasterZone = false;
435 'Please create a master zone for each existing zonegroup to enable this feature';
436 return this.messages.noMasterZone;
438 this.editTitle = 'Edit';
444 getDisableMigrate() {
446 this.realms.length === 0 &&
447 this.zonegroups.length === 1 &&
448 this.zonegroups[0].name === 'default' &&
449 this.zones.length === 1 &&
450 this.zones[0].name === 'default'
452 this.showMigrateAction = true;
454 this.showMigrateAction = false;
456 return this.showMigrateAction;
459 isDeleteDisabled(node: TreeNode): boolean {
460 let disable: boolean = false;
461 let masterZonegroupCount: number = 0;
462 if (node.data.type === 'realm' && node.data.is_default && this.realms.length < 2) {
466 if (node.data.type === 'zonegroup') {
467 if (this.zonegroups.length < 2) {
468 this.deleteTitle = 'You can not delete the only zonegroup available';
470 } else if (node.data.is_default) {
471 this.deleteTitle = 'You can not delete the default zonegroup';
473 } else if (node.data.is_master) {
474 for (let zonegroup of this.zonegroups) {
475 if (zonegroup.is_master === true) {
476 masterZonegroupCount++;
477 if (masterZonegroupCount > 1) break;
480 if (masterZonegroupCount < 2) {
481 this.deleteTitle = 'You can not delete the only master zonegroup available';
487 if (node.data.type === 'zone') {
488 if (this.zones.length < 2) {
489 this.deleteTitle = 'You can not delete the only zone available';
491 } else if (node.data.is_default) {
492 this.deleteTitle = 'You can not delete the default zone';
494 } else if (node.data.is_master && node.data.zone_zonegroup.zones.length < 2) {
496 'You can not delete the master zone as there are no more zones in this zonegroup';
502 this.deleteTitle = 'Delete';
508 delete(node: TreeNode) {
509 if (node.data.type === 'realm') {
510 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
511 itemDescription: $localize`${node.data.type} ${node.data.name}`,
512 itemNames: [`${node.data.name}`],
513 submitAction: () => {
514 this.rgwRealmService.delete(node.data.name).subscribe(
516 this.modalRef.close();
517 this.notificationService.show(
518 NotificationType.success,
519 $localize`Realm: '${node.data.name}' deleted successfully`
523 this.modalRef.componentInstance.stopLoadingSpinner();
528 } else if (node.data.type === 'zonegroup') {
529 this.modalRef = this.modalService.show(RgwMultisiteZonegroupDeletionFormComponent, {
532 } else if (node.data.type === 'zone') {
533 this.modalRef = this.modalService.show(RgwMultisiteZoneDeletionFormComponent, {
541 const fnWaitUntilReconnected = () => {
542 observableTimer(2000).subscribe(() => {
543 // Trigger an API request to check if the connection is
545 this.mgrModuleService.list().subscribe(
547 // Resume showing the notification toasties.
548 this.notificationService.suspendToasties(false);
549 // Unblock the whole UI.
551 // Reload the data table content.
552 this.notificationService.show(NotificationType.success, $localize`Enabled RGW Module`);
553 this.router.navigateByUrl('/', { skipLocationChange: true }).then(() => {
554 this.router.navigate(['/rgw/multisite']);
556 // Reload the data table content.
559 fnWaitUntilReconnected();
565 if (!this.rgwModuleStatus) {
566 $obs = this.mgrModuleService.enable('rgw');
571 // Suspend showing the notification toasties.
572 this.notificationService.suspendToasties(true);
573 // Block the whole UI to prevent user interactions until
574 // the connection to the backend is reestablished
575 this.blockUI.start($localize`Reconnecting, please wait ...`);
576 fnWaitUntilReconnected();