You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Datasources: Support reading data from Grafana API (#609)
* Datasources: Support read
I messed up in my last PR (#607)
Turns out that in the Terraform logic, `TypeMap` means that it's a map[string]string, so the provider was writing strings as values
The Grafana API accepted those string values but the plugins were essentially misconfigured. I noticed this while trying to implement the read functionality
To fix that, I changed the `json_data_map` and `secure_json_data_map` to `json_data_encoded` and `secure_json_data_encoded`
The flow for users is essentially the same but they have to `jsonencode(<map>)`. This allows Terraform to keep the correct attribute types
Closes#253
Needs grafana/grafana-api-golang-client#108
* Update client
-`database_name` (String) (Required by some data source types) The name of the database to use on the selected data source server. Defaults to ``.
107
107
-`http_headers` (Map of String, Sensitive) Custom HTTP headers
108
108
-`is_default` (Boolean) Whether to set the data source as default. This should only be `true` to a single data source. Defaults to `false`.
109
-
-`json_data` (Block List, Deprecated) (Required by some data source types). Deprecated: Use json_data_map instead. It supports arbitrary JSON data, and therefore all attributes. (see [below for nested schema](#nestedblock--json_data))
110
-
-`json_data_map` (Map of String) Replaces the json_data attribute, this attribute can be used to pass configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI.
109
+
-`json_data` (Block List, Deprecated) (Required by some data source types). Deprecated: Use json_data_encoded instead. It supports arbitrary JSON data, and therefore all attributes. (see [below for nested schema](#nestedblock--json_data))
110
+
-`json_data_encoded` (String) Serialized JSON string containing the json data. Replaces the json_data attribute, this attribute can be used to pass configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI.
111
111
-`password` (String, Sensitive, Deprecated) (Required by some data source types) The password to use to authenticate to the data source. Deprecated: Use secure_json_data.password instead. This attribute is removed in Grafana 9.0+. Defaults to ``.
112
112
-`secure_json_data` (Block List, Deprecated) Deprecated: Use secure_json_data instead. It supports arbitrary JSON data, and therefore all attributes. (see [below for nested schema](#nestedblock--secure_json_data))
113
-
-`secure_json_data_map` (Map of String, Sensitive) Replaces the secure_json_data attribute, this attribute can be used to pass secure configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI.
113
+
-`secure_json_data_encoded` (String, Sensitive) Serialized JSON string containing the secure json data. Replaces the secure_json_data attribute, this attribute can be used to pass secure configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI.
114
114
-`uid` (String) Unique identifier. If unset, this will be automatically generated.
115
115
-`url` (String) The URL for the data source. The type of URL required varies depending on the chosen data source type.
116
116
-`username` (String) (Required by some data source types) The username to use to authenticate to the data source. Defaults to ``.
// Set this attribute on imports so that the condition in the read function is met
43
+
// This means that when we're importing, we'll always read the JSON data from the API into this attribute
44
+
rd.Set("json_data_encoded", "{}")
40
45
_, err:=strconv.ParseInt(rd.Id(), 10, 64)
41
46
iferr!=nil {
42
47
// If the ID is not a number, then it may be a UID
@@ -109,8 +114,8 @@ source selected (via the 'type' argument).
109
114
"json_data": {
110
115
Type: schema.TypeList,
111
116
Optional: true,
112
-
Description: "(Required by some data source types). Deprecated: Use json_data_map instead. It supports arbitrary JSON data, and therefore all attributes.",
113
-
Deprecated: "Use json_data_map instead. It supports arbitrary JSON data, and therefore all attributes.",
117
+
Description: "(Required by some data source types). Deprecated: Use json_data_encoded instead. It supports arbitrary JSON data, and therefore all attributes.",
118
+
Deprecated: "Use json_data_encoded instead. It supports arbitrary JSON data, and therefore all attributes.",
114
119
Elem: &schema.Resource{
115
120
Schema: map[string]*schema.Schema{
116
121
"assume_role_arn": {
@@ -557,18 +562,30 @@ source selected (via the 'type' argument).
557
562
Default: "",
558
563
Description: "(Required by some data source types) The username to use to authenticate to the data source.",
559
564
},
560
-
"json_data_map": {
561
-
Type: schema.TypeMap,
565
+
"json_data_encoded": {
566
+
Type: schema.TypeString,
562
567
Optional: true,
563
-
ConflictsWith: []string{"json_data"},
564
-
Description: "Replaces the json_data attribute, this attribute can be used to pass configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI.",
Description: "Serialized JSON string containing the json data. Replaces the json_data attribute, this attribute can be used to pass configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI.",
570
+
ValidateFunc: validation.StringIsJSON,
571
+
StateFunc: func(vinterface{}) string {
572
+
json, _:=structure.NormalizeJsonString(v)
573
+
returnjson
574
+
},
575
+
DiffSuppressFunc: SuppressEquivalentJSONDiffs,
565
576
},
566
-
"secure_json_data_map": {
567
-
Type: schema.TypeMap,
577
+
"secure_json_data_encoded": {
578
+
Type: schema.TypeString,
568
579
Optional: true,
569
580
Sensitive: true,
570
-
ConflictsWith: []string{"secure_json_data"},
571
-
Description: "Replaces the secure_json_data attribute, this attribute can be used to pass secure configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI.",
Description: "Serialized JSON string containing the secure json data. Replaces the secure_json_data attribute, this attribute can be used to pass secure configuration options to the data source. To figure out what options a datasource has available, see its docs or inspect the network data when saving it from the Grafana UI.",
583
+
ValidateFunc: validation.StringIsJSON,
584
+
StateFunc: func(vinterface{}) string {
585
+
json, _:=structure.NormalizeJsonString(v)
586
+
returnjson
587
+
},
588
+
DiffSuppressFunc: SuppressEquivalentJSONDiffs,
572
589
},
573
590
},
574
591
}
@@ -639,6 +656,43 @@ func ReadDataSource(ctx context.Context, d *schema.ResourceData, meta interface{
639
656
d.Set("username", dataSource.User)
640
657
d.Set("uid", dataSource.UID)
641
658
659
+
// If `json_data` is not set, then we'll use the new attribute: `json_data_encoded`. This allows support of imports.
0 commit comments