From 6102adfc5542860a89670025632fb0dc2c345b04 Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 9 Apr 2024 14:29:13 -0700 Subject: [PATCH] feat: add labels validations (#10387) (#17806) [upstream:e5ad07ac8e3776862d985a21ec2b5fff92c068a1] Signed-off-by: Modular Magician --- .changelog/10387.txt | 3 +++ .../storage/resource_storage_bucket.go | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 .changelog/10387.txt diff --git a/.changelog/10387.txt b/.changelog/10387.txt new file mode 100644 index 00000000000..b69d94c0d5b --- /dev/null +++ b/.changelog/10387.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +storage: added labels validation to 'google_storage_bucket' resource +``` \ No newline at end of file diff --git a/google/services/storage/resource_storage_bucket.go b/google/services/storage/resource_storage_bucket.go index deaee2d05a8..ec26e190962 100644 --- a/google/services/storage/resource_storage_bucket.go +++ b/google/services/storage/resource_storage_bucket.go @@ -9,6 +9,7 @@ import ( "fmt" "log" "math" + "regexp" "runtime" "strconv" "strings" @@ -94,10 +95,11 @@ func ResourceStorageBucket() *schema.Resource { }, "labels": { - Type: schema.TypeMap, - Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, - Description: `A set of key/value label pairs to assign to the bucket.`, + 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.`, }, "terraform_labels": { @@ -519,6 +521,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