Skip to content

Commit

Permalink
feat: add labels validations (#10387) (#17806)
Browse files Browse the repository at this point in the history
[upstream:e5ad07ac8e3776862d985a21ec2b5fff92c068a1]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Apr 9, 2024
1 parent 10f3226 commit 6102adf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/10387.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
storage: added labels validation to 'google_storage_bucket' resource
```
26 changes: 22 additions & 4 deletions google/services/storage/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"log"
"math"
"regexp"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6102adf

Please sign in to comment.