1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import * as _ from 'lodash';
7 import { ConfigurationService } from '../../../../shared/api/configuration.service';
8 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
9 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
10 import { CdValidators } from '../../../../shared/forms/cd-validators';
11 import { NotificationService } from '../../../../shared/services/notification.service';
12 import { ConfigFormCreateRequestModel } from './configuration-form-create-request.model';
13 import { ConfigFormModel } from './configuration-form.model';
16 selector: 'cd-configuration-form',
17 templateUrl: './configuration-form.component.html',
18 styleUrls: ['./configuration-form.component.scss']
20 export class ConfigurationFormComponent implements OnInit {
21 configForm: CdFormGroup;
22 response: ConfigFormModel;
25 humanReadableType: string;
28 patternHelpText: string;
29 availSections = ['global', 'mon', 'mgr', 'osd', 'mds', 'client'];
32 private route: ActivatedRoute,
33 private router: Router,
34 private configService: ConfigurationService,
35 private notificationService: NotificationService
41 const formControls = {
42 name: new FormControl({ value: null }),
43 desc: new FormControl({ value: null }),
44 long_desc: new FormControl({ value: null }),
45 values: new FormGroup({}),
46 default: new FormControl({ value: null }),
47 daemon_default: new FormControl({ value: null }),
48 services: new FormControl([])
51 this.availSections.forEach((section) => {
52 formControls.values.addControl(section, new FormControl(null));
55 this.configForm = new CdFormGroup(formControls);
59 this.route.params.subscribe((params: { name: string }) => {
60 const configName = params.name;
61 this.configService.get(configName).subscribe((resp: ConfigFormModel) => {
62 this.setResponse(resp);
67 getType(type: string): any {
72 humanReadable: 'Positive integer value',
74 patternHelpText: 'The entered value needs to be a positive number.',
81 humanReadable: 'Integer value',
82 patternHelpText: 'The entered value needs to be a number.',
89 humanReadable: 'Positive integer value (size)',
91 patternHelpText: 'The entered value needs to be a positive number.',
98 humanReadable: 'Positive integer value (secs)',
100 patternHelpText: 'The entered value needs to be a positive number.',
102 allowsNegative: false
107 humanReadable: 'Decimal value',
108 patternHelpText: 'The entered value needs to be a number or decimal.',
112 { name: 'std::string', inputType: 'text', humanReadable: 'Text', isNumberType: false },
114 name: 'entity_addr_t',
116 humanReadable: 'IPv4 or IPv6 address',
117 patternHelpText: 'The entered value needs to be a valid IP address.',
123 humanReadable: 'UUID',
125 'The entered value is not a valid UUID, e.g.: 67dcac9f-2c03-4d6c-b7bd-1210b3a259a8',
128 { name: 'bool', inputType: 'checkbox', humanReadable: 'Boolean value', isNumberType: false }
131 let currentType = null;
133 knownTypes.forEach((knownType) => {
134 if (knownType.name === type) {
135 currentType = knownType;
139 if (currentType !== null) {
143 throw new Error('Found unknown type "' + type + '" for config option.');
146 getValidators(configOption: any): ValidatorFn[] {
147 const typeParams = this.getType(configOption.type);
148 this.patternHelpText = typeParams.patternHelpText;
150 if (typeParams.isNumberType) {
151 const validators = [];
153 if (configOption.max && configOption.max !== '') {
154 this.maxValue = configOption.max;
155 validators.push(Validators.max(configOption.max));
158 if ('min' in configOption && configOption.min !== '') {
159 this.minValue = configOption.min;
160 validators.push(Validators.min(configOption.min));
161 } else if ('defaultMin' in typeParams) {
162 this.minValue = typeParams.defaultMin;
163 validators.push(Validators.min(typeParams.defaultMin));
166 if (configOption.type === 'double') {
167 validators.push(CdValidators.decimalNumber());
169 validators.push(CdValidators.number(typeParams.allowsNegative));
173 } else if (configOption.type === 'entity_addr_t') {
174 return [CdValidators.ip()];
175 } else if (configOption.type === 'uuid_d') {
176 return [CdValidators.uuid()];
180 getStep(type: string, value: number): number | undefined {
181 const numberTypes = ['uint64_t', 'int64_t', 'size_t', 'secs'];
183 if (numberTypes.includes(type)) {
187 if (type === 'double') {
188 if (value !== null) {
189 const stringVal = value.toString();
190 if (stringVal.indexOf('.') !== -1) {
191 // Value type double and contains decimal characters
192 const decimal = value.toString().split('.');
193 return Math.pow(10, -decimal[1].length);
203 setResponse(response: ConfigFormModel) {
204 this.response = response;
205 const validators = this.getValidators(response);
207 this.configForm.get('name').setValue(response.name);
208 this.configForm.get('desc').setValue(response.desc);
209 this.configForm.get('long_desc').setValue(response.long_desc);
210 this.configForm.get('default').setValue(response.default);
211 this.configForm.get('daemon_default').setValue(response.daemon_default);
212 this.configForm.get('services').setValue(response.services);
214 if (this.response.value) {
215 this.response.value.forEach((value) => {
216 // Check value type. If it's a boolean value we need to convert it because otherwise we
217 // would use the string representation. That would cause issues for e.g. checkboxes.
218 let sectionValue = null;
219 if (value.value === 'true') {
221 } else if (value.value === 'false') {
222 sectionValue = false;
224 sectionValue = value.value;
229 .setValue(sectionValue);
233 this.availSections.forEach((section) => {
237 .setValidators(validators);
240 const currentType = this.getType(response.type);
241 this.type = currentType.name;
242 this.inputType = currentType.inputType;
243 this.humanReadableType = currentType.humanReadable;
246 createRequest(): ConfigFormCreateRequestModel | null {
249 this.availSections.forEach((section) => {
250 const sectionValue = this.configForm.getValue(section);
252 values.push({ section: section, value: sectionValue });
256 if (!_.isEqual(this.response.value, values)) {
257 const request = new ConfigFormCreateRequestModel();
258 request.name = this.configForm.getValue('name');
259 request.value = values;
267 const request = this.createRequest();
270 this.configService.create(request).subscribe(
272 this.notificationService.show(
273 NotificationType.success,
274 'Config option ' + request.name + ' has been updated.',
275 'Update config option'
277 this.router.navigate(['/configuration']);
280 this.configForm.setErrors({ cdSubmitButton: true });
285 this.router.navigate(['/configuration']);