]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/configuration/configuration-form/configuration-form.component.ts
import ceph nautilus 14.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / cluster / configuration / configuration-form / configuration-form.component.ts
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 { I18n } from '@ngx-translate/i18n-polyfill';
6 import * as _ from 'lodash';
7
8 import { ConfigurationService } from '../../../../shared/api/configuration.service';
9 import { ConfigFormModel } from '../../../../shared/components/config-option/config-option.model';
10 import { ConfigOptionTypes } from '../../../../shared/components/config-option/config-option.types';
11 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
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 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 private i18n: I18n
38 ) {
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 });
66 });
67 }
68
69 getValidators(configOption: any): ValidatorFn[] {
70 const typeValidators = ConfigOptionTypes.getTypeValidators(configOption);
71 if (typeValidators) {
72 this.patternHelpText = typeValidators.patternHelpText;
73
74 if ('max' in typeValidators && typeValidators.max !== '') {
75 this.maxValue = typeValidators.max;
76 }
77
78 if ('min' in typeValidators && typeValidators.min !== '') {
79 this.minValue = typeValidators.min;
80 }
81
82 return typeValidators.validators;
83 }
84 }
85
86 getStep(type: string, value: number): number | undefined {
87 return ConfigOptionTypes.getTypeStep(type, value);
88 }
89
90 setResponse(response: ConfigFormModel) {
91 this.response = response;
92 const validators = this.getValidators(response);
93
94 this.configForm.get('name').setValue(response.name);
95 this.configForm.get('desc').setValue(response.desc);
96 this.configForm.get('long_desc').setValue(response.long_desc);
97 this.configForm.get('default').setValue(response.default);
98 this.configForm.get('daemon_default').setValue(response.daemon_default);
99 this.configForm.get('services').setValue(response.services);
100
101 if (this.response.value) {
102 this.response.value.forEach((value) => {
103 // Check value type. If it's a boolean value we need to convert it because otherwise we
104 // would use the string representation. That would cause issues for e.g. checkboxes.
105 let sectionValue = null;
106 if (value.value === 'true') {
107 sectionValue = true;
108 } else if (value.value === 'false') {
109 sectionValue = false;
110 } else {
111 sectionValue = value.value;
112 }
113 this.configForm
114 .get('values')
115 .get(value.section)
116 .setValue(sectionValue);
117 });
118 }
119
120 this.availSections.forEach((section) => {
121 this.configForm
122 .get('values')
123 .get(section)
124 .setValidators(validators);
125 });
126
127 const currentType = ConfigOptionTypes.getType(response.type);
128 this.type = currentType.name;
129 this.inputType = currentType.inputType;
130 this.humanReadableType = currentType.humanReadable;
131 }
132
133 createRequest(): ConfigFormCreateRequestModel | null {
134 const values = [];
135
136 this.availSections.forEach((section) => {
137 const sectionValue = this.configForm.getValue(section);
138 if (sectionValue) {
139 values.push({ section: section, value: sectionValue });
140 }
141 });
142
143 if (!_.isEqual(this.response.value, values)) {
144 const request = new ConfigFormCreateRequestModel();
145 request.name = this.configForm.getValue('name');
146 request.value = values;
147 return request;
148 }
149
150 return null;
151 }
152
153 submit() {
154 const request = this.createRequest();
155
156 if (request) {
157 this.configService.create(request).subscribe(
158 () => {
159 this.notificationService.show(
160 NotificationType.success,
161 this.i18n('Updated config option {{name}}', { name: request.name })
162 );
163 this.router.navigate(['/configuration']);
164 },
165 () => {
166 this.configForm.setErrors({ cdSubmitButton: true });
167 }
168 );
169 }
170
171 this.router.navigate(['/configuration']);
172 }
173 }