Skip to content

Commit

Permalink
added cas for delete
Browse files Browse the repository at this point in the history
  • Loading branch information
aahel committed Sep 26, 2023
1 parent 89d5604 commit c32de38
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
13 changes: 13 additions & 0 deletions consul/key_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 9 additions & 2 deletions consul/resource_consul_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit c32de38

Please sign in to comment.