Skip to content

Commit 878e05e

Browse files
justinTMinkeljulienduchesne
authored
add data_source_library_panel (#370)
* Squashed commit of the following: commit 5aa74af Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:58:30 2022 -0800 get latest client fork, should fix 404 commit 26d19a2 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:42:00 2022 -0800 add dashboard_ids to resource and datasource commit 3021653 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:29:32 2022 -0800 update docs commit f30de96 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:29:23 2022 -0800 fix resource description whitespace commit e269c92 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:26:48 2022 -0800 get forked client latest commit 4d9f688 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:19:42 2022 -0800 add get by name commit 50d9e22 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:12:21 2022 -0800 fix Computed=false for uid in datasource commit 121bc5d Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:10:19 2022 -0800 fix name schema commit a4453da Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:07:46 2022 -0800 remove ExactlyOneOf for uid temporarily commit 7a33d51 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:00:21 2022 -0800 try refactoring library panel schemaa and datasource commit b16cb1f Author: Justin Mai <[email protected]> Date: Wed Feb 2 18:20:38 2022 -0800 try new test resource grafana_library_panel commit a13eefe Author: Justin Mai <[email protected]> Date: Wed Feb 2 18:13:51 2022 -0800 fix silly Go whitespace again commit 8916a8b Merge: 9678e64 0a08c26 Author: JM <[email protected]> Date: Wed Feb 2 18:10:27 2022 -0800 Merge branch 'master' into data_source_library_panel_connections commit 9678e64 Author: Justin Mai <[email protected]> Date: Wed Feb 2 18:08:00 2022 -0800 try fix data_source_library_panel commit 9d6575e Author: Justin Mai <[email protected]> Date: Tue Feb 1 19:49:35 2022 -0800 use new resource dame for datasource test commit af03cd6 Author: Justin Mai <[email protected]> Date: Tue Feb 1 19:44:42 2022 -0800 fix debug test commit 88d6552 Author: Justin Mai <[email protected]> Date: Tue Feb 1 19:40:06 2022 -0800 try to debug inconsistent 404 commit 8d4c30e Author: Justin Mai <[email protected]> Date: Tue Feb 1 19:18:47 2022 -0800 add folder_id to library panel commit d66c83a Author: Justin Mai <[email protected]> Date: Tue Feb 1 18:57:30 2022 -0800 fix silly Go whitespace issue commit 19ba00b Author: Justin Mai <[email protected]> Date: Tue Feb 1 18:50:31 2022 -0800 go mod tidy commit 28c76a8 Merge: a2a3717 524ee0d Author: JM <[email protected]> Date: Tue Feb 1 16:24:01 2022 -0800 Merge branch 'master' into data_source_library_panel_connections commit a2a3717 Author: Justin Mai <[email protected]> Date: Tue Feb 1 16:21:53 2022 -0800 update docs commit de062d1 Author: Justin Mai <[email protected]> Date: Tue Feb 1 16:21:03 2022 -0800 get resource name AFTER creation commit c22eff6 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:58:40 2022 -0800 reinstate original tests commit 11dda72 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:46 2022 -0800 go mod tidy commit 9626bc4 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:20 2022 -0800 Revert "go mod tidy" This reverts commit a090615. commit 8251f4a Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:18 2022 -0800 Revert "add example dashboard with connected library panels" This reverts commit 7fa71c9. commit e0d5c72 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:17 2022 -0800 Revert "fix example library panel dashboard" This reverts commit 898f65b. commit 9554434 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:15 2022 -0800 Revert "fix example" This reverts commit 544a498. commit d98bfe8 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:13 2022 -0800 Revert "fix example" This reverts commit d07b2ce. commit d07b2ce Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:50:02 2022 -0800 fix example commit 544a498 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:19:22 2022 -0800 fix example commit 898f65b Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:11:38 2022 -0800 fix example library panel dashboard commit 7fa71c9 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:05:34 2022 -0800 add example dashboard with connected library panels commit a090615 Author: Justin Mai <[email protected]> Date: Tue Feb 1 04:40:31 2022 -0800 go mod tidy commit 33e7185 Author: Justin Mai <[email protected]> Date: Tue Feb 1 04:33:34 2022 -0800 skip tests for version <8.0 commit a1c655c Author: Justin Mai <[email protected]> Date: Tue Feb 1 04:27:06 2022 -0800 update docs commit 81e060f Author: Justin Mai <[email protected]> Date: Tue Feb 1 04:26:45 2022 -0800 fix test v8.0/v8.1 Error: Provider produced inconsistent final plan commit d284570 Author: Justin Mai <[email protected]> Date: Tue Feb 1 04:06:07 2022 -0800 add Elem Type for dashboard_ids commit 2b3da4a Author: Justin Mai <[email protected]> Date: Tue Feb 1 03:52:27 2022 -0800 try fix for oss tests commit b89deb8 Author: Justin Mai <[email protected]> Date: Tue Feb 1 03:38:37 2022 -0800 update docs commit 3eac90a Merge: 8811b22 d10442a Author: Justin Mai <[email protected]> Date: Tue Feb 1 03:38:13 2022 -0800 Merge branch 'data_source_library_panel_connections' of github.com:justinTM/terraform-provider-grafana into data_source_library_panel_connections commit 8811b22 Author: Justin Mai <[email protected]> Date: Tue Feb 1 03:37:50 2022 -0800 remove redeclared test funcs after master merge commit d10442a Merge: 40467f2 3a2978b Author: JM <[email protected]> Date: Tue Feb 1 03:21:11 2022 -0800 Merge branch 'master' into data_source_library_panel_connections commit 40467f2 Author: Justin Mai <[email protected]> Date: Tue Feb 1 03:15:42 2022 -0800 fix test error commit 85f1234 Author: Justin Mai <[email protected]> Date: Tue Feb 1 01:43:47 2022 -0800 remove testAccLibraryPanelDashboardCheckDestroy and others commit d10422d Author: Justin Mai <[email protected]> Date: Tue Feb 1 01:40:43 2022 -0800 add testAccLibraryPanelCheckExists and others commit b3f0373 Author: Justin Mai <[email protected]> Date: Tue Feb 1 01:25:56 2022 -0800 add example dashboard containing library panel commit 522de3a Author: Justin Mai <[email protected]> Date: Tue Feb 1 01:25:34 2022 -0800 use client v0.2.5 commit 9d7f852 Author: Justin Mai <[email protected]> Date: Tue Feb 1 01:24:35 2022 -0800 use ExactlyOneOf instead of switch case commit 4c82dd7 Merge: 43ba937 af071c8 Author: Justin Mai <[email protected]> Date: Mon Jan 31 18:45:26 2022 -0800 Merge branch 'master' of github.com:grafana/terraform-provider-grafana into data_source_library_panel_connections commit 43ba937 Author: Justin Mai <[email protected]> Date: Thu Jan 27 20:28:40 2022 -0800 initial commit * clean up unneeded code/comments * clean up test *.tf * reinstate TestCheckResourceAttr's * fix name check in test datasource_library_panel * clean up example * fix tests: remove id check (id's are strings not integers for library panels) * parent 0a08c26 author Justin Mai <[email protected]> 1643861512 -0800 committer Justin Mai <[email protected]> 1643873261 -0800 Squashed commit of the following: commit 5aa74af Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:58:30 2022 -0800 get latest client fork, should fix 404 commit 26d19a2 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:42:00 2022 -0800 add dashboard_ids to resource and datasource commit 3021653 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:29:32 2022 -0800 update docs commit f30de96 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:29:23 2022 -0800 fix resource description whitespace commit e269c92 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:26:48 2022 -0800 get forked client latest commit 4d9f688 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:19:42 2022 -0800 add get by name commit 50d9e22 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:12:21 2022 -0800 fix Computed=false for uid in datasource commit 121bc5d Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:10:19 2022 -0800 fix name schema commit a4453da Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:07:46 2022 -0800 remove ExactlyOneOf for uid temporarily commit 7a33d51 Author: Justin Mai <[email protected]> Date: Wed Feb 2 19:00:21 2022 -0800 try refactoring library panel schemaa and datasource commit b16cb1f Author: Justin Mai <[email protected]> Date: Wed Feb 2 18:20:38 2022 -0800 try new test resource grafana_library_panel commit a13eefe Author: Justin Mai <[email protected]> Date: Wed Feb 2 18:13:51 2022 -0800 fix silly Go whitespace again commit 8916a8b Merge: 9678e64 0a08c26 Author: JM <[email protected]> Date: Wed Feb 2 18:10:27 2022 -0800 Merge branch 'master' into data_source_library_panel_connections commit 9678e64 Author: Justin Mai <[email protected]> Date: Wed Feb 2 18:08:00 2022 -0800 try fix data_source_library_panel commit 9d6575e Author: Justin Mai <[email protected]> Date: Tue Feb 1 19:49:35 2022 -0800 use new resource dame for datasource test commit af03cd6 Author: Justin Mai <[email protected]> Date: Tue Feb 1 19:44:42 2022 -0800 fix debug test commit 88d6552 Author: Justin Mai <[email protected]> Date: Tue Feb 1 19:40:06 2022 -0800 try to debug inconsistent 404 commit 8d4c30e Author: Justin Mai <[email protected]> Date: Tue Feb 1 19:18:47 2022 -0800 add folder_id to library panel commit d66c83a Author: Justin Mai <[email protected]> Date: Tue Feb 1 18:57:30 2022 -0800 fix silly Go whitespace issue commit 19ba00b Author: Justin Mai <[email protected]> Date: Tue Feb 1 18:50:31 2022 -0800 go mod tidy commit 28c76a8 Merge: a2a3717 524ee0d Author: JM <[email protected]> Date: Tue Feb 1 16:24:01 2022 -0800 Merge branch 'master' into data_source_library_panel_connections commit a2a3717 Author: Justin Mai <[email protected]> Date: Tue Feb 1 16:21:53 2022 -0800 update docs commit de062d1 Author: Justin Mai <[email protected]> Date: Tue Feb 1 16:21:03 2022 -0800 get resource name AFTER creation commit c22eff6 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:58:40 2022 -0800 reinstate original tests commit 11dda72 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:46 2022 -0800 go mod tidy commit 9626bc4 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:20 2022 -0800 Revert "go mod tidy" This reverts commit a090615. commit 8251f4a Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:18 2022 -0800 Revert "add example dashboard with connected library panels" This reverts commit 7fa71c9. commit e0d5c72 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:17 2022 -0800 Revert "fix example library panel dashboard" This reverts commit 898f65b. commit 9554434 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:15 2022 -0800 Revert "fix example" This reverts commit 544a498. commit d98bfe8 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:56:13 2022 -0800 Revert "fix example" This reverts commit d07b2ce. commit d07b2ce Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:50:02 2022 -0800 fix example commit 544a498 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:19:22 2022 -0800 fix example commit 898f65b Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:11:38 2022 -0800 fix example library panel dashboard commit 7fa71c9 Author: Justin Mai <[email protected]> Date: Tue Feb 1 05:05:34 2022 -0800 add example dashboard with connected library panels commit a090615 Author: Justin Mai <[email protected]> Date: Tue Feb 1 04:40:31 2022 -0800 go mod tidy commit 33e7185 Author: Justin Mai <[email protected]> Date: Tue Feb 1 04:33:34 2022 -0800 skip tests for version <8.0 commit a1c655c Author: Justin Mai <[email protected]> Date: Tue Feb 1 04:27:06 2022 -0800 update docs commit 81e060f Author: Justin Mai <[email protected]> Date: Tue Feb 1 04:26:45 2022 -0800 fix test v8.0/v8.1 Error: Provider produced inconsistent final plan commit d284570 Author: Justin Mai <[email protected]> Date: Tue Feb 1 04:06:07 2022 -0800 add Elem Type for dashboard_ids commit 2b3da4a Author: Justin Mai <[email protected]> Date: Tue Feb 1 03:52:27 2022 -0800 try fix for oss tests commit b89deb8 Author: Justin Mai <[email protected]> Date: Tue Feb 1 03:38:37 2022 -0800 update docs commit 3eac90a Merge: 8811b22 d10442a Author: Justin Mai <[email protected]> Date: Tue Feb 1 03:38:13 2022 -0800 Merge branch 'data_source_library_panel_connections' of github.com:justinTM/terraform-provider-grafana into data_source_library_panel_connections commit 8811b22 Author: Justin Mai <[email protected]> Date: Tue Feb 1 03:37:50 2022 -0800 remove redeclared test funcs after master merge commit d10442a Merge: 40467f2 3a2978b Author: JM <[email protected]> Date: Tue Feb 1 03:21:11 2022 -0800 Merge branch 'master' into data_source_library_panel_connections commit 40467f2 Author: Justin Mai <[email protected]> Date: Tue Feb 1 03:15:42 2022 -0800 fix test error commit 85f1234 Author: Justin Mai <[email protected]> Date: Tue Feb 1 01:43:47 2022 -0800 remove testAccLibraryPanelDashboardCheckDestroy and others commit d10422d Author: Justin Mai <[email protected]> Date: Tue Feb 1 01:40:43 2022 -0800 add testAccLibraryPanelCheckExists and others commit b3f0373 Author: Justin Mai <[email protected]> Date: Tue Feb 1 01:25:56 2022 -0800 add example dashboard containing library panel commit 522de3a Author: Justin Mai <[email protected]> Date: Tue Feb 1 01:25:34 2022 -0800 use client v0.2.5 commit 9d7f852 Author: Justin Mai <[email protected]> Date: Tue Feb 1 01:24:35 2022 -0800 use ExactlyOneOf instead of switch case commit 4c82dd7 Merge: 43ba937 af071c8 Author: Justin Mai <[email protected]> Date: Mon Jan 31 18:45:26 2022 -0800 Merge branch 'master' of github.com:grafana/terraform-provider-grafana into data_source_library_panel_connections commit 43ba937 Author: Justin Mai <[email protected]> Date: Thu Jan 27 20:28:40 2022 -0800 initial commit clean up unneeded code/comments clean up example reinstate TestCheckResourceAttr's add test for dashboard with library panel fix name check in test datasource_library_panel fix test errors fix example fix example fix index of resource test framework does not support for_each fix typo clean up example try alternative dashboard from library panel connection clean up test *.tf reinstate TestCheckResourceAttr's fix name check in test datasource_library_panel fix typo clean up example fix bad merge * fix typo * remove libraryPanel attributes from dashboard JSON other than name and uid * refactor to normalize function * try delete() for keys in libraryPanel not name or uid * add debug info * add test normalize func panels[].libraryPanel.!<name|uid> is removed * fix test noramlize func * use unmarshalDashboardConfigJSON for test * try fix normalize func * try debug normalize func * try fix normalize func * use sjson to set deeply nested JSON values * remove panel ID's in normalize func * try fix normalize func * revert regression * try fix * try fix try fix try fix try fix try fix try fix try fix try fix * move library panel dashboard example to datasource for Docs * update docs * remove duplicate example tf * go mod tidy * remove commented line * remove commented line * Update grafana/resource_library_panel.go Co-authored-by: Leandro López <[email protected]> * terraform fmt * fix typo * update docs * Remove sjson dependency * Add nil check Co-authored-by: Leandro López <[email protected]> Co-authored-by: Julien Duchesne <[email protected]>
1 parent 4c86b85 commit 878e05e

