]>
Commit | Line | Data |
---|---|---|
39ae355f | 1 | import { HttpParams } from '@angular/common/http'; |
a4b75251 | 2 | import { Component, Input, OnInit, ViewChild } from '@angular/core'; |
adb31ebb | 3 | import { AbstractControl, Validators } from '@angular/forms'; |
a4b75251 | 4 | import { ActivatedRoute, Router } from '@angular/router'; |
adb31ebb | 5 | |
a4b75251 | 6 | import { NgbActiveModal, NgbTypeahead } from '@ng-bootstrap/ng-bootstrap'; |
f67539c2 TL |
7 | import _ from 'lodash'; |
8 | import { merge, Observable, Subject } from 'rxjs'; | |
9 | import { debounceTime, distinctUntilChanged, filter, map } from 'rxjs/operators'; | |
adb31ebb | 10 | |
f67539c2 TL |
11 | import { CephServiceService } from '~/app/shared/api/ceph-service.service'; |
12 | import { HostService } from '~/app/shared/api/host.service'; | |
13 | import { PoolService } from '~/app/shared/api/pool.service'; | |
14 | import { SelectMessages } from '~/app/shared/components/select/select-messages.model'; | |
15 | import { SelectOption } from '~/app/shared/components/select/select-option.model'; | |
16 | import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants'; | |
17 | import { CdForm } from '~/app/shared/forms/cd-form'; | |
18 | import { CdFormBuilder } from '~/app/shared/forms/cd-form-builder'; | |
19 | import { CdFormGroup } from '~/app/shared/forms/cd-form-group'; | |
20 | import { CdValidators } from '~/app/shared/forms/cd-validators'; | |
21 | import { FinishedTask } from '~/app/shared/models/finished-task'; | |
b3b6e05e | 22 | import { CephServiceSpec } from '~/app/shared/models/service.interface'; |
f67539c2 | 23 | import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service'; |
adb31ebb TL |
24 | |
25 | @Component({ | |
26 | selector: 'cd-service-form', | |
27 | templateUrl: './service-form.component.html', | |
28 | styleUrls: ['./service-form.component.scss'] | |
29 | }) | |
f67539c2 | 30 | export class ServiceFormComponent extends CdForm implements OnInit { |
522d829b | 31 | readonly RGW_SVC_ID_PATTERN = /^([^.]+)(\.([^.]+)\.([^.]+))?$/; |
39ae355f | 32 | readonly MDS_SVC_ID_PATTERN = /^[a-zA-Z_.-][a-zA-Z0-9_.-]*$/; |
20effc67 TL |
33 | readonly SNMP_DESTINATION_PATTERN = /^[^\:]+:[0-9]/; |
34 | readonly SNMP_ENGINE_ID_PATTERN = /^[0-9A-Fa-f]{10,64}/g; | |
2a845540 | 35 | readonly INGRESS_SUPPORTED_SERVICE_TYPES = ['rgw', 'nfs']; |
f67539c2 TL |
36 | @ViewChild(NgbTypeahead, { static: false }) |
37 | typeahead: NgbTypeahead; | |
38 | ||
a4b75251 TL |
39 | @Input() hiddenServices: string[] = []; |
40 | ||
41 | @Input() editing = false; | |
42 | ||
43 | @Input() serviceName: string; | |
44 | ||
45 | @Input() serviceType: string; | |
46 | ||
adb31ebb TL |
47 | serviceForm: CdFormGroup; |
48 | action: string; | |
49 | resource: string; | |
50 | serviceTypes: string[] = []; | |
2a845540 | 51 | serviceIds: string[] = []; |
adb31ebb TL |
52 | hosts: any; |
53 | labels: string[]; | |
f67539c2 TL |
54 | labelClick = new Subject<string>(); |
55 | labelFocus = new Subject<string>(); | |
adb31ebb | 56 | pools: Array<object>; |
b3b6e05e | 57 | services: Array<CephServiceSpec> = []; |
a4b75251 | 58 | pageURL: string; |
2a845540 | 59 | serviceList: CephServiceSpec[]; |
adb31ebb | 60 | |
adb31ebb TL |
61 | constructor( |
62 | public actionLabels: ActionLabelsI18n, | |
63 | private cephServiceService: CephServiceService, | |
64 | private formBuilder: CdFormBuilder, | |
65 | private hostService: HostService, | |
adb31ebb TL |
66 | private poolService: PoolService, |
67 | private router: Router, | |
a4b75251 TL |
68 | private taskWrapperService: TaskWrapperService, |
69 | private route: ActivatedRoute, | |
70 | public activeModal: NgbActiveModal | |
adb31ebb | 71 | ) { |
f67539c2 TL |
72 | super(); |
73 | this.resource = $localize`service`; | |
adb31ebb TL |
74 | this.hosts = { |
75 | options: [], | |
f67539c2 TL |
76 | messages: new SelectMessages({ |
77 | empty: $localize`There are no hosts.`, | |
78 | filter: $localize`Filter hosts` | |
79 | }) | |
adb31ebb TL |
80 | }; |
81 | this.createForm(); | |
82 | } | |
83 | ||
84 | createForm() { | |
85 | this.serviceForm = this.formBuilder.group({ | |
86 | // Global | |
87 | service_type: [null, [Validators.required]], | |
88 | service_id: [ | |
89 | null, | |
90 | [ | |
39ae355f TL |
91 | CdValidators.composeIf( |
92 | { | |
93 | service_type: 'mds' | |
94 | }, | |
95 | [ | |
96 | Validators.required, | |
97 | CdValidators.custom('mdsPattern', (value: string) => { | |
98 | if (_.isEmpty(value)) { | |
99 | return false; | |
100 | } | |
101 | return !this.MDS_SVC_ID_PATTERN.test(value); | |
102 | }) | |
103 | ] | |
104 | ), | |
adb31ebb TL |
105 | CdValidators.requiredIf({ |
106 | service_type: 'nfs' | |
107 | }), | |
108 | CdValidators.requiredIf({ | |
109 | service_type: 'iscsi' | |
110 | }), | |
b3b6e05e TL |
111 | CdValidators.requiredIf({ |
112 | service_type: 'ingress' | |
113 | }), | |
adb31ebb TL |
114 | CdValidators.composeIf( |
115 | { | |
116 | service_type: 'rgw' | |
117 | }, | |
118 | [ | |
119 | Validators.required, | |
120 | CdValidators.custom('rgwPattern', (value: string) => { | |
121 | if (_.isEmpty(value)) { | |
122 | return false; | |
123 | } | |
522d829b | 124 | return !this.RGW_SVC_ID_PATTERN.test(value); |
adb31ebb TL |
125 | }) |
126 | ] | |
2a845540 TL |
127 | ), |
128 | CdValidators.custom('uniqueName', (service_id: string) => { | |
129 | return this.serviceIds && this.serviceIds.includes(service_id); | |
130 | }) | |
adb31ebb TL |
131 | ] |
132 | ], | |
133 | placement: ['hosts'], | |
134 | label: [ | |
135 | null, | |
136 | [ | |
137 | CdValidators.requiredIf({ | |
138 | placement: 'label', | |
139 | unmanaged: false | |
140 | }) | |
141 | ] | |
142 | ], | |
143 | hosts: [[]], | |
20effc67 | 144 | count: [null, [CdValidators.number(false)]], |
adb31ebb | 145 | unmanaged: [false], |
a4b75251 | 146 | // iSCSI |
adb31ebb TL |
147 | pool: [ |
148 | null, | |
149 | [ | |
adb31ebb | 150 | CdValidators.requiredIf({ |
2a845540 | 151 | service_type: 'iscsi' |
adb31ebb TL |
152 | }) |
153 | ] | |
154 | ], | |
adb31ebb | 155 | // RGW |
20effc67 | 156 | rgw_frontend_port: [null, [CdValidators.number(false)]], |
adb31ebb TL |
157 | // iSCSI |
158 | trusted_ip_list: [null], | |
20effc67 | 159 | api_port: [null, [CdValidators.number(false)]], |
adb31ebb TL |
160 | api_user: [ |
161 | null, | |
162 | [ | |
163 | CdValidators.requiredIf({ | |
164 | service_type: 'iscsi', | |
165 | unmanaged: false | |
166 | }) | |
167 | ] | |
168 | ], | |
169 | api_password: [ | |
170 | null, | |
171 | [ | |
172 | CdValidators.requiredIf({ | |
173 | service_type: 'iscsi', | |
174 | unmanaged: false | |
175 | }) | |
176 | ] | |
177 | ], | |
b3b6e05e TL |
178 | // Ingress |
179 | backend_service: [ | |
180 | null, | |
181 | [ | |
182 | CdValidators.requiredIf({ | |
2a845540 | 183 | service_type: 'ingress' |
b3b6e05e TL |
184 | }) |
185 | ] | |
186 | ], | |
187 | virtual_ip: [ | |
188 | null, | |
189 | [ | |
190 | CdValidators.requiredIf({ | |
2a845540 TL |
191 | service_type: 'ingress' |
192 | }) | |
193 | ] | |
194 | ], | |
195 | frontend_port: [ | |
196 | null, | |
197 | [ | |
198 | CdValidators.number(false), | |
199 | CdValidators.requiredIf({ | |
200 | service_type: 'ingress' | |
201 | }) | |
202 | ] | |
203 | ], | |
204 | monitor_port: [ | |
205 | null, | |
206 | [ | |
207 | CdValidators.number(false), | |
208 | CdValidators.requiredIf({ | |
209 | service_type: 'ingress' | |
b3b6e05e TL |
210 | }) |
211 | ] | |
212 | ], | |
b3b6e05e TL |
213 | virtual_interface_networks: [null], |
214 | // RGW, Ingress & iSCSI | |
adb31ebb TL |
215 | ssl: [false], |
216 | ssl_cert: [ | |
217 | '', | |
218 | [ | |
219 | CdValidators.composeIf( | |
220 | { | |
221 | service_type: 'rgw', | |
222 | unmanaged: false, | |
223 | ssl: true | |
224 | }, | |
522d829b | 225 | [Validators.required, CdValidators.pemCert()] |
adb31ebb TL |
226 | ), |
227 | CdValidators.composeIf( | |
228 | { | |
229 | service_type: 'iscsi', | |
230 | unmanaged: false, | |
231 | ssl: true | |
232 | }, | |
233 | [Validators.required, CdValidators.sslCert()] | |
33c7a0ef TL |
234 | ), |
235 | CdValidators.composeIf( | |
236 | { | |
237 | service_type: 'ingress', | |
238 | unmanaged: false, | |
239 | ssl: true | |
240 | }, | |
241 | [Validators.required, CdValidators.pemCert()] | |
adb31ebb TL |
242 | ) |
243 | ] | |
244 | ], | |
245 | ssl_key: [ | |
246 | '', | |
247 | [ | |
adb31ebb TL |
248 | CdValidators.composeIf( |
249 | { | |
250 | service_type: 'iscsi', | |
251 | unmanaged: false, | |
252 | ssl: true | |
253 | }, | |
254 | [Validators.required, CdValidators.sslPrivKey()] | |
255 | ) | |
256 | ] | |
20effc67 TL |
257 | ], |
258 | // snmp-gateway | |
259 | snmp_version: [ | |
260 | null, | |
261 | [ | |
262 | CdValidators.requiredIf({ | |
263 | service_type: 'snmp-gateway' | |
264 | }) | |
265 | ] | |
266 | ], | |
267 | snmp_destination: [ | |
268 | null, | |
269 | { | |
270 | validators: [ | |
271 | CdValidators.requiredIf({ | |
272 | service_type: 'snmp-gateway' | |
273 | }), | |
274 | CdValidators.custom('snmpDestinationPattern', (value: string) => { | |
275 | if (_.isEmpty(value)) { | |
276 | return false; | |
277 | } | |
278 | return !this.SNMP_DESTINATION_PATTERN.test(value); | |
279 | }) | |
280 | ] | |
281 | } | |
282 | ], | |
283 | engine_id: [ | |
284 | null, | |
285 | [ | |
286 | CdValidators.requiredIf({ | |
287 | service_type: 'snmp-gateway' | |
288 | }), | |
289 | CdValidators.custom('snmpEngineIdPattern', (value: string) => { | |
290 | if (_.isEmpty(value)) { | |
291 | return false; | |
292 | } | |
293 | return !this.SNMP_ENGINE_ID_PATTERN.test(value); | |
294 | }) | |
295 | ] | |
296 | ], | |
297 | auth_protocol: [ | |
298 | 'SHA', | |
299 | [ | |
300 | CdValidators.requiredIf({ | |
301 | service_type: 'snmp-gateway' | |
302 | }) | |
303 | ] | |
304 | ], | |
305 | privacy_protocol: [null], | |
306 | snmp_community: [ | |
307 | null, | |
308 | [ | |
309 | CdValidators.requiredIf({ | |
310 | snmp_version: 'V2c' | |
311 | }) | |
312 | ] | |
313 | ], | |
314 | snmp_v3_auth_username: [ | |
315 | null, | |
316 | [ | |
317 | CdValidators.requiredIf({ | |
318 | service_type: 'snmp-gateway' | |
319 | }) | |
320 | ] | |
321 | ], | |
322 | snmp_v3_auth_password: [ | |
323 | null, | |
324 | [ | |
325 | CdValidators.requiredIf({ | |
326 | service_type: 'snmp-gateway' | |
327 | }) | |
328 | ] | |
329 | ], | |
330 | snmp_v3_priv_password: [ | |
331 | null, | |
332 | [ | |
333 | CdValidators.requiredIf({ | |
334 | privacy_protocol: { op: '!empty' } | |
335 | }) | |
336 | ] | |
1e59de90 TL |
337 | ], |
338 | grafana_port: [null, [CdValidators.number(false)]], | |
339 | grafana_admin_password: [null] | |
adb31ebb TL |
340 | }); |
341 | } | |
342 | ||
343 | ngOnInit(): void { | |
344 | this.action = this.actionLabels.CREATE; | |
a4b75251 TL |
345 | if (this.router.url.includes('services/(modal:create')) { |
346 | this.pageURL = 'services'; | |
347 | } else if (this.router.url.includes('services/(modal:edit')) { | |
348 | this.editing = true; | |
349 | this.pageURL = 'services'; | |
350 | this.route.params.subscribe((params: { type: string; name: string }) => { | |
351 | this.serviceName = params.name; | |
352 | this.serviceType = params.type; | |
353 | }); | |
354 | } | |
2a845540 | 355 | |
39ae355f TL |
356 | this.cephServiceService |
357 | .list(new HttpParams({ fromObject: { limit: -1, offset: 0 } })) | |
358 | .observable.subscribe((services: CephServiceSpec[]) => { | |
359 | this.serviceList = services; | |
360 | this.services = services.filter((service: any) => | |
361 | this.INGRESS_SUPPORTED_SERVICE_TYPES.includes(service.service_type) | |
362 | ); | |
363 | }); | |
2a845540 | 364 | |
adb31ebb | 365 | this.cephServiceService.getKnownTypes().subscribe((resp: Array<string>) => { |
f67539c2 TL |
366 | // Remove service types: |
367 | // osd - This is deployed a different way. | |
368 | // container - This should only be used in the CLI. | |
a4b75251 TL |
369 | this.hiddenServices.push('osd', 'container'); |
370 | ||
371 | this.serviceTypes = _.difference(resp, this.hiddenServices).sort(); | |
adb31ebb | 372 | }); |
a4b75251 | 373 | this.hostService.list('false').subscribe((resp: object[]) => { |
adb31ebb TL |
374 | const options: SelectOption[] = []; |
375 | _.forEach(resp, (host: object) => { | |
376 | if (_.get(host, 'sources.orchestrator', false)) { | |
377 | const option = new SelectOption(false, _.get(host, 'hostname'), ''); | |
378 | options.push(option); | |
379 | } | |
380 | }); | |
381 | this.hosts.options = [...options]; | |
382 | }); | |
383 | this.hostService.getLabels().subscribe((resp: string[]) => { | |
384 | this.labels = resp; | |
385 | }); | |
386 | this.poolService.getList().subscribe((resp: Array<object>) => { | |
387 | this.pools = resp; | |
388 | }); | |
a4b75251 TL |
389 | |
390 | if (this.editing) { | |
391 | this.action = this.actionLabels.EDIT; | |
392 | this.disableForEditing(this.serviceType); | |
39ae355f TL |
393 | this.cephServiceService |
394 | .list(new HttpParams({ fromObject: { limit: -1, offset: 0 } }), this.serviceName) | |
395 | .observable.subscribe((response: CephServiceSpec[]) => { | |
396 | const formKeys = ['service_type', 'service_id', 'unmanaged']; | |
397 | formKeys.forEach((keys) => { | |
398 | this.serviceForm.get(keys).setValue(response[0][keys]); | |
399 | }); | |
400 | if (!response[0]['unmanaged']) { | |
401 | const placementKey = Object.keys(response[0]['placement'])[0]; | |
402 | let placementValue: string; | |
403 | ['hosts', 'label'].indexOf(placementKey) >= 0 | |
404 | ? (placementValue = placementKey) | |
405 | : (placementValue = 'hosts'); | |
406 | this.serviceForm.get('placement').setValue(placementValue); | |
407 | this.serviceForm.get('count').setValue(response[0]['placement']['count']); | |
408 | if (response[0]?.placement[placementValue]) { | |
409 | this.serviceForm.get(placementValue).setValue(response[0]?.placement[placementValue]); | |
a4b75251 | 410 | } |
39ae355f TL |
411 | } |
412 | switch (this.serviceType) { | |
413 | case 'iscsi': | |
414 | const specKeys = ['pool', 'api_password', 'api_user', 'trusted_ip_list', 'api_port']; | |
415 | specKeys.forEach((key) => { | |
416 | this.serviceForm.get(key).setValue(response[0].spec[key]); | |
417 | }); | |
418 | this.serviceForm.get('ssl').setValue(response[0].spec?.api_secure); | |
419 | if (response[0].spec?.api_secure) { | |
420 | this.serviceForm.get('ssl_cert').setValue(response[0].spec?.ssl_cert); | |
421 | this.serviceForm.get('ssl_key').setValue(response[0].spec?.ssl_key); | |
422 | } | |
423 | break; | |
424 | case 'rgw': | |
a4b75251 | 425 | this.serviceForm |
39ae355f TL |
426 | .get('rgw_frontend_port') |
427 | .setValue(response[0].spec?.rgw_frontend_port); | |
428 | this.serviceForm.get('ssl').setValue(response[0].spec?.ssl); | |
429 | if (response[0].spec?.ssl) { | |
430 | this.serviceForm | |
431 | .get('ssl_cert') | |
432 | .setValue(response[0].spec?.rgw_frontend_ssl_certificate); | |
433 | } | |
434 | break; | |
435 | case 'ingress': | |
436 | const ingressSpecKeys = [ | |
437 | 'backend_service', | |
438 | 'virtual_ip', | |
439 | 'frontend_port', | |
440 | 'monitor_port', | |
441 | 'virtual_interface_networks', | |
442 | 'ssl' | |
20effc67 | 443 | ]; |
39ae355f TL |
444 | ingressSpecKeys.forEach((key) => { |
445 | this.serviceForm.get(key).setValue(response[0].spec[key]); | |
20effc67 | 446 | }); |
39ae355f TL |
447 | if (response[0].spec?.ssl) { |
448 | this.serviceForm.get('ssl_cert').setValue(response[0].spec?.ssl_cert); | |
449 | this.serviceForm.get('ssl_key').setValue(response[0].spec?.ssl_key); | |
450 | } | |
451 | break; | |
452 | case 'snmp-gateway': | |
453 | const snmpCommonSpecKeys = ['snmp_version', 'snmp_destination']; | |
454 | snmpCommonSpecKeys.forEach((key) => { | |
455 | this.serviceForm.get(key).setValue(response[0].spec[key]); | |
456 | }); | |
457 | if (this.serviceForm.getValue('snmp_version') === 'V3') { | |
458 | const snmpV3SpecKeys = [ | |
459 | 'engine_id', | |
460 | 'auth_protocol', | |
461 | 'privacy_protocol', | |
462 | 'snmp_v3_auth_username', | |
463 | 'snmp_v3_auth_password', | |
464 | 'snmp_v3_priv_password' | |
465 | ]; | |
466 | snmpV3SpecKeys.forEach((key) => { | |
467 | if (key !== null) { | |
468 | if ( | |
469 | key === 'snmp_v3_auth_username' || | |
470 | key === 'snmp_v3_auth_password' || | |
471 | key === 'snmp_v3_priv_password' | |
472 | ) { | |
473 | this.serviceForm.get(key).setValue(response[0].spec['credentials'][key]); | |
474 | } else { | |
475 | this.serviceForm.get(key).setValue(response[0].spec[key]); | |
476 | } | |
477 | } | |
478 | }); | |
479 | } else { | |
480 | this.serviceForm | |
481 | .get('snmp_community') | |
482 | .setValue(response[0].spec['credentials']['snmp_community']); | |
483 | } | |
484 | break; | |
1e59de90 TL |
485 | case 'grafana': |
486 | this.serviceForm.get('grafana_port').setValue(response[0].spec.port); | |
487 | this.serviceForm | |
488 | .get('grafana_admin_password') | |
489 | .setValue(response[0].spec.initial_admin_password); | |
490 | break; | |
39ae355f TL |
491 | } |
492 | }); | |
a4b75251 | 493 | } |
adb31ebb TL |
494 | } |
495 | ||
2a845540 TL |
496 | getServiceIds(selectedServiceType: string) { |
497 | this.serviceIds = this.serviceList | |
39ae355f | 498 | ?.filter((service) => service['service_type'] === selectedServiceType) |
2a845540 TL |
499 | .map((service) => service['service_id']); |
500 | } | |
501 | ||
a4b75251 TL |
502 | disableForEditing(serviceType: string) { |
503 | const disableForEditKeys = ['service_type', 'service_id']; | |
504 | disableForEditKeys.forEach((key) => { | |
505 | this.serviceForm.get(key).disable(); | |
506 | }); | |
507 | switch (serviceType) { | |
508 | case 'ingress': | |
509 | this.serviceForm.get('backend_service').disable(); | |
510 | } | |
adb31ebb TL |
511 | } |
512 | ||
f67539c2 TL |
513 | searchLabels = (text$: Observable<string>) => { |
514 | return merge( | |
515 | text$.pipe(debounceTime(200), distinctUntilChanged()), | |
516 | this.labelFocus, | |
517 | this.labelClick.pipe(filter(() => !this.typeahead.isPopupOpen())) | |
518 | ).pipe( | |
519 | map((value) => | |
520 | this.labels | |
521 | .filter((label: string) => label.toLowerCase().indexOf(value.toLowerCase()) > -1) | |
522 | .slice(0, 10) | |
523 | ) | |
524 | ); | |
525 | }; | |
526 | ||
adb31ebb TL |
527 | fileUpload(files: FileList, controlName: string) { |
528 | const file: File = files[0]; | |
529 | const reader = new FileReader(); | |
f67539c2 | 530 | reader.addEventListener('load', (event: ProgressEvent<FileReader>) => { |
adb31ebb | 531 | const control: AbstractControl = this.serviceForm.get(controlName); |
f67539c2 | 532 | control.setValue(event.target.result); |
adb31ebb TL |
533 | control.markAsDirty(); |
534 | control.markAsTouched(); | |
535 | control.updateValueAndValidity(); | |
536 | }); | |
537 | reader.readAsText(file, 'utf8'); | |
538 | } | |
539 | ||
b3b6e05e TL |
540 | prePopulateId() { |
541 | const control: AbstractControl = this.serviceForm.get('service_id'); | |
542 | const backendService = this.serviceForm.getValue('backend_service'); | |
543 | // Set Id as read-only | |
544 | control.reset({ value: backendService, disabled: true }); | |
545 | } | |
546 | ||
adb31ebb TL |
547 | onSubmit() { |
548 | const self = this; | |
a4b75251 | 549 | const values: object = this.serviceForm.getRawValue(); |
adb31ebb | 550 | const serviceType: string = values['service_type']; |
a4b75251 TL |
551 | let taskUrl = `service/${URLVerbs.CREATE}`; |
552 | if (this.editing) { | |
553 | taskUrl = `service/${URLVerbs.EDIT}`; | |
554 | } | |
adb31ebb TL |
555 | const serviceSpec: object = { |
556 | service_type: serviceType, | |
557 | placement: {}, | |
558 | unmanaged: values['unmanaged'] | |
559 | }; | |
522d829b TL |
560 | let svcId: string; |
561 | if (serviceType === 'rgw') { | |
562 | const svcIdMatch = values['service_id'].match(this.RGW_SVC_ID_PATTERN); | |
563 | svcId = svcIdMatch[1]; | |
564 | if (svcIdMatch[3]) { | |
565 | serviceSpec['rgw_realm'] = svcIdMatch[3]; | |
566 | serviceSpec['rgw_zone'] = svcIdMatch[4]; | |
567 | } | |
568 | } else { | |
569 | svcId = values['service_id']; | |
570 | } | |
571 | const serviceId: string = svcId; | |
adb31ebb TL |
572 | let serviceName: string = serviceType; |
573 | if (_.isString(serviceId) && !_.isEmpty(serviceId)) { | |
574 | serviceName = `${serviceType}.${serviceId}`; | |
575 | serviceSpec['service_id'] = serviceId; | |
576 | } | |
2a845540 TL |
577 | |
578 | // These services has some fields to be | |
579 | // filled out even if unmanaged is true | |
580 | switch (serviceType) { | |
581 | case 'ingress': | |
582 | serviceSpec['backend_service'] = values['backend_service']; | |
583 | serviceSpec['service_id'] = values['backend_service']; | |
584 | if (_.isNumber(values['frontend_port']) && values['frontend_port'] > 0) { | |
585 | serviceSpec['frontend_port'] = values['frontend_port']; | |
586 | } | |
587 | if (_.isString(values['virtual_ip']) && !_.isEmpty(values['virtual_ip'])) { | |
588 | serviceSpec['virtual_ip'] = values['virtual_ip'].trim(); | |
589 | } | |
590 | if (_.isNumber(values['monitor_port']) && values['monitor_port'] > 0) { | |
591 | serviceSpec['monitor_port'] = values['monitor_port']; | |
592 | } | |
593 | break; | |
594 | ||
595 | case 'iscsi': | |
596 | serviceSpec['pool'] = values['pool']; | |
597 | break; | |
598 | ||
599 | case 'snmp-gateway': | |
600 | serviceSpec['credentials'] = {}; | |
601 | serviceSpec['snmp_version'] = values['snmp_version']; | |
602 | serviceSpec['snmp_destination'] = values['snmp_destination']; | |
603 | if (values['snmp_version'] === 'V3') { | |
604 | serviceSpec['engine_id'] = values['engine_id']; | |
605 | serviceSpec['auth_protocol'] = values['auth_protocol']; | |
606 | serviceSpec['credentials']['snmp_v3_auth_username'] = values['snmp_v3_auth_username']; | |
607 | serviceSpec['credentials']['snmp_v3_auth_password'] = values['snmp_v3_auth_password']; | |
608 | if (values['privacy_protocol'] !== null) { | |
609 | serviceSpec['privacy_protocol'] = values['privacy_protocol']; | |
610 | serviceSpec['credentials']['snmp_v3_priv_password'] = values['snmp_v3_priv_password']; | |
611 | } | |
612 | } else { | |
613 | serviceSpec['credentials']['snmp_community'] = values['snmp_community']; | |
614 | } | |
615 | break; | |
616 | } | |
617 | ||
adb31ebb TL |
618 | if (!values['unmanaged']) { |
619 | switch (values['placement']) { | |
620 | case 'hosts': | |
621 | if (values['hosts'].length > 0) { | |
622 | serviceSpec['placement']['hosts'] = values['hosts']; | |
623 | } | |
624 | break; | |
625 | case 'label': | |
626 | serviceSpec['placement']['label'] = values['label']; | |
627 | break; | |
628 | } | |
629 | if (_.isNumber(values['count']) && values['count'] > 0) { | |
630 | serviceSpec['placement']['count'] = values['count']; | |
631 | } | |
632 | switch (serviceType) { | |
adb31ebb TL |
633 | case 'rgw': |
634 | if (_.isNumber(values['rgw_frontend_port']) && values['rgw_frontend_port'] > 0) { | |
635 | serviceSpec['rgw_frontend_port'] = values['rgw_frontend_port']; | |
636 | } | |
637 | serviceSpec['ssl'] = values['ssl']; | |
638 | if (values['ssl']) { | |
a4b75251 | 639 | serviceSpec['rgw_frontend_ssl_certificate'] = values['ssl_cert']?.trim(); |
adb31ebb TL |
640 | } |
641 | break; | |
642 | case 'iscsi': | |
adb31ebb | 643 | if (_.isString(values['trusted_ip_list']) && !_.isEmpty(values['trusted_ip_list'])) { |
f67539c2 | 644 | serviceSpec['trusted_ip_list'] = values['trusted_ip_list'].trim(); |
adb31ebb TL |
645 | } |
646 | if (_.isNumber(values['api_port']) && values['api_port'] > 0) { | |
647 | serviceSpec['api_port'] = values['api_port']; | |
648 | } | |
649 | serviceSpec['api_user'] = values['api_user']; | |
650 | serviceSpec['api_password'] = values['api_password']; | |
651 | serviceSpec['api_secure'] = values['ssl']; | |
652 | if (values['ssl']) { | |
a4b75251 TL |
653 | serviceSpec['ssl_cert'] = values['ssl_cert']?.trim(); |
654 | serviceSpec['ssl_key'] = values['ssl_key']?.trim(); | |
adb31ebb TL |
655 | } |
656 | break; | |
b3b6e05e | 657 | case 'ingress': |
b3b6e05e TL |
658 | serviceSpec['ssl'] = values['ssl']; |
659 | if (values['ssl']) { | |
a4b75251 TL |
660 | serviceSpec['ssl_cert'] = values['ssl_cert']?.trim(); |
661 | serviceSpec['ssl_key'] = values['ssl_key']?.trim(); | |
b3b6e05e TL |
662 | } |
663 | serviceSpec['virtual_interface_networks'] = values['virtual_interface_networks']; | |
664 | break; | |
1e59de90 TL |
665 | case 'grafana': |
666 | serviceSpec['port'] = values['grafana_port']; | |
667 | serviceSpec['initial_admin_password'] = values['grafana_admin_password']; | |
adb31ebb TL |
668 | } |
669 | } | |
a4b75251 | 670 | |
adb31ebb TL |
671 | this.taskWrapperService |
672 | .wrapTaskAroundCall({ | |
a4b75251 | 673 | task: new FinishedTask(taskUrl, { |
adb31ebb TL |
674 | service_name: serviceName |
675 | }), | |
2a845540 TL |
676 | call: this.editing |
677 | ? this.cephServiceService.update(serviceSpec) | |
678 | : this.cephServiceService.create(serviceSpec) | |
adb31ebb TL |
679 | }) |
680 | .subscribe({ | |
681 | error() { | |
682 | self.serviceForm.setErrors({ cdSubmitButton: true }); | |
683 | }, | |
a4b75251 TL |
684 | complete: () => { |
685 | this.pageURL === 'services' | |
686 | ? this.router.navigate([this.pageURL, { outlets: { modal: null } }]) | |
687 | : this.activeModal.close(); | |
adb31ebb TL |
688 | } |
689 | }); | |
690 | } | |
20effc67 TL |
691 | |
692 | clearValidations() { | |
693 | const snmpVersion = this.serviceForm.getValue('snmp_version'); | |
694 | const privacyProtocol = this.serviceForm.getValue('privacy_protocol'); | |
695 | if (snmpVersion === 'V3') { | |
696 | this.serviceForm.get('snmp_community').clearValidators(); | |
697 | } else { | |
698 | this.serviceForm.get('engine_id').clearValidators(); | |
699 | this.serviceForm.get('auth_protocol').clearValidators(); | |
700 | this.serviceForm.get('privacy_protocol').clearValidators(); | |
701 | this.serviceForm.get('snmp_v3_auth_username').clearValidators(); | |
702 | this.serviceForm.get('snmp_v3_auth_password').clearValidators(); | |
703 | } | |
704 | if (privacyProtocol === null) { | |
705 | this.serviceForm.get('snmp_v3_priv_password').clearValidators(); | |
706 | } | |
707 | } | |
adb31ebb | 708 | } |