1 import { Component, Inject, OnInit, Optional } from '@angular/core';
2 import { AbstractControl, UntypedFormControl, Validators } from '@angular/forms';
4 import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
5 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
6 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
7 import { FinishedTask } from '~/app/shared/models/finished-task';
8 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
9 import { PoolEditPeerResponseModel } from './pool-edit-peer-response.model';
10 import { BaseModal } from 'carbon-components-angular';
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 extends BaseModal implements OnInit {
18 editPeerForm: CdFormGroup;
20 containerClass: 'theme-default'
24 response: PoolEditPeerResponseModel;
27 public actionLabels: ActionLabelsI18n,
28 private rbdMirroringService: RbdMirroringService,
29 private taskWrapper: TaskWrapperService,
31 @Inject('poolName') public poolName: string,
32 @Optional() @Inject('peerUUID') public peerUUID = '',
33 @Optional() @Inject('mode') public mode = ''
40 this.editPeerForm = new CdFormGroup({
41 clusterName: new UntypedFormControl('', {
42 validators: [Validators.required, this.validateClusterName]
44 clientID: new UntypedFormControl('', {
45 validators: [Validators.required, this.validateClientID]
47 monAddr: new UntypedFormControl('', {
48 validators: [this.validateMonAddr]
50 key: new UntypedFormControl('', {
51 validators: [this.validateKey]
57 this.pattern = `${this.poolName}/${this.peerUUID}`;
58 if (this.mode === 'edit') {
59 this.rbdMirroringService
60 .getPeer(this.poolName, this.peerUUID)
61 .subscribe((resp: PoolEditPeerResponseModel) => {
62 this.setResponse(resp);
67 validateClusterName(control: AbstractControl) {
68 if (!control.value.match(/^[\w\-_]*$/)) {
69 return { invalidClusterName: { value: control.value } };
75 validateClientID(control: AbstractControl) {
76 if (!control.value.match(/^(?!client\.)[\w\-_.]*$/)) {
77 return { invalidClientID: { value: control.value } };
83 validateMonAddr(control: AbstractControl) {
84 if (!control.value.match(/^[,; ]*([\w.\-_\[\]]+(:[\d]+)?[,; ]*)*$/)) {
85 return { invalidMonAddr: { value: control.value } };
91 validateKey(control: AbstractControl) {
93 if (control.value === '' || !!atob(control.value)) {
97 return { invalidKey: { value: control.value } };
100 setResponse(response: PoolEditPeerResponseModel) {
101 this.response = response;
102 this.editPeerForm.get('clusterName').setValue(response.cluster_name);
103 this.editPeerForm.get('clientID').setValue(response.client_id);
104 this.editPeerForm.get('monAddr').setValue(response.mon_host);
105 this.editPeerForm.get('key').setValue(response.key);
109 const request = new PoolEditPeerResponseModel();
110 request.cluster_name = this.editPeerForm.getValue('clusterName');
111 request.client_id = this.editPeerForm.getValue('clientID');
112 request.mon_host = this.editPeerForm.getValue('monAddr');
113 request.key = this.editPeerForm.getValue('key');
116 if (this.mode === 'edit') {
117 action = this.taskWrapper.wrapTaskAroundCall({
118 task: new FinishedTask('rbd/mirroring/peer/edit', {
119 pool_name: this.poolName
121 call: this.rbdMirroringService.updatePeer(this.poolName, this.peerUUID, request)
124 action = this.taskWrapper.wrapTaskAroundCall({
125 task: new FinishedTask('rbd/mirroring/peer/add', {
126 pool_name: this.poolName
128 call: this.rbdMirroringService.addPeer(this.poolName, request)
133 error: () => this.editPeerForm.setErrors({ cdSubmitButton: true }),
135 this.rbdMirroringService.refresh();