Merge pull request #396 from hashicorp/f-consul-schema

Port Consul provider to new helper.Schema, add Token support
This commit is contained in:
Mitchell Hashimoto 2014-10-14 12:21:23 -07:00
commit 6fea336c09
7 changed files with 226 additions and 234 deletions

View File

@ -3,13 +3,10 @@ package main
import ( import (
"github.com/hashicorp/terraform/builtin/providers/consul" "github.com/hashicorp/terraform/builtin/providers/consul"
"github.com/hashicorp/terraform/plugin" "github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
) )
func main() { func main() {
plugin.Serve(&plugin.ServeOpts{ plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider { ProviderFunc: consul.Provider,
return new(consul.ResourceProvider)
},
}) })
} }

View File

@ -1,116 +1,229 @@
package consul package consul
import ( import (
"bytes"
"fmt" "fmt"
"log" "log"
"strconv" "strconv"
"github.com/armon/consul-api" "github.com/armon/consul-api"
"github.com/hashicorp/terraform/flatmap" "github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/config" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/diff"
"github.com/hashicorp/terraform/terraform"
) )
func resource_consul_keys_validation() *config.Validator { func resourceConsulKeys() *schema.Resource {
return &config.Validator{ return &schema.Resource{
Required: []string{ Create: resourceConsulKeysCreate,
"key.*.name", Update: resourceConsulKeysCreate,
"key.*.path", Read: resourceConsulKeysRead,
}, Delete: resourceConsulKeysDelete,
Optional: []string{
"datacenter", Schema: map[string]*schema.Schema{
"key.*.value", "datacenter": &schema.Schema{
"key.*.default", Type: schema.TypeString,
"key.*.delete", Optional: true,
Computed: true,
ForceNew: true,
},
"token": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"keys": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"path": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"default": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"delete": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
},
},
Set: resourceConsulKeysHash,
},
"var": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
}, },
} }
} }
func resource_consul_keys_update(
s *terraform.InstanceState, func resourceConsulKeysHash(v interface{}) int {
d *terraform.InstanceDiff, var buf bytes.Buffer
meta interface{}) (*terraform.InstanceState, error) { m := v.(map[string]interface{})
return resource_consul_keys_create(s, d, meta) buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["path"].(string)))
return hashcode.String(buf.String())
} }
func resource_consul_keys_create( func resourceConsulKeysCreate(d *schema.ResourceData, meta interface{}) error {
s *terraform.InstanceState, client := meta.(*consulapi.Client)
d *terraform.InstanceDiff, kv := client.KV()
meta interface{}) (*terraform.InstanceState, error) {
p := meta.(*ResourceProvider)
// Merge the diff into the state so that we have all the attributes // Resolve the datacenter first, all the other keys are dependent
// properly. // on this.
rs := s.MergeDiff(d) var dc string
rs.ID = "consul" if v, ok := d.GetOk("datacenter"); ok {
dc = v.(string)
// Check if the datacenter should be computed log.Printf("[DEBUG] Consul datacenter: %s", dc)
dc := rs.Attributes["datacenter"] } else {
if aDiff, ok := d.Attributes["datacenter"]; ok && aDiff.NewComputed { log.Printf("[DEBUG] Resolving Consul datacenter...")
var err error var err error
dc, err = get_dc(p.client) dc, err = get_dc(client)
if err != nil { if err != nil {
return rs, fmt.Errorf("Failed to get agent datacenter: %v", err) return err
} }
rs.Attributes["datacenter"] = dc }
var token string
if v, ok := d.GetOk("token"); ok {
token = v.(string)
} }
// Get the keys // Setup the operations using the datacenter
keys, ok := flatmap.Expand(rs.Attributes, "key").([]interface{}) qOpts := consulapi.QueryOptions{Datacenter: dc, Token: token}
if !ok { wOpts := consulapi.WriteOptions{Datacenter: dc, Token: token}
return rs, fmt.Errorf("Failed to unroll keys")
}
kv := p.client.KV() // Store the computed vars
qOpts := consulapi.QueryOptions{Datacenter: dc} vars := make(map[string]string)
wOpts := consulapi.WriteOptions{Datacenter: dc}
for idx, raw := range keys { // Extract the keys
keys := d.Get("keys").(*schema.Set).List()
for _, raw := range keys {
key, path, sub, err := parse_key(raw) key, path, sub, err := parse_key(raw)
if err != nil { if err != nil {
return rs, err return err
} }
if valueRaw, ok := sub["value"]; ok { if valueRaw, ok := sub["value"]; ok {
value, ok := valueRaw.(string) value, ok := valueRaw.(string)
if !ok { if !ok {
return rs, fmt.Errorf("Failed to get value for key '%s'", key) return fmt.Errorf("Failed to get value for key '%s'", key)
} }
log.Printf("[DEBUG] Setting key '%s' to '%v' in %s", path, value, dc) log.Printf("[DEBUG] Setting key '%s' to '%v' in %s", path, value, dc)
pair := consulapi.KVPair{Key: path, Value: []byte(value)} pair := consulapi.KVPair{Key: path, Value: []byte(value)}
if _, err := kv.Put(&pair, &wOpts); err != nil { if _, err := kv.Put(&pair, &wOpts); err != nil {
return rs, fmt.Errorf("Failed to set Consul key '%s': %v", path, err) return fmt.Errorf("Failed to set Consul key '%s': %v", path, err)
} }
rs.Attributes[fmt.Sprintf("var.%s", key)] = value vars[key] = value
rs.Attributes[fmt.Sprintf("key.%d.value", idx)] = value sub["value"] = value
} else { } else {
log.Printf("[DEBUG] Getting key '%s' in %s", path, dc) log.Printf("[DEBUG] Getting key '%s' in %s", path, dc)
pair, _, err := kv.Get(path, &qOpts) pair, _, err := kv.Get(path, &qOpts)
if err != nil { if err != nil {
return rs, fmt.Errorf("Failed to get Consul key '%s': %v", path, err) return fmt.Errorf("Failed to get Consul key '%s': %v", path, err)
} }
rs.Attributes[fmt.Sprintf("var.%s", key)] = attribute_value(sub, key, pair) value := attribute_value(sub, key, pair)
vars[key] = value
sub["value"] = value
} }
} }
return rs, nil
// Update the resource
d.SetId("consul")
d.Set("datacenter", dc)
d.Set("keys", keys)
d.Set("var", vars)
return nil
} }
func resource_consul_keys_destroy( func resourceConsulKeysRead(d *schema.ResourceData, meta interface{}) error {
s *terraform.InstanceState, client := meta.(*consulapi.Client)
meta interface{}) error {
p := meta.(*ResourceProvider)
client := p.client
kv := client.KV() kv := client.KV()
// Get the keys // Get the DC, error if not available.
keys, ok := flatmap.Expand(s.Attributes, "key").([]interface{}) var dc string
if !ok { if v, ok := d.GetOk("datacenter"); ok {
return fmt.Errorf("Failed to unroll keys") dc = v.(string)
log.Printf("[DEBUG] Consul datacenter: %s", dc)
} else {
return fmt.Errorf("Missing datacenter configuration")
}
var token string
if v, ok := d.GetOk("token"); ok {
token = v.(string)
} }
dc := s.Attributes["datacenter"] // Setup the operations using the datacenter
wOpts := consulapi.WriteOptions{Datacenter: dc} qOpts := consulapi.QueryOptions{Datacenter: dc, Token: token}
// Store the computed vars
vars := make(map[string]string)
// Extract the keys
keys := d.Get("keys").(*schema.Set).List()
for _, raw := range keys {
key, path, sub, err := parse_key(raw)
if err != nil {
return err
}
log.Printf("[DEBUG] Refreshing value of key '%s' in %s", path, dc)
pair, _, err := kv.Get(path, &qOpts)
if err != nil {
return fmt.Errorf("Failed to get value for path '%s' from Consul: %v", path, err)
}
value := attribute_value(sub, key, pair)
vars[key] = value
sub["value"] = value
}
// Update the resource
d.Set("keys", keys)
d.Set("var", vars)
return nil
}
func resourceConsulKeysDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*consulapi.Client)
kv := client.KV()
// Get the DC, error if not available.
var dc string
if v, ok := d.GetOk("datacenter"); ok {
dc = v.(string)
log.Printf("[DEBUG] Consul datacenter: %s", dc)
} else {
return fmt.Errorf("Missing datacenter configuration")
}
var token string
if v, ok := d.GetOk("token"); ok {
token = v.(string)
}
// Setup the operations using the datacenter
wOpts := consulapi.WriteOptions{Datacenter: dc, Token: token}
// Extract the keys
keys := d.Get("keys").(*schema.Set).List()
for _, raw := range keys { for _, raw := range keys {
_, path, sub, err := parse_key(raw) _, path, sub, err := parse_key(raw)
if err != nil { if err != nil {
@ -128,76 +241,12 @@ func resource_consul_keys_destroy(
return fmt.Errorf("Failed to delete Consul key '%s': %v", path, err) return fmt.Errorf("Failed to delete Consul key '%s': %v", path, err)
} }
} }
// Clear the ID
d.SetId("")
return nil return nil
} }
func resource_consul_keys_diff(
s *terraform.InstanceState,
c *terraform.ResourceConfig,
meta interface{}) (*terraform.InstanceDiff, error) {
// Determine the list of computed variables
var computed []string
keys, ok := flatmap.Expand(flatmap.Flatten(c.Config), "key").([]interface{})
if !ok {
goto AFTER
}
for _, sub := range keys {
key, _, _, err := parse_key(sub)
if err != nil {
continue
}
computed = append(computed, "var."+key)
}
AFTER:
b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{
"datacenter": diff.AttrTypeCreate,
"key": diff.AttrTypeUpdate,
},
ComputedAttrsUpdate: computed,
}
return b.Diff(s, c)
}
func resource_consul_keys_refresh(
s *terraform.InstanceState,
meta interface{}) (*terraform.InstanceState, error) {
p := meta.(*ResourceProvider)
client := p.client
kv := client.KV()
// Get the list of keys
keys, ok := flatmap.Expand(s.Attributes, "key").([]interface{})
if !ok {
return s, fmt.Errorf("Failed to unroll keys")
}
// Update each key
dc := s.Attributes["datacenter"]
opts := consulapi.QueryOptions{Datacenter: dc}
for idx, raw := range keys {
key, path, sub, err := parse_key(raw)
if err != nil {
return s, err
}
log.Printf("[DEBUG] Refreshing value of key '%s' in %s", path, dc)
pair, _, err := kv.Get(path, &opts)
if err != nil {
return s, fmt.Errorf("Failed to get value for path '%s' from Consul: %v", path, err)
}
setVal := attribute_value(sub, key, pair)
s.Attributes[fmt.Sprintf("var.%s", key)] = setVal
if _, ok := sub["value"]; ok {
s.Attributes[fmt.Sprintf("key.%d.value", idx)] = setVal
}
}
return s, nil
}
// parse_key is used to parse a key into a name, path, config or error // parse_key is used to parse a key into a name, path, config or error
func parse_key(raw interface{}) (string, string, map[string]interface{}, error) { func parse_key(raw interface{}) (string, string, map[string]interface{}, error) {
sub, ok := raw.(map[string]interface{}) sub, ok := raw.(map[string]interface{})
@ -217,7 +266,8 @@ func parse_key(raw interface{}) (string, string, map[string]interface{}, error)
return key, path, sub, nil return key, path, sub, nil
} }
// attribute_value determienes the value for a key // attribute_value determines the value for a key, potentially
// using a default value if provided.
func attribute_value(sub map[string]interface{}, key string, pair *consulapi.KVPair) string { func attribute_value(sub map[string]interface{}, key string, pair *consulapi.KVPair) string {
// Use the value if given // Use the value if given
if pair != nil { if pair != nil {

View File

@ -29,7 +29,7 @@ func TestAccConsulKeys(t *testing.T) {
} }
func testAccCheckConsulKeysDestroy(s *terraform.State) error { func testAccCheckConsulKeysDestroy(s *terraform.State) error {
kv := testAccProvider.client.KV() kv := testAccProvider.Meta().(*consulapi.Client).KV()
opts := &consulapi.QueryOptions{Datacenter: "nyc1"} opts := &consulapi.QueryOptions{Datacenter: "nyc1"}
pair, _, err := kv.Get("test/set", opts) pair, _, err := kv.Get("test/set", opts)
if err != nil { if err != nil {
@ -43,7 +43,7 @@ func testAccCheckConsulKeysDestroy(s *terraform.State) error {
func testAccCheckConsulKeysExists() resource.TestCheckFunc { func testAccCheckConsulKeysExists() resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
kv := testAccProvider.client.KV() kv := testAccProvider.Meta().(*consulapi.Client).KV()
opts := &consulapi.QueryOptions{Datacenter: "nyc1"} opts := &consulapi.QueryOptions{Datacenter: "nyc1"}
pair, _, err := kv.Get("test/set", opts) pair, _, err := kv.Get("test/set", opts)
if err != nil { if err != nil {

View File

@ -3,71 +3,40 @@ package consul
import ( import (
"log" "log"
"github.com/armon/consul-api" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/mapstructure"
) )
type ResourceProvider struct { // Provider returns a terraform.ResourceProvider.
Config Config func Provider() terraform.ResourceProvider {
client *consulapi.Client return &schema.Provider{
} Schema: map[string]*schema.Schema{
"datacenter": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
func (p *ResourceProvider) Input( "address": &schema.Schema{
input terraform.UIInput, Type: schema.TypeString,
c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) { Optional: true,
return c, nil },
}
func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
v := &config.Validator{
Optional: []string{
"datacenter",
"address",
}, },
ResourcesMap: map[string]*schema.Resource{
"consul_keys": resourceConsulKeys(),
},
ConfigureFunc: providerConfigure,
} }
return v.Validate(c)
} }
func (p *ResourceProvider) ValidateResource( func providerConfigure(d *schema.ResourceData) (interface{}, error) {
t string, c *terraform.ResourceConfig) ([]string, []error) { var config Config
return resourceMap.Validate(t, c) configRaw := d.Get("").(map[string]interface{})
} if err := mapstructure.Decode(configRaw, &config); err != nil {
return nil, err
func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error {
if _, err := config.Decode(&p.Config, c.Config); err != nil {
return err
} }
log.Printf("[INFO] Initializing Consul client") log.Printf("[INFO] Initializing Consul client")
var err error return config.Client()
p.client, err = p.Config.Client()
if err != nil {
return err
}
return nil
}
func (p *ResourceProvider) Apply(
info *terraform.InstanceInfo,
s *terraform.InstanceState,
d *terraform.InstanceDiff) (*terraform.InstanceState, error) {
return resourceMap.Apply(info, s, d, p)
}
func (p *ResourceProvider) Diff(
info *terraform.InstanceInfo,
s *terraform.InstanceState,
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
return resourceMap.Diff(info, s, c, p)
}
func (p *ResourceProvider) Refresh(
info *terraform.InstanceInfo,
s *terraform.InstanceState) (*terraform.InstanceState, error) {
return resourceMap.Refresh(info, s, p)
}
func (p *ResourceProvider) Resources() []terraform.ResourceType {
return resourceMap.Resources()
} }

View File

@ -1,30 +1,35 @@
package consul package consul
import ( import (
"reflect"
"testing" "testing"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
var testAccProviders map[string]terraform.ResourceProvider var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *ResourceProvider var testAccProvider *schema.Provider
func init() { func init() {
testAccProvider = new(ResourceProvider) testAccProvider = Provider().(*schema.Provider)
testAccProvider.Config.Address = "demo.consul.io:80"
testAccProviders = map[string]terraform.ResourceProvider{ testAccProviders = map[string]terraform.ResourceProvider{
"consul": testAccProvider, "consul": testAccProvider,
} }
} }
func TestResourceProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestResourceProvider_impl(t *testing.T) { func TestResourceProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = new(ResourceProvider) var _ terraform.ResourceProvider = Provider()
} }
func TestResourceProvider_Configure(t *testing.T) { func TestResourceProvider_Configure(t *testing.T) {
rp := new(ResourceProvider) rp := Provider()
raw := map[string]interface{}{ raw := map[string]interface{}{
"address": "demo.consul.io:80", "address": "demo.consul.io:80",
@ -40,13 +45,4 @@ func TestResourceProvider_Configure(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
expected := Config{
Address: "demo.consul.io:80",
Datacenter: "nyc1",
}
if !reflect.DeepEqual(rp.Config, expected) {
t.Fatalf("bad: %#v", rp.Config)
}
} }

View File

@ -1,24 +0,0 @@
package consul
import (
"github.com/hashicorp/terraform/helper/resource"
)
// resourceMap is the mapping of resources we support to their basic
// operations. This makes it easy to implement new resource types.
var resourceMap *resource.Map
func init() {
resourceMap = &resource.Map{
Mapping: map[string]resource.Resource{
"consul_keys": resource.Resource{
ConfigValidator: resource_consul_keys_validation(),
Create: resource_consul_keys_create,
Destroy: resource_consul_keys_destroy,
Update: resource_consul_keys_update,
Diff: resource_consul_keys_diff,
Refresh: resource_consul_keys_refresh,
},
},
}
}

View File

@ -16,6 +16,7 @@ and to expose infrastructure details to clients.
``` ```
resource "consul_keys" "app" { resource "consul_keys" "app" {
datacenter = "nyc1" datacenter = "nyc1"
token = "abcd"
# Read the launch AMI from Consul # Read the launch AMI from Consul
key { key {
@ -46,6 +47,9 @@ The following arguments are supported:
* `datacenter` - (Optional) The datacenter to use. This overrides the * `datacenter` - (Optional) The datacenter to use. This overrides the
datacenter in the provider setup and the agent's default datacenter. datacenter in the provider setup and the agent's default datacenter.
* `token` - (Optional) The ACL token to use. This overrides the
token that the agent provides by default.
* `key` - (Required) Specifies a key in Consul to be read or written. * `key` - (Required) Specifies a key in Consul to be read or written.
Supported values documented below. Supported values documented below.