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