forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_store.go
110 lines (93 loc) · 2.62 KB
/
entity_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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package etcd
import (
"context"
"errors"
"fmt"
"github.com/coreos/etcd/clientv3"
"github.com/gogo/protobuf/proto"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/backend/store"
)
const (
entityPathPrefix = "entities"
)
var (
entityKeyBuilder = store.NewKeyBuilder(entityPathPrefix)
)
func getEntityPath(entity *corev2.Entity) string {
return entityKeyBuilder.WithResource(entity).Build(entity.Name)
}
// GetEntitiesPath gets the path of the entity store
func GetEntitiesPath(ctx context.Context, name string) string {
return entityKeyBuilder.WithContext(ctx).Build(name)
}
// DeleteEntity deletes an Entity.
func (s *Store) DeleteEntity(ctx context.Context, e *corev2.Entity) error {
if err := e.Validate(); err != nil {
return err
}
_, err := s.client.Delete(ctx, getEntityPath(e))
return err
}
// DeleteEntityByName deletes an Entity by its name.
func (s *Store) DeleteEntityByName(ctx context.Context, name string) error {
if name == "" {
return errors.New("must specify name")
}
_, err := s.client.Delete(ctx, GetEntitiesPath(ctx, name))
return err
}
// GetEntityByName gets an Entity by its name.
func (s *Store) GetEntityByName(ctx context.Context, name string) (*corev2.Entity, error) {
if name == "" {
return nil, errors.New("must specify name")
}
resp, err := s.client.Get(ctx, GetEntitiesPath(ctx, name), clientv3.WithLimit(1))
if err != nil {
return nil, err
}
if len(resp.Kvs) != 1 {
return nil, nil
}
entity := &corev2.Entity{}
if err := unmarshal(resp.Kvs[0].Value, entity); err != nil {
return nil, err
}
if entity.Labels == nil {
entity.Labels = make(map[string]string)
}
if entity.Annotations == nil {
entity.Annotations = make(map[string]string)
}
return entity, nil
}
// GetEntities returns the entities for the namespace in the supplied context.
func (s *Store) GetEntities(ctx context.Context, pred *store.SelectionPredicate) ([]*corev2.Entity, error) {
entities := []*corev2.Entity{}
err := List(ctx, s.client, GetEntitiesPath, &entities, pred)
return entities, err
}
// UpdateEntity updates an Entity.
func (s *Store) UpdateEntity(ctx context.Context, e *corev2.Entity) error {
if err := e.Validate(); err != nil {
return err
}
eStr, err := proto.Marshal(e)
if err != nil {
return err
}
cmp := clientv3.Compare(clientv3.Version(getNamespacePath(e.Namespace)), ">", 0)
req := clientv3.OpPut(getEntityPath(e), string(eStr))
res, err := s.client.Txn(ctx).If(cmp).Then(req).Commit()
if err != nil {
return err
}
if !res.Succeeded {
return fmt.Errorf(
"could not create the entity %s in namespace %s",
e.Name,
e.Namespace,
)
}
return nil
}