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

98.11% Statements 52/53
76.92% Branches 10/13
91.67% Functions 11/12
97.96% Lines 48/49

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 1355x 5x   5x   5x 5x 5x 5x 5x             5x           7x               7x 7x 7x   7x     5x 7x                               6x 6x   1x     1x         5x   1x       5x   1x       5x   1x       5x 22x   21x     1x     5x 1x 1x 1x 1x 1x     5x 2x 2x 2x 2x 2x       1x             1x               2x       2x 2x       5x  
import { Component, OnInit } from '@angular/core';
import { AbstractControl, FormControl, Validators } from '@angular/forms';
 
import { BsModalRef } from 'ngx-bootstrap/modal';
 
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 { PoolEditPeerResponseModel } from './pool-edit-peer-response.model';
 
@Component({
  selector: 'cd-pool-edit-peer-modal',
  template: require('./pool-edit-peer-modal.component.html'),
  styles: []
})
export class PoolEditPeerModalComponent implements OnInit {
  mode: string;
  poolName: string;
  peerUUID: string;
 
  editPeerForm: CdFormGroup;
  bsConfig = {
    containerClass: 'theme-default'
  };
  pattern: string;
 
  response: PoolEditPeerResponseModel;
 
  constructor(
    public modalRef: BsModalRef,
    private rbdMirroringService: RbdMirroringService,
    private taskWrapper: TaskWrapperService
  ) {
    this.createForm();
  }
 
  createForm() {
    this.editPeerForm = new CdFormGroup({
      clusterName: new FormControl('', {
        validators: [Validators.required, this.validateClusterName]
      }),
      clientID: new FormControl('', {
        validators: [Validators.required, this.validateClientID]
      }),
      monAddr: new FormControl('', {
        validators: [this.validateMonAddr]
      }),
      key: new FormControl('', {
        validators: [this.validateKey]
      })
    });
  }
 
  ngOnInit() {
    this.pattern = `${this.poolName}/${this.peerUUID}`;
    if (this.mode === 'edit') {
      this.rbdMirroringService
        .getPeer(this.poolName, this.peerUUID)
        .subscribe((resp: PoolEditPeerResponseModel) => {
          this.setResponse(resp);
        });
    }
  }
 
  validateClusterName(control: AbstractControl) {
    if (!control.value.match(/^[\w\-_]*$/)) {
      return { invalidClusterName: { value: control.value } };
    }
  }
 
  validateClientID(control: AbstractControl) {
    if (!control.value.match(/^(?!client\.)[\w\-_.]*$/)) {
      return { invalidClientID: { value: control.value } };
    }
  }
 
  validateMonAddr(control: AbstractControl) {
    if (!control.value.match(/^[,; ]*([\w.\-_\[\]]+(:[\d]+)?[,; ]*)*$/)) {
      return { invalidMonAddr: { value: control.value } };
    }
  }
 
  validateKey(control: AbstractControl) {
    try {
      if (control.value === '' || !!atob(control.value)) {
        return null;
      }
    } catch (error) {}
    return { invalidKey: { value: control.value } };
  }
 
  setResponse(response: PoolEditPeerResponseModel) {
    this.response = response;
    this.editPeerForm.get('clusterName').setValue(response.cluster_name);
    this.editPeerForm.get('clientID').setValue(response.client_id);
    this.editPeerForm.get('monAddr').setValue(response.mon_host);
    this.editPeerForm.get('key').setValue(response.key);
  }
 
  update() {
    const request = new PoolEditPeerResponseModel();
    request.cluster_name = this.editPeerForm.getValue('clusterName');
    request.client_id = this.editPeerForm.getValue('clientID');
    request.mon_host = this.editPeerForm.getValue('monAddr');
    request.key = this.editPeerForm.getValue('key');
 
    let action;
    if (this.mode === 'edit') {
      action = this.taskWrapper.wrapTaskAroundCall({
        task: new FinishedTask('rbd/mirroring/peer/edit', {
          pool_name: this.poolName
        }),
        call: this.rbdMirroringService.updatePeer(this.poolName, this.peerUUID, request)
      });
    } else {
      action = this.taskWrapper.wrapTaskAroundCall({
        task: new FinishedTask('rbd/mirroring/peer/add', {
          pool_name: this.poolName
        }),
        call: this.rbdMirroringService.addPeer(this.poolName, request)
      });
    }
 
    action.subscribe(
      undefined,
      () => this.editPeerForm.setErrors({ cdSubmitButton: true }),
      () => {
        this.rbdMirroringService.refresh();
        this.modalRef.hide();
      }
    );
  }
}