Skip to content

Commit

Permalink
OCM-1308 | fix: filter empty subnet id
Browse files Browse the repository at this point in the history
Signed-off-by: Maggie Chen <[email protected]>

add test

Signed-off-by: Maggie Chen <[email protected]>

refactor

Signed-off-by: Maggie Chen <[email protected]>
  • Loading branch information
chenz4027 committed Apr 19, 2024
1 parent 86498a9 commit eb8027e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cmd/create/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2058,9 +2058,9 @@ func run(cmd *cobra.Command, _ []string) {
}

// Subnet IDs
subnetIDs := args.subnetIDs
subnetIDs := helper.FilterEmpty(args.subnetIDs)
subnetsProvided := len(subnetIDs) > 0
r.Reporter.Debugf("Received the following subnetIDs: %v", args.subnetIDs)
r.Reporter.Debugf("Received the following subnetIDs: %v", subnetIDs)
// If the user has set the availability zones (allowed for non-BYOVPC clusters), don't prompt the BYOVPC message
if !useExistingVPC && !subnetsProvided && !isAvailabilityZonesSet && interactive.Enabled() {
existingVPCHelp := "To install into an existing VPC you need to ensure that your VPC is configured " +
Expand Down Expand Up @@ -2092,7 +2092,7 @@ func run(cmd *cobra.Command, _ []string) {
var subnets []ec2types.Subnet
mapSubnetIDToSubnet := make(map[string]aws.Subnet)
if useExistingVPC || subnetsProvided {
initialSubnets, err := getInitialValidSubnets(awsClient, args.subnetIDs, r.Reporter)
initialSubnets, err := getInitialValidSubnets(awsClient, subnetIDs, r.Reporter)
if err != nil {
r.Reporter.Errorf("Failed to get the list of subnets: %s", err)
os.Exit(1)
Expand Down
10 changes: 10 additions & 0 deletions pkg/helper/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,13 @@ func ChunkSlice[T any](slice []T, chunkSize int) [][]T {
func IsBYOVPC(cluster *cmv1.Cluster) bool {
return len(cluster.AWS().SubnetIDs()) > 0
}

func FilterEmpty(s []string) []string {
var r []string
for _, str := range s {
if str != "" {
r = append(r, str)
}
}
return r
}
8 changes: 8 additions & 0 deletions pkg/helper/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,13 @@ var _ = Describe("Helper", func() {
Entry("Uses the first 27 characters of the cluster name when the cluster name is > 27 chars",
strings.Repeat("a", 54), strings.Repeat("a", 27)))
})

var _ = Context("FilterEmpty()", func() {
It("OK: get filters subnets", func() {
subnets := []string{"test1", "test2", ""}
filteredSubnets := FilterEmpty(subnets)
Expect(filteredSubnets).To(Equal([]string{"test1", "test2"}))
})
})
})
})

0 comments on commit eb8027e

Please sign in to comment.