forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
51 lines (42 loc) · 1.24 KB
/
registry.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
package registry
import (
"encoding/json"
"fmt"
etcdErr "github.com/coreos/fleet/third_party/github.com/coreos/etcd/error"
"github.com/coreos/fleet/third_party/github.com/coreos/go-etcd/etcd"
)
const DefaultKeyPrefix = "/_coreos.com/fleet/"
// EtcdRegistry fulfils the Registry interface and uses etcd as a backend
type EtcdRegistry struct {
etcd *etcd.Client
keyPrefix string
}
// New creates a new EtcdRegistry with the given parameters
func New(client *etcd.Client, keyPrefix string) (registry Registry) {
return &EtcdRegistry{client, keyPrefix}
}
func (r *EtcdRegistry) GetDebugInfo() (string, error) {
resp, err := r.etcd.RawGet(r.keyPrefix, true, true)
if err != nil {
return "", err
}
return string(resp.Body), nil
}
func marshal(obj interface{}) (string, error) {
encoded, err := json.Marshal(obj)
if err == nil {
return string(encoded), nil
}
return "", fmt.Errorf("unable to JSON-serialize object: %s", err)
}
func unmarshal(val string, obj interface{}) error {
err := json.Unmarshal([]byte(val), &obj)
if err == nil {
return nil
}
return fmt.Errorf("unable to JSON-deserialize object: %s", err)
}
func isKeyNotFound(err error) bool {
e, ok := err.(*etcd.EtcdError)
return ok && e.ErrorCode == etcdErr.EcodeKeyNotFound
}