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 { TaskWrapperService } from '~/app/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 types:
201 // osd - This is deployed a different way.
202 // container - This should only be used in the CLI.
203 this.serviceTypes = _.difference(resp, ['container', 'osd']).sort();
205 this.hostService.list().subscribe((resp: object[]) => {
206 const options: SelectOption[] = [];
207 _.forEach(resp, (host: object) => {
208 if (_.get(host, 'sources.orchestrator', false)) {
209 const option = new SelectOption(false, _.get(host, 'hostname'), '');
210 options.push(option);
213 this.hosts.options = [...options];
215 this.hostService.getLabels().subscribe((resp: string[]) => {
218 this.poolService.getList().subscribe((resp: Array<object>) => {
224 this.router.navigate(['/services']);
227 searchLabels = (text$: Observable<string>) => {
229 text$.pipe(debounceTime(200), distinctUntilChanged()),
231 this.labelClick.pipe(filter(() => !this.typeahead.isPopupOpen()))
235 .filter((label: string) => label.toLowerCase().indexOf(value.toLowerCase()) > -1)
241 fileUpload(files: FileList, controlName: string) {
242 const file: File = files[0];
243 const reader = new FileReader();
244 reader.addEventListener('load', (event: ProgressEvent<FileReader>) => {
245 const control: AbstractControl = this.serviceForm.get(controlName);
246 control.setValue(event.target.result);
247 control.markAsDirty();
248 control.markAsTouched();
249 control.updateValueAndValidity();
251 reader.readAsText(file, 'utf8');
256 const values: object = this.serviceForm.value;
257 const serviceId: string = values['service_id'];
258 const serviceType: string = values['service_type'];
259 const serviceSpec: object = {
260 service_type: serviceType,
262 unmanaged: values['unmanaged']
264 let serviceName: string = serviceType;
265 if (_.isString(serviceId) && !_.isEmpty(serviceId)) {
266 serviceName = `${serviceType}.${serviceId}`;
267 serviceSpec['service_id'] = serviceId;
269 if (!values['unmanaged']) {
270 switch (values['placement']) {
272 if (values['hosts'].length > 0) {
273 serviceSpec['placement']['hosts'] = values['hosts'];
277 serviceSpec['placement']['label'] = values['label'];
280 if (_.isNumber(values['count']) && values['count'] > 0) {
281 serviceSpec['placement']['count'] = values['count'];
283 switch (serviceType) {
285 serviceSpec['pool'] = values['pool'];
286 if (_.isString(values['namespace']) && !_.isEmpty(values['namespace'])) {
287 serviceSpec['namespace'] = values['namespace'];
291 if (_.isNumber(values['rgw_frontend_port']) && values['rgw_frontend_port'] > 0) {
292 serviceSpec['rgw_frontend_port'] = values['rgw_frontend_port'];
294 serviceSpec['ssl'] = values['ssl'];
296 serviceSpec['rgw_frontend_ssl_certificate'] = values['ssl_cert'].trim();
297 serviceSpec['rgw_frontend_ssl_key'] = values['ssl_key'].trim();
301 serviceSpec['pool'] = values['pool'];
302 if (_.isString(values['trusted_ip_list']) && !_.isEmpty(values['trusted_ip_list'])) {
303 serviceSpec['trusted_ip_list'] = values['trusted_ip_list'].trim();
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 });