forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_id_store.go
48 lines (40 loc) · 999 Bytes
/
cluster_id_store.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
package etcd
import (
"context"
"fmt"
"github.com/coreos/etcd/clientv3"
"github.com/sensu/sensu-go/backend/store"
)
const (
clusterIDPrefix = "cluster_id"
)
var (
clusterIDKeyBuilder = store.NewKeyBuilder(clusterIDPrefix)
)
// CreateClusterID creates a sensu cluster id
func (s *Store) CreateClusterID(ctx context.Context, id string) error {
key := clusterIDKeyBuilder.Build("")
req := clientv3.OpPut(key, id)
resp, err := s.client.Txn(ctx).Then(req).Commit()
if err != nil {
return err
}
if !resp.Succeeded {
return &store.ErrInternal{
Message: fmt.Sprintf("could not update the key %s", key),
}
}
return nil
}
// GetClusterID gets the sensu cluster id
func (s *Store) GetClusterID(ctx context.Context) (string, error) {
key := clusterIDKeyBuilder.Build("")
resp, err := s.client.Get(ctx, key, clientv3.WithLimit(1))
if err != nil {
return "", err
}
if len(resp.Kvs) == 0 {
return "", &store.ErrNotFound{Key: key}
}
return string(resp.Kvs[0].Value), err
}