Skip to content

Commit

Permalink
feat: add labels validations (#10387)
Browse files Browse the repository at this point in the history
  • Loading branch information
EladLeev authored Apr 9, 2024
1 parent ca3b14e commit e5ad07a
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"log"
"math"
"regexp"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -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.`,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e5ad07a

Please sign in to comment.