Skip to content

Commit

Permalink
Add organization id to the provider.
Browse files Browse the repository at this point in the history
Use the change in grafana/grafana-api-golang-client#9 to
apply an organization id to the provider.

Usage might look like this:

```hcl
provider "grafana" {
  url = "someurl"
  auth = "adminuser:somepass"
}

resource "grafana_organization" "neworg" {
  name = "neworg"
  admin = "neworgadmin"
}

provider "grafana" {
  alias = "neworg"
  url = "someurl"
  auth = "neworgadmin:somepass"
  org_id = grafana_organization.neworg.org_id
}

resource "grafana_data_source" "neworg_graphite" {
  provider = grafana.neworg
  type = "graphite"
  name = "neworg-graphite"
}
```

Co-authored-by: hansnqyr <[email protected]>
  • Loading branch information
medains and hansnqyr committed Oct 28, 2020
1 parent 6fca1d6 commit 41fc759
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-grafana
go 1.14

require (
github.com/grafana/grafana-api-golang-client v0.0.0-20201019145005-e01a63d40166
github.com/grafana/grafana-api-golang-client v0.0.0-20201028095551-dd7fea8a2fce
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform v0.12.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ github.com/gophercloud/utils v0.0.0-20190128072930-fbb6ab446f01 h1:OgCNGSnEalfkR
github.com/gophercloud/utils v0.0.0-20190128072930-fbb6ab446f01/go.mod h1:wjDF8z83zTeg5eMLml5EBSlAhbF7G8DobyI1YsMuyzw=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grafana/grafana-api-golang-client v0.0.0-20201019145005-e01a63d40166 h1:Kl177VV8TQuyCQVYPF3grDYdRcINbSGP2aIuRqM7VZI=
github.com/grafana/grafana-api-golang-client v0.0.0-20201019145005-e01a63d40166/go.mod h1:jFjwT3lvwl4JKqCw3guRJvlQ1/fmhER1h3Zgix3z7jw=
github.com/grafana/grafana-api-golang-client v0.0.0-20201028095551-dd7fea8a2fce h1:98pwTaBO0LTcF6t7j8AIt3PGKdDtZg2/2/RPbr8tf/M=
github.com/grafana/grafana-api-golang-client v0.0.0-20201028095551-dd7fea8a2fce/go.mod h1:jFjwT3lvwl4JKqCw3guRJvlQ1/fmhER1h3Zgix3z7jw=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down
7 changes: 7 additions & 0 deletions grafana/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("GRAFANA_AUTH", nil),
Description: "Credentials for accessing the Grafana API.",
},
"org_id": {
Type: schema.TypeInt,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("GRAFANA_ORG_ID", 1),
Description: "Organization id for resources",
},
},

ResourcesMap: map[string]*schema.Resource{
Expand All @@ -51,6 +57,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
cli.Transport = logging.NewTransport("Grafana", cli.Transport)
cfg := gapi.Config{
Client: cli,
OrgID: d.Get("org_id").(int64),
}
if len(auth) == 2 {
cfg.BasicAuth = url.UserPassword(auth[0], auth[1])
Expand Down
3 changes: 3 additions & 0 deletions grafana/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("GRAFANA_AUTH"); v == "" {
t.Fatal("GRAFANA_AUTH must be set for acceptance tests")
}
if v := os.Getenv("GRAFANA_AUTH"); v == "" {
t.Fatal("GRAFANA_AUTH must be set for acceptance tests")
}
}
9 changes: 7 additions & 2 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ The provider configuration block accepts the following arguments:
are provided in a single string and separated by a colon. May alternatively
be set via the ``GRAFANA_AUTH`` environment variable.

* org_id - (Required) The organization id to operate on within grafana.
Default org_id is 1. May alternatively be set via the
GRAFANA_ORG_ID environment variable.

Use the navigation to the left to read about the available resources.

## Example Usage

```hcl
provider "grafana" {
url = "http://grafana.example.com/"
auth = "1234abcd"
url = "http://grafana.example.com/"
auth = "1234abcd"
org_id = 1
}
resource "grafana_dashboard" "metrics" {
Expand Down

0 comments on commit 41fc759

Please sign in to comment.