Skip to content

Commit 2f0f121

Browse files
authored
grafana_folder: make uid field computed when not specified (#2314)
* grafana_folder: make uid field computed when not specified Add Computed: true to uid field schema to allow it to be populated from API response when not provided by user. This enables the data source to be used in resource references that depend on the uid value. * Add grafana version check.
1 parent 752c4f4 commit 2f0f121

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

internal/resources/grafana/data_source_folder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func datasourceFolder() *common.DataSource {
3030
"uid": {
3131
Type: schema.TypeString,
3232
Optional: true,
33+
Computed: true, // If not set by user, this will be populated by reading the folder.
3334
Description: "The uid of the folder. If not set, only the title of the folder is used to find the folder.",
3435
},
3536
"prevent_destroy_if_not_empty": nil,

internal/resources/grafana/data_source_folder_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,53 @@ data "grafana_folder" "f3" {
257257
}
258258
`, name)
259259
}
260+
261+
func TestAccDatasourceFolderUidSetCorrectly(t *testing.T) {
262+
// Test uses nested folders feature, which was introduced in Grafana 10 (behind feature-toggle) and made GA in Grafana 11.
263+
testutils.CheckOSSTestsEnabled(t, ">=10.3.0")
264+
265+
var folder models.Folder
266+
randomName := acctest.RandStringFromCharSet(6, acctest.CharSetAlpha)
267+
268+
resource.ParallelTest(t, resource.TestCase{
269+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
270+
CheckDestroy: resource.ComposeTestCheckFunc(
271+
folderCheckExists.destroyed(&folder, nil),
272+
),
273+
Steps: []resource.TestStep{
274+
{
275+
// Find folder by title. We should get correct uid back.
276+
Config: fmt.Sprintf(`
277+
resource "grafana_folder" "folder" {
278+
title = "%[1]s"
279+
}
280+
data "grafana_folder" "test" {
281+
depends_on = ["grafana_folder.folder"]
282+
title = "%[1]s"
283+
}
284+
resource "grafana_folder" "nested_folder" {
285+
title = "%[1]s Nested"
286+
parent_folder_uid = data.grafana_folder.test.uid
287+
}
288+
`, randomName),
289+
Check: resource.ComposeTestCheckFunc(
290+
folderCheckExists.exists("grafana_folder.folder", &folder),
291+
resource.TestCheckResourceAttr("grafana_folder.nested_folder", "title", randomName+" Nested"),
292+
// Next line doesn't work, because resource.TestCheckResourceAttr is called too early -- before folder.UID is set.
293+
// resource.TestCheckResourceAttr("grafana_folder.nested_folder", "parent_folder_uid", folder.UID)
294+
//
295+
// This works:
296+
resource.TestCheckResourceAttrWith("grafana_folder.nested_folder", "parent_folder_uid", func(value string) error {
297+
if folder.UID == "" {
298+
return fmt.Errorf("grafana_folder.folder.uid should not be empty")
299+
}
300+
if value == folder.UID {
301+
return nil
302+
}
303+
return fmt.Errorf("expected uid to be %q but got %q", folder.UID, value)
304+
}),
305+
),
306+
},
307+
},
308+
})
309+
}

0 commit comments

Comments
 (0)