-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Grafana Provider, with Data Source and Dashboard resources (#6206)
* 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
1 parent
3de2ab0
commit 158a90b
Showing
125 changed files
with
61,456 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.