Skip to content

Commit

Permalink
SecurityGroupFilter to SecurityGroupParam
Browse files Browse the repository at this point in the history
  • Loading branch information
mdbooth committed Mar 28, 2024
1 parent 3cea05d commit de283e6
Show file tree
Hide file tree
Showing 24 changed files with 1,040 additions and 649 deletions.
83 changes: 43 additions & 40 deletions api/v1alpha5/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,8 @@ func Convert_v1beta1_PortOpts_To_v1alpha5_PortOpts(in *infrav1.PortOpts, out *Po
if len(in.SecurityGroups) > 0 {
out.SecurityGroupFilters = make([]SecurityGroupParam, len(in.SecurityGroups))
for i := range in.SecurityGroups {
securityGroupParam := &out.SecurityGroupFilters[i]
if in.SecurityGroups[i].ID != "" {
securityGroupParam.UUID = in.SecurityGroups[i].ID
} else {
if err := Convert_v1beta1_SecurityGroupFilter_To_v1alpha5_SecurityGroupFilter(&in.SecurityGroups[i], &securityGroupParam.Filter, s); err != nil {
return err
}
if err := Convert_v1beta1_SecurityGroupParam_To_v1alpha5_SecurityGroupParam(&in.SecurityGroups[i], &out.SecurityGroupFilters[i], s); err != nil {
return err
}
}
}
Expand Down Expand Up @@ -395,46 +390,48 @@ func Convert_v1alpha5_PortOpts_To_v1beta1_PortOpts(in *PortOpts, out *infrav1.Po
}

if len(in.SecurityGroups) > 0 || len(in.SecurityGroupFilters) > 0 {
out.SecurityGroups = make([]infrav1.SecurityGroupFilter, 0, len(in.SecurityGroups)+len(in.SecurityGroupFilters))
out.SecurityGroups = make([]infrav1.SecurityGroupParam, 0, len(in.SecurityGroups)+len(in.SecurityGroupFilters))
for i := range in.SecurityGroupFilters {
sgParam := &in.SecurityGroupFilters[i]
switch {
case sgParam.UUID != "":
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupFilter{ID: sgParam.UUID})
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupParam{ID: &sgParam.UUID})
case sgParam.Name != "":
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupFilter{Name: sgParam.Name})
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupParam{Filter: &infrav1.SecurityGroupFilter{Name: sgParam.Name}})
case sgParam.Filter != (SecurityGroupFilter{}):
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupFilter{})
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupParam{})
outSG := &out.SecurityGroups[len(out.SecurityGroups)-1]
if err := Convert_v1alpha5_SecurityGroupFilter_To_v1beta1_SecurityGroupFilter(&sgParam.Filter, outSG, s); err != nil {
outSG.Filter = &infrav1.SecurityGroupFilter{}
if err := Convert_v1alpha5_SecurityGroupFilter_To_v1beta1_SecurityGroupFilter(&sgParam.Filter, outSG.Filter, s); err != nil {
return err
}
}
}
for _, id := range in.SecurityGroups {
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupFilter{ID: id})
for i := range in.SecurityGroups {
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupParam{ID: &in.SecurityGroups[i]})
}
}

if len(in.SecurityGroups) > 0 || len(in.SecurityGroupFilters) > 0 {
out.SecurityGroups = make([]infrav1.SecurityGroupFilter, 0, len(in.SecurityGroups)+len(in.SecurityGroupFilters))
out.SecurityGroups = make([]infrav1.SecurityGroupParam, 0, len(in.SecurityGroups)+len(in.SecurityGroupFilters))
for i := range in.SecurityGroupFilters {
sgParam := &in.SecurityGroupFilters[i]
switch {
case sgParam.UUID != "":
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupFilter{ID: sgParam.UUID})
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupParam{ID: &sgParam.UUID})
case sgParam.Name != "":
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupFilter{Name: sgParam.Name})
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupParam{Filter: &infrav1.SecurityGroupFilter{Name: sgParam.Name}})
case sgParam.Filter != (SecurityGroupFilter{}):
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupFilter{})
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupParam{})
outSG := &out.SecurityGroups[len(out.SecurityGroups)-1]
if err := Convert_v1alpha5_SecurityGroupFilter_To_v1beta1_SecurityGroupFilter(&sgParam.Filter, outSG, s); err != nil {
outSG.Filter = &infrav1.SecurityGroupFilter{}
if err := Convert_v1alpha5_SecurityGroupFilter_To_v1beta1_SecurityGroupFilter(&sgParam.Filter, outSG.Filter, s); err != nil {
return err
}
}
}
for _, id := range in.SecurityGroups {
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupFilter{ID: id})
for i := range in.SecurityGroups {
out.SecurityGroups = append(out.SecurityGroups, infrav1.SecurityGroupParam{ID: &in.SecurityGroups[i]})
}
}

