Skip to content

Commit

Permalink
ClusterClass: clarify that enum works with all types
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer committed Dec 9, 2021
1 parent 30ae87d commit 3529f92
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 7 deletions.
10 changes: 5 additions & 5 deletions api/v1beta1/clusterclass_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,6 @@ type JSONSchemaProps struct {
// +optional
Pattern string `json:"pattern,omitempty"`

// Enum is the list of valid values of the variable.
// NOTE: Can only be set if type is string.
// +optional
Enum []apiextensionsv1.JSON `json:"enum,omitempty"`

// Maximum is the maximum of an integer or number variable.
// If ExclusiveMaximum is false, the variable is valid if it is lower than, or equal to, the value of Maximum.
// If ExclusiveMaximum is true, the variable is valid if it is strictly lower than the value of Maximum.
Expand All @@ -256,6 +251,11 @@ type JSONSchemaProps struct {
// +optional
ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`

// Enum is the list of valid values of the variable.
// NOTE: Can be set for all types.
// +optional
Enum []apiextensionsv1.JSON `json:"enum,omitempty"`

// Default is the default value of the variable.
// NOTE: Can be set for all types.
// +optional
Expand Down
161 changes: 159 additions & 2 deletions internal/topology/variables/cluster_variable_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func Test_ValidateClusterVariable(t *testing.T) {
wantErr: true,
},
{
name: "Valid enum",
name: "Valid enum string",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Name: "location",
Required: true,
Expand All @@ -364,7 +364,7 @@ func Test_ValidateClusterVariable(t *testing.T) {
},
},
{
name: "Fails, value does not match one of the enum values",
name: "Fails, value does not match one of the enum string values",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Name: "location",
Required: true,
Expand All @@ -386,6 +386,51 @@ func Test_ValidateClusterVariable(t *testing.T) {
},
wantErr: true,
},
{
name: "Valid enum integer",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Name: "location",
Required: true,
Schema: clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "integer",
Enum: []apiextensionsv1.JSON{
{Raw: []byte(`1`)},
{Raw: []byte(`2`)},
},
},
},
},
clusterVariable: &clusterv1.ClusterVariable{
Name: "location",
Value: apiextensionsv1.JSON{
Raw: []byte(`1`),
},
},
},
{
name: "Fails, value does not match one of the enum integer values",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Name: "location",
Required: true,
Schema: clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "string",
Enum: []apiextensionsv1.JSON{
{Raw: []byte(`1`)},
{Raw: []byte(`2`)},
},
},
},
},
clusterVariable: &clusterv1.ClusterVariable{
Name: "location",
Value: apiextensionsv1.JSON{
Raw: []byte(`3`),
},
},
wantErr: true,
},
{
name: "Valid object",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Expand Down Expand Up @@ -525,6 +570,67 @@ func Test_ValidateClusterVariable(t *testing.T) {
},
},
},
{
name: "Valid enum object",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Name: "enumObject",
Required: true,
Schema: clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "object",
Properties: map[string]clusterv1.JSONSchemaProps{
"location": {
Type: "string",
},
"url": {
Type: "string",
},
},
Enum: []apiextensionsv1.JSON{
{Raw: []byte(`{"location": "us-east-1","url":"us-east-1-url"}`)},
{Raw: []byte(`{"location": "us-east-2","url":"us-east-2-url"}`)},
},
},
},
},
clusterVariable: &clusterv1.ClusterVariable{
Name: "enumObject",
Value: apiextensionsv1.JSON{
Raw: []byte(`{"location": "us-east-2","url":"us-east-2-url"}`),
},
},
},
{
name: "Fails, value does not match one of the enum object values",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Name: "enumObject",
Required: true,
Schema: clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "object",
Properties: map[string]clusterv1.JSONSchemaProps{
"location": {
Type: "string",
},
"url": {
Type: "string",
},
},
Enum: []apiextensionsv1.JSON{
{Raw: []byte(`{"location": "us-east-1","url":"us-east-1-url"}`)},
{Raw: []byte(`{"location": "us-east-2","url":"us-east-2-url"}`)},
},
},
},
},
clusterVariable: &clusterv1.ClusterVariable{
Name: "enumObject",
Value: apiextensionsv1.JSON{
Raw: []byte(`{"location": "us-east-2","url":"wrong-url"}`),
},
},
wantErr: true,
},
{
name: "Valid array",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Expand Down Expand Up @@ -645,6 +751,57 @@ func Test_ValidateClusterVariable(t *testing.T) {
},
wantErr: true,
},
{
name: "Valid array object",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Name: "enumArray",
Required: true,
Schema: clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "array",
Items: &clusterv1.JSONSchemaProps{
Type: "string",
},
Enum: []apiextensionsv1.JSON{
{Raw: []byte(`["1","2","3"]`)},
{Raw: []byte(`["4","5","6"]`)},
},
},
},
},
clusterVariable: &clusterv1.ClusterVariable{
Name: "enumArray",
Value: apiextensionsv1.JSON{
Raw: []byte(`["1","2","3"]`),
},
},
},
{
name: "Fails, value does not match one of the enum array values",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Name: "enumArray",
Required: true,
Schema: clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "array",
Items: &clusterv1.JSONSchemaProps{
Type: "string",
},
Enum: []apiextensionsv1.JSON{
{Raw: []byte(`["1","2","3"]`)},
{Raw: []byte(`["4","5","6"]`)},
},
},
},
},
clusterVariable: &clusterv1.ClusterVariable{
Name: "enumArray",
Value: apiextensionsv1.JSON{
Raw: []byte(`["7","8","9"]`),
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 3529f92

Please sign in to comment.