Skip to content

Commit 0367f15

Browse files
Feature: data source folder (#312)
* Add Folder Datasource Add Folder Datasource Add Folder Datasource * Update grafana/data_source_folder_test.go Co-authored-by: Julien Duchesne <[email protected]> * Add regexp import Co-authored-by: Puneeth Nanjundaswamy <[email protected]> Co-authored-by: Puneeth <[email protected]>
1 parent aa0b8be commit 0367f15

File tree

5 files changed

+157
-1
lines changed

5 files changed

+157
-1
lines changed

docs/data-sources/folder.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_folder Data Source - terraform-provider-grafana"
4+
subcategory: ""
5+
description: |-
6+
Official documentation https://grafana.com/docs/grafana/latest/dashboards/dashboard_folders/HTTP API https://grafana.com/docs/grafana/latest/http_api/folder/
7+
---
8+
9+
# grafana_folder (Data Source)
10+
11+
* [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/dashboard_folders/)
12+
* [HTTP API](https://grafana.com/docs/grafana/latest/http_api/folder/)
13+
14+
## Example Usage
15+
16+
```terraform
17+
resource "grafana_folder" "test" {
18+
title = "test-folder"
19+
}
20+
21+
data "grafana_folder" "from_title" {
22+
title = grafana_folder.test.title
23+
}
24+
```
25+
26+
<!-- schema generated by tfplugindocs -->
27+
## Schema
28+
29+
### Required
30+
31+
- **title** (String) The name of the Grafana folder.
32+
33+
### Read-Only
34+
35+
- **id** (Number) The numerical ID of the Grafana folder.
36+
- **uid** (String) The uid of the Grafana folder.
37+
38+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "grafana_folder" "test" {
2+
title = "test-folder"
3+
}
4+
5+
data "grafana_folder" "from_title" {
6+
title = grafana_folder.test.title
7+
}

grafana/data_source_folder.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package grafana
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
8+
gapi "github.com/grafana/grafana-api-golang-client"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
)
12+
13+
func DatasourceFolder() *schema.Resource {
14+
return &schema.Resource{
15+
Description: `
16+
* [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/dashboard_folders/)
17+
* [HTTP API](https://grafana.com/docs/grafana/latest/http_api/folder/)
18+
`,
19+
ReadContext: dataSourceFolderRead,
20+
Schema: map[string]*schema.Schema{
21+
"title": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
Description: "The name of the Grafana folder.",
25+
},
26+
"id": {
27+
Type: schema.TypeInt,
28+
Computed: true,
29+
Description: "The numerical ID of the Grafana folder.",
30+
},
31+
"uid": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
Description: "The uid of the Grafana folder.",
35+
},
36+
},
37+
}
38+
}
39+
40+
func findFolderWithTitle(client *gapi.Client, title string) (*gapi.Folder, error) {
41+
folders, err := client.Folders()
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
for _, f := range folders {
47+
if f.Title == title {
48+
return &f, nil
49+
}
50+
}
51+
52+
return nil, fmt.Errorf("no folder with title %q", title)
53+
}
54+
55+
func dataSourceFolderRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
56+
client := meta.(*client).gapi
57+
title := d.Get("title").(string)
58+
folder, err := findFolderWithTitle(client, title)
59+
60+
if err != nil {
61+
return diag.FromErr(err)
62+
}
63+
64+
id := strconv.FormatInt(folder.ID, 10)
65+
d.SetId(id)
66+
d.Set("uid", folder.UID)
67+
d.Set("title", folder.Title)
68+
69+
return nil
70+
}

grafana/data_source_folder_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//go:build oss
2+
// +build oss
3+
4+
package grafana
5+
6+
import (
7+
"regexp"
8+
"testing"
9+
10+
gapi "github.com/grafana/grafana-api-golang-client"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
)
13+
14+
func TestAccDatasourceFolder(t *testing.T) {
15+
var folder gapi.Folder
16+
checks := []resource.TestCheckFunc{
17+
testAccFolderCheckExists("grafana_folder.test", &folder),
18+
resource.TestCheckResourceAttr(
19+
"data.grafana_folder.from_title", "title", "test-folder",
20+
),
21+
resource.TestMatchResourceAttr(
22+
"data.grafana_folder.from_title", "id", regexp.MustCompile(`^\d+$`),
23+
),
24+
resource.TestMatchResourceAttr(
25+
"data.grafana_folder.from_title", "uid", regexp.MustCompile(`^[a-zA-Z0-9-]+$`),
26+
),
27+
}
28+
29+
resource.Test(t, resource.TestCase{
30+
PreCheck: func() { testAccPreCheck(t) },
31+
ProviderFactories: testAccProviderFactories,
32+
CheckDestroy: testAccFolderCheckDestroy(&folder),
33+
Steps: []resource.TestStep{
34+
{
35+
Config: testAccExample(t, "data-sources/grafana_folder/data-source.tf"),
36+
Check: resource.ComposeTestCheckFunc(checks...),
37+
},
38+
},
39+
})
40+
}

grafana/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ func Provider(version string) func() *schema.Provider {
122122

123123
DataSourcesMap: map[string]*schema.Resource{
124124
// Grafana
125-
"grafana_user": DatasourceUser(),
125+
"grafana_folder": DatasourceFolder(),
126+
"grafana_user": DatasourceUser(),
126127

127128
// Synthetic Monitoring
128129
"grafana_synthetic_monitoring_probe": dataSourceSyntheticMonitoringProbe(),

0 commit comments

Comments
 (0)