github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/backend/remote-state/consul/client.go (about) 1 package consul 2 3 import ( 4 "crypto/md5" 5 6 consulapi "github.com/hashicorp/consul/api" 7 "github.com/hashicorp/terraform/state/remote" 8 ) 9 10 // RemoteClient is a remote client that stores data in Consul. 11 type RemoteClient struct { 12 Client *consulapi.Client 13 Path string 14 } 15 16 func (c *RemoteClient) Get() (*remote.Payload, error) { 17 pair, _, err := c.Client.KV().Get(c.Path, nil) 18 if err != nil { 19 return nil, err 20 } 21 if pair == nil { 22 return nil, nil 23 } 24 25 md5 := md5.Sum(pair.Value) 26 return &remote.Payload{ 27 Data: pair.Value, 28 MD5: md5[:], 29 }, nil 30 } 31 32 func (c *RemoteClient) Put(data []byte) error { 33 kv := c.Client.KV() 34 _, err := kv.Put(&consulapi.KVPair{ 35 Key: c.Path, 36 Value: data, 37 }, nil) 38 return err 39 } 40 41 func (c *RemoteClient) Delete() error { 42 kv := c.Client.KV() 43 _, err := kv.Delete(c.Path, nil) 44 return err 45 }