Skip to content

Commit

Permalink
Create cluster - validate subnets count interactively
Browse files Browse the repository at this point in the history
Use the interactive validation to provide fast feedback to the user,
to achieve a better user experience.

Related: SDA-6331
  • Loading branch information
oriAdler committed Jul 5, 2022
1 parent 52d5a43 commit c16927a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 35 deletions.
45 changes: 10 additions & 35 deletions cmd/create/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,9 @@ func run(cmd *cobra.Command, _ []string) {
Required: false,
Options: options,
Default: defaultOptions,
Validators: []interactive.Validator{
interactive.SubnetsCountValidator(multiAZ, privateLink),
},
})
if err != nil {
r.Reporter.Errorf("Expected valid subnet IDs: %s", err)
Expand All @@ -1280,10 +1283,13 @@ func run(cmd *cobra.Command, _ []string) {
}
}

err = validateSubnetsCount(multiAZ, privateLink, len(subnetIDs))
if err != nil {
r.Reporter.Errorf("%s", err)
os.Exit(1)
// Validate subnets in the case the user has provided them using the `args.subnets`
if subnetsProvided {
err = aws.ValidateSubnetsCount(multiAZ, privateLink, len(subnetIDs))
if err != nil {
r.Reporter.Errorf("%s", err)
os.Exit(1)
}
}

for _, subnet := range subnetIDs {
Expand Down Expand Up @@ -2100,37 +2106,6 @@ func validateExpiration() (expiration time.Time, err error) {
return
}

const (
BYOVPCSingleAZSubnetsCount = 2
BYOVPCMultiAZSubnetsCount = 6
privateLinkSingleAZSubnetsCount = 1
privateLinkMultiAZSubnetsCount = 3
)

func validateSubnetsCount(multiAZ bool, privateLink bool, subnetsInputCount int) error {
if privateLink {
if multiAZ && subnetsInputCount != privateLinkMultiAZSubnetsCount {
return fmt.Errorf("The number of subnets for a multi-AZ private link cluster should be %d, "+
"instead received: %d", privateLinkMultiAZSubnetsCount, subnetsInputCount)
}
if !multiAZ && subnetsInputCount != privateLinkSingleAZSubnetsCount {
return fmt.Errorf("The number of subnets for a single AZ private link cluster should be %d, "+
"instead received: %d", privateLinkSingleAZSubnetsCount, subnetsInputCount)
}
} else {
if multiAZ && subnetsInputCount != BYOVPCMultiAZSubnetsCount {
return fmt.Errorf("The number of subnets for a multi-AZ cluster should be %d, "+
"instead received: %d", BYOVPCMultiAZSubnetsCount, subnetsInputCount)
}
if !multiAZ && subnetsInputCount != BYOVPCSingleAZSubnetsCount {
return fmt.Errorf("The number of subnets for a single AZ cluster should be %d, "+
"instead received: %d", BYOVPCSingleAZSubnetsCount, subnetsInputCount)
}
}

return nil
}

const (
singleAZCount = 1
multiAZCount = 3
Expand Down
31 changes: 31 additions & 0 deletions pkg/aws/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,34 @@ func SetSubnetOption(subnet, zone string) string {
func ParseSubnet(subnetOption string) string {
return strings.Split(subnetOption, " ")[0]
}

const (
BYOVPCSingleAZSubnetsCount = 2
BYOVPCMultiAZSubnetsCount = 6
privateLinkSingleAZSubnetsCount = 1
privateLinkMultiAZSubnetsCount = 3
)

func ValidateSubnetsCount(multiAZ bool, privateLink bool, subnetsInputCount int) error {
if privateLink {
if multiAZ && subnetsInputCount != privateLinkMultiAZSubnetsCount {
return fmt.Errorf("The number of subnets for a multi-AZ private link cluster should be %d, "+
"instead received: %d", privateLinkMultiAZSubnetsCount, subnetsInputCount)
}
if !multiAZ && subnetsInputCount != privateLinkSingleAZSubnetsCount {
return fmt.Errorf("The number of subnets for a single AZ private link cluster should be %d, "+
"instead received: %d", privateLinkSingleAZSubnetsCount, subnetsInputCount)
}
} else {
if multiAZ && subnetsInputCount != BYOVPCMultiAZSubnetsCount {
return fmt.Errorf("The number of subnets for a multi-AZ cluster should be %d, "+
"instead received: %d", BYOVPCMultiAZSubnetsCount, subnetsInputCount)
}
if !multiAZ && subnetsInputCount != BYOVPCSingleAZSubnetsCount {
return fmt.Errorf("The number of subnets for a single AZ cluster should be %d, "+
"instead received: %d", BYOVPCSingleAZSubnetsCount, subnetsInputCount)
}
}

return nil
}
14 changes: 14 additions & 0 deletions pkg/interactive/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"regexp"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/openshift/rosa/pkg/aws"
)

const doubleQuotesToRemove = "\"\""
Expand Down Expand Up @@ -134,3 +136,15 @@ func RegExpBoolean(restr string) Validator {
return fmt.Errorf("can only validate boolean values, got %v", val)
}
}

// SubnetsCountValidator get a slice of `[]core.OptionAnswer` as an interface.
// e.g. core.OptionAnswer { Value: subnet-04f67939f44a97dbe (us-west-2b), Index: 0 }
func SubnetsCountValidator(multiAZ bool, privateLink bool) Validator {
return func(input interface{}) error {
if answers, ok := input.([]core.OptionAnswer); ok {
return aws.ValidateSubnetsCount(multiAZ, privateLink, len(answers))
}

return fmt.Errorf("can only validate a slice of string, got %v", input)
}
}

0 comments on commit c16927a

Please sign in to comment.