1 import { Component, OnInit, ViewChild } from '@angular/core';
2 import { AbstractControl, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
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';
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';
25 selector: 'cd-service-form',
26 templateUrl: './service-form.component.html',
27 styleUrls: ['./service-form.component.scss']
29 export class ServiceFormComponent extends CdForm implements OnInit {
30 @ViewChild(NgbTypeahead, { static: false })
31 typeahead: NgbTypeahead;
33 serviceForm: CdFormGroup;
36 serviceTypes: string[] = [];
39 labelClick = new Subject<string>();
40 labelFocus = new Subject<string>();
42 services: Array<CephServiceSpec> = [];
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
54 this.resource = $localize`service`;
57 messages: new SelectMessages({
58 empty: $localize`There are no hosts.`,
59 filter: $localize`Filter hosts`
66 this.serviceForm = this.formBuilder.group({
68 service_type: [null, [Validators.required]],
72 CdValidators.requiredIf({
75 CdValidators.requiredIf({
78 CdValidators.requiredIf({
81 CdValidators.requiredIf({
82 service_type: 'ingress'
84 CdValidators.composeIf(
90 CdValidators.custom('rgwPattern', (value: string) => {
91 if (_.isEmpty(value)) {
94 return !/^[^.]+\.[^.]+(\.[^.]+)?$/.test(value);
100 placement: ['hosts'],
104 CdValidators.requiredIf({
111 count: [null, [CdValidators.number(false), Validators.min(1)]],
117 CdValidators.requiredIf({
121 CdValidators.requiredIf({
122 service_type: 'iscsi',
132 [CdValidators.number(false), Validators.min(1), Validators.max(65535)]
135 trusted_ip_list: [null],
136 api_port: [null, [CdValidators.number(false), Validators.min(1), Validators.max(65535)]],
140 CdValidators.requiredIf({
141 service_type: 'iscsi',
149 CdValidators.requiredIf({
150 service_type: 'iscsi',
159 CdValidators.requiredIf({
160 service_type: 'ingress',
168 CdValidators.requiredIf({
169 service_type: 'ingress',
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
182 CdValidators.composeIf(
188 [Validators.required, CdValidators.sslCert()]
190 CdValidators.composeIf(
192 service_type: 'iscsi',
196 [Validators.required, CdValidators.sslCert()]
203 CdValidators.composeIf(
209 [Validators.required, CdValidators.sslPrivKey()]
211 CdValidators.composeIf(
213 service_type: 'iscsi',
217 [Validators.required, CdValidators.sslPrivKey()]
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();
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);
240 this.hosts.options = [...options];
242 this.hostService.getLabels().subscribe((resp: string[]) => {
245 this.poolService.getList().subscribe((resp: Array<object>) => {
248 this.cephServiceService.list().subscribe((services: CephServiceSpec[]) => {
249 this.services = services.filter((service: any) => service.service_type === 'rgw');
254 this.router.navigate(['/services']);
257 searchLabels = (text$: Observable<string>) => {
259 text$.pipe(debounceTime(200), distinctUntilChanged()),
261 this.labelClick.pipe(filter(() => !this.typeahead.isPopupOpen()))
265 .filter((label: string) => label.toLowerCase().indexOf(value.toLowerCase()) > -1)
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();
281 reader.readAsText(file, 'utf8');
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 });
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,
299 unmanaged: values['unmanaged']
301 let serviceName: string = serviceType;
302 if (_.isString(serviceId) && !_.isEmpty(serviceId)) {
303 serviceName = `${serviceType}.${serviceId}`;
304 serviceSpec['service_id'] = serviceId;
306 if (!values['unmanaged']) {
307 switch (values['placement']) {
309 if (values['hosts'].length > 0) {
310 serviceSpec['placement']['hosts'] = values['hosts'];
314 serviceSpec['placement']['label'] = values['label'];
317 if (_.isNumber(values['count']) && values['count'] > 0) {
318 serviceSpec['placement']['count'] = values['count'];
320 switch (serviceType) {
322 serviceSpec['pool'] = values['pool'];
323 if (_.isString(values['namespace']) && !_.isEmpty(values['namespace'])) {
324 serviceSpec['namespace'] = values['namespace'];
328 if (_.isNumber(values['rgw_frontend_port']) && values['rgw_frontend_port'] > 0) {
329 serviceSpec['rgw_frontend_port'] = values['rgw_frontend_port'];
331 serviceSpec['ssl'] = values['ssl'];
333 serviceSpec['rgw_frontend_ssl_certificate'] = values['ssl_cert'].trim();
334 serviceSpec['rgw_frontend_ssl_key'] = values['ssl_key'].trim();
338 serviceSpec['pool'] = values['pool'];
339 if (_.isString(values['trusted_ip_list']) && !_.isEmpty(values['trusted_ip_list'])) {
340 serviceSpec['trusted_ip_list'] = values['trusted_ip_list'].trim();
342 if (_.isNumber(values['api_port']) && values['api_port'] > 0) {
343 serviceSpec['api_port'] = values['api_port'];
345 serviceSpec['api_user'] = values['api_user'];
346 serviceSpec['api_password'] = values['api_password'];
347 serviceSpec['api_secure'] = values['ssl'];
349 serviceSpec['ssl_cert'] = values['ssl_cert'].trim();
350 serviceSpec['ssl_key'] = values['ssl_key'].trim();
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();
359 if (_.isNumber(values['frontend_port']) && values['frontend_port'] > 0) {
360 serviceSpec['frontend_port'] = values['frontend_port'];
362 if (_.isNumber(values['monitor_port']) && values['monitor_port'] > 0) {
363 serviceSpec['monitor_port'] = values['monitor_port'];
365 serviceSpec['ssl'] = values['ssl'];
367 serviceSpec['ssl_cert'] = values['ssl_cert'].trim();
368 serviceSpec['ssl_key'] = values['ssl_key'].trim();
370 serviceSpec['virtual_interface_networks'] = values['virtual_interface_networks'];
374 this.taskWrapperService
375 .wrapTaskAroundCall({
376 task: new FinishedTask(`service/${URLVerbs.CREATE}`, {
377 service_name: serviceName
379 call: this.cephServiceService.create(serviceSpec)
383 self.serviceForm.setErrors({ cdSubmitButton: true });