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

added cas support for consul_keys resource #356

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
13 changes: 13 additions & 0 deletions consul/key_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ func (c *keyClient) Put(path, value string, flags int) error {
return nil
}

func (c *keyClient) Cas(path, value string, flags int, cas int) (bool, error) {
log.Printf(
"[DEBUG] Setting key '%s' to '%v' with cas %d in %s",
path, value, cas, c.wOpts.Datacenter,
)
pair := consulapi.KVPair{Key: path, Value: []byte(value), Flags: uint64(flags), ModifyIndex: uint64(cas)}
written, _, err := c.client.CAS(&pair, c.wOpts)
if err != nil {
return false, fmt.Errorf("failed to write Consul key '%s': %s", path, err)
}
return written, nil
}

func (c *keyClient) Delete(path string) error {
log.Printf(
"[DEBUG] Deleting key '%s' in %s",
Expand Down
17 changes: 14 additions & 3 deletions consul/resource_consul_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ func resourceConsulKeys() *schema.Resource {
Optional: true,
Default: 0,
},

"cas": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
},
"default": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -111,7 +115,6 @@ func resourceConsulKeys() *schema.Resource {

func resourceConsulKeysCreateUpdate(d *schema.ResourceData, meta interface{}) error {
keyClient := newKeyClient(d, meta)

if d.HasChange("key") {
o, n := d.GetChange("key")
if o == nil {
Expand Down Expand Up @@ -154,7 +157,15 @@ func resourceConsulKeysCreateUpdate(d *schema.ResourceData, meta interface{}) er
}

flags := sub["flags"].(int)

cas := sub["cas"].(int)
if cas != -1 {
_, err := keyClient.Cas(path, value, flags, cas)
if err != nil {
return err
}
addedPaths[path] = true
continue
}
if err := keyClient.Put(path, value, flags); err != nil {
return err
}
Expand Down
24 changes: 13 additions & 11 deletions consul/resource_consul_keys_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ func TestConsulKeysMigrateState(t *testing.T) {
},
Expected: map[string]string{
"key.#": "2",
"key.3630941097.default": "",
"key.3630941097.delete": "true",
"key.3630941097.name": "temp",
"key.3630941097.path": "foo/foo",
"key.3630941097.value": "",
"key.3975462262.path": "foo/bar",
"key.3975462262.default": "",
"key.3975462262.delete": "false",
"key.3975462262.name": "hello",
"key.3975462262.value": "world",
"key.3975462262.flags": "0",
"key.1529757638.default": "",
"key.1529757638.delete": "true",
"key.3609964659.name": "hello",
"key.1529757638.path": "foo/foo",
"key.1529757638.value": "",
"key.1529757638.flags": "0",
"key.1529757638.cas": "0",
"key.3609964659.path": "foo/bar",
"key.3609964659.default": "",
"key.3609964659.delete": "false",
"key.1529757638.name": "temp",
"key.3609964659.value": "world",
"key.3609964659.cas": "0",
},
},
}
Expand Down
31 changes: 30 additions & 1 deletion consul/resource_consul_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAccConsulKeys_basic(t *testing.T) {
testAccCheckConsulKeysValue("consul_keys.app", "enabled", "true"),
testAccCheckConsulKeysValue("consul_keys.app", "set", "acceptance"),
testAccCheckConsulKeysValue("consul_keys.app", "remove_one", "hello"),
resource.TestCheckResourceAttr("consul_keys.app", "key.4258512057.flags", "0"),
resource.TestCheckResourceAttr("consul_keys.app", "key.3606807749.flags", "0"),
),
},
{
Expand All @@ -42,6 +42,24 @@ func TestAccConsulKeys_basic(t *testing.T) {
})
}

func TestAccConsulKeys_Cas(t *testing.T) {
providers, _ := startTestServer(t)

resource.Test(t, resource.TestCase{
Providers: providers,
Steps: []resource.TestStep{
{
Config: testAccConsulKeysConfig_Cas,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("consul_keys.app", "key.2637474718.value", "testVal"),
resource.TestCheckResourceAttr("consul_keys.app", "key.2637474718.path", "test/testKey"),
resource.TestCheckResourceAttr("consul_keys.app", "key.2637474718.cas", "0"),
),
},
},
})
}

func TestAccConsulKeys_EmptyValue(t *testing.T) {
providers, client := startTestServer(t)

Expand Down Expand Up @@ -286,3 +304,14 @@ resource "consul_keys" "dc2" {
}
}
`

const testAccConsulKeysConfig_Cas = `
resource "consul_keys" "app" {
datacenter = "dc1"
key {
path = "test/testKey"
value = "testVal"
cas = 0
}
}
`
Loading