1 import { Component, OnInit } from '@angular/core';
2 import { AbstractControl, FormControl, Validators } from '@angular/forms';
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
6 import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
7 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
8 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
9 import { FinishedTask } from '~/app/shared/models/finished-task';
10 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
11 import { PoolEditPeerResponseModel } from './pool-edit-peer-response.model';
14 selector: 'cd-pool-edit-peer-modal',
15 templateUrl: './pool-edit-peer-modal.component.html',
16 styleUrls: ['./pool-edit-peer-modal.component.scss']
18 export class PoolEditPeerModalComponent implements OnInit {
23 editPeerForm: CdFormGroup;
25 containerClass: 'theme-default'
29 response: PoolEditPeerResponseModel;
32 public activeModal: NgbActiveModal,
33 public actionLabels: ActionLabelsI18n,
34 private rbdMirroringService: RbdMirroringService,
35 private taskWrapper: TaskWrapperService
41 this.editPeerForm = new CdFormGroup({
42 clusterName: new FormControl('', {
43 validators: [Validators.required, this.validateClusterName]
45 clientID: new FormControl('', {
46 validators: [Validators.required, this.validateClientID]
48 monAddr: new FormControl('', {
49 validators: [this.validateMonAddr]
51 key: new FormControl('', {
52 validators: [this.validateKey]
58 this.pattern = `${this.poolName}/${this.peerUUID}`;
59 if (this.mode === 'edit') {
60 this.rbdMirroringService
61 .getPeer(this.poolName, this.peerUUID)
62 .subscribe((resp: PoolEditPeerResponseModel) => {
63 this.setResponse(resp);
68 validateClusterName(control: AbstractControl) {
69 if (!control.value.match(/^[\w\-_]*$/)) {
70 return { invalidClusterName: { value: control.value } };
76 validateClientID(control: AbstractControl) {
77 if (!control.value.match(/^(?!client\.)[\w\-_.]*$/)) {
78 return { invalidClientID: { value: control.value } };
84 validateMonAddr(control: AbstractControl) {
85 if (!control.value.match(/^[,; ]*([\w.\-_\[\]]+(:[\d]+)?[,; ]*)*$/)) {
86 return { invalidMonAddr: { value: control.value } };
92 validateKey(control: AbstractControl) {
94 if (control.value === '' || !!atob(control.value)) {
98 return { invalidKey: { value: control.value } };
101 setResponse(response: PoolEditPeerResponseModel) {
102 this.response = response;
103 this.editPeerForm.get('clusterName').setValue(response.cluster_name);
104 this.editPeerForm.get('clientID').setValue(response.client_id);
105 this.editPeerForm.get('monAddr').setValue(response.mon_host);
106 this.editPeerForm.get('key').setValue(response.key);
110 const request = new PoolEditPeerResponseModel();
111 request.cluster_name = this.editPeerForm.getValue('clusterName');
112 request.client_id = this.editPeerForm.getValue('clientID');
113 request.mon_host = this.editPeerForm.getValue('monAddr');
114 request.key = this.editPeerForm.getValue('key');
117 if (this.mode === 'edit') {
118 action = this.taskWrapper.wrapTaskAroundCall({
119 task: new FinishedTask('rbd/mirroring/peer/edit', {
120 pool_name: this.poolName
122 call: this.rbdMirroringService.updatePeer(this.poolName, this.peerUUID, request)
125 action = this.taskWrapper.wrapTaskAroundCall({
126 task: new FinishedTask('rbd/mirroring/peer/add', {
127 pool_name: this.poolName
129 call: this.rbdMirroringService.addPeer(this.poolName, request)
134 error: () => this.editPeerForm.setErrors({ cdSubmitButton: true }),
136 this.rbdMirroringService.refresh();
137 this.activeModal.close();