forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_client.go
96 lines (86 loc) · 2.4 KB
/
key_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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) GetUnderPrefix(pathPrefix string) (map[string]string, error) {
log.Printf(
"[DEBUG] Listing keys under '%s' in %s",
pathPrefix, c.qOpts.Datacenter,
)
pairs, _, err := c.client.List(pathPrefix, c.qOpts)
if err != nil {
return nil, fmt.Errorf(
"Failed to list Consul keys under prefix '%s': %s", pathPrefix, err,
)
}
value := map[string]string{}
for _, pair := range pairs {
subKey := pair.Key[len(pathPrefix):]
value[subKey] = 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
}
func (c *keyClient) DeleteUnderPrefix(pathPrefix string) error {
log.Printf(
"[DEBUG] Deleting all keys under prefix '%s' in %s",
pathPrefix, c.wOpts.Datacenter,
)
if _, err := c.client.DeleteTree(pathPrefix, c.wOpts); err != nil {
return fmt.Errorf("Failed to delete Consul keys under '%s': %s", pathPrefix, err)
}
return nil
}