]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
113c5c3dd6bfa034f4f608d2f34c0dab5979216f
[ceph.git] /
1 import { Component, Inject, OnInit, Optional } from '@angular/core';
2 import { AbstractControl, UntypedFormControl, Validators } from '@angular/forms';
3
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';
11
12 @Component({
13   selector: 'cd-pool-edit-peer-modal',
14   templateUrl: './pool-edit-peer-modal.component.html',
15   styleUrls: ['./pool-edit-peer-modal.component.scss']
16 })
17 export class PoolEditPeerModalComponent extends BaseModal implements OnInit {
18   editPeerForm: CdFormGroup;
19   bsConfig = {
20     containerClass: 'theme-default'
21   };
22   pattern: string;
23
24   response: PoolEditPeerResponseModel;
25
26   constructor(
27     public actionLabels: ActionLabelsI18n,
28     private rbdMirroringService: RbdMirroringService,
29     private taskWrapper: TaskWrapperService,
30
31     @Inject('poolName') public poolName: string,
32     @Optional() @Inject('peerUUID') public peerUUID = '',
33     @Optional() @Inject('mode') public mode = ''
34   ) {
35     super();
36     this.createForm();
37   }
38
39   createForm() {
40     this.editPeerForm = new CdFormGroup({
41       clusterName: new UntypedFormControl('', {
42         validators: [Validators.required, this.validateClusterName]
43       }),
44       clientID: new UntypedFormControl('', {
45         validators: [Validators.required, this.validateClientID]
46       }),
47       monAddr: new UntypedFormControl('', {
48         validators: [this.validateMonAddr]
49       }),
50       key: new UntypedFormControl('', {
51         validators: [this.validateKey]
52       })
53     });
54   }
55
56   ngOnInit() {
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);
63         });
64     }
65   }
66
67   validateClusterName(control: AbstractControl) {
68     if (!control.value.match(/^[\w\-_]*$/)) {
69       return { invalidClusterName: { value: control.value } };
70     }
71
72     return undefined;
73   }
74
75   validateClientID(control: AbstractControl) {
76     if (!control.value.match(/^(?!client\.)[\w\-_.]*$/)) {
77       return { invalidClientID: { value: control.value } };
78     }
79
80     return undefined;
81   }
82
83   validateMonAddr(control: AbstractControl) {
84     if (!control.value.match(/^[,; ]*([\w.\-_\[\]]+(:[\d]+)?[,; ]*)*$/)) {
85       return { invalidMonAddr: { value: control.value } };
86     }
87
88     return undefined;
89   }
90
91   validateKey(control: AbstractControl) {
92     try {
93       if (control.value === '' || !!atob(control.value)) {
94         return null;
95       }
96     } catch (error) {}
97     return { invalidKey: { value: control.value } };
98   }
99
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);
106   }
107
108   update() {
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');
114
115     let action;
116     if (this.mode === 'edit') {
117       action = this.taskWrapper.wrapTaskAroundCall({
118         task: new FinishedTask('rbd/mirroring/peer/edit', {
119           pool_name: this.poolName
120         }),
121         call: this.rbdMirroringService.updatePeer(this.poolName, this.peerUUID, request)
122       });
123     } else {
124       action = this.taskWrapper.wrapTaskAroundCall({
125         task: new FinishedTask('rbd/mirroring/peer/add', {
126           pool_name: this.poolName
127         }),
128         call: this.rbdMirroringService.addPeer(this.poolName, request)
129       });
130     }
131
132     action.subscribe({
133       error: () => this.editPeerForm.setErrors({ cdSubmitButton: true }),
134       complete: () => {
135         this.rbdMirroringService.refresh();
136         this.closeModal();
137       }
138     });
139   }
140 }