-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
grafana_library_panels
datasource (#1651)
Closes #376 Just doing this one to have a first plugin framework datasource
- Loading branch information
1 parent
8ab0a2c
commit f14dcad
Showing
11 changed files
with
302 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "grafana_library_panels Data Source - terraform-provider-grafana" | ||
subcategory: "Grafana OSS" | ||
description: |- | ||
--- | ||
|
||
# grafana_library_panels (Data Source) | ||
|
||
|
||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "grafana_library_panel" "test" { | ||
name = "panelname" | ||
model_json = jsonencode({ | ||
title = "test name" | ||
type = "text" | ||
version = 0 | ||
description = "test description" | ||
}) | ||
} | ||
resource "grafana_folder" "test" { | ||
title = "Panel Folder" | ||
uid = "panelname-folder" | ||
} | ||
resource "grafana_library_panel" "folder" { | ||
name = "panelname In Folder" | ||
folder_uid = grafana_folder.test.uid | ||
model_json = jsonencode({ | ||
gridPos = { | ||
x = 0 | ||
y = 0 | ||
h = 10 | ||
w = 10 | ||
} | ||
title = "panel" | ||
type = "text" | ||
version = 0 | ||
}) | ||
} | ||
data "grafana_library_panels" "all" { | ||
depends_on = [grafana_library_panel.folder, grafana_library_panel.test] | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Optional | ||
|
||
- `org_id` (String) The Organization ID. If not set, the default organization is used for basic authentication, or the one that owns your service account for token authentication. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `panels` (Set of Object) (see [below for nested schema](#nestedatt--panels)) | ||
|
||
<a id="nestedatt--panels"></a> | ||
### Nested Schema for `panels` | ||
|
||
Read-Only: | ||
|
||
- `description` (String) | ||
- `folder_uid` (String) | ||
- `model_json` (String) | ||
- `name` (String) | ||
- `uid` (String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
examples/data-sources/grafana_library_panels/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
resource "grafana_library_panel" "test" { | ||
name = "panelname" | ||
model_json = jsonencode({ | ||
title = "test name" | ||
type = "text" | ||
version = 0 | ||
description = "test description" | ||
}) | ||
} | ||
|
||
resource "grafana_folder" "test" { | ||
title = "Panel Folder" | ||
uid = "panelname-folder" | ||
} | ||
|
||
resource "grafana_library_panel" "folder" { | ||
name = "panelname In Folder" | ||
folder_uid = grafana_folder.test.uid | ||
model_json = jsonencode({ | ||
gridPos = { | ||
x = 0 | ||
y = 0 | ||
h = 10 | ||
w = 10 | ||
} | ||
title = "panel" | ||
type = "text" | ||
version = 0 | ||
}) | ||
} | ||
|
||
data "grafana_library_panels" "all" { | ||
depends_on = [grafana_library_panel.folder, grafana_library_panel.test] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
internal/resources/grafana/data_source_library_panels.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package grafana | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/grafana/grafana-openapi-client-go/client/library_elements" | ||
"github.com/grafana/terraform-provider-grafana/v3/internal/common" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var dataSourceLibraryPanelsName = "grafana_library_panels" | ||
|
||
func datasourceLibraryPanels() *common.DataSource { | ||
return common.NewDataSource( | ||
common.CategoryGrafanaOSS, | ||
dataSourceLibraryPanelsName, | ||
&libraryPanelsDataSource{}, | ||
) | ||
} | ||
|
||
type libraryPanelsDataSource struct { | ||
basePluginFrameworkDataSource | ||
} | ||
|
||
func (r *libraryPanelsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = dataSourceLibraryPanelsName | ||
} | ||
|
||
func (r *libraryPanelsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"org_id": pluginFrameworkOrgIDAttribute(), | ||
"panels": schema.SetAttribute{ | ||
Computed: true, | ||
ElementType: types.ObjectType{ | ||
AttrTypes: map[string]attr.Type{ | ||
"name": types.StringType, | ||
"uid": types.StringType, | ||
"description": types.StringType, | ||
"folder_uid": types.StringType, | ||
"model_json": types.StringType, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
type libraryPanelsDataSourcePanelModel struct { | ||
Name types.String `tfsdk:"name"` | ||
UID types.String `tfsdk:"uid"` | ||
Description types.String `tfsdk:"description"` | ||
FolderUID types.String `tfsdk:"folder_uid"` | ||
ModelJSON types.String `tfsdk:"model_json"` | ||
} | ||
|
||
type libraryPanelsDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
OrgID types.String `tfsdk:"org_id"` | ||
Panels []libraryPanelsDataSourcePanelModel `tfsdk:"panels"` | ||
} | ||
|
||
func (r *libraryPanelsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
// Read Terraform state data into the model | ||
var data libraryPanelsDataSourceModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
|
||
// Read from API | ||
client, _, err := r.clientFromNewOrgResource(data.OrgID.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics = diag.Diagnostics{diag.NewErrorDiagnostic("Failed to create client", err.Error())} | ||
return | ||
} | ||
params := library_elements.NewGetLibraryElementsParams().WithKind(common.Ref(libraryPanelKind)) | ||
apiResp, err := client.LibraryElements.GetLibraryElements(params) | ||
if err != nil { | ||
resp.Diagnostics = diag.Diagnostics{diag.NewErrorDiagnostic("Failed to get library panels", err.Error())} | ||
return | ||
} | ||
for _, panel := range apiResp.Payload.Result.Elements { | ||
modelJSONBytes, err := json.Marshal(panel.Model) | ||
if err != nil { | ||
resp.Diagnostics = diag.Diagnostics{diag.NewErrorDiagnostic("Failed to get library panel JSON", err.Error())} | ||
return | ||
} | ||
data.Panels = append(data.Panels, libraryPanelsDataSourcePanelModel{ | ||
Name: types.StringValue(panel.Name), | ||
UID: types.StringValue(panel.UID), | ||
Description: types.StringValue(panel.Description), | ||
FolderUID: types.StringValue(panel.Meta.FolderUID), | ||
ModelJSON: types.StringValue(string(modelJSONBytes)), | ||
}) | ||
} | ||
data.ID = types.StringValue(data.OrgID.ValueString()) | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} |
38 changes: 38 additions & 0 deletions
38
internal/resources/grafana/data_source_library_panels_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package grafana_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/grafana/terraform-provider-grafana/v3/internal/testutils" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDatasourceLibraryPanels_basic(t *testing.T) { | ||
testutils.CheckOSSTestsEnabled(t, ">=8.0.0") | ||
|
||
randomName := acctest.RandString(10) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testutils.TestAccExampleWithReplace(t, "data-sources/grafana_library_panels/data-source.tf", map[string]string{ | ||
"panelname": randomName, | ||
}), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckTypeSetElemNestedAttrs("data.grafana_library_panels.all", "panels.*", map[string]string{ | ||
"description": "test description", | ||
"folder_uid": "", | ||
"panels.0.name": randomName, | ||
}), | ||
resource.TestCheckTypeSetElemNestedAttrs("data.grafana_library_panels.all", "panels.*", map[string]string{ | ||
"description": "", | ||
"folder_uid": randomName + "-folder", | ||
"panels.0.name": randomName + " In Folder", | ||
}), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters