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

InfluxDB Provider and influxdb_database resource #3478

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 12 additions & 0 deletions builtin/bins/provider-influxdb/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/influxdb"
"github.com/hashicorp/terraform/plugin"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: influxdb.Provider,
})
}
1 change: 1 addition & 0 deletions builtin/bins/provider-influxdb/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
73 changes: 73 additions & 0 deletions builtin/providers/influxdb/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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{
"influxdb_database": ResourceDatabase(),
},

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))
}
36 changes: 36 additions & 0 deletions builtin/providers/influxdb/provider_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
99 changes: 99 additions & 0 deletions builtin/providers/influxdb/resource_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package influxdb

import (
"fmt"

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

func ResourceDatabase() *schema.Resource {
return &schema.Resource{
Create: CreateDatabase,
Read: ReadDatabase,
Delete: DeleteDatabase,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

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

name := d.Get("name").(string)
queryStr := fmt.Sprintf("CREATE DATABASE %s", quoteIdentifier(name))
query := client.Query{
Command: queryStr,
}

resp, err := conn.Query(query)
if err != nil {
return err
}
if resp.Err != nil {
return resp.Err
}

d.SetId(name)

return nil
}

func ReadDatabase(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*client.Client)
name := d.Id()

// InfluxDB doesn't have a command to check the existence of a single
// database, so we instead must read the list of all databases and see
// if ours is present in it.
query := client.Query{
Command: "SHOW DATABASES",
}

resp, err := conn.Query(query)
if err != nil {
return err
}
if resp.Err != nil {
return resp.Err
}

for _, result := range resp.Results[0].Series[0].Values {
if result[0] == name {
return nil
}
}

// If we fell out here then we didn't find our database in the list.
d.SetId("")

return nil
}

func DeleteDatabase(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*client.Client)
name := d.Id()

queryStr := fmt.Sprintf("DROP DATABASE %s", quoteIdentifier(name))
query := client.Query{
Command: queryStr,
}

resp, err := conn.Query(query)
if err != nil {
return err
}
if resp.Err != nil {
return resp.Err
}

d.SetId("")

return nil
}
31 changes: 31 additions & 0 deletions builtin/providers/influxdb/resource_database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package influxdb

import (
"testing"

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

func TestAccDatabase(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDatabaseConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"influxdb_database.test", "name", "terraform-test",
),
),
},
},
})
}

var testAccDatabaseConfig = `

resource "influxdb_database" "test" {
name = "terraform-test"
}

`
1 change: 1 addition & 0 deletions website/source/assets/stylesheets/_docs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ body.layout-docker,
body.layout-dyn,
body.layout-google,
body.layout-heroku,
body.layout-influxdb,
body.layout-mailgun,
body.layout-mysql,
body.layout-openstack,
Expand Down
40 changes: 40 additions & 0 deletions website/source/docs/providers/influxdb/index.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
layout: "influxdb"
page_title: "Provider: InfluxDB"
sidebar_current: "docs-influxdb-index"
description: |-
The InfluxDB provider configures databases, etc on an InfluxDB server.
---

# InfluxDB Provider

The InfluxDB provider allows Terraform to create Databases in
[InfluxDB](https://influxdb.com/). InfluxDB is a database server optimized
for time-series data.

The provider configuration block accepts the following arguments:

* ``url`` - (Optional) The root URL of a InfluxDB server. May alternatively be
set via the ``INFLUXDB_URL`` environment variable. Defaults to
`http://localhost:8086/`.

* ``username`` - (Optional) The name of the user to use when making requests.
May alternatively be set via the ``INFLUXDB_USERNAME`` environment variable.

* ``password`` - (Optional) The password to use when making requests.
May alternatively be set via the ``INFLUXDB_PASSWORD`` environment variable.

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

## Example Usage

```
provider "influxdb" {
url = "http://influxdb.example.com/"
username = "terraform"
}

resource "influxdb_database" "metrics" {
name = "awesome_app"
}
```
30 changes: 30 additions & 0 deletions website/source/docs/providers/influxdb/r/database.html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
layout: "influxdb"
page_title: "InfluxDB: influxdb_database"
sidebar_current: "docs-influxdb-resource-database"
description: |-
The influxdb_database resource allows an InfluxDB database to be created.
---

# influxdb\_database

The database resource allows a database to be created on an InfluxDB server.

## Example Usage

```
resource "influxdb_database" "metrics" {
name = "awesome_app"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name for the database. This must be unique on the
InfluxDB server.

## Attributes Reference

This resource exports no further attributes.
4 changes: 4 additions & 0 deletions website/source/layouts/docs.erb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@
<a href="/docs/providers/heroku/index.html">Heroku</a>
</li>

<li<%= sidebar_current("docs-providers-influxdb") %>>
<a href="/docs/providers/influxdb/index.html">InfluxDB</a>
</li>

<li<%= sidebar_current("docs-providers-mailgun") %>>
<a href="/docs/providers/mailgun/index.html">Mailgun</a>
</li>
Expand Down
26 changes: 26 additions & 0 deletions website/source/layouts/influxdb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<% wrap_layout :inner do %>
<% content_for :sidebar do %>
<div class="docs-sidebar hidden-print affix-top" role="complementary">
<ul class="nav docs-sidenav">
<li<%= sidebar_current("docs-home") %>>
<a href="/docs/providers/index.html">&laquo; Documentation Home</a>
</li>

<li<%= sidebar_current("docs-influxdb-index") %>>
<a href="/docs/providers/influxdb/index.html">InfluxDB Provider</a>
</li>

<li<%= sidebar_current(/^docs-influxdb-resource/) %>>
<a href="#">Resources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-influxdb-resource-database") %>>
<a href="/docs/providers/influxdb/r/database.html">influxdb_database</a>
</li>
</ul>
</li>
</ul>
</div>
<% end %>

<%= yield %>
<% end %>