]>
Commit | Line | Data |
---|---|---|
adb31ebb TL |
1 | import { Component, OnInit } from '@angular/core'; |
2 | import { AbstractControl, Validators } from '@angular/forms'; | |
3 | import { Router } from '@angular/router'; | |
4 | ||
5 | import { I18n } from '@ngx-translate/i18n-polyfill'; | |
6 | import * as _ from 'lodash'; | |
7 | import { Observable } from 'rxjs'; | |
8 | import { map } from 'rxjs/operators'; | |
9 | ||
10 | import { CephServiceService } from '../../../../shared/api/ceph-service.service'; | |
11 | import { HostService } from '../../../../shared/api/host.service'; | |
12 | import { PoolService } from '../../../../shared/api/pool.service'; | |
13 | import { SelectMessages } from '../../../../shared/components/select/select-messages.model'; | |
14 | import { SelectOption } from '../../../../shared/components/select/select-option.model'; | |
15 | import { ActionLabelsI18n, URLVerbs } from '../../../../shared/constants/app.constants'; | |
16 | import { CdFormBuilder } from '../../../../shared/forms/cd-form-builder'; | |
17 | import { CdFormGroup } from '../../../../shared/forms/cd-form-group'; | |
18 | import { CdValidators } from '../../../../shared/forms/cd-validators'; | |
19 | import { FinishedTask } from '../../../../shared/models/finished-task'; | |
20 | import { TaskWrapperService } from '../../../../shared/services/task-wrapper.service'; | |
21 | ||
22 | @Component({ | |
23 | selector: 'cd-service-form', | |
24 | templateUrl: './service-form.component.html', | |
25 | styleUrls: ['./service-form.component.scss'] | |
26 | }) | |
27 | export class ServiceFormComponent implements OnInit { | |
28 | serviceForm: CdFormGroup; | |
29 | action: string; | |
30 | resource: string; | |
31 | serviceTypes: string[] = []; | |
32 | hosts: any; | |
33 | labels: string[]; | |
34 | pools: Array<object>; | |
35 | ||
36 | searchLabels: Observable<any> = new Observable((observer: any) => { | |
37 | observer.next(this.serviceForm.getValue('label')); | |
38 | }).pipe( | |
39 | map((value: string) => { | |
40 | const result = this.labels | |
41 | .filter((label: string) => label.toLowerCase().indexOf(value.toLowerCase()) > -1) | |
42 | .slice(0, 10); | |
43 | return result; | |
44 | }) | |
45 | ); | |
46 | ||
47 | constructor( | |
48 | public actionLabels: ActionLabelsI18n, | |
49 | private cephServiceService: CephServiceService, | |
50 | private formBuilder: CdFormBuilder, | |
51 | private hostService: HostService, | |
52 | private i18n: I18n, | |
53 | private poolService: PoolService, | |
54 | private router: Router, | |
55 | private taskWrapperService: TaskWrapperService | |
56 | ) { | |
57 | this.resource = this.i18n(`service`); | |
58 | this.hosts = { | |
59 | options: [], | |
60 | messages: new SelectMessages( | |
61 | { | |
62 | empty: this.i18n(`There are no hosts.`), | |
63 | filter: this.i18n(`Filter hosts`) | |
64 | }, | |
65 | this.i18n | |
66 | ) | |
67 | }; | |
68 | this.createForm(); | |
69 | } | |
70 | ||
71 | createForm() { | |
72 | this.serviceForm = this.formBuilder.group({ | |
73 | // Global | |
74 | service_type: [null, [Validators.required]], | |
75 | service_id: [ | |
76 | null, | |
77 | [ | |
78 | CdValidators.requiredIf({ | |
79 | service_type: 'mds' | |
80 | }), | |
81 | CdValidators.requiredIf({ | |
82 | service_type: 'nfs' | |
83 | }), | |
84 | CdValidators.requiredIf({ | |
85 | service_type: 'iscsi' | |
86 | }), | |
87 | CdValidators.composeIf( | |
88 | { | |
89 | service_type: 'rgw' | |
90 | }, | |
91 | [ | |
92 | Validators.required, | |
93 | CdValidators.custom('rgwPattern', (value: string) => { | |
94 | if (_.isEmpty(value)) { | |
95 | return false; | |
96 | } | |
97 | return !/^[^.]+\.[^.]+(\.[^.]+)?$/.test(value); | |
98 | }) | |
99 | ] | |
100 | ) | |
101 | ] | |
102 | ], | |
103 | placement: ['hosts'], | |
104 | label: [ | |
105 | null, | |
106 | [ | |
107 | CdValidators.requiredIf({ | |
108 | placement: 'label', | |
109 | unmanaged: false | |
110 | }) | |
111 | ] | |
112 | ], | |
113 | hosts: [[]], | |
114 | count: [null, [CdValidators.number(false), Validators.min(1)]], | |
115 | unmanaged: [false], | |
116 | // NFS & iSCSI | |
117 | pool: [ | |
118 | null, | |
119 | [ | |
120 | CdValidators.requiredIf({ | |
121 | service_type: 'nfs', | |
122 | unmanaged: false | |
123 | }), | |
124 | CdValidators.requiredIf({ | |
125 | service_type: 'iscsi', | |
126 | unmanaged: false | |
127 | }) | |
128 | ] | |
129 | ], | |
130 | // NFS | |
131 | namespace: [null], | |
132 | // RGW | |
133 | rgw_frontend_port: [ | |
134 | null, | |
135 | [CdValidators.number(false), Validators.min(1), Validators.max(65535)] | |
136 | ], | |
137 | // iSCSI | |
138 | trusted_ip_list: [null], | |
139 | api_port: [null, [CdValidators.number(false), Validators.min(1), Validators.max(65535)]], | |
140 | api_user: [ | |
141 | null, | |
142 | [ | |
143 | CdValidators.requiredIf({ | |
144 | service_type: 'iscsi', | |
145 | unmanaged: false | |
146 | }) | |
147 | ] | |
148 | ], | |
149 | api_password: [ | |
150 | null, | |
151 | [ | |
152 | CdValidators.requiredIf({ | |
153 | service_type: 'iscsi', | |
154 | unmanaged: false | |
155 | }) | |
156 | ] | |
157 | ], | |
158 | // RGW & iSCSI | |
159 | ssl: [false], | |
160 | ssl_cert: [ | |
161 | '', | |
162 | [ | |
163 | CdValidators.composeIf( | |
164 | { | |
165 | service_type: 'rgw', | |
166 | unmanaged: false, | |
167 | ssl: true | |
168 | }, | |
169 | [Validators.required, CdValidators.sslCert()] | |
170 | ), | |
171 | CdValidators.composeIf( | |
172 | { | |
173 | service_type: 'iscsi', | |
174 | unmanaged: false, | |
175 | ssl: true | |
176 | }, | |
177 | [Validators.required, CdValidators.sslCert()] | |
178 | ) | |
179 | ] | |
180 | ], | |
181 | ssl_key: [ | |
182 | '', | |
183 | [ | |
184 | CdValidators.composeIf( | |
185 | { | |
186 | service_type: 'rgw', | |
187 | unmanaged: false, | |
188 | ssl: true | |
189 | }, | |
190 | [Validators.required, CdValidators.sslPrivKey()] | |
191 | ), | |
192 | CdValidators.composeIf( | |
193 | { | |
194 | service_type: 'iscsi', | |
195 | unmanaged: false, | |
196 | ssl: true | |
197 | }, | |
198 | [Validators.required, CdValidators.sslPrivKey()] | |
199 | ) | |
200 | ] | |
201 | ] | |
202 | }); | |
203 | } | |
204 | ||
205 | ngOnInit(): void { | |
206 | this.action = this.actionLabels.CREATE; | |
207 | this.cephServiceService.getKnownTypes().subscribe((resp: Array<string>) => { | |
208 | // Remove service type 'osd', this is deployed a different way. | |
209 | this.serviceTypes = _.difference(resp, ['osd']).sort(); | |
210 | }); | |
211 | this.hostService.list().subscribe((resp: object[]) => { | |
212 | const options: SelectOption[] = []; | |
213 | _.forEach(resp, (host: object) => { | |
214 | if (_.get(host, 'sources.orchestrator', false)) { | |
215 | const option = new SelectOption(false, _.get(host, 'hostname'), ''); | |
216 | options.push(option); | |
217 | } | |
218 | }); | |
219 | this.hosts.options = [...options]; | |
220 | }); | |
221 | this.hostService.getLabels().subscribe((resp: string[]) => { | |
222 | this.labels = resp; | |
223 | }); | |
224 | this.poolService.getList().subscribe((resp: Array<object>) => { | |
225 | this.pools = resp; | |
226 | }); | |
227 | } | |
228 | ||
229 | goToListView() { | |
230 | this.router.navigate(['/services']); | |
231 | } | |
232 | ||
233 | fileUpload(files: FileList, controlName: string) { | |
234 | const file: File = files[0]; | |
235 | const reader = new FileReader(); | |
236 | reader.addEventListener('load', (event: ProgressEvent) => { | |
237 | const control: AbstractControl = this.serviceForm.get(controlName); | |
238 | control.setValue((event.target as FileReader).result); | |
239 | control.markAsDirty(); | |
240 | control.markAsTouched(); | |
241 | control.updateValueAndValidity(); | |
242 | }); | |
243 | reader.readAsText(file, 'utf8'); | |
244 | } | |
245 | ||
246 | onSubmit() { | |
247 | const self = this; | |
248 | const values: object = this.serviceForm.value; | |
249 | const serviceId: string = values['service_id']; | |
250 | const serviceType: string = values['service_type']; | |
251 | const serviceSpec: object = { | |
252 | service_type: serviceType, | |
253 | placement: {}, | |
254 | unmanaged: values['unmanaged'] | |
255 | }; | |
256 | let serviceName: string = serviceType; | |
257 | if (_.isString(serviceId) && !_.isEmpty(serviceId)) { | |
258 | serviceName = `${serviceType}.${serviceId}`; | |
259 | serviceSpec['service_id'] = serviceId; | |
260 | } | |
261 | if (!values['unmanaged']) { | |
262 | switch (values['placement']) { | |
263 | case 'hosts': | |
264 | if (values['hosts'].length > 0) { | |
265 | serviceSpec['placement']['hosts'] = values['hosts']; | |
266 | } | |
267 | break; | |
268 | case 'label': | |
269 | serviceSpec['placement']['label'] = values['label']; | |
270 | break; | |
271 | } | |
272 | if (_.isNumber(values['count']) && values['count'] > 0) { | |
273 | serviceSpec['placement']['count'] = values['count']; | |
274 | } | |
275 | switch (serviceType) { | |
276 | case 'nfs': | |
277 | serviceSpec['pool'] = values['pool']; | |
278 | if (_.isString(values['namespace']) && !_.isEmpty(values['namespace'])) { | |
279 | serviceSpec['namespace'] = values['namespace']; | |
280 | } | |
281 | break; | |
282 | case 'rgw': | |
283 | if (_.isNumber(values['rgw_frontend_port']) && values['rgw_frontend_port'] > 0) { | |
284 | serviceSpec['rgw_frontend_port'] = values['rgw_frontend_port']; | |
285 | } | |
286 | serviceSpec['ssl'] = values['ssl']; | |
287 | if (values['ssl']) { | |
288 | serviceSpec['rgw_frontend_ssl_certificate'] = values['ssl_cert'].trim(); | |
289 | serviceSpec['rgw_frontend_ssl_key'] = values['ssl_key'].trim(); | |
290 | } | |
291 | break; | |
292 | case 'iscsi': | |
293 | serviceSpec['pool'] = values['pool']; | |
294 | if (_.isString(values['trusted_ip_list']) && !_.isEmpty(values['trusted_ip_list'])) { | |
295 | let parts = _.split(values['trusted_ip_list'], ','); | |
296 | parts = _.map(parts, _.trim); | |
297 | serviceSpec['trusted_ip_list'] = parts; | |
298 | } | |
299 | if (_.isNumber(values['api_port']) && values['api_port'] > 0) { | |
300 | serviceSpec['api_port'] = values['api_port']; | |
301 | } | |
302 | serviceSpec['api_user'] = values['api_user']; | |
303 | serviceSpec['api_password'] = values['api_password']; | |
304 | serviceSpec['api_secure'] = values['ssl']; | |
305 | if (values['ssl']) { | |
306 | serviceSpec['ssl_cert'] = values['ssl_cert'].trim(); | |
307 | serviceSpec['ssl_key'] = values['ssl_key'].trim(); | |
308 | } | |
309 | break; | |
310 | } | |
311 | } | |
312 | this.taskWrapperService | |
313 | .wrapTaskAroundCall({ | |
314 | task: new FinishedTask(`service/${URLVerbs.CREATE}`, { | |
315 | service_name: serviceName | |
316 | }), | |
317 | call: this.cephServiceService.create(serviceSpec) | |
318 | }) | |
319 | .subscribe({ | |
320 | error() { | |
321 | self.serviceForm.setErrors({ cdSubmitButton: true }); | |
322 | }, | |
323 | complete() { | |
324 | self.goToListView(); | |
325 | } | |
326 | }); | |
327 | } | |
328 | } |