forked from kyma-project/control-plane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plans_schema.go
106 lines (96 loc) · 3.2 KB
/
plans_schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package broker
type RootSchema struct {
Schema string `json:"$schema"`
Type
Properties interface{} `json:"properties"`
Required []string `json:"required"`
// Specified to true enables form view on website
ShowFormView bool `json:"_show_form_view"`
// Specifies in what order properties will be displayed on the form
ControlsOrder []string `json:"_controlsOrder"`
}
type ProvisioningProperties struct {
Name Type `json:"name"`
Region *Type `json:"region,omitempty"`
MachineType *Type `json:"machineType,omitempty"`
AutoScalerMin *Type `json:"autoScalerMin,omitempty"`
AutoScalerMax *Type `json:"autoScalerMax,omitempty"`
ZonesCount *Type `json:"zonesCount,omitempty"`
}
type Type struct {
Type string `json:"type"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Minimum int `json:"minimum,omitempty"`
Maximum int `json:"maximum,omitempty"`
MinLength int `json:"minLength,omitempty"`
MaxLength int `json:"maxLength,omitempty"`
// Regex pattern to match against string type of fields.
// If not specified for strings user can pass empty string with whitespaces only.
Pattern string `json:"pattern,omitempty"`
Default interface{} `json:"default,omitempty"`
Example interface{} `json:"example,omitempty"`
Enum []interface{} `json:"enum,omitempty"`
Items []Type `json:"items,omitempty"`
AdditionalItems *bool `json:"additionalItems,omitempty"`
UniqueItems *bool `json:"uniqueItems,omitempty"`
}
func NameProperty() Type {
return Type{
Type: "string",
Title: "Cluster Name",
// Allows for all alphanumeric characters, '_', and '-'
Pattern: "^[a-zA-Z0-9-]*$",
MinLength: 1,
}
}
// NewProvisioningProperties creates a new properties for different plans
// Note that the order of properties will be the same in the form on the website
func NewProvisioningProperties(machineTypes []string, regions []string) ProvisioningProperties {
return ProvisioningProperties{
Name: NameProperty(),
Region: &Type{
Type: "string",
Enum: ToInterfaceSlice(regions),
},
MachineType: &Type{
Type: "string",
Enum: ToInterfaceSlice(machineTypes),
},
AutoScalerMin: &Type{
Type: "integer",
Minimum: 2,
Default: 2,
Description: "Specifies the minimum number of virtual machines to create",
},
AutoScalerMax: &Type{
Type: "integer",
Minimum: 2,
Maximum: 40,
Default: 10,
Description: "Specifies the maximum number of virtual machines to create",
},
}
}
func NewSchema(properties ProvisioningProperties, controlsOrder []string) RootSchema {
return RootSchema{
Schema: "http://json-schema.org/draft-04/schema#",
Type: Type{
Type: "object",
},
Properties: properties,
ShowFormView: true,
Required: []string{"name"},
ControlsOrder: controlsOrder,
}
}
func DefaultControlsOrder() []string {
return []string{"name", "region", "machineType", "autoScalerMin", "autoScalerMax"}
}
func ToInterfaceSlice(input []string) []interface{} {
interfaces := make([]interface{}, len(input))
for i, item := range input {
interfaces[i] = item
}
return interfaces
}