Skip to content

Commit 9a4e140

Browse files
authored
sort subnets by id (#1750)
1 parent 0ebac0e commit 9a4e140

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

pkg/networking/subnet_resolver.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func (r *defaultSubnetsResolver) ResolveViaDiscovery(ctx context.Context, opts .
156156
if err := r.validateSubnetsMinimalCount(chosenSubnets, subnetLocale, resolveOpts); err != nil {
157157
return nil, err
158158
}
159+
sortSubnetsByID(chosenSubnets)
159160
return chosenSubnets, nil
160161
}
161162

@@ -219,7 +220,7 @@ func (r *defaultSubnetsResolver) ResolveViaNameOrIDSlice(ctx context.Context, su
219220
if err := r.validateSubnetsMinimalCount(resolvedSubnets, subnetLocale, resolveOpts); err != nil {
220221
return nil, err
221222
}
222-
223+
sortSubnetsByID(resolvedSubnets)
223224
return resolvedSubnets, nil
224225
}
225226

@@ -290,3 +291,10 @@ func buildSDKSubnetLocaleType(subnet *ec2sdk.Subnet) subnetLocaleType {
290291
// TODO: add localZone as well once we have fixed logic to compute whether it's localZone.
291292
return subnetLocaleTypeAvailabilityZone
292293
}
294+
295+
// sortSubnetsByID sorts given subnets slice by subnetID.
296+
func sortSubnetsByID(subnets []*ec2sdk.Subnet) {
297+
sort.Slice(subnets, func(i, j int) bool {
298+
return awssdk.StringValue(subnets[i].SubnetId) < awssdk.StringValue(subnets[j].SubnetId)
299+
})
300+
}

pkg/networking/subnet_resolver_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,3 +944,74 @@ func Test_buildSDKSubnetLocaleType(t *testing.T) {
944944
})
945945
}
946946
}
947+
948+
func Test_sortSubnetsByID(t *testing.T) {
949+
type args struct {
950+
subnets []*ec2sdk.Subnet
951+
}
952+
tests := []struct {
953+
name string
954+
args args
955+
wantSubnets []*ec2sdk.Subnet
956+
}{
957+
{
958+
name: "subnets already sorted",
959+
args: args{
960+
subnets: []*ec2sdk.Subnet{
961+
{
962+
SubnetId: awssdk.String("subnet-a"),
963+
},
964+
{
965+
SubnetId: awssdk.String("subnet-b"),
966+
}, {
967+
SubnetId: awssdk.String("subnet-c"),
968+
},
969+
},
970+
},
971+
wantSubnets: []*ec2sdk.Subnet{
972+
{
973+
SubnetId: awssdk.String("subnet-a"),
974+
},
975+
{
976+
SubnetId: awssdk.String("subnet-b"),
977+
}, {
978+
SubnetId: awssdk.String("subnet-c"),
979+
},
980+
},
981+
},
982+
{
983+
name: "subnets not sorted",
984+
args: args{
985+
subnets: []*ec2sdk.Subnet{
986+
{
987+
SubnetId: awssdk.String("subnet-c"),
988+
},
989+
{
990+
SubnetId: awssdk.String("subnet-b"),
991+
},
992+
{
993+
SubnetId: awssdk.String("subnet-a"),
994+
},
995+
},
996+
},
997+
wantSubnets: []*ec2sdk.Subnet{
998+
{
999+
SubnetId: awssdk.String("subnet-a"),
1000+
},
1001+
{
1002+
SubnetId: awssdk.String("subnet-b"),
1003+
},
1004+
{
1005+
SubnetId: awssdk.String("subnet-c"),
1006+
},
1007+
},
1008+
},
1009+
}
1010+
for _, tt := range tests {
1011+
t.Run(tt.name, func(t *testing.T) {
1012+
subnetsClone := append(tt.args.subnets[:0:0], tt.args.subnets...)
1013+
sortSubnetsByID(subnetsClone)
1014+
assert.Equal(t, tt.wantSubnets, subnetsClone)
1015+
})
1016+
}
1017+
}

0 commit comments

Comments
 (0)