]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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 | } | |
9f95a23c TL |
70 | |
71 | return undefined; | |
11fdf7f2 TL |
72 | } |
73 | ||
74 | validateClientID(control: AbstractControl) { | |
75 | if (!control.value.match(/^(?!client\.)[\w\-_.]*$/)) { | |
76 | return { invalidClientID: { value: control.value } }; | |
77 | } | |
9f95a23c TL |
78 | |
79 | return undefined; | |
11fdf7f2 TL |
80 | } |
81 | ||
82 | validateMonAddr(control: AbstractControl) { | |
83 | if (!control.value.match(/^[,; ]*([\w.\-_\[\]]+(:[\d]+)?[,; ]*)*$/)) { | |
84 | return { invalidMonAddr: { value: control.value } }; | |
85 | } | |
9f95a23c TL |
86 | |
87 | return undefined; | |
11fdf7f2 TL |
88 | } |
89 | ||
90 | validateKey(control: AbstractControl) { | |
91 | try { | |
92 | if (control.value === '' || !!atob(control.value)) { | |
93 | return null; | |
94 | } | |
95 | } catch (error) {} | |
96 | return { invalidKey: { value: control.value } }; | |
97 | } | |
98 | ||
99 | setResponse(response: PoolEditPeerResponseModel) { | |
100 | this.response = response; | |
101 | this.editPeerForm.get('clusterName').setValue(response.cluster_name); | |
102 | this.editPeerForm.get('clientID').setValue(response.client_id); | |
103 | this.editPeerForm.get('monAddr').setValue(response.mon_host); | |
104 | this.editPeerForm.get('key').setValue(response.key); | |
105 | } | |
106 | ||
107 | update() { | |
108 | const request = new PoolEditPeerResponseModel(); | |
109 | request.cluster_name = this.editPeerForm.getValue('clusterName'); | |
110 | request.client_id = this.editPeerForm.getValue('clientID'); | |
111 | request.mon_host = this.editPeerForm.getValue('monAddr'); | |
112 | request.key = this.editPeerForm.getValue('key'); | |
113 | ||
114 | let action; | |
115 | if (this.mode === 'edit') { | |
116 | action = this.taskWrapper.wrapTaskAroundCall({ | |
117 | task: new FinishedTask('rbd/mirroring/peer/edit', { | |
118 | pool_name: this.poolName | |
119 | }), | |
120 | call: this.rbdMirroringService.updatePeer(this.poolName, this.peerUUID, request) | |
121 | }); | |
122 | } else { | |
123 | action = this.taskWrapper.wrapTaskAroundCall({ | |
124 | task: new FinishedTask('rbd/mirroring/peer/add', { | |
125 | pool_name: this.poolName | |
126 | }), | |
127 | call: this.rbdMirroringService.addPeer(this.poolName, request) | |
128 | }); | |
129 | } | |
130 | ||
131 | action.subscribe( | |
132 | undefined, | |
133 | () => this.editPeerForm.setErrors({ cdSubmitButton: true }), | |
134 | () => { | |
135 | this.rbdMirroringService.refresh(); | |
136 | this.modalRef.hide(); | |
137 | } | |
138 | ); | |
139 | } | |
140 | } |