Skip to content

Commit 6d9205f

Browse files
Add folder update (#447)
* Add folder update Allows updating either the `uid` or the `title` attributes Requires grafana/grafana-api-golang-client#75 Closes #442 * Fix `strings.Replace`
1 parent f077402 commit 6d9205f

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/Masterminds/semver/v3 v3.1.1
7-
github.com/grafana/grafana-api-golang-client v0.4.8
7+
github.com/grafana/grafana-api-golang-client v0.5.0
88
github.com/grafana/machine-learning-go-client v0.1.1
99
github.com/grafana/synthetic-monitoring-agent v0.8.0
1010
github.com/grafana/synthetic-monitoring-api-go-client v0.6.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8
113113
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
114114
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
115115
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
116-
github.com/grafana/grafana-api-golang-client v0.4.8 h1:0I4e679ojcGHlS4OxmhHKECFYG/nIrrjBpTkQCGRp8A=
117-
github.com/grafana/grafana-api-golang-client v0.4.8/go.mod h1:24W29gPe9yl0/3A9X624TPkAOR8DpHno490cPwnkv8E=
116+
github.com/grafana/grafana-api-golang-client v0.5.0 h1:wLb8izoz4rEtgwpD6ORDmpqWnKoLUzGspFjX1n1e00o=
117+
github.com/grafana/grafana-api-golang-client v0.5.0/go.mod h1:24W29gPe9yl0/3A9X624TPkAOR8DpHno490cPwnkv8E=
118118
github.com/grafana/machine-learning-go-client v0.1.1 h1:Gw6cX8xAd6IVF2LApkXOIdBK8Gzz07B3jQPukecw7fc=
119119
github.com/grafana/machine-learning-go-client v0.1.1/go.mod h1:QFfZz8NkqVF8++skjkKQXJEZfpCYd8S0yTWJUpsLLTA=
120120
github.com/grafana/synthetic-monitoring-agent v0.8.0 h1:X2fMX8J9SKLk9g4jvnNOmMsv0zFOjRP5Rv6f1TGMVzI=

grafana/provider_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ func testAccExample(t *testing.T, path string) string {
188188
return string(example)
189189
}
190190

191+
// testAccExampleWithReplace works like testAccExample, but replaces strings in the example.
192+
func testAccExampleWithReplace(t *testing.T, path string, replaceMap map[string]string) string {
193+
example := testAccExample(t, path)
194+
for k, v := range replaceMap {
195+
example = strings.ReplaceAll(example, k, v)
196+
}
197+
return example
198+
}
199+
191200
func accTestsEnabled(t *testing.T, envVarName string) bool {
192201
v, ok := os.LookupEnv(envVarName)
193202
if !ok {

grafana/resource_folder.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func ResourceFolder() *schema.Resource {
2323
CreateContext: CreateFolder,
2424
DeleteContext: DeleteFolder,
2525
ReadContext: ReadFolder,
26+
UpdateContext: UpdateFolder,
2627
Importer: &schema.ResourceImporter{
2728
StateContext: schema.ImportStatePassthroughContext,
2829
},
@@ -37,13 +38,11 @@ func ResourceFolder() *schema.Resource {
3738
Type: schema.TypeString,
3839
Computed: true,
3940
Optional: true,
40-
ForceNew: true,
4141
Description: "Unique identifier.",
4242
},
4343
"title": {
4444
Type: schema.TypeString,
4545
Required: true,
46-
ForceNew: true,
4746
Description: "The title of the folder.",
4847
},
4948
"url": {
@@ -79,6 +78,18 @@ func CreateFolder(ctx context.Context, d *schema.ResourceData, meta interface{})
7978
return ReadFolder(ctx, d, meta)
8079
}
8180

81+
func UpdateFolder(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
82+
client := meta.(*client).gapi
83+
84+
oldUID, newUID := d.GetChange("uid")
85+
86+
if err := client.UpdateFolder(oldUID.(string), d.Get("title").(string), newUID.(string)); err != nil {
87+
return diag.FromErr(err)
88+
}
89+
90+
return ReadFolder(ctx, d, meta)
91+
}
92+
8293
func ReadFolder(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
8394
gapiURL := meta.(*client).gapiURL
8495
client := meta.(*client).gapi

grafana/resource_folder_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,42 @@ func TestAccFolder_basic(t *testing.T) {
5151
ImportState: true,
5252
ImportStateVerify: true,
5353
},
54+
// Change the title of one folder, change the UID of the other. They shouldn't change IDs (the folder doesn't have to be recreated)
55+
{
56+
Config: testAccExampleWithReplace(t, "resources/grafana_folder/resource.tf", map[string]string{
57+
"Terraform Test Folder": "Terraform Test Folder Updated",
58+
"test-folder-uid": "test-folder-uid-other",
59+
}),
60+
Check: resource.ComposeTestCheckFunc(
61+
testAccFolderIDDidntChange("grafana_folder.test_folder", &folder),
62+
resource.TestMatchResourceAttr("grafana_folder.test_folder", "id", idRegexp),
63+
resource.TestMatchResourceAttr("grafana_folder.test_folder", "uid", uidRegexp),
64+
resource.TestCheckResourceAttr("grafana_folder.test_folder", "title", "Terraform Test Folder Updated"),
65+
66+
testAccFolderIDDidntChange("grafana_folder.test_folder_with_uid", &folderWithUID),
67+
resource.TestMatchResourceAttr("grafana_folder.test_folder_with_uid", "id", idRegexp),
68+
resource.TestCheckResourceAttr("grafana_folder.test_folder_with_uid", "uid", "test-folder-uid-other"),
69+
resource.TestCheckResourceAttr("grafana_folder.test_folder_with_uid", "title", "Terraform Test Folder Updated With UID"),
70+
),
71+
},
5472
},
5573
})
5674
}
5775

76+
func testAccFolderIDDidntChange(rn string, folder *gapi.Folder) resource.TestCheckFunc {
77+
return func(s *terraform.State) error {
78+
oldID := strconv.FormatInt(folder.ID, 10)
79+
newFolder, ok := s.RootModule().Resources[rn]
80+
if !ok {
81+
return fmt.Errorf("folder not found: %s", rn)
82+
}
83+
if newFolder.Primary.ID != oldID {
84+
return fmt.Errorf("folder id has changed: %s -> %s", oldID, newFolder.Primary.ID)
85+
}
86+
return nil
87+
}
88+
}
89+
5890
func testAccFolderCheckExists(rn string, folder *gapi.Folder) resource.TestCheckFunc {
5991
return func(s *terraform.State) error {
6092
rs, ok := s.RootModule().Resources[rn]

0 commit comments

Comments
 (0)