Skip to content

Commit

Permalink
Grafana Provider, with Data Source and Dashboard resources (#6206)
Browse files Browse the repository at this point in the history
* Grafana provider

* grafana_data_source resource.

Allows data sources to be created in Grafana. Supports all data source
types that are accepted in the current version of Grafana, and will
support any future ones that fit into the existing structure.

* Vendoring of apparentlymart/go-grafana-api

This is in anticipation of adding a Grafana provider plugin.

* grafana_dashboard resource

* Website documentation for the Grafana provider.
  • Loading branch information
apparentlymart authored and stack72 committed May 20, 2016
1 parent 3de2ab0 commit 158a90b
Show file tree
Hide file tree
Showing 125 changed files with 61,456 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions builtin/bins/provider-grafana/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/hashicorp/terraform/builtin/providers/grafana"
"github.com/hashicorp/terraform/plugin"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: grafana.Provider,
})
}
41 changes: 41 additions & 0 deletions builtin/providers/grafana/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package grafana

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"

gapi "github.com/apparentlymart/go-grafana-api"
)

func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"url": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("GRAFANA_URL", nil),
Description: "URL of the root of the target Grafana server.",
},
"auth": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("GRAFANA_AUTH", nil),
Description: "Credentials for accessing the Grafana API.",
},
},

ResourcesMap: map[string]*schema.Resource{
"grafana_dashboard": ResourceDashboard(),
"grafana_data_source": ResourceDataSource(),
},

ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return gapi.New(
d.Get("auth").(string),
d.Get("url").(string),
)
}
54 changes: 54 additions & 0 deletions builtin/providers/grafana/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package grafana

import (
"os"
"testing"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

// To run these acceptance tests, you will need a Grafana server.
// Grafana can be downloaded here: http://grafana.org/download/
//
// The tests will need an API key to authenticate with the server. To create
// one, use the menu for one of your installation's organizations (The
// "Main Org." is fine if you've just done a fresh installation to run these
// tests) to reach the "API Keys" admin page.
//
// Giving the API key the Admin role is the easiest way to ensure enough
// access is granted to run all of the tests.
//
// Once you've created the API key, set the GRAFANA_URL and GRAFANA_AUTH
// environment variables to the Grafana base URL and the API key respectively,
// and then run:
// make testacc TEST=./builtin/providers/grafana

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider

func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"grafana": testAccProvider,
}
}

func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("GRAFANA_URL"); v == "" {
t.Fatal("GRAFANA_URL must be set for acceptance tests")
}
if v := os.Getenv("GRAFANA_AUTH"); v == "" {
t.Fatal("GRAFANA_AUTH must be set for acceptance tests")
}
}
127 changes: 127 additions & 0 deletions builtin/providers/grafana/resource_dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package grafana

import (
"encoding/json"
"fmt"

"github.com/hashicorp/terraform/helper/schema"

gapi "github.com/apparentlymart/go-grafana-api"
)

func ResourceDashboard() *schema.Resource {
return &schema.Resource{
Create: CreateDashboard,
Delete: DeleteDashboard,
Read: ReadDashboard,

Schema: map[string]*schema.Schema{
"slug": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},

"config_json": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: NormalizeDashboardConfigJSON,
ValidateFunc: ValidateDashboardConfigJSON,
},
},
}
}

func CreateDashboard(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)

model := prepareDashboardModel(d.Get("config_json").(string))

resp, err := client.SaveDashboard(model, false)
if err != nil {
return err
}

d.SetId(resp.Slug)

return ReadDashboard(d, meta)
}

func ReadDashboard(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)

slug := d.Id()

dashboard, err := client.Dashboard(slug)
if err != nil {
return err
}

configJSONBytes, err := json.Marshal(dashboard.Model)
if err != nil {
return err
}

configJSON := NormalizeDashboardConfigJSON(string(configJSONBytes))

d.SetId(dashboard.Meta.Slug)
d.Set("slug", dashboard.Meta.Slug)
d.Set("config_json", configJSON)

return nil
}

func DeleteDashboard(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)

slug := d.Id()
return client.DeleteDashboard(slug)
}

func prepareDashboardModel(configJSON string) map[string]interface{} {
configMap := map[string]interface{}{}
err := json.Unmarshal([]byte(configJSON), &configMap)
if err != nil {
// The validate function should've taken care of this.
panic(fmt.Errorf("Invalid JSON got into prepare func"))
}

delete(configMap, "id")
configMap["version"] = 0

return configMap
}

func ValidateDashboardConfigJSON(configI interface{}, k string) ([]string, []error) {
configJSON := configI.(string)
configMap := map[string]interface{}{}
err := json.Unmarshal([]byte(configJSON), &configMap)
if err != nil {
return nil, []error{err}
}
return nil, nil
}

func NormalizeDashboardConfigJSON(configI interface{}) string {
configJSON := configI.(string)

configMap := map[string]interface{}{}
err := json.Unmarshal([]byte(configJSON), &configMap)
if err != nil {
// The validate function should've taken care of this.
return ""
}

// Some properties are managed by this provider and are thus not
// significant when included in the JSON.
delete(configMap, "id")
delete(configMap, "version")

ret, err := json.Marshal(configMap)
if err != nil {
// Should never happen.
return configJSON
}

return string(ret)
}
Loading

0 comments on commit 158a90b

Please sign in to comment.