Skip to content

Commit 8020b13

Browse files
Add folder UID validation (#1551)
Closes #1543 Using the regex found here: https://github.com/grafana/grafana/blob/main/pkg/util/shortid_generator.go#L33-L35
1 parent c244dab commit 8020b13

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

internal/resources/grafana/resource_alerting_rule_group.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ This resource requires Grafana 9.1.0 or later.
5555
Description: "The name of the rule group.",
5656
},
5757
"folder_uid": {
58-
Type: schema.TypeString,
59-
Required: true,
60-
ForceNew: true,
61-
Description: "The UID of the folder that the group belongs to.",
58+
Type: schema.TypeString,
59+
Required: true,
60+
ForceNew: true,
61+
Description: "The UID of the folder that the group belongs to.",
62+
ValidateFunc: folderUIDValidation,
6263
},
6364
"interval_seconds": {
6465
Type: schema.TypeInt,

internal/resources/grafana/resource_folder.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package grafana
33
import (
44
"context"
55
"encoding/json"
6+
"regexp"
67
"strconv"
78

89
goapi "github.com/grafana/grafana-openapi-client-go/client"
@@ -13,8 +14,11 @@ import (
1314
"github.com/grafana/terraform-provider-grafana/v2/internal/common"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1618
)
1719

20+
var folderUIDValidation = validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9\-\_]+$`), "folder UIDs can only be alphanumeric, dashes, or underscores")
21+
1822
func resourceFolder() *common.Resource {
1923
schema := &schema.Resource{
2024

@@ -34,11 +38,12 @@ func resourceFolder() *common.Resource {
3438
Schema: map[string]*schema.Schema{
3539
"org_id": orgIDAttribute(),
3640
"uid": {
37-
Type: schema.TypeString,
38-
Computed: true,
39-
Optional: true,
40-
ForceNew: true,
41-
Description: "Unique identifier.",
41+
Type: schema.TypeString,
42+
Computed: true,
43+
Optional: true,
44+
ForceNew: true,
45+
Description: "Unique identifier.",
46+
ValidateFunc: folderUIDValidation,
4247
},
4348
"title": {
4449
Type: schema.TypeString,

internal/resources/grafana/resource_folder_permission.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ Manages the entire set of permissions for a folder. Permissions that aren't spec
3030

3131
Schema: map[string]*schema.Schema{
3232
"folder_uid": {
33-
Type: schema.TypeString,
34-
Required: true,
35-
ForceNew: true,
36-
Description: "The UID of the folder.",
33+
Type: schema.TypeString,
34+
Required: true,
35+
ForceNew: true,
36+
Description: "The UID of the folder.",
37+
ValidateFunc: folderUIDValidation,
3738
},
3839
},
3940
}

internal/resources/grafana/resource_library_panel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Manages Grafana library panels.
7373
_, new = SplitOrgResourceID(new)
7474
return old == new
7575
},
76+
ValidateFunc: folderUIDValidation,
7677
},
7778
"name": {
7879
Type: schema.TypeString,

0 commit comments

Comments
 (0)