influxdb_database resource.

Creates a database on an InfluxDB server. InfluxDB databases don't have
any schema or other settings, so this is a really simple resource.
This commit is contained in:
Martin Atkins 2015-10-10 21:01:49 -07:00 committed by Paul Hinze
parent 5ef646e072
commit 1c07e0de21
3 changed files with 131 additions and 0 deletions

View File

@ -16,6 +16,7 @@ var quoteReplacer = strings.NewReplacer(`"`, `\"`)
func Provider() terraform.ResourceProvider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"influxdb_database": ResourceDatabase(),
},
Schema: map[string]*schema.Schema{

View File

@ -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
}

View File

@ -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"
}
`