]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
aa9ab7b839cac45e0f6a924ccce5f1628773cd45
[ceph-ci.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, FormGroup, ValidatorFn } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
4
5 import * as _ from 'lodash';
6
7 import { ConfigurationService } from '../../../../shared/api/configuration.service';
8 import { ConfigFormModel } from '../../../../shared/components/config-option/config-option.model';
9 import { ConfigOptionTypes } from '../../../../shared/components/config-option/config-option.types';
10 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
11 import { CdForm } from '../../../../shared/forms/cd-form';
12 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
13 import { NotificationService } from '../../../../shared/services/notification.service';
14 import { ConfigFormCreateRequestModel } from './configuration-form-create-request.model';
15
16 @Component({
17   selector: 'cd-configuration-form',
18   templateUrl: './configuration-form.component.html',
19   styleUrls: ['./configuration-form.component.scss']
20 })
21 export class ConfigurationFormComponent extends CdForm implements OnInit {
22   configForm: CdFormGroup;
23   response: ConfigFormModel;
24   type: string;
25   inputType: string;
26   humanReadableType: string;
27   minValue: number;
28   maxValue: number;
29   patternHelpText: string;
30   availSections = ['global', 'mon', 'mgr', 'osd', 'mds', 'client'];
31
32   constructor(
33     private route: ActivatedRoute,
34     private router: Router,
35     private configService: ConfigurationService,
36     private notificationService: NotificationService
37   ) {
38     super();
39     this.createForm();
40   }
41
42   createForm() {
43     const formControls = {
44       name: new FormControl({ value: null }),
45       desc: new FormControl({ value: null }),
46       long_desc: new FormControl({ value: null }),
47       values: new FormGroup({}),
48       default: new FormControl({ value: null }),
49       daemon_default: new FormControl({ value: null }),
50       services: new FormControl([])
51     };
52
53     this.availSections.forEach((section) => {
54       formControls.values.addControl(section, new FormControl(null));
55     });
56
57     this.configForm = new CdFormGroup(formControls);
58   }
59
60   ngOnInit() {
61     this.route.params.subscribe((params: { name: string }) => {
62       const configName = params.name;
63       this.configService.get(configName).subscribe((resp: ConfigFormModel) => {
64         this.setResponse(resp);
65         this.loadingReady();
66       });
67     });
68   }
69
70   getValidators(configOption: any): ValidatorFn[] {
71     const typeValidators = ConfigOptionTypes.getTypeValidators(configOption);
72     if (typeValidators) {
73       this.patternHelpText = typeValidators.patternHelpText;
74
75       if ('max' in typeValidators && typeValidators.max !== '') {
76         this.maxValue = typeValidators.max;
77       }
78
79       if ('min' in typeValidators && typeValidators.min !== '') {
80         this.minValue = typeValidators.min;
81       }
82
83       return typeValidators.validators;
84     }
85
86     return undefined;
87   }
88
89   getStep(type: string, value: number): number | undefined {
90     return ConfigOptionTypes.getTypeStep(type, value);
91   }
92
93   setResponse(response: ConfigFormModel) {
94     this.response = response;
95     const validators = this.getValidators(response);
96
97     this.configForm.get('name').setValue(response.name);
98     this.configForm.get('desc').setValue(response.desc);
99     this.configForm.get('long_desc').setValue(response.long_desc);
100     this.configForm.get('default').setValue(response.default);
101     this.configForm.get('daemon_default').setValue(response.daemon_default);
102     this.configForm.get('services').setValue(response.services);
103
104     if (this.response.value) {
105       this.response.value.forEach((value) => {
106         // Check value type. If it's a boolean value we need to convert it because otherwise we
107         // would use the string representation. That would cause issues for e.g. checkboxes.
108         let sectionValue = null;
109         if (value.value === 'true') {
110           sectionValue = true;
111         } else if (value.value === 'false') {
112           sectionValue = false;
113         } else {
114           sectionValue = value.value;
115         }
116         this.configForm.get('values').get(value.section).setValue(sectionValue);
117       });
118     }
119
120     this.availSections.forEach((section) => {
121       this.configForm.get('values').get(section).setValidators(validators);
122     });
123
124     const currentType = ConfigOptionTypes.getType(response.type);
125     this.type = currentType.name;
126     this.inputType = currentType.inputType;
127     this.humanReadableType = currentType.humanReadable;
128   }
129
130   createRequest(): ConfigFormCreateRequestModel | null {
131     const values: any[] = [];
132
133     this.availSections.forEach((section) => {
134       const sectionValue = this.configForm.getValue(section);
135       if (sectionValue !== null && sectionValue !== '') {
136         values.push({ section: section, value: sectionValue });
137       }
138     });
139
140     if (!_.isEqual(this.response.value, values)) {
141       const request = new ConfigFormCreateRequestModel();
142       request.name = this.configForm.getValue('name');
143       request.value = values;
144       return request;
145     }
146
147     return null;
148   }
149
150   submit() {
151     const request = this.createRequest();
152
153     if (request) {
154       this.configService.create(request).subscribe(
155         () => {
156           this.notificationService.show(
157             NotificationType.success,
158             $localize`Updated config option ${request.name}`
159           );
160           this.router.navigate(['/configuration']);
161         },
162         () => {
163           this.configForm.setErrors({ cdSubmitButton: true });
164         }
165       );
166     }
167
168     this.router.navigate(['/configuration']);
169   }
170 }