diff --git a/api/v1alpha5/conversion.go b/api/v1alpha5/conversion.go index b9439a54eb..79b282dfeb 100644 --- a/api/v1alpha5/conversion.go +++ b/api/v1alpha5/conversion.go @@ -351,9 +351,7 @@ func Convert_v1alpha5_OpenStackMachineSpec_To_v1beta1_OpenStackMachineSpec(in *O } if in.ServerGroupID != "" { - out.ServerGroup = &infrav1.ServerGroupFilter{ID: in.ServerGroupID} - } else { - out.ServerGroup = &infrav1.ServerGroupFilter{} + out.ServerGroup = &infrav1.ServerGroupParam{ID: &in.ServerGroupID} } imageParam := infrav1.ImageParam{} @@ -682,8 +680,8 @@ func Convert_v1beta1_OpenStackMachineSpec_To_v1alpha5_OpenStackMachineSpec(in *i return err } - if in.ServerGroup != nil { - out.ServerGroupID = in.ServerGroup.ID + if in.ServerGroup != nil && in.ServerGroup.ID != nil { + out.ServerGroupID = *in.ServerGroup.ID } if in.Image.ID != nil { diff --git a/api/v1alpha6/openstackcluster_conversion.go b/api/v1alpha6/openstackcluster_conversion.go index 0cc60afb18..912ccd16eb 100644 --- a/api/v1alpha6/openstackcluster_conversion.go +++ b/api/v1alpha6/openstackcluster_conversion.go @@ -434,7 +434,7 @@ func Convert_v1alpha6_Bastion_To_v1beta1_Bastion(in *Bastion, out *infrav1.Basti } if in.Instance.ServerGroupID != "" { - out.Spec.ServerGroup = &infrav1.ServerGroupFilter{ID: in.Instance.ServerGroupID} + out.Spec.ServerGroup = &infrav1.ServerGroupParam{ID: &in.Instance.ServerGroupID} } else { out.Spec.ServerGroup = nil } @@ -464,8 +464,8 @@ func Convert_v1beta1_Bastion_To_v1alpha6_Bastion(in *infrav1.Bastion, out *Basti return err } - if in.Spec.ServerGroup != nil && in.Spec.ServerGroup.ID != "" { - out.Instance.ServerGroupID = in.Spec.ServerGroup.ID + if in.Spec.ServerGroup != nil && in.Spec.ServerGroup.ID != nil { + out.Instance.ServerGroupID = *in.Spec.ServerGroup.ID } } diff --git a/api/v1alpha6/openstackmachine_conversion.go b/api/v1alpha6/openstackmachine_conversion.go index 2b5864d818..709a8dc1d0 100644 --- a/api/v1alpha6/openstackmachine_conversion.go +++ b/api/v1alpha6/openstackmachine_conversion.go @@ -267,7 +267,7 @@ func Convert_v1alpha6_OpenStackMachineSpec_To_v1beta1_OpenStackMachineSpec(in *O } if in.ServerGroupID != "" { - out.ServerGroup = &infrav1.ServerGroupFilter{ID: in.ServerGroupID} + out.ServerGroup = &infrav1.ServerGroupParam{ID: &in.ServerGroupID} } else { out.ServerGroup = nil } @@ -316,8 +316,8 @@ func Convert_v1beta1_OpenStackMachineSpec_To_v1alpha6_OpenStackMachineSpec(in *i return err } - if in.ServerGroup != nil { - out.ServerGroupID = in.ServerGroup.ID + if in.ServerGroup != nil && in.ServerGroup.ID != nil { + out.ServerGroupID = *in.ServerGroup.ID } if in.Image.ID != nil { diff --git a/api/v1alpha7/openstackcluster_conversion.go b/api/v1alpha7/openstackcluster_conversion.go index df703c5eb7..6d976806dc 100644 --- a/api/v1alpha7/openstackcluster_conversion.go +++ b/api/v1alpha7/openstackcluster_conversion.go @@ -403,7 +403,7 @@ func Convert_v1alpha7_Bastion_To_v1beta1_Bastion(in *Bastion, out *infrav1.Basti } if in.Instance.ServerGroupID != "" { - out.Spec.ServerGroup = &infrav1.ServerGroupFilter{ID: in.Instance.ServerGroupID} + out.Spec.ServerGroup = &infrav1.ServerGroupParam{ID: &in.Instance.ServerGroupID} } else { out.Spec.ServerGroup = nil } @@ -433,8 +433,8 @@ func Convert_v1beta1_Bastion_To_v1alpha7_Bastion(in *infrav1.Bastion, out *Basti return err } - if in.Spec.ServerGroup != nil && in.Spec.ServerGroup.ID != "" { - out.Instance.ServerGroupID = in.Spec.ServerGroup.ID + if in.Spec.ServerGroup != nil && in.Spec.ServerGroup.ID != nil { + out.Instance.ServerGroupID = *in.Spec.ServerGroup.ID } } diff --git a/api/v1alpha7/openstackmachine_conversion.go b/api/v1alpha7/openstackmachine_conversion.go index 6547fa10e1..ccca67a04c 100644 --- a/api/v1alpha7/openstackmachine_conversion.go +++ b/api/v1alpha7/openstackmachine_conversion.go @@ -178,7 +178,7 @@ func Convert_v1alpha7_OpenStackMachineSpec_To_v1beta1_OpenStackMachineSpec(in *O } if in.ServerGroupID != "" { - out.ServerGroup = &infrav1.ServerGroupFilter{ID: in.ServerGroupID} + out.ServerGroup = &infrav1.ServerGroupParam{ID: &in.ServerGroupID} } else { out.ServerGroup = nil } @@ -224,8 +224,8 @@ func Convert_v1beta1_OpenStackMachineSpec_To_v1alpha7_OpenStackMachineSpec(in *i return err } - if in.ServerGroup != nil { - out.ServerGroupID = in.ServerGroup.ID + if in.ServerGroup != nil && in.ServerGroup.ID != nil { + out.ServerGroupID = *in.ServerGroup.ID } if in.Image.ID != nil { diff --git a/api/v1beta1/openstackmachine_types.go b/api/v1beta1/openstackmachine_types.go index 3f20a559ff..f1c0550766 100644 --- a/api/v1beta1/openstackmachine_types.go +++ b/api/v1beta1/openstackmachine_types.go @@ -86,7 +86,7 @@ type OpenStackMachineSpec struct { // The server group to assign the machine to. // +optional - ServerGroup *ServerGroupFilter `json:"serverGroup,omitempty"` + ServerGroup *ServerGroupParam `json:"serverGroup,omitempty"` // IdentityRef is a reference to a secret holding OpenStack credentials // to be used when reconciling this machine. If not specified, the diff --git a/api/v1beta1/types.go b/api/v1beta1/types.go index bfdef09c6b..9bf90d826d 100644 --- a/api/v1beta1/types.go +++ b/api/v1beta1/types.go @@ -547,9 +547,30 @@ type AdditionalBlockDevice struct { Storage BlockDeviceStorage `json:"storage"` } +// ServerGroupParam specifies an OpenStack server group. It may be specified by ID or filter, but not both. +// +kubebuilder:validation:MaxProperties:=1 +// +kubebuilder:validation:MinProperties:=1 +type ServerGroupParam struct { + // ID is the ID of the server group to use. + // +kubebuilder:validation:Format:=uuid + ID optional.String `json:"id,omitempty"` + + // Filter specifies a query to select an OpenStack server group. If provided, it cannot be empty. + Filter *ServerGroupFilter `json:"filter,omitempty"` +} + +// ServerGroupFilter specifies a query to select an OpenStack server group. At least one property must be set. +// +kubebuilder:validation:MinProperties:=1 type ServerGroupFilter struct { - ID string `json:"id,omitempty"` - Name string `json:"name,omitempty"` + // Name is the name of a server group to look for. + Name optional.String `json:"name,omitempty"` +} + +func (f *ServerGroupFilter) IsZero() bool { + if f == nil { + return true + } + return f.Name == nil } // BlockDeviceType defines the type of block device to create. diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index 69d71929a1..857c85788b 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -984,8 +984,8 @@ func (in *OpenStackMachineSpec) DeepCopyInto(out *OpenStackMachineSpec) { } if in.ServerGroup != nil { in, out := &in.ServerGroup, &out.ServerGroup - *out = new(ServerGroupFilter) - **out = **in + *out = new(ServerGroupParam) + (*in).DeepCopyInto(*out) } if in.IdentityRef != nil { in, out := &in.IdentityRef, &out.IdentityRef @@ -1563,6 +1563,11 @@ func (in *SecurityGroupStatus) DeepCopy() *SecurityGroupStatus { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ServerGroupFilter) DeepCopyInto(out *ServerGroupFilter) { *out = *in + if in.Name != nil { + in, out := &in.Name, &out.Name + *out = new(string) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ServerGroupFilter. @@ -1575,6 +1580,31 @@ func (in *ServerGroupFilter) DeepCopy() *ServerGroupFilter { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ServerGroupParam) DeepCopyInto(out *ServerGroupParam) { + *out = *in + if in.ID != nil { + in, out := &in.ID, &out.ID + *out = new(string) + **out = **in + } + if in.Filter != nil { + in, out := &in.Filter, &out.Filter + *out = new(ServerGroupFilter) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ServerGroupParam. +func (in *ServerGroupParam) DeepCopy() *ServerGroupParam { + if in == nil { + return nil + } + out := new(ServerGroupParam) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ServerMetadata) DeepCopyInto(out *ServerMetadata) { *out = *in diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml index acdaa6eae7..3310ca1e50 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml @@ -5598,10 +5598,22 @@ spec: type: array serverGroup: description: The server group to assign the machine to. + maxProperties: 1 + minProperties: 1 properties: + filter: + description: Filter specifies a query to select an OpenStack + server group. If provided, it cannot be empty. + minProperties: 1 + properties: + name: + description: Name is the name of a server group to + look for. + type: string + type: object id: - type: string - name: + description: ID is the ID of the server group to use. + format: uuid type: string type: object serverMetadata: diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml index f01e35e574..136e031bb5 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml @@ -3035,10 +3035,24 @@ spec: serverGroup: description: The server group to assign the machine to. + maxProperties: 1 + minProperties: 1 properties: + filter: + description: Filter specifies a query to select + an OpenStack server group. If provided, it cannot + be empty. + minProperties: 1 + properties: + name: + description: Name is the name of a server + group to look for. + type: string + type: object id: - type: string - name: + description: ID is the ID of the server group + to use. + format: uuid type: string type: object serverMetadata: diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachines.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachines.yaml index 1ea294dcb1..5257d76d04 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachines.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachines.yaml @@ -2364,10 +2364,21 @@ spec: type: array serverGroup: description: The server group to assign the machine to. + maxProperties: 1 + minProperties: 1 properties: + filter: + description: Filter specifies a query to select an OpenStack server + group. If provided, it cannot be empty. + minProperties: 1 + properties: + name: + description: Name is the name of a server group to look for. + type: string + type: object id: - type: string - name: + description: ID is the ID of the server group to use. + format: uuid type: string type: object serverMetadata: diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachinetemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachinetemplates.yaml index 4c1b1abb98..111b256b06 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachinetemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachinetemplates.yaml @@ -2045,10 +2045,22 @@ spec: type: array serverGroup: description: The server group to assign the machine to. + maxProperties: 1 + minProperties: 1 properties: + filter: + description: Filter specifies a query to select an OpenStack + server group. If provided, it cannot be empty. + minProperties: 1 + properties: + name: + description: Name is the name of a server group to + look for. + type: string + type: object id: - type: string - name: + description: ID is the ID of the server group to use. + format: uuid type: string type: object serverMetadata: diff --git a/controllers/openstackmachine_controller_test.go b/controllers/openstackmachine_controller_test.go index 53c504e05c..f40802aa98 100644 --- a/controllers/openstackmachine_controller_test.go +++ b/controllers/openstackmachine_controller_test.go @@ -92,7 +92,7 @@ func getDefaultOpenStackMachine() *infrav1.OpenStackMachine { }, ConfigDrive: pointer.Bool(true), SecurityGroups: []infrav1.SecurityGroupParam{}, - ServerGroup: &infrav1.ServerGroupFilter{ID: serverGroupUUID}, + ServerGroup: &infrav1.ServerGroupParam{ID: pointer.String(serverGroupUUID)}, }, Status: infrav1.OpenStackMachineStatus{ Resolved: &infrav1.ResolvedMachineSpec{ diff --git a/docs/book/src/api/v1beta1/api.md b/docs/book/src/api/v1beta1/api.md index b97042eec6..10a8f3e104 100644 --- a/docs/book/src/api/v1beta1/api.md +++ b/docs/book/src/api/v1beta1/api.md @@ -715,8 +715,8 @@ RootVolume serverGroup
- -ServerGroupFilter + +ServerGroupParam @@ -3270,8 +3270,8 @@ RootVolume serverGroup
- -ServerGroupFilter + +ServerGroupParam @@ -3642,8 +3642,8 @@ RootVolume serverGroup
- -ServerGroupFilter + +ServerGroupParam @@ -4778,9 +4778,40 @@ string

(Appears on: +ServerGroupParam) +

+

+

ServerGroupFilter specifies a query to select an OpenStack server group. At least one property must be set.

+

+ + + + + + + + + + + + + +
FieldDescription
+name
+ +string + +
+

Name is the name of a server group to look for.

+
+

ServerGroupParam +

+

+(Appears on: OpenStackMachineSpec)

+

ServerGroupParam specifies an OpenStack server group. It may be specified by ID or filter, but not both.

@@ -4798,16 +4829,20 @@ string diff --git a/pkg/cloud/services/compute/referenced_resources_test.go b/pkg/cloud/services/compute/referenced_resources_test.go index b4419b691d..eca7468d9d 100644 --- a/pkg/cloud/services/compute/referenced_resources_test.go +++ b/pkg/cloud/services/compute/referenced_resources_test.go @@ -67,7 +67,7 @@ func Test_ResolveMachineSpec(t *testing.T) { { testName: "Resources ID passed", spec: infrav1.OpenStackMachineSpec{ - ServerGroup: &infrav1.ServerGroupFilter{ID: serverGroupID1}, + ServerGroup: &infrav1.ServerGroupParam{ID: pointer.String(serverGroupID1)}, Image: infrav1.ImageParam{ID: pointer.String(imageID1)}, }, want: &infrav1.ResolvedMachineSpec{ @@ -90,7 +90,7 @@ func Test_ResolveMachineSpec(t *testing.T) { testName: "Server group empty", spec: infrav1.OpenStackMachineSpec{ Image: infrav1.ImageParam{ID: pointer.String(imageID1)}, - ServerGroup: &infrav1.ServerGroupFilter{}, + ServerGroup: nil, }, want: &infrav1.ResolvedMachineSpec{ ImageID: imageID1, @@ -101,7 +101,7 @@ func Test_ResolveMachineSpec(t *testing.T) { testName: "Server group by Name not found", spec: infrav1.OpenStackMachineSpec{ Image: infrav1.ImageParam{ID: pointer.String(imageID1)}, - ServerGroup: &infrav1.ServerGroupFilter{Name: "test-server-group"}, + ServerGroup: &infrav1.ServerGroupParam{Filter: &infrav1.ServerGroupFilter{Name: pointer.String("test-server-group")}}, }, expectComputeMock: func(m *mock.MockComputeClientMockRecorder) { m.ListServerGroups().Return( @@ -197,6 +197,7 @@ func Test_ResolveMachineSpec(t *testing.T) { return } + g.Expect(err).To(BeNil()) g.Expect(resources).To(Equal(tt.want), cmp.Diff(resources, tt.want)) }) } diff --git a/pkg/cloud/services/compute/servergroup.go b/pkg/cloud/services/compute/servergroup.go index f4118da73f..e36b0edc51 100644 --- a/pkg/cloud/services/compute/servergroup.go +++ b/pkg/cloud/services/compute/servergroup.go @@ -17,6 +17,7 @@ limitations under the License. package compute import ( + "errors" "fmt" "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups" @@ -26,18 +27,18 @@ import ( // GetServerGroupID looks up a server group using the passed filter and returns // its ID. It'll return an error when server group is not found or there are multiple. -func (s *Service) GetServerGroupID(serverGroupFilter *infrav1.ServerGroupFilter) (string, error) { - if serverGroupFilter.ID != "" { - return serverGroupFilter.ID, nil +func (s *Service) GetServerGroupID(serverGroupParam *infrav1.ServerGroupParam) (string, error) { + if serverGroupParam.ID != nil { + return *serverGroupParam.ID, nil } - if serverGroupFilter.Name == "" { - // empty filter produced no server group, but also no error - return "", nil + if serverGroupParam.Filter == nil || serverGroupParam.Filter.Name == nil { + // Should have been caught by validation + return "", errors.New("server group param is empty") } // otherwise fallback to looking up by name, which is slower - serverGroup, err := s.getServerGroupByName(serverGroupFilter.Name) + serverGroup, err := s.getServerGroupByName(*serverGroupParam.Filter.Name) if err != nil { return "", err } diff --git a/pkg/cloud/services/compute/servergroup_test.go b/pkg/cloud/services/compute/servergroup_test.go index 2771ccf26a..b73b496dcf 100644 --- a/pkg/cloud/services/compute/servergroup_test.go +++ b/pkg/cloud/services/compute/servergroup_test.go @@ -23,6 +23,7 @@ import ( "github.com/go-logr/logr/testr" "github.com/golang/mock/gomock" "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups" + "k8s.io/utils/pointer" infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1" "sigs.k8s.io/cluster-api-provider-openstack/pkg/clients/mock" @@ -34,31 +35,31 @@ func TestService_GetServerGroupID(t *testing.T) { const serverGroupID2 = "8f536889-5198-42d7-8314-cb78f4f4755c" tests := []struct { - testName string - serverGroupFilter *infrav1.ServerGroupFilter - expect func(m *mock.MockComputeClientMockRecorder) - want string - wantErr bool + testName string + serverGroupParam *infrav1.ServerGroupParam + expect func(m *mock.MockComputeClientMockRecorder) + want string + wantErr bool }{ { - testName: "Return server group ID from filter if only filter (with ID) given", - serverGroupFilter: &infrav1.ServerGroupFilter{ID: serverGroupID1}, + testName: "Return server group ID from filter if only filter (with ID) given", + serverGroupParam: &infrav1.ServerGroupParam{ID: pointer.String(serverGroupID1)}, expect: func(m *mock.MockComputeClientMockRecorder) { }, want: serverGroupID1, wantErr: false, }, { - testName: "Return no server group if empty filter is given", - serverGroupFilter: &infrav1.ServerGroupFilter{}, + testName: "Return error if empty filter is given", + serverGroupParam: &infrav1.ServerGroupParam{}, expect: func(m *mock.MockComputeClientMockRecorder) { }, want: "", - wantErr: false, + wantErr: true, }, { - testName: "Return server group ID from filter if only filter (with name) given", - serverGroupFilter: &infrav1.ServerGroupFilter{Name: "test-server-group"}, + testName: "Return server group ID from filter if only filter (with name) given", + serverGroupParam: &infrav1.ServerGroupParam{Filter: &infrav1.ServerGroupFilter{Name: pointer.String("test-server-group")}}, expect: func(m *mock.MockComputeClientMockRecorder) { m.ListServerGroups().Return( []servergroups.ServerGroup{{ID: serverGroupID1, Name: "test-server-group"}}, @@ -68,8 +69,8 @@ func TestService_GetServerGroupID(t *testing.T) { wantErr: false, }, { - testName: "Return no results", - serverGroupFilter: &infrav1.ServerGroupFilter{Name: "test-server-group"}, + testName: "Return no results", + serverGroupParam: &infrav1.ServerGroupParam{Filter: &infrav1.ServerGroupFilter{Name: pointer.String("test-server-group")}}, expect: func(m *mock.MockComputeClientMockRecorder) { m.ListServerGroups().Return( []servergroups.ServerGroup{}, @@ -79,8 +80,8 @@ func TestService_GetServerGroupID(t *testing.T) { wantErr: true, }, { - testName: "Return multiple results", - serverGroupFilter: &infrav1.ServerGroupFilter{Name: "test-server-group"}, + testName: "Return multiple results", + serverGroupParam: &infrav1.ServerGroupParam{Filter: &infrav1.ServerGroupFilter{Name: pointer.String("test-server-group")}}, expect: func(m *mock.MockComputeClientMockRecorder) { m.ListServerGroups().Return( []servergroups.ServerGroup{ @@ -93,8 +94,8 @@ func TestService_GetServerGroupID(t *testing.T) { wantErr: true, }, { - testName: "OpenStack returns error", - serverGroupFilter: &infrav1.ServerGroupFilter{Name: "test-server-group"}, + testName: "OpenStack returns error", + serverGroupParam: &infrav1.ServerGroupParam{Filter: &infrav1.ServerGroupFilter{Name: pointer.String("test-server-group")}}, expect: func(m *mock.MockComputeClientMockRecorder) { m.ListServerGroups().Return( nil, @@ -116,7 +117,7 @@ func TestService_GetServerGroupID(t *testing.T) { } tt.expect(mockScopeFactory.ComputeClient.EXPECT()) - got, err := s.GetServerGroupID(tt.serverGroupFilter) + got, err := s.GetServerGroupID(tt.serverGroupParam) if (err != nil) != tt.wantErr { t.Errorf("Service.getServerGroupID() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/test/e2e/suites/apivalidations/filters_test.go b/test/e2e/suites/apivalidations/filters_test.go index c451a620e3..08b8cdfccd 100644 --- a/test/e2e/suites/apivalidations/filters_test.go +++ b/test/e2e/suites/apivalidations/filters_test.go @@ -406,4 +406,39 @@ var _ = Describe("Filter API validations", func() { Expect(k8sClient.Create(ctx, cluster)).NotTo(Succeed(), "OpenStackCluster creation should fail") }) }) + + Context("ServerGroupParam", func() { + It("should allow setting ID", func() { + machine.Spec.ServerGroup = &infrav1.ServerGroupParam{ID: pointer.String("06c32c52-f207-4f6a-a769-bbcbe5a43f5c")} + Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "OpenStackMachine creation should succeed") + }) + + It("should allow setting non-empty Filter", func() { + machine.Spec.ServerGroup = &infrav1.ServerGroupParam{Filter: &infrav1.ServerGroupFilter{Name: pointer.String("foo")}} + Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "OpenStackMachine creation should succeed") + }) + + It("should not allow setting empty param", func() { + machine.Spec.ServerGroup = &infrav1.ServerGroupParam{} + Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "OpenStackMachine creation should fail") + }) + + It("should not allow setting invalid id", func() { + machine.Spec.ServerGroup = &infrav1.ServerGroupParam{ID: pointer.String("foo")} + Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "OpenStackMachine creation should fail") + }) + + It("should not allow setting empty Filter", func() { + machine.Spec.ServerGroup = &infrav1.ServerGroupParam{Filter: &infrav1.ServerGroupFilter{}} + Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "OpenStackMachine creation should fail") + }) + + It("should not allow setting both ID and Filter", func() { + machine.Spec.ServerGroup = &infrav1.ServerGroupParam{ + ID: pointer.String("06c32c52-f207-4f6a-a769-bbcbe5a43f5c"), + Filter: &infrav1.ServerGroupFilter{Name: pointer.String("foo")}, + } + Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "OpenStackMachine creation should fail") + }) + }) }) diff --git a/test/helpers/fuzzerfuncs.go b/test/helpers/fuzzerfuncs.go index b181e02c45..063bfe9b58 100644 --- a/test/helpers/fuzzerfuncs.go +++ b/test/helpers/fuzzerfuncs.go @@ -149,5 +149,9 @@ func InfraV1FuzzerFuncs() []interface{} { func(param *infrav1.RouterParam, c fuzz.Continue) { fuzzFilterParam(¶m.ID, ¶m.Filter, c) }, + + func(param *infrav1.SecurityGroupParam, c fuzz.Continue) { + fuzzFilterParam(¶m.ID, ¶m.Filter, c) + }, } }
+

ID is the ID of the server group to use.

-name
+filter
-string + +ServerGroupFilter +
+

Filter specifies a query to select an OpenStack server group. If provided, it cannot be empty.