diff --git a/consul/key_client.go b/consul/key_client.go index b95f3705..22254f3e 100644 --- a/consul/key_client.go +++ b/consul/key_client.go @@ -88,6 +88,19 @@ func (c *keyClient) Cas(path, value string, flags int, cas int) (bool, error) { return written, nil } +func (c *keyClient) DeleteCas(path string, cas int) (bool, error) { + log.Printf( + "[DEBUG] Deleting key '%s' in %s with cas %d", + path, c.wOpts.Datacenter, cas, + ) + pair := consulapi.KVPair{Key: path, ModifyIndex: uint64(cas)} + written, _, err := c.client.DeleteCAS(&pair, c.wOpts) + if err != nil { + return false, fmt.Errorf("failed to delete Consul key '%s': %s", path, err) + } + return written, nil +} + func (c *keyClient) Delete(path string) error { log.Printf( "[DEBUG] Deleting key '%s' in %s", diff --git a/consul/resource_consul_keys.go b/consul/resource_consul_keys.go index 9506075e..7e984141 100644 --- a/consul/resource_consul_keys.go +++ b/consul/resource_consul_keys.go @@ -158,7 +158,7 @@ func resourceConsulKeysCreateUpdate(d *schema.ResourceData, meta interface{}) er flags := sub["flags"].(int) cas := sub["cas"].(int) - if cas != -1 { + if cas >= 0 { _, err := keyClient.Cas(path, value, flags, cas) if err != nil { return err @@ -273,7 +273,14 @@ func resourceConsulKeysDelete(d *schema.ResourceData, meta interface{}) error { if !ok || !shouldDelete { continue } - + cas := sub["cas"].(int) + if cas > 0 { + _, err := keyClient.DeleteCas(path, cas) + if err != nil { + return err + } + continue + } if err := keyClient.Delete(path); err != nil { return err }