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 PDC network and PDC network token resources #1975

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

aangelisc
Copy link

@aangelisc aangelisc commented Jan 3, 2025

Feedback welcome here (my first time creating some TF resources), this is essentially a duplicate of the access policy and token resources with some minor changes that are specific to PDC.

  • Add resource lister function that will return a slice of resources

Fixes #1222
Fixes #1638

- Add resource lister function that will return a slice of resources
@aangelisc aangelisc self-assigned this Jan 3, 2025
@aangelisc aangelisc requested review from a team as code owners January 3, 2025 17:39
Copy link

github-actions bot commented Jan 3, 2025

In order to lower resource usage and have a faster runtime, PRs will not run Cloud tests automatically.
To do so, a Grafana Labs employee must trigger the cloud acceptance tests workflow manually.

@aangelisc aangelisc requested review from julienduchesne and dafydd-t and removed request for a team January 3, 2025 19:15
Copy link

@dafydd-t dafydd-t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good! Thanks a lot for starting it off. I've added some comments around the naming of things for now. We need a more thorough review of the terraform itself, we can get to that soon.

Comment on lines 13 to 14
pdc_network_id = grafana_cloud_private_datasource_connect.test.network_id
region = "us"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we pass the whole network resource as input? Then we could get the region from the network schema, and we wouldn't need to repeat the region input in the token schema. This would feel more natural to me, since a token MUST be in the same region as its Access Policy

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is straightforward. I've updated the example to use the region from the previously created network.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I'll leave this comment open in case someone with some TF experience has input.

internal/resources/grafana/resource_data_source.go Outdated Show resolved Hide resolved
@aangelisc
Copy link
Author

Updated based on your suggestions @dafydd-t. I've also added a data source to allow retrieval of networks that may not be managed by TF.

@aangelisc aangelisc requested a review from dafydd-t January 6, 2025 21:02
@@ -59,6 +59,7 @@ data "grafana_data_source" "from_uid" {
- `id` (String) The ID of this resource.
- `is_default` (Boolean) Whether to set the data source as default. This should only be `true` to a single data source.
- `json_data_encoded` (String) Serialized JSON string containing the json data. 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. Note that keys in this map are usually camelCased.
- `private_datasource_connect_network_id` (String) (Can only be used with data sources in Grafana Cloud) The ID of the private data source network to use with this data source.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `private_datasource_connect_network_id` (String) (Can only be used with data sources in Grafana Cloud) The ID of the private data source network to use with this data source.
- `private_datasource_connect_network_id` (String) (Can only be used with data sources in Grafana Cloud) The ID of the Private Data source Connect network to use with this data source.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

page_title: "grafana_cloud_private_data_source_connect_networks Data Source - terraform-provider-grafana"
subcategory: "Cloud"
description: |-
Fetches Private data source connect networks from Grafana Cloud.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fetches Private data source connect networks from Grafana Cloud.
Fetches Private Data source Connect networks from Grafana Cloud.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doh i see this is generated, but please update in the source

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in source


# grafana_cloud_private_data_source_connect_networks (Data Source)

Fetches Private data source connect networks from Grafana Cloud.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fetches Private data source connect networks from Grafana Cloud.
Fetches Private Data source Connect networks from Grafana Cloud.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

)
}

type PrivateDatasourceConnectNetworksDataSource struct {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we be consistent about typing on Datasource please?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be OK to use PDC in the type names too - up to you.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to using PDC

@dafydd-t
Copy link

dafydd-t commented Jan 8, 2025

@aangelisc do you have an example terraform plan that you used to run this locally? I'd love to give it a go

@aangelisc
Copy link
Author

Hey @dafydd-t, I've pushed some more updates 😊

You should be able to test this with the following tf:

terraform {
  required_providers {
    grafana = {
      source = "grafana/grafana"
    }
  }
}

provider "grafana" {
  alias                     = "cloud"
  cloud_access_policy_token = "<token>"
}


data "grafana_cloud_stack" "stack" {
  provider = grafana.cloud

  slug = "stack"
}

resource "grafana_cloud_stack_service_account" "cloud_sa" {
  provider   = grafana.cloud
  stack_slug = data.grafana_cloud_stack.stack.slug

  name        = "pdc-test-sa"
  role        = "Admin"
  is_disabled = false
}

resource "grafana_cloud_stack_service_account_token" "cloud_sa" {
  provider   = grafana.cloud
  stack_slug = data.grafana_cloud_stack.stack.slug

  name               = "terraform serviceaccount key"
  service_account_id = grafana_cloud_stack_service_account.cloud_sa.id
}

provider "grafana" {
  alias = "stack"

  url                       = data.grafana_cloud_stack.stack.url
  auth                      = grafana_cloud_stack_service_account_token.cloud_sa.key
  cloud_access_policy_token = "<token>"
}

resource "grafana_cloud_private_data_source_connect_network" "test" {
  provider = grafana.stack

  region           = data.grafana_cloud_stack.stack.region_slug
  name             = "test-pdc"
  display_name     = "Test PDC"
  stack_identifier = data.grafana_cloud_stack.stack.id
}

resource "grafana_cloud_private_data_source_connect_network_token" "test" {
  provider = grafana.stack

  pdc_network_id = grafana_cloud_private_data_source_connect_network.test.pdc_network_id
  region         = grafana_cloud_private_data_source_connect_network.test.region
  name           = "my-pdc-token"
  display_name   = "My PDC Token"
}

resource "grafana_data_source" "influxdb" {
  provider = grafana.stack

  type                = "influxdb"
  name                = "pdc-test-influx"
  url                 = "http://influxdb.example.net:8086/"
  basic_auth_enabled  = true
  basic_auth_username = "username"
  database_name       = "dbname" // Example: influxdb_database.metrics.name

  json_data_encoded = jsonencode({
    authType          = "default"
    basicAuthPassword = "mypassword"
  })

  private_data_source_connect_network_id = grafana_cloud_private_data_source_connect_network.test.pdc_network_id
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Private Datasource Connect (PDC) in Data Source Support Grafana Private Datasource Connect (PDC)
2 participants