1 import { Component, OnDestroy, OnInit, TemplateRef, ViewChild } from '@angular/core';
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
5 import { Observable, Subscriber, Subscription } from 'rxjs';
7 import { RbdMirroringService } from '../../../../shared/api/rbd-mirroring.service';
8 import { CriticalConfirmationModalComponent } from '../../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
9 import { Icons } from '../../../../shared/enum/icons.enum';
10 import { CdTableAction } from '../../../../shared/models/cd-table-action';
11 import { CdTableSelection } from '../../../../shared/models/cd-table-selection';
12 import { FinishedTask } from '../../../../shared/models/finished-task';
13 import { Permission } from '../../../../shared/models/permissions';
14 import { AuthStorageService } from '../../../../shared/services/auth-storage.service';
15 import { TaskWrapperService } from '../../../../shared/services/task-wrapper.service';
16 import { PoolEditModeModalComponent } from '../pool-edit-mode-modal/pool-edit-mode-modal.component';
17 import { PoolEditPeerModalComponent } from '../pool-edit-peer-modal/pool-edit-peer-modal.component';
20 selector: 'cd-mirroring-pools',
21 templateUrl: './pool-list.component.html',
22 styleUrls: ['./pool-list.component.scss']
24 export class PoolListComponent implements OnInit, OnDestroy {
25 @ViewChild('healthTmpl', { static: true })
26 healthTmpl: TemplateRef<any>;
30 permission: Permission;
31 tableActions: CdTableAction[];
32 selection = new CdTableSelection();
40 private authStorageService: AuthStorageService,
41 private rbdMirroringService: RbdMirroringService,
42 private modalService: BsModalService,
43 private taskWrapper: TaskWrapperService,
47 this.permission = this.authStorageService.getPermissions().rbdMirroring;
49 const editModeAction: CdTableAction = {
52 click: () => this.editModeModal(),
53 name: this.i18n('Edit Mode'),
54 canBePrimary: () => true
56 const addPeerAction: CdTableAction = {
59 name: this.i18n('Add Peer'),
60 click: () => this.editPeersModal('add'),
61 disable: () => !this.selection.first() || this.selection.first().mirror_mode === 'disabled',
62 visible: () => !this.getPeerUUID(),
63 canBePrimary: () => false
65 const editPeerAction: CdTableAction = {
68 name: this.i18n('Edit Peer'),
69 click: () => this.editPeersModal('edit'),
70 visible: () => !!this.getPeerUUID()
72 const deletePeerAction: CdTableAction = {
75 name: this.i18n('Delete Peer'),
76 click: () => this.deletePeersModal(),
77 visible: () => !!this.getPeerUUID()
79 this.tableActions = [editModeAction, addPeerAction, editPeerAction, deletePeerAction];
84 { prop: 'name', name: this.i18n('Name'), flexGrow: 2 },
85 { prop: 'mirror_mode', name: this.i18n('Mode'), flexGrow: 2 },
86 { prop: 'leader_id', name: this.i18n('Leader'), flexGrow: 2 },
87 { prop: 'image_local_count', name: this.i18n('# Local'), flexGrow: 2 },
88 { prop: 'image_remote_count', name: this.i18n('# Remote'), flexGrow: 2 },
91 name: this.i18n('Health'),
92 cellTemplate: this.healthTmpl,
97 this.subs = this.rbdMirroringService.subscribeSummary((data) => {
98 this.data = data.content_data.pools;
102 ngOnDestroy(): void {
103 this.subs.unsubscribe();
107 this.rbdMirroringService.refresh();
111 const initialState = {
112 poolName: this.selection.first().name
114 this.modalRef = this.modalService.show(PoolEditModeModalComponent, { initialState });
117 editPeersModal(mode: string) {
118 const initialState = {
119 poolName: this.selection.first().name,
122 if (mode === 'edit') {
123 initialState['peerUUID'] = this.getPeerUUID();
125 this.modalRef = this.modalService.show(PoolEditPeerModalComponent, { initialState });
129 const poolName = this.selection.first().name;
130 const peerUUID = this.getPeerUUID();
132 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
134 itemDescription: this.i18n('mirror peer'),
135 itemNames: [`${poolName} (${peerUUID})`],
136 submitActionObservable: () =>
137 new Observable((observer: Subscriber<any>) => {
139 .wrapTaskAroundCall({
140 task: new FinishedTask('rbd/mirroring/peer/delete', {
143 call: this.rbdMirroringService.deletePeer(poolName, peerUUID)
147 (resp) => observer.error(resp),
149 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;