|
| 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