Skip to content

Commit

Permalink
data_source_folder: Use OpenAPI client (#1134)
Browse files Browse the repository at this point in the history
Use https://github.com/grafana/grafana-openapi-client-go instead of the manually generated client
  • Loading branch information
julienduchesne authored Nov 8, 2023
1 parent e544ebb commit d193c97
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
4 changes: 4 additions & 0 deletions docs/data-sources/folder.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ data "grafana_folder" "from_title" {

- `title` (String) The name of the Grafana folder.

### Optional

- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.

### Read-Only

- `id` (Number) The numerical ID of the Grafana folder.
Expand Down
49 changes: 32 additions & 17 deletions internal/resources/grafana/data_source_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"strconv"

gapi "github.com/grafana/grafana-api-golang-client"
goapi "github.com/grafana/grafana-openapi-client-go/client"
"github.com/grafana/grafana-openapi-client-go/client/folders"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -19,6 +21,7 @@ func DatasourceFolder() *schema.Resource {
`,
ReadContext: dataSourceFolderRead,
Schema: map[string]*schema.Schema{
"org_id": orgIDAttribute(),
"title": {
Type: schema.TypeString,
Required: true,
Expand All @@ -43,34 +46,46 @@ func DatasourceFolder() *schema.Resource {
}
}

func findFolderWithTitle(client *gapi.Client, title string) (*gapi.Folder, error) {
folders, err := client.Folders()
if err != nil {
return nil, err
}
func findFolderWithTitle(client *goapi.GrafanaHTTPAPI, title string) (*models.Folder, error) {
var page int64 = 1

for _, f := range folders {
if f.Title == title {
// Query the folder by UID, that API has additional information
return client.FolderByUID(f.UID)
for {
params := folders.NewGetFoldersParams().WithPage(&page)
resp, err := client.Folders.GetFolders(params, nil)
if err != nil {
return nil, err
}

if len(resp.Payload) == 0 {
return nil, fmt.Errorf("folder with title %s not found", title)
}

for _, folder := range resp.Payload {
if folder.Title == title {
getParams := folders.NewGetFolderByUIDParams().WithFolderUID(folder.UID)
resp, err := client.Folders.GetFolderByUID(getParams, nil)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
}
}

return nil, fmt.Errorf("no folder with title %q", title)
page++
}
}

func dataSourceFolderRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
metaClient := meta.(*common.Client)
client := meta.(*common.Client).GrafanaAPI
title := d.Get("title").(string)
folder, err := findFolderWithTitle(client, title)
client, orgID := OAPIClientFromNewOrgResource(meta, d)

folder, err := findFolderWithTitle(client, d.Get("title").(string))
if err != nil {
return diag.FromErr(err)
}

id := strconv.FormatInt(folder.ID, 10)
d.SetId(id)
d.SetId(strconv.FormatInt(folder.ID, 10))
d.Set("org_id", strconv.FormatInt(orgID, 10))
d.Set("uid", folder.UID)
d.Set("title", folder.Title)
d.Set("url", metaClient.GrafanaSubpath(folder.URL))
Expand Down

0 comments on commit d193c97

Please sign in to comment.