1 import { Component, OnDestroy, OnInit, TemplateRef, ViewChild } from '@angular/core';
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import { Observable, Subscriber, Subscription } from 'rxjs';
6 import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
7 import { TableStatusViewCache } from '~/app/shared/classes/table-status-view-cache';
8 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
9 import { Icons } from '~/app/shared/enum/icons.enum';
10 import { CdTableAction } from '~/app/shared/models/cd-table-action';
11 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
12 import { FinishedTask } from '~/app/shared/models/finished-task';
13 import { Permission } from '~/app/shared/models/permissions';
14 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
15 import { ModalService } from '~/app/shared/services/modal.service';
16 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
17 import { PoolEditModeModalComponent } from '../pool-edit-mode-modal/pool-edit-mode-modal.component';
18 import { PoolEditPeerModalComponent } from '../pool-edit-peer-modal/pool-edit-peer-modal.component';
21 selector: 'cd-mirroring-pools',
22 templateUrl: './pool-list.component.html',
23 styleUrls: ['./pool-list.component.scss']
25 export class PoolListComponent implements OnInit, OnDestroy {
26 @ViewChild('healthTmpl', { static: true })
27 healthTmpl: TemplateRef<any>;
31 permission: Permission;
32 tableActions: CdTableAction[];
33 selection = new CdTableSelection();
35 modalRef: NgbModalRef;
40 tableStatus = new TableStatusViewCache();
43 private authStorageService: AuthStorageService,
44 private rbdMirroringService: RbdMirroringService,
45 private modalService: ModalService,
46 private taskWrapper: TaskWrapperService
49 this.permission = this.authStorageService.getPermissions().rbdMirroring;
51 const editModeAction: CdTableAction = {
54 click: () => this.editModeModal(),
55 name: $localize`Edit Mode`,
56 canBePrimary: () => true
58 const addPeerAction: CdTableAction = {
61 name: $localize`Add Peer`,
62 click: () => this.editPeersModal('add'),
63 disable: () => !this.selection.first() || this.selection.first().mirror_mode === 'disabled',
64 visible: () => !this.getPeerUUID(),
65 canBePrimary: () => false
67 const editPeerAction: CdTableAction = {
70 name: $localize`Edit Peer`,
71 click: () => this.editPeersModal('edit'),
72 visible: () => !!this.getPeerUUID()
74 const deletePeerAction: CdTableAction = {
77 name: $localize`Delete Peer`,
78 click: () => this.deletePeersModal(),
79 visible: () => !!this.getPeerUUID()
81 this.tableActions = [editModeAction, addPeerAction, editPeerAction, deletePeerAction];
86 { prop: 'name', name: $localize`Name`, flexGrow: 2 },
87 { prop: 'mirror_mode', name: $localize`Mode`, flexGrow: 2 },
88 { prop: 'leader_id', name: $localize`Leader`, flexGrow: 2 },
89 { prop: 'image_local_count', name: $localize`# Local`, flexGrow: 2 },
90 { prop: 'image_remote_count', name: $localize`# Remote`, flexGrow: 2 },
93 name: $localize`Health`,
94 cellTemplate: this.healthTmpl,
99 this.subs = this.rbdMirroringService.subscribeSummary((data) => {
100 this.data = data.content_data.pools;
101 this.tableStatus = new TableStatusViewCache(data.status);
105 ngOnDestroy(): void {
106 this.subs.unsubscribe();
110 this.rbdMirroringService.refresh();
114 const initialState = {
115 poolName: this.selection.first().name
117 this.modalRef = this.modalService.show(PoolEditModeModalComponent, initialState);
120 editPeersModal(mode: string) {
121 const initialState = {
122 poolName: this.selection.first().name,
125 if (mode === 'edit') {
126 initialState['peerUUID'] = this.getPeerUUID();
128 this.modalRef = this.modalService.show(PoolEditPeerModalComponent, initialState);
132 const poolName = this.selection.first().name;
133 const peerUUID = this.getPeerUUID();
135 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
136 itemDescription: $localize`mirror peer`,
137 itemNames: [`${poolName} (${peerUUID})`],
138 submitActionObservable: () =>
139 new Observable((observer: Subscriber<any>) => {
141 .wrapTaskAroundCall({
142 task: new FinishedTask('rbd/mirroring/peer/delete', {
145 call: this.rbdMirroringService.deletePeer(poolName, peerUUID)
148 error: (resp) => observer.error(resp),
150 this.rbdMirroringService.refresh();
159 const selection = this.selection.first();
160 const pool = this.data.find((o) => selection && selection.name === o['name']);
161 if (pool && pool['peer_uuids']) {
162 return pool['peer_uuids'][0];
168 updateSelection(selection: CdTableSelection) {
169 this.selection = selection;