diff --git a/builtin/bins/provider-influxdb/main.go b/builtin/bins/provider-influxdb/main.go new file mode 100644 index 0000000000..84d0959570 --- /dev/null +++ b/builtin/bins/provider-influxdb/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "github.com/hashicorp/terraform/builtin/providers/influxdb" + "github.com/hashicorp/terraform/plugin" +) + +func main() { + plugin.Serve(&plugin.ServeOpts{ + ProviderFunc: influxdb.Provider, + }) +} diff --git a/builtin/bins/provider-influxdb/main_test.go b/builtin/bins/provider-influxdb/main_test.go new file mode 100644 index 0000000000..06ab7d0f9a --- /dev/null +++ b/builtin/bins/provider-influxdb/main_test.go @@ -0,0 +1 @@ +package main diff --git a/builtin/providers/influxdb/provider.go b/builtin/providers/influxdb/provider.go new file mode 100644 index 0000000000..d578a902e3 --- /dev/null +++ b/builtin/providers/influxdb/provider.go @@ -0,0 +1,72 @@ +package influxdb + +import ( + "fmt" + "net/url" + "strings" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/terraform" + "github.com/influxdb/influxdb/client" +) + +var quoteReplacer = strings.NewReplacer(`"`, `\"`) + +// Provider returns a terraform.ResourceProvider. +func Provider() terraform.ResourceProvider { + return &schema.Provider{ + ResourcesMap: map[string]*schema.Resource{ + }, + + Schema: map[string]*schema.Schema{ + "url": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + DefaultFunc: schema.EnvDefaultFunc( + "INFLUXDB_URL", "http://localhost:8086/", + ), + }, + "username": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + DefaultFunc: schema.EnvDefaultFunc("INFLUXDB_USERNAME", ""), + }, + "password": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + DefaultFunc: schema.EnvDefaultFunc("INFLUXDB_PASSWORD", ""), + }, + }, + + ConfigureFunc: Configure, + } +} + +func Configure(d *schema.ResourceData) (interface{}, error) { + url, err := url.Parse(d.Get("url").(string)) + if err != nil { + return nil, fmt.Errorf("invalid InfluxDB URL: %s", err) + } + + config := client.Config{ + URL: *url, + Username: d.Get("username").(string), + Password: d.Get("password").(string), + } + + conn, err := client.NewClient(config) + if err != nil { + return nil, err + } + + _, _, err = conn.Ping() + if err != nil { + return nil, fmt.Errorf("error pinging server: %s", err) + } + + return conn, nil +} + +func quoteIdentifier(ident string) string { + return fmt.Sprintf(`"%s"`, quoteReplacer.Replace(ident)) +} diff --git a/builtin/providers/influxdb/provider_test.go b/builtin/providers/influxdb/provider_test.go new file mode 100644 index 0000000000..d985527126 --- /dev/null +++ b/builtin/providers/influxdb/provider_test.go @@ -0,0 +1,36 @@ +package influxdb + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/terraform" +) + +// To run these acceptance tests, you will need an InfluxDB server. +// If you download an InfluxDB distribution and run it with its default +// settings, on the same host where the tests are being run, then these tests +// should work with no further configuration. +// +// To run the tests against a remote InfluxDB server, set the INFLUXDB_URL, +// INFLUXDB_USERNAME and INFLUXDB_PASSWORD environment variables. + +var testAccProviders map[string]terraform.ResourceProvider +var testAccProvider *schema.Provider + +func init() { + testAccProvider = Provider().(*schema.Provider) + testAccProviders = map[string]terraform.ResourceProvider{ + "influxdb": 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() +}