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

Consul param should be "key" #480

Merged
merged 8 commits into from
Oct 20, 2014
Merged
22 changes: 9 additions & 13 deletions builtin/providers/consul/resource_consul_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func resourceConsulKeys() *schema.Resource {
Optional: true,
},

"keys": &schema.Schema{
"key": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Expand Down Expand Up @@ -60,6 +60,7 @@ func resourceConsulKeys() *schema.Resource {
"delete": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
Expand Down Expand Up @@ -113,19 +114,15 @@ func resourceConsulKeysCreate(d *schema.ResourceData, meta interface{}) error {
vars := make(map[string]string)

// Extract the keys
keys := d.Get("keys").(*schema.Set).List()
keys := d.Get("key").(*schema.Set).List()
for _, raw := range keys {
key, path, sub, err := parse_key(raw)
if err != nil {
return err
}

if valueRaw, ok := sub["value"]; ok {
value, ok := valueRaw.(string)
if !ok {
return fmt.Errorf("Failed to get value for key '%s'", key)
}

value := sub["value"].(string)
if value != "" {
log.Printf("[DEBUG] Setting key '%s' to '%v' in %s", path, value, dc)
pair := consulapi.KVPair{Key: path, Value: []byte(value)}
if _, err := kv.Put(&pair, &wOpts); err != nil {
Expand All @@ -142,14 +139,13 @@ func resourceConsulKeysCreate(d *schema.ResourceData, meta interface{}) error {
}
value := attribute_value(sub, key, pair)
vars[key] = value
sub["value"] = value
}
}

// Update the resource
d.SetId("consul")
d.Set("datacenter", dc)
d.Set("keys", keys)
d.Set("key", keys)
d.Set("var", vars)
return nil
}
Expand Down Expand Up @@ -178,7 +174,7 @@ func resourceConsulKeysRead(d *schema.ResourceData, meta interface{}) error {
vars := make(map[string]string)

// Extract the keys
keys := d.Get("keys").(*schema.Set).List()
keys := d.Get("key").(*schema.Set).List()
for _, raw := range keys {
key, path, sub, err := parse_key(raw)
if err != nil {
Expand All @@ -197,7 +193,7 @@ func resourceConsulKeysRead(d *schema.ResourceData, meta interface{}) error {
}

// Update the resource
d.Set("keys", keys)
d.Set("key", keys)
d.Set("var", vars)
return nil
}
Expand All @@ -223,7 +219,7 @@ func resourceConsulKeysDelete(d *schema.ResourceData, meta interface{}) error {
wOpts := consulapi.WriteOptions{Datacenter: dc, Token: token}

// Extract the keys
keys := d.Get("keys").(*schema.Set).List()
keys := d.Get("key").(*schema.Set).List()
for _, raw := range keys {
_, path, sub, err := parse_key(raw)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions builtin/providers/consul/resource_consul_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestAccConsulKeys(t *testing.T) {

func testAccCheckConsulKeysDestroy(s *terraform.State) error {
kv := testAccProvider.Meta().(*consulapi.Client).KV()
opts := &consulapi.QueryOptions{Datacenter: "nyc1"}
opts := &consulapi.QueryOptions{Datacenter: "nyc3"}
pair, _, err := kv.Get("test/set", opts)
if err != nil {
return err
Expand All @@ -44,7 +44,7 @@ func testAccCheckConsulKeysDestroy(s *terraform.State) error {
func testAccCheckConsulKeysExists() resource.TestCheckFunc {
return func(s *terraform.State) error {
kv := testAccProvider.Meta().(*consulapi.Client).KV()
opts := &consulapi.QueryOptions{Datacenter: "nyc1"}
opts := &consulapi.QueryOptions{Datacenter: "nyc3"}
pair, _, err := kv.Get("test/set", opts)
if err != nil {
return err
Expand Down Expand Up @@ -78,7 +78,7 @@ func testAccCheckConsulKeysValue(n, attr, val string) resource.TestCheckFunc {

const testAccConsulKeysConfig = `
resource "consul_keys" "app" {
datacenter = "nyc1"
datacenter = "nyc3"
key {
name = "time"
path = "global/time"
Expand Down
10 changes: 9 additions & 1 deletion builtin/providers/consul/resource_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consul
import (
"testing"

"github.com/armon/consul-api"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
Expand All @@ -16,6 +17,13 @@ func init() {
testAccProviders = map[string]terraform.ResourceProvider{
"consul": testAccProvider,
}

// Use the demo address for the acceptance tests
testAccProvider.ConfigureFunc = func(d *schema.ResourceData) (interface{}, error) {
conf := consulapi.DefaultConfig()
conf.Address = "demo.consul.io:80"
return consulapi.NewClient(conf)
}
}

func TestResourceProvider(t *testing.T) {
Expand All @@ -33,7 +41,7 @@ func TestResourceProvider_Configure(t *testing.T) {

raw := map[string]interface{}{
"address": "demo.consul.io:80",
"datacenter": "nyc1",
"datacenter": "nyc3",
}

rawConfig, err := config.NewRawConfig(raw)
Expand Down
114 changes: 62 additions & 52 deletions helper/schema/resource_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ type getSource byte
const (
getSourceState getSource = 1 << iota
getSourceConfig
getSourceDiff
getSourceSet
getSourceExact
getSourceMax = getSourceSet
getSourceExact // Only get from the _exact_ level
getSourceDiff // Apply the diff on top our level
getSourceLevelMask getSource = getSourceState | getSourceConfig | getSourceSet
getSourceMax getSource = getSourceSet
)

// getResult is the internal structure that is generated when a Get
Expand Down Expand Up @@ -82,7 +83,7 @@ func (d *ResourceData) Get(key string) interface{} {
// set and the new value is. This is common, for example, for boolean
// fields which have a zero value of false.
func (d *ResourceData) GetChange(key string) (interface{}, interface{}) {
o, n := d.getChange(key, getSourceConfig, getSourceDiff)
o, n := d.getChange(key, getSourceConfig, getSourceConfig|getSourceDiff)
return o.Value, n.Value
}

Expand All @@ -93,7 +94,7 @@ func (d *ResourceData) GetChange(key string) (interface{}, interface{}) {
// The first result will not necessarilly be nil if the value doesn't exist.
// The second result should be checked to determine this information.
func (d *ResourceData) GetOk(key string) (interface{}, bool) {
r := d.getRaw(key, getSourceSet)
r := d.getRaw(key, getSourceSet|getSourceDiff)
return r.Value, r.Exists
}

Expand Down Expand Up @@ -289,12 +290,21 @@ func (d *ResourceData) getSet(
// entire set must come from set, diff, state, etc. So we go backwards
// and once we get a result, we take it. Or, we never get a result.
var raw getResult
for listSource := source; listSource > 0; listSource >>= 1 {
if source&getSourceExact != 0 && listSource != source {
sourceLevel := source & getSourceLevelMask
sourceFlags := source & ^getSourceLevelMask
for listSource := sourceLevel; listSource > 0; listSource >>= 1 {
// If we're already asking for an exact source and it doesn't
// match, then leave since the original source was the match.
if sourceFlags&getSourceExact != 0 && listSource != sourceLevel {
break
}

raw = d.getList(k, nil, schema, listSource|getSourceExact)
// The source we get from is the level we're on, plus the flags
// we had, plus the exact flag.
getSource := listSource
getSource |= sourceFlags
getSource |= getSourceExact
raw = d.getList(k, nil, schema, getSource)
if raw.Exists {
break
}
Expand Down Expand Up @@ -384,11 +394,13 @@ func (d *ResourceData) getMap(
resultSet := false
prefix := k + "."

exact := source&getSourceExact != 0
source &^= getSourceExact
flags := source & ^getSourceLevelMask
level := source & getSourceLevelMask
exact := flags&getSourceExact != 0
diff := flags&getSourceDiff != 0

if !exact || source == getSourceState {
if d.state != nil && source >= getSourceState {
if !exact || level == getSourceState {
if d.state != nil && level >= getSourceState {
for k, _ := range d.state.Attributes {
if !strings.HasPrefix(k, prefix) {
continue
Expand All @@ -401,7 +413,7 @@ func (d *ResourceData) getMap(
}
}

if d.config != nil && source == getSourceConfig {
if d.config != nil && level == getSourceConfig {
// For config, we always set the result to exactly what was requested
if mraw, ok := d.config.Get(k); ok {
result = make(map[string]interface{})
Expand Down Expand Up @@ -433,27 +445,25 @@ func (d *ResourceData) getMap(
}
}

if !exact || source == getSourceDiff {
if d.diff != nil && source >= getSourceDiff {
for k, v := range d.diff.Attributes {
if !strings.HasPrefix(k, prefix) {
continue
}
resultSet = true
if d.diff != nil && diff {
for k, v := range d.diff.Attributes {
if !strings.HasPrefix(k, prefix) {
continue
}
resultSet = true

single := k[len(prefix):]
single := k[len(prefix):]

if v.NewRemoved {
delete(result, single)
} else {
result[single] = d.getPrimitive(k, nil, elemSchema, source).Value
}
if v.NewRemoved {
delete(result, single)
} else {
result[single] = d.getPrimitive(k, nil, elemSchema, source).Value
}
}
}

if !exact || source == getSourceSet {
if d.setMap != nil && source >= getSourceSet {
if !exact || level == getSourceSet {
if d.setMap != nil && level >= getSourceSet {
cleared := false
for k, _ := range d.setMap {
if !strings.HasPrefix(k, prefix) {
Expand Down Expand Up @@ -577,8 +587,10 @@ func (d *ResourceData) getPrimitive(
var result string
var resultProcessed interface{}
var resultComputed, resultSet bool
exact := source&getSourceExact != 0
source &^= getSourceExact
flags := source & ^getSourceLevelMask
source = source & getSourceLevelMask
exact := flags&getSourceExact != 0
diff := flags&getSourceDiff != 0

if !exact || source == getSourceState {
if d.state != nil && source >= getSourceState {
Expand All @@ -604,28 +616,26 @@ func (d *ResourceData) getPrimitive(
resultComputed = d.config.IsComputed(k)
}

if !exact || source == getSourceDiff {
if d.diff != nil && source >= getSourceDiff {
attrD, ok := d.diff.Attributes[k]
if ok {
if !attrD.NewComputed {
result = attrD.New
if attrD.NewExtra != nil {
// If NewExtra != nil, then we have processed data as the New,
// so we store that but decode the unprocessed data into result
resultProcessed = result

err := mapstructure.WeakDecode(attrD.NewExtra, &result)
if err != nil {
panic(err)
}
}
if d.diff != nil && diff {
attrD, ok := d.diff.Attributes[k]
if ok {
if !attrD.NewComputed {
result = attrD.New
if attrD.NewExtra != nil {
// If NewExtra != nil, then we have processed data as the New,
// so we store that but decode the unprocessed data into result
resultProcessed = result

resultSet = true
} else {
result = ""
resultSet = false
err := mapstructure.WeakDecode(attrD.NewExtra, &result)
if err != nil {
panic(err)
}
}

resultSet = true
} else {
result = ""
resultSet = false
}
}
}
Expand Down Expand Up @@ -1101,14 +1111,14 @@ func (d *ResourceData) stateSingle(
func (d *ResourceData) stateSource(prefix string) getSource {
// If we're not doing a partial apply, then get the set level
if !d.partial {
return getSourceSet
return getSourceSet | getSourceDiff
}

// Otherwise, only return getSourceSet if its in the partial map.
// Otherwise we use state level only.
for k, _ := range d.partialMap {
if strings.HasPrefix(prefix, k) {
return getSourceSet
return getSourceSet | getSourceDiff
}
}

Expand Down
Loading