]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
aee978849a7b6eb2d91fd2c70c91c29c83410573
[ceph.git] /
1 import { Component, OnInit, ViewChild } from '@angular/core';
2 import { AbstractControl, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
4
5 import { NgbTypeahead } from '@ng-bootstrap/ng-bootstrap';
6 import _ from 'lodash';
7 import { merge, Observable, Subject } from 'rxjs';
8 import { debounceTime, distinctUntilChanged, filter, map } from 'rxjs/operators';
9
10 import { CephServiceService } from '~/app/shared/api/ceph-service.service';
11 import { HostService } from '~/app/shared/api/host.service';
12 import { PoolService } from '~/app/shared/api/pool.service';
13 import { SelectMessages } from '~/app/shared/components/select/select-messages.model';
14 import { SelectOption } from '~/app/shared/components/select/select-option.model';
15 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
16 import { CdForm } from '~/app/shared/forms/cd-form';
17 import { CdFormBuilder } from '~/app/shared/forms/cd-form-builder';
18 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
19 import { CdValidators } from '~/app/shared/forms/cd-validators';
20 import { FinishedTask } from '~/app/shared/models/finished-task';
21 import { CephServiceSpec } from '~/app/shared/models/service.interface';
22 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
23
24 @Component({
25   selector: 'cd-service-form',
26   templateUrl: './service-form.component.html',
27   styleUrls: ['./service-form.component.scss']
28 })
29 export class ServiceFormComponent extends CdForm implements OnInit {
30   @ViewChild(NgbTypeahead, { static: false })
31   typeahead: NgbTypeahead;
32
33   serviceForm: CdFormGroup;
34   action: string;
35   resource: string;
36   serviceTypes: string[] = [];
37   hosts: any;
38   labels: string[];
39   labelClick = new Subject<string>();
40   labelFocus = new Subject<string>();
41   pools: Array<object>;
42   services: Array<CephServiceSpec> = [];
43
44   constructor(
45     public actionLabels: ActionLabelsI18n,
46     private cephServiceService: CephServiceService,
47     private formBuilder: CdFormBuilder,
48     private hostService: HostService,
49     private poolService: PoolService,
50     private router: Router,
51     private taskWrapperService: TaskWrapperService
52   ) {
53     super();
54     this.resource = $localize`service`;
55     this.hosts = {
56       options: [],
57       messages: new SelectMessages({
58         empty: $localize`There are no hosts.`,
59         filter: $localize`Filter hosts`
60       })
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           }),
81           CdValidators.requiredIf({
82             service_type: 'ingress'
83           }),
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       ],
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
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>) => {
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();
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     });
248     this.cephServiceService.list().subscribe((services: CephServiceSpec[]) => {
249       this.services = services.filter((service: any) => service.service_type === 'rgw');
250     });
251   }
252
253   goToListView() {
254     this.router.navigate(['/services']);
255   }
256
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
271   fileUpload(files: FileList, controlName: string) {
272     const file: File = files[0];
273     const reader = new FileReader();
274     reader.addEventListener('load', (event: ProgressEvent<FileReader>) => {
275       const control: AbstractControl = this.serviceForm.get(controlName);
276       control.setValue(event.target.result);
277       control.markAsDirty();
278       control.markAsTouched();
279       control.updateValueAndValidity();
280     });
281     reader.readAsText(file, 'utf8');
282   }
283
284   onSubmit() {
285     const self = this;
286     const values: object = this.serviceForm.value;
287     const serviceId: string = values['service_id'];
288     const serviceType: string = values['service_type'];
289     const serviceSpec: object = {
290       service_type: serviceType,
291       placement: {},
292       unmanaged: values['unmanaged']
293     };
294     let serviceName: string = serviceType;
295     if (_.isString(serviceId) && !_.isEmpty(serviceId)) {
296       serviceName = `${serviceType}.${serviceId}`;
297       serviceSpec['service_id'] = serviceId;
298     }
299     if (!values['unmanaged']) {
300       switch (values['placement']) {
301         case 'hosts':
302           if (values['hosts'].length > 0) {
303             serviceSpec['placement']['hosts'] = values['hosts'];
304           }
305           break;
306         case 'label':
307           serviceSpec['placement']['label'] = values['label'];
308           break;
309       }
310       if (_.isNumber(values['count']) && values['count'] > 0) {
311         serviceSpec['placement']['count'] = values['count'];
312       }
313       switch (serviceType) {
314         case 'nfs':
315           serviceSpec['pool'] = values['pool'];
316           if (_.isString(values['namespace']) && !_.isEmpty(values['namespace'])) {
317             serviceSpec['namespace'] = values['namespace'];
318           }
319           break;
320         case 'rgw':
321           if (_.isNumber(values['rgw_frontend_port']) && values['rgw_frontend_port'] > 0) {
322             serviceSpec['rgw_frontend_port'] = values['rgw_frontend_port'];
323           }
324           serviceSpec['ssl'] = values['ssl'];
325           if (values['ssl']) {
326             serviceSpec['rgw_frontend_ssl_certificate'] = values['ssl_cert'].trim();
327             serviceSpec['rgw_frontend_ssl_key'] = values['ssl_key'].trim();
328           }
329           break;
330         case 'iscsi':
331           serviceSpec['pool'] = values['pool'];
332           if (_.isString(values['trusted_ip_list']) && !_.isEmpty(values['trusted_ip_list'])) {
333             serviceSpec['trusted_ip_list'] = values['trusted_ip_list'].trim();
334           }
335           if (_.isNumber(values['api_port']) && values['api_port'] > 0) {
336             serviceSpec['api_port'] = values['api_port'];
337           }
338           serviceSpec['api_user'] = values['api_user'];
339           serviceSpec['api_password'] = values['api_password'];
340           serviceSpec['api_secure'] = values['ssl'];
341           if (values['ssl']) {
342             serviceSpec['ssl_cert'] = values['ssl_cert'].trim();
343             serviceSpec['ssl_key'] = values['ssl_key'].trim();
344           }
345           break;
346         case 'ingress':
347           serviceSpec['backend_service'] = values['backend_service'];
348           if (_.isString(values['virtual_ip']) && !_.isEmpty(values['virtual_ip'])) {
349             serviceSpec['virtual_ip'] = values['virtual_ip'].trim();
350           }
351           if (_.isNumber(values['frontend_port']) && values['frontend_port'] > 0) {
352             serviceSpec['frontend_port'] = values['frontend_port'];
353           }
354           if (_.isNumber(values['monitor_port']) && values['monitor_port'] > 0) {
355             serviceSpec['monitor_port'] = values['monitor_port'];
356           }
357           serviceSpec['ssl'] = values['ssl'];
358           if (values['ssl']) {
359             serviceSpec['ssl_cert'] = values['ssl_cert'].trim();
360             serviceSpec['ssl_key'] = values['ssl_key'].trim();
361           }
362           serviceSpec['virtual_interface_networks'] = values['virtual_interface_networks'];
363           break;
364       }
365     }
366     this.taskWrapperService
367       .wrapTaskAroundCall({
368         task: new FinishedTask(`service/${URLVerbs.CREATE}`, {
369           service_name: serviceName
370         }),
371         call: this.cephServiceService.create(serviceSpec)
372       })
373       .subscribe({
374         error() {
375           self.serviceForm.setErrors({ cdSubmitButton: true });
376         },
377         complete() {
378           self.goToListView();
379         }
380       });
381   }
382 }