]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/configuration/configuration-form/configuration-form.types.ts
buildsys: use download.ceph.com to download source tar ball
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / cluster / configuration / configuration-form / configuration-form.types.ts
1 import { Validators } from '@angular/forms';
2 import { CdValidators } from '../../../../shared/forms/cd-validators';
3 import { ConfigFormModel } from './configuration-form.model';
4
5 import * as _ from 'lodash';
6
7 export class ConfigOptionTypes {
8 // TODO: I18N
9 private static knownTypes: Array<any> = [
10 {
11 name: 'uint',
12 inputType: 'number',
13 humanReadable: 'Unsigned integer value',
14 defaultMin: 0,
15 patternHelpText: 'The entered value needs to be an unsigned number.',
16 isNumberType: true,
17 allowsNegative: false
18 },
19 {
20 name: 'int',
21 inputType: 'number',
22 humanReadable: 'Integer value',
23 patternHelpText: 'The entered value needs to be a number.',
24 isNumberType: true,
25 allowsNegative: true
26 },
27 {
28 name: 'size',
29 inputType: 'number',
30 humanReadable: 'Unsigned integer value (>=16bit)',
31 defaultMin: 0,
32 patternHelpText: 'The entered value needs to be a unsigned number.',
33 isNumberType: true,
34 allowsNegative: false
35 },
36 {
37 name: 'secs',
38 inputType: 'number',
39 humanReadable: 'Number of seconds',
40 defaultMin: 1,
41 patternHelpText: 'The entered value needs to be a number >= 1.',
42 isNumberType: true,
43 allowsNegative: false
44 },
45 {
46 name: 'float',
47 inputType: 'number',
48 humanReadable: 'Double value',
49 patternHelpText: 'The entered value needs to be a number or decimal.',
50 isNumberType: true,
51 allowsNegative: true
52 },
53 { name: 'str', inputType: 'text', humanReadable: 'Text', isNumberType: false },
54 {
55 name: 'addr',
56 inputType: 'text',
57 humanReadable: 'IPv4 or IPv6 address',
58 patternHelpText: 'The entered value needs to be a valid IP address.',
59 isNumberType: false
60 },
61 {
62 name: 'uuid',
63 inputType: 'text',
64 humanReadable: 'UUID',
65 patternHelpText:
66 'The entered value is not a valid UUID, e.g.: 67dcac9f-2c03-4d6c-b7bd-1210b3a259a8',
67 isNumberType: false
68 },
69 { name: 'bool', inputType: 'checkbox', humanReadable: 'Boolean value', isNumberType: false }
70 ];
71
72 public static getType(type: string): any {
73 const currentType = _.find(this.knownTypes, (t) => {
74 return t.name === type;
75 });
76
77 if (currentType !== undefined) {
78 return currentType;
79 }
80
81 throw new Error('Found unknown type "' + type + '" for config option.');
82 }
83
84 public static getTypeValidators(configOption: ConfigFormModel): any {
85 const typeParams = ConfigOptionTypes.getType(configOption.type);
86
87 if (typeParams.name === 'bool' || typeParams.name === 'str') {
88 return;
89 }
90
91 const typeValidators = { validators: [], patternHelpText: typeParams.patternHelpText };
92
93 if (typeParams.isNumberType) {
94 if (configOption.max && configOption.max !== '') {
95 typeValidators['max'] = configOption.max;
96 typeValidators.validators.push(Validators.max(configOption.max));
97 }
98
99 if (configOption.min && configOption.min !== '') {
100 typeValidators['min'] = configOption.min;
101 typeValidators.validators.push(Validators.min(configOption.min));
102 } else if ('defaultMin' in typeParams) {
103 typeValidators['min'] = typeParams.defaultMin;
104 typeValidators.validators.push(Validators.min(typeParams.defaultMin));
105 }
106
107 if (configOption.type === 'float') {
108 typeValidators.validators.push(CdValidators.decimalNumber());
109 } else {
110 typeValidators.validators.push(CdValidators.number(typeParams.allowsNegative));
111 }
112 } else if (configOption.type === 'addr') {
113 typeValidators.validators = [CdValidators.ip()];
114 } else if (configOption.type === 'uuid') {
115 typeValidators.validators = [CdValidators.uuid()];
116 }
117
118 return typeValidators;
119 }
120 }