All files / src/app/ceph/block/mirroring/pool-list pool-list.component.ts

57.97% Statements 40/69
57.14% Branches 16/28
16% Functions 4/25
57.81% Lines 37/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 1714x   4x 4x 4x   4x 4x   4x 4x   4x 4x 4x 4x             4x   4x           2x             2x 2x 2x 2x 2x 2x   2x 2x                                                               2x     4x 2x                           2x   2x           4x       4x 2x     4x             4x                     4x                                                         4x               4x     4x  
import { Component, OnDestroy, OnInit, TemplateRef, ViewChild } from '@angular/core';
 
import { I18n } from '@ngx-translate/i18n-polyfill';
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
import { Observable, Subscriber, Subscription } from 'rxjs';
 
import { RbdMirroringService } from '../../../../shared/api/rbd-mirroring.service';
import { CriticalConfirmationModalComponent } from '../../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
import { CdTableAction } from '../../../../shared/models/cd-table-action';
import { CdTableSelection } from '../../../../shared/models/cd-table-selection';
import { FinishedTask } from '../../../../shared/models/finished-task';
import { Permission } from '../../../../shared/models/permissions';
import { AuthStorageService } from '../../../../shared/services/auth-storage.service';
import { TaskWrapperService } from '../../../../shared/services/task-wrapper.service';
import { PoolEditModeModalComponent } from '../pool-edit-mode-modal/pool-edit-mode-modal.component';
import { PoolEditPeerModalComponent } from '../pool-edit-peer-modal/pool-edit-peer-modal.component';
 
@Component({
  selector: 'cd-mirroring-pools',
  template: require('./pool-list.component.html'),
  styles: []
})
export class PoolListComponent implements OnInit, OnDestroy {
  @ViewChild('healthTmpl')
  healthTmpl: TemplateRef<any>;
 
  subs: Subscription;
 
  permission: Permission;
  tableActions: CdTableAction[];
  selection = new CdTableSelection();
 
  modalRef: BsModalRef;
 
  data: [];
  columns: {};
 
  constructor(
    private authStorageService: AuthStorageService,
    private rbdMirroringService: RbdMirroringService,
    private modalService: BsModalService,
    private taskWrapper: TaskWrapperService,
    private i18n: I18n
  ) {
    this.data = [];
    this.permission = this.authStorageService.getPermissions().rbdMirroring;
 
    const editModeAction: CdTableAction = {
      permission: 'update',
      icon: 'fa-edit',
      click: () => this.editModeModal(),
      name: this.i18n('Edit Mode'),
      canBePrimary: () => true
    };
    const addPeerAction: CdTableAction = {
      permission: 'create',
      icon: 'fa-plus',
      name: this.i18n('Add Peer'),
      click: () => this.editPeersModal('add'),
      disable: () => !this.selection.first() || this.selection.first().mirror_mode === 'disabled',
      visible: () => !this.getPeerUUID(),
      canBePrimary: () => false
    };
    const editPeerAction: CdTableAction = {
      permission: 'update',
      icon: 'fa-exchange',
      name: this.i18n('Edit Peer'),
      click: () => this.editPeersModal('edit'),
      visible: () => !!this.getPeerUUID()
    };
    const deletePeerAction: CdTableAction = {
      permission: 'delete',
      icon: 'fa-times',
      name: this.i18n('Delete Peer'),
      click: () => this.deletePeersModal(),
      visible: () => !!this.getPeerUUID()
    };
    this.tableActions = [editModeAction, addPeerAction, editPeerAction, deletePeerAction];
  }
 
  ngOnInit() {
    this.columns = [
      { prop: 'name', name: this.i18n('Name'), flexGrow: 2 },
      { prop: 'mirror_mode', name: this.i18n('Mode'), flexGrow: 2 },
      { prop: 'leader_id', name: this.i18n('Leader'), flexGrow: 2 },
      { prop: 'image_local_count', name: this.i18n('# Local'), flexGrow: 2 },
      { prop: 'image_remote_count', name: this.i18n('# Remote'), flexGrow: 2 },
      {
        prop: 'health',
        name: this.i18n('Health'),
        cellTemplate: this.healthTmpl,
        flexGrow: 1
      }
    ];
 
    this.subs = this.rbdMirroringService.subscribeSummary((data: any) => {
      if (!data) {
        return;
      }
      this.data = data.content_data.pools;
    });
  }
 
  ngOnDestroy(): void {
    this.subs.unsubscribe();
  }
 
  refresh() {
    this.rbdMirroringService.refresh();
  }
 
  editModeModal() {
    const initialState = {
      poolName: this.selection.first().name
    };
    this.modalRef = this.modalService.show(PoolEditModeModalComponent, { initialState });
  }
 
  editPeersModal(mode) {
    const initialState = {
      poolName: this.selection.first().name,
      mode: mode
    };
    if (mode === 'edit') {
      initialState['peerUUID'] = this.getPeerUUID();
    }
    this.modalRef = this.modalService.show(PoolEditPeerModalComponent, { initialState });
  }
 
  deletePeersModal() {
    const poolName = this.selection.first().name;
    const peerUUID = this.getPeerUUID();
 
    this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
      initialState: {
        itemDescription: this.i18n('mirror peer'),
        submitActionObservable: () =>
          new Observable((observer: Subscriber<any>) => {
            this.taskWrapper
              .wrapTaskAroundCall({
                task: new FinishedTask('rbd/mirroring/peer/delete', {
                  pool_name: poolName
                }),
                call: this.rbdMirroringService.deletePeer(poolName, peerUUID)
              })
              .subscribe(
                undefined,
                (resp) => observer.error(resp),
                () => {
                  this.rbdMirroringService.refresh();
                  observer.complete();
                }
              );
          })
      }
    });
  }
 
  getPeerUUID() {
    const selection = this.selection.first();
    const pool = this.data.find((o) => selection && selection.name === o['name']);
    if (pool && pool['peer_uuids']) {
      return pool['peer_uuids'][0];
    }
  }
 
  updateSelection(selection: CdTableSelection) {
    this.selection = selection;
  }
}