Skip to content

Commit 3fce9e8

Browse files
committed
Added new resource for data source cache config
1 parent c8bb737 commit 3fce9e8

File tree

6 files changed

+406
-0
lines changed

6 files changed

+406
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "grafana_data_source_cache_config Resource - terraform-provider-grafana"
4+
subcategory: "Grafana Enterprise"
5+
description: |-
6+
Manages cache configuration for a data source (Grafana Enterprise).
7+
Use this resource to enable or disable caching for a particular data source. You can also tune the TTL settings for the cache behaviour, or choose to use defaults.
8+
Deleting this resource will cause the cache to be disabled for the target data source.
9+
---
10+
11+
# grafana_data_source_cache_config (Resource)
12+
13+
Manages cache configuration for a data source (Grafana Enterprise).
14+
15+
Use this resource to enable or disable caching for a particular data source. You can also tune the TTL settings for the cache behaviour, or choose to use defaults.
16+
17+
Deleting this resource will cause the cache to be disabled for the target data source.
18+
19+
## Example Usage
20+
21+
```terraform
22+
resource "grafana_data_source" "loki" {
23+
type = "loki"
24+
name = "loki"
25+
url = "http://localhost:3100"
26+
}
27+
28+
resource "grafana_data_source_cache_config" "loki_cache" {
29+
datasource_uid = grafana_data_source.loki.uid
30+
enabled = true
31+
use_default_ttl = false
32+
ttl_queries_ms = 60000
33+
ttl_resources_ms = 300000
34+
}
35+
```
36+
37+
<!-- schema generated by tfplugindocs -->
38+
## Schema
39+
40+
### Required
41+
42+
- `datasource_uid` (String) UID of the data source to configure.
43+
44+
### Optional
45+
46+
- `enabled` (Boolean) Whether caching is enabled for this data source.
47+
- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
48+
- `ttl_queries_ms` (Number) TTL for query caching, in milliseconds. Ignored if use_default_ttl is true.
49+
- `ttl_resources_ms` (Number) TTL for resource caching, in milliseconds. Ignored if use_default_ttl is true.
50+
- `use_default_ttl` (Boolean) If true, use Grafana's default TTLs instead of custom values.
51+
52+
### Read-Only
53+
54+
- `id` (String) The ID of this resource.
55+
56+
## Import
57+
58+
Import is supported using the following syntax:
59+
60+
```shell
61+
terraform import grafana_data_source_cache_config.name "{{ datasource_uid }}"
62+
terraform import grafana_data_source_cache_config.name "{{ orgID }}:{{ datasource_uid }}"
63+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
terraform import grafana_data_source_cache_config.name "{{ datasource_uid }}"
2+
terraform import grafana_data_source_cache_config.name "{{ orgID }}:{{ datasource_uid }}"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
resource "grafana_data_source" "loki" {
2+
type = "loki"
3+
name = "loki"
4+
url = "http://localhost:3100"
5+
}
6+
7+
resource "grafana_data_source_cache_config" "loki_cache" {
8+
datasource_uid = grafana_data_source.loki.uid
9+
enabled = true
10+
use_default_ttl = false
11+
ttl_queries_ms = 60000
12+
ttl_resources_ms = 300000
13+
}
14+
15+
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package grafana
2+
3+
import (
4+
"context"
5+
"strconv"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
10+
goapi "github.com/grafana/grafana-openapi-client-go/client"
11+
"github.com/grafana/grafana-openapi-client-go/models"
12+
"github.com/grafana/terraform-provider-grafana/v4/internal/common"
13+
)
14+
15+
func resourceDataSourceCacheConfig() *common.Resource {
16+
schema := &schema.Resource{
17+
Description: `
18+
Manages cache configuration for a data source (Grafana Enterprise).
19+
20+
Use this resource to enable or disable caching for a particular data source. You can also tune the TTL settings for the cache behaviour, or choose to use defaults.
21+
22+
Deleting this resource will cause the cache to be disabled for the target data source.
23+
`,
24+
25+
CreateContext: CreateOrUpdateDataSourceCacheConfig,
26+
UpdateContext: CreateOrUpdateDataSourceCacheConfig,
27+
ReadContext: ReadDataSourceCacheConfig,
28+
DeleteContext: DeleteDataSourceCacheConfig,
29+
Importer: &schema.ResourceImporter{
30+
StateContext: schema.ImportStatePassthroughContext,
31+
},
32+
33+
Schema: map[string]*schema.Schema{
34+
"org_id": orgIDAttribute(),
35+
"datasource_uid": {
36+
Type: schema.TypeString,
37+
Required: true,
38+
ForceNew: true,
39+
Description: "UID of the data source to configure.",
40+
},
41+
"enabled": {
42+
Type: schema.TypeBool,
43+
Optional: true,
44+
Description: "Whether caching is enabled for this data source.",
45+
},
46+
"use_default_ttl": {
47+
Type: schema.TypeBool,
48+
Optional: true,
49+
Description: "If true, use Grafana's default TTLs instead of custom values.",
50+
},
51+
"ttl_queries_ms": {
52+
Type: schema.TypeInt,
53+
Optional: true,
54+
Description: "TTL for query caching, in milliseconds. Ignored if use_default_ttl is true.",
55+
},
56+
"ttl_resources_ms": {
57+
Type: schema.TypeInt,
58+
Optional: true,
59+
Description: "TTL for resource caching, in milliseconds. Ignored if use_default_ttl is true.",
60+
},
61+
},
62+
}
63+
64+
return common.NewLegacySDKResource(
65+
common.CategoryGrafanaEnterprise,
66+
"grafana_data_source_cache_config",
67+
orgResourceIDString("datasource_uid"),
68+
schema,
69+
)
70+
}
71+
72+
func CreateOrUpdateDataSourceCacheConfig(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
73+
client, _ := OAPIClientFromNewOrgResource(meta, d)
74+
dsUID := d.Get("datasource_uid").(string)
75+
76+
body := &models.CacheConfigSetter{
77+
DataSourceUID: dsUID,
78+
}
79+
if v, ok := d.GetOkExists("enabled"); ok {
80+
body.Enabled = v.(bool)
81+
}
82+
if v, ok := d.GetOkExists("use_default_ttl"); ok {
83+
body.UseDefaultTTL = v.(bool)
84+
}
85+
if v, ok := d.GetOkExists("ttl_queries_ms"); ok {
86+
body.TTLQueriesMs = int64(v.(int))
87+
}
88+
if v, ok := d.GetOkExists("ttl_resources_ms"); ok {
89+
body.TTLResourcesMs = int64(v.(int))
90+
}
91+
92+
_, err := client.Enterprise.SetDataSourceCacheConfig(dsUID, body)
93+
if err != nil {
94+
return diag.FromErr(err)
95+
}
96+
97+
// Fetch DS to get OrgID for resource ID
98+
if err := setIDFromDatasource(ctx, d, client, dsUID); err != nil {
99+
return diag.FromErr(err)
100+
}
101+
return ReadDataSourceCacheConfig(ctx, d, meta)
102+
}
103+
104+
func ReadDataSourceCacheConfig(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
105+
client, _, idStr := OAPIClientFromExistingOrgResource(meta, d.Id())
106+
107+
// idStr is datasource UID
108+
resp, err := client.Enterprise.GetDataSourceCacheConfig(idStr)
109+
if err, shouldReturn := common.CheckReadError("datasource cache config", d, err); shouldReturn {
110+
return err
111+
}
112+
cc := resp.GetPayload()
113+
114+
// Also read DS to set org_id and ensure ID is correct
115+
dsResp, err := client.Datasources.GetDataSourceByUID(idStr)
116+
if err, shouldReturn := common.CheckReadError("datasource", d, err); shouldReturn {
117+
return err
118+
}
119+
ds := dsResp.GetPayload()
120+
121+
d.Set("datasource_uid", cc.DataSourceUID)
122+
d.Set("org_id", strconv.FormatInt(ds.OrgID, 10))
123+
d.Set("enabled", cc.Enabled)
124+
d.Set("use_default_ttl", cc.UseDefaultTTL)
125+
d.Set("ttl_queries_ms", int(cc.TTLQueriesMs))
126+
d.Set("ttl_resources_ms", int(cc.TTLResourcesMs))
127+
128+
// Ensure ID matches current org/datasource
129+
d.SetId(MakeOrgResourceID(ds.OrgID, idStr))
130+
return nil
131+
}
132+
133+
func DeleteDataSourceCacheConfig(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
134+
client, _, idStr := OAPIClientFromExistingOrgResource(meta, d.Id())
135+
_, err := client.Enterprise.DisableDataSourceCache(idStr)
136+
if err != nil {
137+
return diag.FromErr(err)
138+
}
139+
return nil
140+
}
141+
142+
func setIDFromDatasource(ctx context.Context, d *schema.ResourceData, client *goapi.GrafanaHTTPAPI, dataSourceUID string) error {
143+
resp, err := client.Datasources.GetDataSourceByUID(dataSourceUID)
144+
if err != nil {
145+
return err
146+
}
147+
ds := resp.GetPayload()
148+
d.SetId(MakeOrgResourceID(ds.OrgID, ds.UID))
149+
return nil
150+
}

0 commit comments

Comments
 (0)