Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Caching support to Datasources #765

Open
fatbasstard opened this issue Jan 5, 2023 · 10 comments
Open

Add Caching support to Datasources #765

fatbasstard opened this issue Jan 5, 2023 · 10 comments

Comments

@fatbasstard
Copy link
Contributor

Hi,

Currently datasource caching (Documentation) is not supported by the Provider.

We're moving to Enterprise instances and would like to enable Caching using Terraform. Asked Grafana about provisioning option and got this reply:

Hello, 
 
Unfortunately as of now, is not possible to enable/disable cache for a datasource via provisioning, but there is an API you could programmatically call to enable it as part of your deployment:
 
/api/datasources/:datasourceUId/cache/enable
/api/datasources/:datasourceUId/cache/disable
 
These APIs are yet to be documented but you should be able to use them.
 
In regards of having this feature enabled as part of provisioning, I can certainly pass this over to our Voice of the Customer team to let them know you're interested in this. 
 
Please let me know if you have any further questions or comments. 

So the API supports it, can this be added to the data_source resources?

@MJoeWatson
Copy link

I'd love to see this added to the provider as well. @julienduchesne Presumably this wouldn't be part of the data_source resource since enterprise features tend to be separate resources? Happy to help, but would like to understand first what the preferred approach would be!

@julienduchesne
Copy link
Member

julienduchesne commented Jun 30, 2023

Seems like there's an API for it: https://grafana.com/docs/grafana/latest/developers/http_api/query_and_resource_caching/

Even though it's an enterprise feature, it's a 1:1 mapping with a datasource so I think it makes most sense to be in the datasource resource

We could query it only in the presence of the caching block. Example:

This doesn't query it:

resource "grafana_data_source" "whatever" {
  name = "..."
  ...
}

While this will query it:

resource "grafana_data_source" "whatever" {
  name = "..."
  ...
  caching {
    enabled = true|false # required attribute
  }
}

When the block is present, we query the API. All attributes from the API are resolved

@fatbasstard
Copy link
Contributor Author

Hi @cindy ,

Now that the GO library supports it, any sight on when this will be added to Terraform? Really looking forward to using it (would save me from manually enabled caching all over the place 😛

@cindy
Copy link

cindy commented Sep 8, 2023

@fatbasstard Thanks for the ping. I'll pick this back up 👍

@itsjusth
Copy link

Any progress on this one? I have been looking for this functionality.

@ImTomEddy
Copy link

I was going to pick this up as it's also a feature I require however, I believe it is blocked by grafana/grafana#86453.

The current iteration of the terraform provider only depends on https://github.com/grafana/grafana-openapi-client-go (and not https://github.com/grafana/grafana-api-golang-client) which does not include any methods to access the cache endpoints. This is due to the cache endpoints being missing from the OpenAPI spec being used to generate the client.

@jtackaberry
Copy link

I was going to pick this up as it's also a feature I require however, I believe it is blocked by grafana/grafana#86453.

Looks like your PR is merged. Are you still planning to push this one along @ImTomEddy?

@ImTomEddy
Copy link

I was going to pick this up as it's also a feature I require however, I believe it is blocked by grafana/grafana#86453.

Looks like your PR is merged. Are you still planning to push this one along @ImTomEddy?

Unfortunately, I've not had the time since the team at Grafana were able to get this added to the OpenAPI spec.

@KoenR3
Copy link

KoenR3 commented Jun 10, 2024

This would also be very handy for us! Now we have to manage caching manually

@abstrask
Copy link

abstrask commented Oct 10, 2024

Until supported by the Grafana provider, I've used Mastercard's generic restapi provider to manage data source cache, like so:

terraform {
  required_providers {
    restapi = {
      source  = "mastercard/restapi"
      version = "~> 1.20.0"
    }
  }
}

variable "grafana_url" {}

variable "datasource_uid" {}

variable "bearer_token" {}

provider "restapi" {
  # https://registry.terraform.io/providers/Mastercard/restapi/latest/docs
  uri                  = "${var.grafana_url}/api"
  write_returns_object = true
  headers = {
    "Authorization" = "Bearer ${var.bearer_token}"
    "Content-Type"  = "application/json"
  }
}

resource "restapi_object" "datasource_cache_config" {
  # https://grafana.com/docs/grafana/latest/developers/http_api/query_and_resource_caching/
  # https://editor.swagger.io/?url=https://raw.githubusercontent.com/grafana/grafana/main/public/openapi3.json
  path = "/datasources/{id}/cache"
  data = jsonencode({
    enabled = true
  })
  id_attribute   = "dataSourceUID"
  object_id      = var.datasource_uid
  read_path      = "/datasources/{id}/cache"
  update_method  = "POST"
  update_path    = "/datasources/{id}/cache"
  destroy_method = "POST"
  destroy_path   = "/datasources/{id}/cache/disable"
}

output "api_response" {
  value = jsondecode(restapi_object.datasource_cache_config.api_response)
}

@Duologic Duologic added bug and removed enhancement labels Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests