Skip to content

Commit

Permalink
add cluster name validation to infractl
Browse files Browse the repository at this point in the history
  • Loading branch information
tommartensen committed Sep 8, 2023
1 parent 2a6c61d commit aa9966b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
26 changes: 25 additions & 1 deletion cmd/infractl/cluster/create/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ func run(ctx context.Context, conn *grpc.ClientConn, cmd *cobra.Command, args []
displayUserNotes(cmd, args, &req)

if len(args) > 1 {
req.Parameters["name"] = args[1]
name := args[1]
err := validateName(name)
if err != nil {
return nil, err
}
req.Parameters["name"] = name
} else {
name, err := determineName(ctx, conn, args[0])
if err != nil {
Expand Down Expand Up @@ -146,6 +151,25 @@ func run(ctx context.Context, conn *grpc.ClientConn, cmd *cobra.Command, args []
return prettyResourceByID(*clusterID), nil
}

func validateName(name string) error {
if len(name) < 3 {
return errors.New("cluster name too short")
}
if len(name) > 28 {
return errors.New("cluster name too long")
}

match, err := regexp.MatchString(`^(?:[a-z](?:[-a-z0-9]{0,28}[a-z0-9])?)$`, name)
if err != nil {
return err
}
if !match {
return errors.New("The name does not match its requirements. Only lowercase letters, numbers, and '-' allowed, must start with a letter and end with a letter or number.")
}

return nil
}

func determineWorkingEnvironment() {
workingEnvironment.gitTopLevel = ""
workingEnvironment.tag = ""
Expand Down
10 changes: 10 additions & 0 deletions cmd/infractl/cluster/create/create.bats
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ setup() {
assert_output --partial "parameter \"main-image\" was not provided"
}

@test "provided name is too short" {
run infractl create test-qa-demo ab
assert_failure
assert_output --partial "Error: cluster name too short"
}

# @test "provided name is too long"

# @test "provided name does not match regex"

infractl() {
"$ROOT"/bin/infractl -e localhost:8443 -k "$@"
}
Expand Down

0 comments on commit aa9966b

Please sign in to comment.