forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
registry.go
46 lines (37 loc) · 1 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
package registry
import (
"encoding/json"
"fmt"
"github.com/coreos/fleet/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
}
func NewEtcdRegistry(client etcd.Client, keyPrefix string) *EtcdRegistry {
return &EtcdRegistry{client, keyPrefix}
}
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.Error)
return ok && e.ErrorCode == etcd.ErrorKeyNotFound
}
func isNodeExist(err error) bool {
e, ok := err.(etcd.Error)
return ok && e.ErrorCode == etcd.ErrorNodeExist
}