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