File tree

13 files changed

+365
-9
lines changed

13 files changed

+365
-9
lines changed

docs/data-sources/library_panel.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_library_panel Data Source - terraform-provider-grafana"
4+
subcategory: ""
5+
description: |-
6+
Data source for retrieving a single library panel by name or uid.
7+
---
8+
9+
# grafana_library_panel (Data Source)
10+
11+
Data source for retrieving a single library panel by name or uid.
12+
13+
## Example Usage
14+
15+
```terraform
16+
// create a minimal library panel inside the General folder
17+
resource "grafana_library_panel" "test" {
18+
name = "test name"
19+
folder_id = 0 // General folder
20+
model_json = jsonencode({
21+
title = "test name"
22+
type = "text"
23+
version = 0
24+
})
25+
}
26+
27+
data "grafana_library_panel" "from_name" {
28+
name = grafana_library_panel.test.name
29+
}
30+
31+
data "grafana_library_panel" "from_uid" {
32+
uid = grafana_library_panel.test.id
33+
}
34+
35+
// create library panels to be added to a dashboard
36+
resource "grafana_library_panel" "dashboard" {
37+
name = "panel"
38+
folder_id = 0
39+
model_json = jsonencode({
40+
gridPos = {
41+
x = 0
42+
y = 0
43+
h = 10
44+
w = 10 }
45+
title = "panel"
46+
type = "text"
47+
version = 0 })
48+
}
49+
50+
// create a dashboard using the library panel
51+
// `merge()` will add `libraryPanel` attribute to each library panel JSON
52+
// Grafana will then connect any library panels found in dashboard JSON
53+
resource "grafana_dashboard" "with_library_panel" {
54+
config_json = jsonencode({
55+
id = 12345
56+
panels = [
57+
merge(jsondecode(grafana_library_panel.dashboard.model_json), {
58+
libraryPanel = {
59+
name = grafana_library_panel.dashboard.name
60+
uid = grafana_library_panel.dashboard.uid
61+
}
62+
})
63+
]
64+
title = "Production Overview"
65+
tags = ["templated"]
66+
timezone = "browser"
67+
schemaVersion = 16
68+
version = 0
69+
refresh = "25s"
70+
})
71+
}
72+
73+
// dashboard_ids list attribute should contain dashboard id 12345
74+
data "grafana_library_panel" "connected_to_dashboard" {
75+
uid = grafana_library_panel.dashboard.uid
76+
77+
// the dashboard must be created before reading the library panel data
78+
depends_on = [grafana_dashboard.with_library_panel]
79+
}
80+
81+
data "grafana_dashboard" "from_library_panel_connection" {
82+
dashboard_id = data.grafana_library_panel.connected_to_dashboard.dashboard_ids[0]
83+
}
84+
```
85+
86+
<!-- schema generated by tfplugindocs -->
87+
## Schema
88+
89+
### Optional
90+
91+
- **id** (String) The ID of this resource.
92+
- **name** (String) Name of the library panel.
93+
- **uid** (String) The unique identifier (UID) of a library panel uniquely identifies library panels between multiple Grafana installs. It’s automatically generated unless you specify it during library panel creation.The UID provides consistent URLs for accessing library panels and when syncing library panels between multiple Grafana installs.
94+
95+
### Read-Only
96+
97+
- **created** (String) Timestamp when the library panel was created.
98+
- **dashboard_ids** (List of Number) Numerical IDs of Grafana dashboards containing the library panel.
99+
- **description** (String) Description of the library panel.
100+
- **folder_id** (Number) ID of the folder where the library panel is stored.
101+
- **folder_name** (String) Name of the folder containing the library panel.
102+
- **folder_uid** (String) Unique ID (UID) of the folder containing the library panel.
103+
- **model_json** (String) The JSON model for the library panel.
104+
- **org_id** (Number) The numeric ID of the library panel computed by Grafana.
105+
- **panel_id** (Number) The numeric ID of the library panel computed by Grafana.
106+
- **type** (String) Type of the library panel (eg. text).
107+
- **updated** (String) Timestamp when the library panel was last modified.
108+
- **version** (Number) Version of the library panel.
109+
110+

