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 '../../../../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 { CdForm } from '../../../../shared/forms/cd-form';
17 import { CdFormBuilder } from '../../../../shared/forms/cd-form-builder';
18 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
19 import { CdValidators } from '../../../../shared/forms/cd-validators';
20 import { FinishedTask } from '../../../../shared/models/finished-task';
21 import { TaskWrapperService } from '../../../../shared/services/task-wrapper.service';
24 selector: 'cd-service-form',
25 templateUrl: './service-form.component.html',
26 styleUrls: ['./service-form.component.scss']
28 export class ServiceFormComponent extends CdForm implements OnInit {
29 @ViewChild(NgbTypeahead, { static: false })
30 typeahead: NgbTypeahead;
32 serviceForm: CdFormGroup;
35 serviceTypes: string[] = [];
38 labelClick = new Subject<string>();
39 labelFocus = new Subject<string>();
43 public actionLabels: ActionLabelsI18n,
44 private cephServiceService: CephServiceService,
45 private formBuilder: CdFormBuilder,
46 private hostService: HostService,
47 private poolService: PoolService,
48 private router: Router,
49 private taskWrapperService: TaskWrapperService
52 this.resource = $localize`service`;
55 messages: new SelectMessages({
56 empty: $localize`There are no hosts.`,
57 filter: $localize`Filter hosts`
64 this.serviceForm = this.formBuilder.group({
66 service_type: [null, [Validators.required]],
70 CdValidators.requiredIf({
73 CdValidators.requiredIf({
76 CdValidators.requiredIf({
79 CdValidators.composeIf(
85 CdValidators.custom('rgwPattern', (value: string) => {
86 if (_.isEmpty(value)) {
89 return !/^[^.]+\.[^.]+(\.[^.]+)?$/.test(value);
99 CdValidators.requiredIf({
106 count: [null, [CdValidators.number(false), Validators.min(1)]],
112 CdValidators.requiredIf({
116 CdValidators.requiredIf({
117 service_type: 'iscsi',
127 [CdValidators.number(false), Validators.min(1), Validators.max(65535)]
130 trusted_ip_list: [null],
131 api_port: [null, [CdValidators.number(false), Validators.min(1), Validators.max(65535)]],
135 CdValidators.requiredIf({
136 service_type: 'iscsi',
144 CdValidators.requiredIf({
145 service_type: 'iscsi',
155 CdValidators.composeIf(
161 [Validators.required, CdValidators.sslCert()]
163 CdValidators.composeIf(
165 service_type: 'iscsi',
169 [Validators.required, CdValidators.sslCert()]
176 CdValidators.composeIf(
182 [Validators.required, CdValidators.sslPrivKey()]
184 CdValidators.composeIf(
186 service_type: 'iscsi',
190 [Validators.required, CdValidators.sslPrivKey()]
198 this.action = this.actionLabels.CREATE;
199 this.cephServiceService.getKnownTypes().subscribe((resp: Array<string>) => {
200 // Remove service type 'osd', this is deployed a different way.
201 this.serviceTypes = _.difference(resp, ['osd']).sort();
203 this.hostService.list().subscribe((resp: object[]) => {
204 const options: SelectOption[] = [];
205 _.forEach(resp, (host: object) => {
206 if (_.get(host, 'sources.orchestrator', false)) {
207 const option = new SelectOption(false, _.get(host, 'hostname'), '');
208 options.push(option);
211 this.hosts.options = [...options];
213 this.hostService.getLabels().subscribe((resp: string[]) => {
216 this.poolService.getList().subscribe((resp: Array<object>) => {
222 this.router.navigate(['/services']);
225 searchLabels = (text$: Observable<string>) => {
227 text$.pipe(debounceTime(200), distinctUntilChanged()),
229 this.labelClick.pipe(filter(() => !this.typeahead.isPopupOpen()))
233 .filter((label: string) => label.toLowerCase().indexOf(value.toLowerCase()) > -1)
239 fileUpload(files: FileList, controlName: string) {
240 const file: File = files[0];
241 const reader = new FileReader();
242 reader.addEventListener('load', (event: ProgressEvent<FileReader>) => {
243 const control: AbstractControl = this.serviceForm.get(controlName);
244 control.setValue(event.target.result);
245 control.markAsDirty();
246 control.markAsTouched();
247 control.updateValueAndValidity();
249 reader.readAsText(file, 'utf8');
254 const values: object = this.serviceForm.value;
255 const serviceId: string = values['service_id'];
256 const serviceType: string = values['service_type'];
257 const serviceSpec: object = {
258 service_type: serviceType,
260 unmanaged: values['unmanaged']
262 let serviceName: string = serviceType;
263 if (_.isString(serviceId) && !_.isEmpty(serviceId)) {
264 serviceName = `${serviceType}.${serviceId}`;
265 serviceSpec['service_id'] = serviceId;
267 if (!values['unmanaged']) {
268 switch (values['placement']) {
270 if (values['hosts'].length > 0) {
271 serviceSpec['placement']['hosts'] = values['hosts'];
275 serviceSpec['placement']['label'] = values['label'];
278 if (_.isNumber(values['count']) && values['count'] > 0) {
279 serviceSpec['placement']['count'] = values['count'];
281 switch (serviceType) {
283 serviceSpec['pool'] = values['pool'];
284 if (_.isString(values['namespace']) && !_.isEmpty(values['namespace'])) {
285 serviceSpec['namespace'] = values['namespace'];
289 if (_.isNumber(values['rgw_frontend_port']) && values['rgw_frontend_port'] > 0) {
290 serviceSpec['rgw_frontend_port'] = values['rgw_frontend_port'];
292 serviceSpec['ssl'] = values['ssl'];
294 serviceSpec['rgw_frontend_ssl_certificate'] = values['ssl_cert'].trim();
295 serviceSpec['rgw_frontend_ssl_key'] = values['ssl_key'].trim();
299 serviceSpec['pool'] = values['pool'];
300 if (_.isString(values['trusted_ip_list']) && !_.isEmpty(values['trusted_ip_list'])) {
301 let parts = _.split(values['trusted_ip_list'], ',');
302 parts = _.map(parts, _.trim);
303 serviceSpec['trusted_ip_list'] = parts;
305 if (_.isNumber(values['api_port']) && values['api_port'] > 0) {
306 serviceSpec['api_port'] = values['api_port'];
308 serviceSpec['api_user'] = values['api_user'];
309 serviceSpec['api_password'] = values['api_password'];
310 serviceSpec['api_secure'] = values['ssl'];
312 serviceSpec['ssl_cert'] = values['ssl_cert'].trim();
313 serviceSpec['ssl_key'] = values['ssl_key'].trim();
318 this.taskWrapperService
319 .wrapTaskAroundCall({
320 task: new FinishedTask(`service/${URLVerbs.CREATE}`, {
321 service_name: serviceName
323 call: this.cephServiceService.create(serviceSpec)
327 self.serviceForm.setErrors({ cdSubmitButton: true });