1 import { Component, OnInit } from '@angular/core';
2 import { AbstractControl, FormControl, Validators } from '@angular/forms';
4 import { BsModalRef } from 'ngx-bootstrap/modal';
6 import { RbdMirroringService } from '../../../../shared/api/rbd-mirroring.service';
7 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
8 import { FinishedTask } from '../../../../shared/models/finished-task';
9 import { TaskWrapperService } from '../../../../shared/services/task-wrapper.service';
10 import { PoolEditPeerResponseModel } from './pool-edit-peer-response.model';
13 selector: 'cd-pool-edit-peer-modal',
14 templateUrl: './pool-edit-peer-modal.component.html',
15 styleUrls: ['./pool-edit-peer-modal.component.scss']
17 export class PoolEditPeerModalComponent implements OnInit {
22 editPeerForm: CdFormGroup;
24 containerClass: 'theme-default'
28 response: PoolEditPeerResponseModel;
31 public modalRef: BsModalRef,
32 private rbdMirroringService: RbdMirroringService,
33 private taskWrapper: TaskWrapperService
39 this.editPeerForm = new CdFormGroup({
40 clusterName: new FormControl('', {
41 validators: [Validators.required, this.validateClusterName]
43 clientID: new FormControl('', {
44 validators: [Validators.required, this.validateClientID]
46 monAddr: new FormControl('', {
47 validators: [this.validateMonAddr]
49 key: new FormControl('', {
50 validators: [this.validateKey]
56 this.pattern = `${this.poolName}/${this.peerUUID}`;
57 if (this.mode === 'edit') {
58 this.rbdMirroringService
59 .getPeer(this.poolName, this.peerUUID)
60 .subscribe((resp: PoolEditPeerResponseModel) => {
61 this.setResponse(resp);
66 validateClusterName(control: AbstractControl) {
67 if (!control.value.match(/^[\w\-_]*$/)) {
68 return { invalidClusterName: { value: control.value } };
72 validateClientID(control: AbstractControl) {
73 if (!control.value.match(/^(?!client\.)[\w\-_.]*$/)) {
74 return { invalidClientID: { value: control.value } };
78 validateMonAddr(control: AbstractControl) {
79 if (!control.value.match(/^[,; ]*([\w.\-_\[\]]+(:[\d]+)?[,; ]*)*$/)) {
80 return { invalidMonAddr: { value: control.value } };
84 validateKey(control: AbstractControl) {
86 if (control.value === '' || !!atob(control.value)) {
90 return { invalidKey: { value: control.value } };
93 setResponse(response: PoolEditPeerResponseModel) {
94 this.response = response;
95 this.editPeerForm.get('clusterName').setValue(response.cluster_name);
96 this.editPeerForm.get('clientID').setValue(response.client_id);
97 this.editPeerForm.get('monAddr').setValue(response.mon_host);
98 this.editPeerForm.get('key').setValue(response.key);
102 const request = new PoolEditPeerResponseModel();
103 request.cluster_name = this.editPeerForm.getValue('clusterName');
104 request.client_id = this.editPeerForm.getValue('clientID');
105 request.mon_host = this.editPeerForm.getValue('monAddr');
106 request.key = this.editPeerForm.getValue('key');
109 if (this.mode === 'edit') {
110 action = this.taskWrapper.wrapTaskAroundCall({
111 task: new FinishedTask('rbd/mirroring/peer/edit', {
112 pool_name: this.poolName
114 call: this.rbdMirroringService.updatePeer(this.poolName, this.peerUUID, request)
117 action = this.taskWrapper.wrapTaskAroundCall({
118 task: new FinishedTask('rbd/mirroring/peer/add', {
119 pool_name: this.poolName
121 call: this.rbdMirroringService.addPeer(this.poolName, request)
127 () => this.editPeerForm.setErrors({ cdSubmitButton: true }),
129 this.rbdMirroringService.refresh();
130 this.modalRef.hide();