1 import { Component, OnInit } from '@angular/core';
2 import { AbstractControl, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
5 import { I18n } from '@ngx-translate/i18n-polyfill';
6 import * as _ from 'lodash';
7 import { Observable } from 'rxjs';
8 import { 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 { CdFormBuilder } from '../../../../shared/forms/cd-form-builder';
17 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
18 import { CdValidators } from '../../../../shared/forms/cd-validators';
19 import { FinishedTask } from '../../../../shared/models/finished-task';
20 import { TaskWrapperService } from '../../../../shared/services/task-wrapper.service';
23 selector: 'cd-service-form',
24 templateUrl: './service-form.component.html',
25 styleUrls: ['./service-form.component.scss']
27 export class ServiceFormComponent implements OnInit {
28 serviceForm: CdFormGroup;
31 serviceTypes: string[] = [];
36 searchLabels: Observable<any> = new Observable((observer: any) => {
37 observer.next(this.serviceForm.getValue('label'));
39 map((value: string) => {
40 const result = this.labels
41 .filter((label: string) => label.toLowerCase().indexOf(value.toLowerCase()) > -1)
48 public actionLabels: ActionLabelsI18n,
49 private cephServiceService: CephServiceService,
50 private formBuilder: CdFormBuilder,
51 private hostService: HostService,
53 private poolService: PoolService,
54 private router: Router,
55 private taskWrapperService: TaskWrapperService
57 this.resource = this.i18n(`service`);
60 messages: new SelectMessages(
62 empty: this.i18n(`There are no hosts.`),
63 filter: this.i18n(`Filter hosts`)
72 this.serviceForm = this.formBuilder.group({
74 service_type: [null, [Validators.required]],
78 CdValidators.requiredIf({
81 CdValidators.requiredIf({
84 CdValidators.requiredIf({
87 CdValidators.composeIf(
93 CdValidators.custom('rgwPattern', (value: string) => {
94 if (_.isEmpty(value)) {
97 return !/^[^.]+\.[^.]+(\.[^.]+)?$/.test(value);
103 placement: ['hosts'],
107 CdValidators.requiredIf({
114 count: [null, [CdValidators.number(false), Validators.min(1)]],
120 CdValidators.requiredIf({
124 CdValidators.requiredIf({
125 service_type: 'iscsi',
135 [CdValidators.number(false), Validators.min(1), Validators.max(65535)]
138 trusted_ip_list: [null],
139 api_port: [null, [CdValidators.number(false), Validators.min(1), Validators.max(65535)]],
143 CdValidators.requiredIf({
144 service_type: 'iscsi',
152 CdValidators.requiredIf({
153 service_type: 'iscsi',
163 CdValidators.composeIf(
169 [Validators.required, CdValidators.sslCert()]
171 CdValidators.composeIf(
173 service_type: 'iscsi',
177 [Validators.required, CdValidators.sslCert()]
184 CdValidators.composeIf(
190 [Validators.required, CdValidators.sslPrivKey()]
192 CdValidators.composeIf(
194 service_type: 'iscsi',
198 [Validators.required, CdValidators.sslPrivKey()]
206 this.action = this.actionLabels.CREATE;
207 this.cephServiceService.getKnownTypes().subscribe((resp: Array<string>) => {
208 // Remove service type 'osd', this is deployed a different way.
209 this.serviceTypes = _.difference(resp, ['osd']).sort();
211 this.hostService.list().subscribe((resp: object[]) => {
212 const options: SelectOption[] = [];
213 _.forEach(resp, (host: object) => {
214 if (_.get(host, 'sources.orchestrator', false)) {
215 const option = new SelectOption(false, _.get(host, 'hostname'), '');
216 options.push(option);
219 this.hosts.options = [...options];
221 this.hostService.getLabels().subscribe((resp: string[]) => {
224 this.poolService.getList().subscribe((resp: Array<object>) => {
230 this.router.navigate(['/services']);
233 fileUpload(files: FileList, controlName: string) {
234 const file: File = files[0];
235 const reader = new FileReader();
236 reader.addEventListener('load', (event: ProgressEvent) => {
237 const control: AbstractControl = this.serviceForm.get(controlName);
238 control.setValue((event.target as FileReader).result);
239 control.markAsDirty();
240 control.markAsTouched();
241 control.updateValueAndValidity();
243 reader.readAsText(file, 'utf8');
248 const values: object = this.serviceForm.value;
249 const serviceId: string = values['service_id'];
250 const serviceType: string = values['service_type'];
251 const serviceSpec: object = {
252 service_type: serviceType,
254 unmanaged: values['unmanaged']
256 let serviceName: string = serviceType;
257 if (_.isString(serviceId) && !_.isEmpty(serviceId)) {
258 serviceName = `${serviceType}.${serviceId}`;
259 serviceSpec['service_id'] = serviceId;
261 if (!values['unmanaged']) {
262 switch (values['placement']) {
264 if (values['hosts'].length > 0) {
265 serviceSpec['placement']['hosts'] = values['hosts'];
269 serviceSpec['placement']['label'] = values['label'];
272 if (_.isNumber(values['count']) && values['count'] > 0) {
273 serviceSpec['placement']['count'] = values['count'];
275 switch (serviceType) {
277 serviceSpec['pool'] = values['pool'];
278 if (_.isString(values['namespace']) && !_.isEmpty(values['namespace'])) {
279 serviceSpec['namespace'] = values['namespace'];
283 if (_.isNumber(values['rgw_frontend_port']) && values['rgw_frontend_port'] > 0) {
284 serviceSpec['rgw_frontend_port'] = values['rgw_frontend_port'];
286 serviceSpec['ssl'] = values['ssl'];
288 serviceSpec['rgw_frontend_ssl_certificate'] = values['ssl_cert'].trim();
289 serviceSpec['rgw_frontend_ssl_key'] = values['ssl_key'].trim();
293 serviceSpec['pool'] = values['pool'];
294 if (_.isString(values['trusted_ip_list']) && !_.isEmpty(values['trusted_ip_list'])) {
295 let parts = _.split(values['trusted_ip_list'], ',');
296 parts = _.map(parts, _.trim);
297 serviceSpec['trusted_ip_list'] = parts;
299 if (_.isNumber(values['api_port']) && values['api_port'] > 0) {
300 serviceSpec['api_port'] = values['api_port'];
302 serviceSpec['api_user'] = values['api_user'];
303 serviceSpec['api_password'] = values['api_password'];
304 serviceSpec['api_secure'] = values['ssl'];
306 serviceSpec['ssl_cert'] = values['ssl_cert'].trim();
307 serviceSpec['ssl_key'] = values['ssl_key'].trim();
312 this.taskWrapperService
313 .wrapTaskAroundCall({
314 task: new FinishedTask(`service/${URLVerbs.CREATE}`, {
315 service_name: serviceName
317 call: this.cephServiceService.create(serviceSpec)
321 self.serviceForm.setErrors({ cdSubmitButton: true });