-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
�zurerm_machine_learning_compute_cluster - validate name
- Loading branch information
1 parent
5de26c0
commit 091287e
Showing
3 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
internal/services/machinelearning/validate/compute_cluster_name.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
func ComputeClusterName(i interface{}, k string) (warnings []string, errors []error) { | ||
v, ok := i.(string) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %q to be string", k)) | ||
return | ||
} | ||
|
||
// The portal says: It can include letters, digits and dashes. It must start with a letter, end with a letter or digit, and be between 3 and 32 characters in length. | ||
// If you provide invalid name, the rest api will return an error with the following regex. | ||
if matched := regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9-]{1,30}[a-zA-Z0-9]$`).Match([]byte(v)); !matched { | ||
errors = append(errors, fmt.Errorf("%s must be between 3 and 32 characters, and may only include alphanumeric characters and '-' and must start with a letter, end with a letter or digit", k)) | ||
} | ||
return | ||
} |
84 changes: 84 additions & 0 deletions
84
internal/services/machinelearning/validate/compute_cluster_name_tests.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package validate | ||
|
||
import "testing" | ||
|
||
func TestComputeClusterName(t *testing.T) { | ||
testData := []struct { | ||
input string | ||
expected bool | ||
}{ | ||
{ | ||
// empty | ||
input: "", | ||
expected: false, | ||
}, | ||
{ | ||
// basic example | ||
input: "hello", | ||
expected: true, | ||
}, | ||
{ | ||
// cannot start with a hyphen | ||
input: "-hello", | ||
expected: false, | ||
}, | ||
{ | ||
// cannot start with a digit | ||
input: "0hello", | ||
expected: false, | ||
}, | ||
{ | ||
// can end with a hyphen | ||
input: "hello-", | ||
expected: true, | ||
}, | ||
{ | ||
// cannot contain other special symbols other than hyphens | ||
input: "hello.world", | ||
expected: false, | ||
}, | ||
{ | ||
// undescore in the middle | ||
input: "hello_world", | ||
expected: false, | ||
}, | ||
{ | ||
// hyphen in the middle | ||
input: "hello-world", | ||
expected: true, | ||
}, | ||
{ | ||
// 2 chars | ||
input: "ab", | ||
expected: false, | ||
}, | ||
{ | ||
// 3 chars | ||
input: "abc", | ||
expected: true, | ||
}, | ||
{ | ||
// 32 chars | ||
input: "abcdefghijklmnopqrstuvwxyzabcdef", | ||
expected: true, | ||
}, | ||
{ | ||
// 33 chars | ||
input: "abcdefghijklmnopqrstuvwxyzabcdefg", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q", v.input) | ||
|
||
_, errors := ComputeClusterName(v.input, "name") | ||
actual := len(errors) == 0 | ||
if v.expected != actual { | ||
t.Fatalf("Expected %t but got %t", v.expected, actual) | ||
} | ||
} | ||
} |