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

Grafana Provider, with Data Source and Dashboard resources #6206

Merged
merged 5 commits into from
May 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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