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