From e5ad07ac8e3776862d985a21ec2b5fff92c068a1 Mon Sep 17 00:00:00 2001 From: Elad Leev Date: Tue, 9 Apr 2024 22:25:30 +0100 Subject: [PATCH] feat: add labels validations (#10387) --- .../storage/resource_storage_bucket.go.erb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/mmv1/third_party/terraform/services/storage/resource_storage_bucket.go.erb b/mmv1/third_party/terraform/services/storage/resource_storage_bucket.go.erb index 29c050cae447..a7950ee34bc6 100644 --- a/mmv1/third_party/terraform/services/storage/resource_storage_bucket.go.erb +++ b/mmv1/third_party/terraform/services/storage/resource_storage_bucket.go.erb @@ -8,6 +8,7 @@ import ( "fmt" "log" "math" + "regexp" "runtime" "strconv" "strings" @@ -94,6 +95,7 @@ func ResourceStorageBucket() *schema.Resource { "labels": { Type: schema.TypeMap, + ValidateFunc: labelKeyValidator, Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, Description: `A set of key/value label pairs to assign to the bucket.`, @@ -518,6 +520,22 @@ func ResourceStorageBucket() *schema.Resource { const resourceDataplexGoogleLabelPrefix = "goog-dataplex" const resourceDataplexGoogleProvidedLabelPrefix = "labels." + resourceDataplexGoogleLabelPrefix +var labelKeyRegex = regexp.MustCompile(`^[a-z0-9_-]{1,63}$`) + +func labelKeyValidator(val interface{}, key string) (warns []string, errs []error) { + if val == nil { + return + } + + m := val.(map[string]interface{}) + for k := range m { + if !labelKeyRegex.MatchString(k) { + errs = append(errs, fmt.Errorf("%q is an invalid label key. See https://cloud.google.com/storage/docs/tags-and-labels#bucket-labels", k)) + } + } + return +} + func resourceDataplexLabelDiffSuppress(k, old, new string, d *schema.ResourceData) bool { if strings.HasPrefix(k, resourceDataplexGoogleProvidedLabelPrefix) && new == "" { return true