# All files / src/app/ceph/block/mirroring/pool-edit-mode-modal pool-edit-mode-modal.component.ts

86.27% Statements 44/51
68.42% Branches 13/19
66.67% Functions 8/12
86.96% Lines 40/46

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
    1105x
    5x

    5x
    5x

    5x
    5x
    5x
    5x
    5x

    5x

    3x

    3x

    3x

    3x
    3x
    3x
    3x

    3x

    5x
    3x

    5x
    3x
    3x

    3x
    3x

    3x

    5x
    2x

    5x

    1x

    10x

    5x

    5x
    1x
    1x

    1x

    1x

    1x
    1x

    5x
     import { Component, OnDestroy, OnInit } from '@angular/core';
    import { AbstractControl, FormControl, Validators } from '@angular/forms';

    import { I18n } from '@ngx-translate/i18n-polyfill';
    import { BsModalRef } from 'ngx-bootstrap/modal';
    import { Subscription } from 'rxjs';

    import { RbdMirroringService } from '../../../../shared/api/rbd-mirroring.service';
    import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
    import { FinishedTask } from '../../../../shared/models/finished-task';
    import { TaskWrapperService } from '../../../../shared/services/task-wrapper.service';
    import { PoolEditModeResponseModel } from './pool-edit-mode-response.model';

    @Component({
      selector: 'cd-pool-edit-mode-modal',
      template: require('./pool-edit-mode-modal.component.html'),
      styles: []
    })
    export class PoolEditModeModalComponent implements OnInit, OnDestroy {
      poolName: string;

      subs: Subscription;

      editModeForm: CdFormGroup;
      bsConfig = {
        containerClass: 'theme-default'
      };
      pattern: string;

      response: PoolEditModeResponseModel;
      peerExists = false;

      mirrorModes: Array<{ id: string; name: string }> = [
        { id: 'disabled', name: this.i18n('Disabled') },
        { id: 'pool', name: this.i18n('Pool') },
        { id: 'image', name: this.i18n('Image') }
      ];

      constructor(
        public modalRef: BsModalRef,
        private i18n: I18n,
        private rbdMirroringService: RbdMirroringService,
        private taskWrapper: TaskWrapperService
      ) {
        this.createForm();
      }

      createForm() {
        this.editModeForm = new CdFormGroup({
          mirrorMode: new FormControl('', {
            validators: [Validators.required, this.validateMode.bind(this)]
          })
        });
      }

      ngOnInit() {
        this.pattern = `${this.poolName}`;
        this.rbdMirroringService.getPool(this.poolName).subscribe((resp: PoolEditModeResponseModel) => {
          this.setResponse(resp);
        });

        this.subs = this.rbdMirroringService.subscribeSummary((data: any) => {
          this.peerExists = false;
          if (!data) {
            return;
          }

          const poolData = data.content_data.pools;
          const pool = poolData.find((o) => this.poolName === o['name']);
          this.peerExists = pool && pool['peer_uuids'].length;
        });
      }

      ngOnDestroy(): void {
        this.subs.unsubscribe();
      }

      validateMode(control: AbstractControl) {
        if (control.value === 'disabled' && this.peerExists) {
          return { cannotDisable: { value: control.value } };
        }
        return null;
      }

      setResponse(response: PoolEditModeResponseModel) {
        this.editModeForm.get('mirrorMode').setValue(response.mirror_mode);
      }

      update() {
        const request = new PoolEditModeResponseModel();
        request.mirror_mode = this.editModeForm.getValue('mirrorMode');

        const action = this.taskWrapper.wrapTaskAroundCall({
          task: new FinishedTask('rbd/mirroring/pool/edit', {
            pool_name: this.poolName
          }),
          call: this.rbdMirroringService.updatePool(this.poolName, request)
        });

        action.subscribe(
          undefined,
          () => this.editModeForm.setErrors({ cdSubmitButton: true }),
          () => {
            this.rbdMirroringService.refresh();
            this.modalRef.hide();
          }
        );
      }
    }