-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5210 from apparentlymart/consul-keys-revamp
Make consul_keys behavior less surprising
- Loading branch information
Showing
4 changed files
with
205 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package consul | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
consulapi "github.com/hashicorp/consul/api" | ||
) | ||
|
||
// keyClient is a wrapper around the upstream Consul client that is | ||
// specialized for Terraform's manipulations of the key/value store. | ||
type keyClient struct { | ||
client *consulapi.KV | ||
qOpts *consulapi.QueryOptions | ||
wOpts *consulapi.WriteOptions | ||
} | ||
|
||
func newKeyClient(realClient *consulapi.KV, dc, token string) *keyClient { | ||
qOpts := &consulapi.QueryOptions{Datacenter: dc, Token: token} | ||
wOpts := &consulapi.WriteOptions{Datacenter: dc, Token: token} | ||
|
||
return &keyClient{ | ||
client: realClient, | ||
qOpts: qOpts, | ||
wOpts: wOpts, | ||
} | ||
} | ||
|
||
func (c *keyClient) Get(path string) (string, error) { | ||
log.Printf( | ||
"[DEBUG] Reading key '%s' in %s", | ||
path, c.qOpts.Datacenter, | ||
) | ||
pair, _, err := c.client.Get(path, c.qOpts) | ||
if err != nil { | ||
return "", fmt.Errorf("Failed to read Consul key '%s': %s", path, err) | ||
} | ||
value := "" | ||
if pair != nil { | ||
value = string(pair.Value) | ||
} | ||
return value, nil | ||
} | ||
|
||
func (c *keyClient) Put(path, value string) error { | ||
log.Printf( | ||
"[DEBUG] Setting key '%s' to '%v' in %s", | ||
path, value, c.wOpts.Datacenter, | ||
) | ||
pair := consulapi.KVPair{Key: path, Value: []byte(value)} | ||
if _, err := c.client.Put(&pair, c.wOpts); err != nil { | ||
return fmt.Errorf("Failed to write Consul key '%s': %s", path, err) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *keyClient) Delete(path string) error { | ||
log.Printf( | ||
"[DEBUG] Deleting key '%s' in %s", | ||
path, c.wOpts.Datacenter, | ||
) | ||
if _, err := c.client.Delete(path, c.wOpts); err != nil { | ||
return fmt.Errorf("Failed to delete Consul key '%s': %s", path, err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters