From 091287e40639d14c4c234baca8130e41e9662f66 Mon Sep 17 00:00:00 2001 From: Alexander Pykavy Date: Wed, 22 May 2024 16:14:11 +0200 Subject: [PATCH] zurerm_machine_learning_compute_cluster - validate name --- ...chine_learning_compute_cluster_resource.go | 7 +- .../validate/compute_cluster_name.go | 24 ++++++ .../validate/compute_cluster_name_tests.go | 84 +++++++++++++++++++ 3 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 internal/services/machinelearning/validate/compute_cluster_name.go create mode 100644 internal/services/machinelearning/validate/compute_cluster_name_tests.go diff --git a/internal/services/machinelearning/machine_learning_compute_cluster_resource.go b/internal/services/machinelearning/machine_learning_compute_cluster_resource.go index 5193ca81381e1..1286883ed5506 100644 --- a/internal/services/machinelearning/machine_learning_compute_cluster_resource.go +++ b/internal/services/machinelearning/machine_learning_compute_cluster_resource.go @@ -41,9 +41,10 @@ func resourceComputeCluster() *pluginsdk.Resource { Schema: map[string]*pluginsdk.Schema{ "name": { - Type: pluginsdk.TypeString, - Required: true, - ForceNew: true, + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.ComputeClusterName, }, "machine_learning_workspace_id": { diff --git a/internal/services/machinelearning/validate/compute_cluster_name.go b/internal/services/machinelearning/validate/compute_cluster_name.go new file mode 100644 index 0000000000000..cc31cd9b5f398 --- /dev/null +++ b/internal/services/machinelearning/validate/compute_cluster_name.go @@ -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 +} diff --git a/internal/services/machinelearning/validate/compute_cluster_name_tests.go b/internal/services/machinelearning/validate/compute_cluster_name_tests.go new file mode 100644 index 0000000000000..b863781c9f04c --- /dev/null +++ b/internal/services/machinelearning/validate/compute_cluster_name_tests.go @@ -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) + } + } +}