docs/resources/library_panel.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ resource "grafana_library_panel" "test" {
4444
### Read-Only
4545

4646
- **created** (String) Timestamp when the library panel was created.
47+
- **dashboard_ids** (List of Number) Numerical IDs of Grafana dashboards containing the library panel.
4748
- **description** (String) Description of the library panel.
4849
- **folder_name** (String) Name of the folder containing the library panel.
4950
- **folder_uid** (String) Unique ID (UID) of the folder containing the library panel.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
resource "grafana_library_panel" "test_bad_inputs" {
2+
name = "test name"
3+
model_json = jsonencode({
4+
gridPos = {
5+
h = 8
6+
w = 12
7+
}
8+
id = 1
9+
})
10+
}
11+
12+
data "grafana_library_panel" "bad_from_uid_id" {
13+
uid = grafana_library_panel.test_bad_inputs.id
14+
name = grafana_library_panel.test_bad_inputs.name
15+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// create a minimal library panel inside the General folder
2+
resource "grafana_library_panel" "test" {
3+
name = "test name"
4+
folder_id = 0 // General folder
5+
model_json = jsonencode({
6+
title = "test name"
7+
type = "text"
8+
version = 0
9+
})
10+
}
11+
12+
data "grafana_library_panel" "from_name" {
13+
name = grafana_library_panel.test.name
14+
}
15+
16+
data "grafana_library_panel" "from_uid" {
17+
uid = grafana_library_panel.test.id
18+
}
19+
20+
// create library panels to be added to a dashboard
21+
resource "grafana_library_panel" "dashboard" {
22+
name = "panel"
23+
folder_id = 0
24+
model_json = jsonencode({
25+
gridPos = {
26+
x = 0
27+
y = 0
28+
h = 10
29+
w = 10 }
30+
title = "panel"
31+
type = "text"
32+
version = 0 })
33+
}
34+
35+
// create a dashboard using the library panel
36+
// `merge()` will add `libraryPanel` attribute to each library panel JSON
37+
// Grafana will then connect any library panels found in dashboard JSON
38+
resource "grafana_dashboard" "with_library_panel" {
39+
config_json = jsonencode({
40+
id = 12345
41+
panels = [
42+
merge(jsondecode(grafana_library_panel.dashboard.model_json), {
43+
libraryPanel = {
44+
name = grafana_library_panel.dashboard.name
45+
uid = grafana_library_panel.dashboard.uid
46+
}
47+
})
48+
]
49+
title = "Production Overview"
50+
tags = ["templated"]
51+
timezone = "browser"
52+
schemaVersion = 16
53+
version = 0
54+
refresh = "25s"
55+
})
56+
}
57+
58+
// dashboard_ids list attribute should contain dashboard id 12345
59+
data "grafana_library_panel" "connected_to_dashboard" {
60+
uid = grafana_library_panel.dashboard.uid
61+
62+
// the dashboard must be created before reading the library panel data
63+
depends_on = [grafana_dashboard.with_library_panel]
64+
}
65+
66+
data "grafana_dashboard" "from_library_panel_connection" {
67+
dashboard_id = data.grafana_library_panel.connected_to_dashboard.dashboard_ids[0]
68+
}

go.mod

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

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

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51
455455
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
456456
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
457457
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
458-
github.com/grafana/grafana-api-golang-client v0.2.6 h1:/S9vz8gwwxURnQwU/SDgFl4JaX0PTGNrszBKKeOunF4=
459-
github.com/grafana/grafana-api-golang-client v0.2.6/go.mod h1:24W29gPe9yl0/3A9X624TPkAOR8DpHno490cPwnkv8E=
458+
github.com/grafana/grafana-api-golang-client v0.3.0 h1:eXUYeMIq/FLmdHy0UDFb2lmXQcM/yo6pGSImDJYAk48=
459+
github.com/grafana/grafana-api-golang-client v0.3.0/go.mod h1:24W29gPe9yl0/3A9X624TPkAOR8DpHno490cPwnkv8E=
460460
github.com/grafana/machine-learning-go-client v0.1.1 h1:Gw6cX8xAd6IVF2LApkXOIdBK8Gzz07B3jQPukecw7fc=
461461
github.com/grafana/machine-learning-go-client v0.1.1/go.mod h1:QFfZz8NkqVF8++skjkKQXJEZfpCYd8S0yTWJUpsLLTA=
462462
github.com/grafana/synthetic-monitoring-agent v0.5.0/go.mod h1:oC2+OFrK7WHqrDmilaa8eNNjyB2fMRk7DCj3Z/b/TrA=
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package grafana
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func DatasourceLibraryPanel() *schema.Resource {
11+
panelSchema := datasourceSchemaFromResourceSchema(libraryPanel.Schema)
12+
panelSchema["uid"].Optional = true
13+
panelSchema["name"].Optional = true
14+
panelSchema["uid"].ExactlyOneOf = []string{"uid", "name"}
15+
panelSchema["name"].ExactlyOneOf = []string{"uid", "name"}
16+
17+
return &schema.Resource{
18+
Description: "Data source for retrieving a single library panel by name or uid.",
19+
ReadContext: dataSourceLibraryPanelRead,
20+
Schema: panelSchema,
21+
}
22+
}
23+
24+
func dataSourceLibraryPanelRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
25+
client := meta.(*client).gapi
26+
uid := d.Get("uid").(string)
27+
28+
// get UID from name if specified
29+
name := d.Get("name").(string)
30+
if name != "" {
31+
panel, err := client.LibraryPanelByName(name)
32+
if err != nil {
33+
return diag.FromErr(err)
34+
}
35+
uid = panel.UID
36+
}
37+
38+
d.SetId(uid)
39+
ReadLibraryPanel(ctx, d, meta)
40+
41+
return nil
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package grafana
2+
3+
import (
4+
"testing"
5+
6+
gapi "github.com/grafana/grafana-api-golang-client"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccDatasourceLibraryPanel(t *testing.T) {
11+
CheckOSSTestsEnabled(t)
12+
CheckOSSTestsSemver(t, ">=8.0.0")
13+
14+
var panel gapi.LibraryPanel
15+
// var dashboard gapi.Dashboard
16+
checks := []resource.TestCheckFunc{
17+
testAccLibraryPanelCheckExists("grafana_library_panel.test", &panel),
18+
resource.TestCheckResourceAttr(
19+
"data.grafana_library_panel.from_name", "name", "test name",
20+
),
21+
resource.TestMatchResourceAttr(
22+
"data.grafana_library_panel.from_name", "uid", uidRegexp,
23+
),
24+
resource.TestCheckResourceAttr(
25+
"data.grafana_library_panel.from_uid", "name", "test name",
26+
),
27+
resource.TestMatchResourceAttr(
28+
"data.grafana_library_panel.from_uid", "uid", uidRegexp,
29+
),
30+
}
31+
32+
resource.Test(t, resource.TestCase{
33+
PreCheck: func() { testAccPreCheck(t) },
34+
ProviderFactories: testAccProviderFactories,
35+
CheckDestroy: testAccLibraryPanelCheckDestroy(&panel),
36+
Steps: []resource.TestStep{
37+
{
38+
Config: testAccExample(t, "data-sources/grafana_library_panel/data-source.tf"),
39+
Check: resource.ComposeTestCheckFunc(checks...),
40+
},
41+
},
42+
})
43+
}

grafana/provider.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,11 @@ func Provider(version string) func() *schema.Provider {
154154

155155
DataSourcesMap: map[string]*schema.Resource{
156156
// Grafana
157-
"grafana_dashboard": DatasourceDashboard(),
158-
"grafana_folder": DatasourceFolder(),
159-
"grafana_user": DatasourceUser(),
160-
"grafana_cloud_stack": DataSourceStack(),
157+
"grafana_dashboard": DatasourceDashboard(),
158+
"grafana_folder": DatasourceFolder(),
159+
"grafana_library_panel": DatasourceLibraryPanel(),
160+
"grafana_user": DatasourceUser(),
161+
"grafana_cloud_stack": DataSourceStack(),
161162

162163
// Synthetic Monitoring
163164
"grafana_synthetic_monitoring_probe": dataSourceSyntheticMonitoringProbe(),

grafana/resource_dashboard.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,28 @@ func normalizeDashboardConfigJSON(config interface{}) string {
316316
return c
317317
}
318318
}
319+
319320
delete(dashboardJSON, "id")
320321
delete(dashboardJSON, "version")
322+
323+
// similarly to uid removal above, remove any attributes panels[].libraryPanel.*
324+
// from the dashboard JSON other than "name" or "uid".
325+
// Grafana will populate all other libraryPanel attributes, so delete them to avoid diff.
326+
panels, hasPanels := dashboardJSON["panels"]
327+
if hasPanels {
328+
for _, panel := range panels.([]interface{}) {
329+
panelMap := panel.(map[string]interface{})
330+
delete(panelMap, "id")
331+
if libraryPanel, ok := panelMap["libraryPanel"].(map[string]interface{}); ok {
332+
for k := range libraryPanel {
333+
if k != "name" && k != "uid" {
334+
delete(libraryPanel, k)
335+
}
336+
}
337+
}
338+
}
339+
}
340+
321341
j, _ := json.Marshal(dashboardJSON)
322342
return string(j)
323343
}

0 commit comments

Comments
 (0)