Expand Down Expand Up @@ -521,34 +518,40 @@ func Convert_v1beta1_NetworkStatus_To_v1alpha5_Network(in *infrav1.NetworkStatus
return nil
}

func Convert_v1alpha5_SecurityGroupParam_To_v1beta1_SecurityGroupFilter(in *SecurityGroupParam, out *infrav1.SecurityGroupFilter, s conversion.Scope) error {
// SecurityGroupParam is replaced by its contained SecurityGroupFilter in v1beta1
err := Convert_v1alpha5_SecurityGroupFilter_To_v1beta1_SecurityGroupFilter(&in.Filter, out, s)
if err != nil {
return err
}

func Convert_v1alpha5_SecurityGroupParam_To_v1beta1_SecurityGroupParam(in *SecurityGroupParam, out *infrav1.SecurityGroupParam, s conversion.Scope) error {
if in.UUID != "" {
out.ID = in.UUID
out.ID = &in.UUID
return nil
}

outFilter := &infrav1.SecurityGroupFilter{}
if in.Name != "" {
out.Name = in.Name
outFilter.Name = in.Name
} else {
// SecurityGroupParam is replaced by its contained SecurityGroupFilter in v1beta1
err := Convert_v1alpha5_SecurityGroupFilter_To_v1beta1_SecurityGroupFilter(&in.Filter, outFilter, s)
if err != nil {
return err
}
}

if !outFilter.IsZero() {
out.Filter = outFilter
}
return nil
}

func Convert_v1beta1_SecurityGroupFilter_To_v1alpha5_SecurityGroupParam(in *infrav1.SecurityGroupFilter, out *SecurityGroupParam, s conversion.Scope) error {
// SecurityGroupParam is replaced by its contained SecurityGroupFilter in v1beta1
err := Convert_v1beta1_SecurityGroupFilter_To_v1alpha5_SecurityGroupFilter(in, &out.Filter, s)
if err != nil {
return err
func Convert_v1beta1_SecurityGroupParam_To_v1alpha5_SecurityGroupParam(in *infrav1.SecurityGroupParam, out *SecurityGroupParam, s conversion.Scope) error {
if in.ID != nil {
out.UUID = *in.ID
return nil
}

if in.ID != "" {
out.UUID = in.ID
}
if in.Name != "" {
out.Name = in.Name
if in.Filter != nil {
err := Convert_v1beta1_SecurityGroupFilter_To_v1alpha5_SecurityGroupFilter(in.Filter, &out.Filter, s)
if err != nil {
return err
}
}
return nil
}
Expand Down
32 changes: 22 additions & 10 deletions api/v1alpha5/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions api/v1alpha6/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,19 +455,19 @@ func TestPortOptsConvertTo(t *testing.T) {

// Variables used in the tests
uuids := []string{"abc123", "123abc"}
securityGroupsUuids := []infrav1.SecurityGroupFilter{
{ID: uuids[0]},
{ID: uuids[1]},
securityGroupsUuids := []infrav1.SecurityGroupParam{
{ID: &uuids[0]},
{ID: &uuids[1]},
}
securityGroupFilter := []SecurityGroupParam{
{Name: "one"},
{UUID: "654cba"},
}
securityGroupFilterMerged := []infrav1.SecurityGroupFilter{
{Name: "one"},
{ID: "654cba"},
{ID: uuids[0]},
{ID: uuids[1]},
securityGroupFilterMerged := []infrav1.SecurityGroupParam{
{Filter: &infrav1.SecurityGroupFilter{Name: "one"}},
{ID: pointer.String("654cba")},
{ID: &uuids[0]},
{ID: &uuids[1]},
}
legacyPortProfile := map[string]string{
"capabilities": "[\"switchdev\"]",
Expand Down
6 changes: 6 additions & 0 deletions api/v1alpha6/openstackmachine_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ func restorev1beta1MachineSpec(previous *infrav1.OpenStackMachineSpec, dst *infr
dst.ServerGroup = previous.ServerGroup
dst.Image = previous.Image
dst.FloatingIPPoolRef = previous.FloatingIPPoolRef

if len(dst.SecurityGroups) == len(previous.SecurityGroups) {
for i := range dst.SecurityGroups {
restorev1beta1SecurityGroupParam(&previous.SecurityGroups[i], &dst.SecurityGroups[i])
}
}
}

func convertNetworksToPorts(networks []NetworkParam, s apiconversion.Scope) ([]infrav1.PortOpts, error) {
Expand Down
Loading

0 comments on commit de283e6

Please sign in to comment.