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

Added ability to specify org_id field in resource definitions #60

Closed
wants to merge 10 commits into from

Conversation

bcampoli
Copy link

@bcampoli bcampoli commented May 12, 2019

Hi guys, here I present to you a pretty significant pull request, which adds the basic functionality to specify the org with which you would like to associate a resource with.

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.

This is very cool because now you can use Terraform to manage the multi-tenancy within Grafana!

Affected Resource(s):

Please list the resources as a list, for example:

  • All resources except for grafana_organization

Example:

For example, given the sample configuration (the below example is using just grafana_data_source):

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 observe the following behavior:

image

and

image

The above is just an example for grafana_data_source but this change now applies to all resources in the provider including dashboards, folders, and notifications.

How?

By adding the following header to API calls: X-Grafana-Org-Id (https://community.grafana.com/t/organization-state-in-the-api/7954), one can specify the org they want the API call to be associated with. This allows for a more RESTful API (even though technically it is not). In order to add this header, I had to also modify the client library that this provider was relying on: (https://github.com/emerald-squad/go-grafana-api).

Since this is a new required field, I have had to modify the acceptance tests to add and check for the org_id field.

Related issues:

https://github.com/terraform-providers/terraform-provider-grafana/issues/59
https://github.com/terraform-providers/terraform-provider-grafana/issues/31

@ghost ghost added the size/L label May 12, 2019
@ghost ghost added size/XL and removed size/L labels May 12, 2019
@bcampoli bcampoli changed the title Added ability to specify org_id field in resource defintions Added ability to specify org_id field in resource definitions May 13, 2019
@jussiritamaki
Copy link

Any plans to get this merged any time soon?

@vincentsjtu
Copy link

this is really a good fix, when could the pr be accepted?

@toelke
Copy link

toelke commented Dec 19, 2019

I would also greatly appreciate having this merged. Is there anything I can do to help?

@bcampoli bcampoli requested a review from mlclmj February 3, 2020 14:50
@fatalglitch
Copy link

Status on getting this merged? This is a pretty crucial function of Grafana in many environments

@@ -2,8 +2,8 @@ module github.com/terraform-providers/terraform-provider-grafana

require (
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/emerald-squad/go-grafana-api v0.0.0-20190510131408-a95b9575e28e
Copy link
Contributor

@tonglil tonglil Jun 3, 2020

Choose a reason for hiding this comment

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

Could you please open a PR of the required changes to support orgs to the upstream library?

We will review the changes necessary there and then come back to the changes here.

@anarsen
Copy link

anarsen commented Aug 25, 2020

@bcampoli Looks like this could use a rebase. I'm looking very much forward to having this merged 🙏

@ghost ghost removed the waiting-response label Aug 25, 2020
@fatalglitch
Copy link

Happy to help as needed, let me know what needs to be done to get this merged into master. This would help immensely with managing all our orgs correctly instead of custom scripts we use today

@qvistgaard
Copy link

Any update on this PR?

@alec-pinson
Copy link

alec-pinson commented Oct 21, 2020

We're also waiting for this... @mlclmj any chance please?

@trotttrotttrott
Copy link
Member

Closing in favor of #110 and grafana/grafana-api-golang-client#9.

@alec-pinson
Copy link

@trotttrotttrott it's good but with terraform we can dynamically provision organisations, we cant dymanically loop through providers. example below

locals {
  orgs = {
    Main = {
      admin_user   = "admin"
      create_users = false
      admins       = []
      editors      = []
      viewers      = []
    }
    NewOrg = {
      admin_user   = "admin"
      create_users = true
      admins       = []
      editors      = []
      viewers      = []
    }
  }
}

resource "grafana_organization" "this" {
  for_each     = local.orgs
  name         = each.key
  admin_user   = each.value.admin_user
  create_users = each.value.create_users
  admins       = each.value.admins
  editors      = each.value.editors
  viewers      = each.value.viewers
}

then data source example

locals {
  data_sources = {
    test-dsource = {
      org           = "NewOrg"
      type          = "mssql"
      url           = "blah.example.com:1433"
      database_name = "my_db"
      username      = "my_user"
      password      = "noidea"
    }
  }
}

resource "grafana_data_source" "this" {
  for_each      = local.data_sources
  org_id        = grafana_organization.this[each.value.org].id
  name          = each.key
  type          = each.value.type
  url           = each.value.url
  database_name = try(each.value.database_name, null)
  username      = try(each.value.username, null)
  password      = try(each.value.password, null)
}

this wont be possible

provider "grafana" {
  for_each = local.grafana_providers
  alias = each.key
}
```hcl

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.

10 participants