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

Allow for org_id field in resource definitions #59

Closed
bcampoli opened this issue May 12, 2019 · 4 comments · Fixed by #416
Closed

Allow for org_id field in resource definitions #59

bcampoli opened this issue May 12, 2019 · 4 comments · Fixed by #416

Comments

@bcampoli
Copy link

bcampoli commented May 12, 2019

I think a basic feature that is missing in this provider is the ability to specify to which org you would like a resource to belong. I understand this is in part due to the construction of the Grafana API. The Grafana API is not RESTful, since it maintains an internal state variable of the current_org between API calls. This is makes implementing this feature slightly more difficult.

This is a crucial feature since internally, each Grafana API object is implicitly linked to an org_id. Therefore, in order to align the Terraform Provider with the data-model used by Grafana, the resources should also have an org_id field.

Affected Resource(s)

Please list the resources as a list, for example:

  • All resources except for grafana_organization

Expected Behavior

The below example is just done with grafana_data_source but applies to every resource in the provider.

Given a sample resource configuration like this:

provider "grafana" {
  url  = "http://localhost:3000/"
  auth = "admin:admin"
}


resource "grafana_organization" "org1" {
  name         = "test-provider1"
  admin_user   = "admin"
}

resource "grafana_organization" "org2" {
  name         = "test-provider2"
  admin_user   = "admin"
}

resource "grafana_data_source" "metrics" {
  type          = "graphite"
  name          = "myapp-metrics-test"
  org_id        = "${grafana_organization.org1.id}"  
}

resource "grafana_data_source" "metrics2" {
  type          = "graphite"
  name          = "myapp-metrics-test2"
  org_id        = "${grafana_organization.org2.id}"  
}

resource "grafana_data_source" "metrics3" {
  type          = "graphite"
  name          = "myapp-metrics-test3"
  org_id        = "${grafana_organization.org1.id}"  
}

resource "grafana_data_source" "metrics4" {
  type          = "graphite"
  name          = "myapp-metrics-test4"
  org_id        = "${grafana_organization.org2.id}"  
}

We should expect for 2 datasources to be created in 2 different orgs, like so:
image

and

image

Actual Behavior

An error because these fields are not available in the current version of the provider

References

Related issues: https://github.com/terraform-providers/terraform-provider-grafana/issues/31#issuecomment-491044905

@bcampoli
Copy link
Author

bcampoli commented May 12, 2019

Pull Request

Opened pull request to add this feature here: https://github.com/terraform-providers/terraform-provider-grafana/pull/60

@ingluife
Copy link

ingluife commented Sep 9, 2021

I'd like to have this feature. Waiting for it.

@v-zhuravlev
Copy link

The pull request implementing org_id in resources was closed in favor to org_id in the provider (#60) Unfortunately, this doesn't allow to create and populate organisation and resources in it in one run of terraform. Only objects in existing orgs can be created which is somehow halfway, in my opinion. Should this approach be reconsidered?

@julienduchesne
Copy link
Member

julienduchesne commented Dec 17, 2021

Most API endpoints do not support the org id parameter and in cases where they do, the header seems to have priority. I feel like support would be all over the place if we were to allow org_id in some resources. There is one exception, which is the admin API: https://grafana.com/docs/grafana/latest/http_api/admin/

As described here: https://grafana.com/docs/grafana/latest/http_api/auth/#x-grafana-org-id-header, the header has no effect in that API

To answer your question (on how to create an org and then use it in a single TF file), you can use multiple providers:

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

provider "grafana" {
  alias = "bootstrap"
  url   = "http://localhost:3000"
  auth  = "admin:admin"
}

resource "grafana_organization" "test_org" {
  provider = grafana.bootstrap
  name     = "test-org"
}

provider "grafana" {
  url    = "http://localhost:3000"
  auth   = "admin:admin"
  org_id = grafana_organization.test_org.id
}

resource "grafana_team" "test_team" {
  name = "test-team"
}

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 a pull request may close this issue.

5 participants