-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_store.go
94 lines (78 loc) · 2.31 KB
/
handler_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
package etcd
import (
"context"
"errors"
"fmt"
"github.com/coreos/etcd/clientv3"
"github.com/gogo/protobuf/proto"
"github.com/sensu/sensu-go/backend/store"
"github.com/sensu/sensu-go/types"
)
var (
handlersPathPrefix = "handlers"
handlerKeyBuilder = store.NewKeyBuilder(handlersPathPrefix)
)
func getHandlerPath(handler *types.Handler) string {
return handlerKeyBuilder.WithResource(handler).Build(handler.Name)
}
// GetHandlersPath gets the path of the handler store.
func GetHandlersPath(ctx context.Context, name string) string {
return handlerKeyBuilder.WithContext(ctx).Build(name)
}
// DeleteHandlerByName deletes a Handler by name.
func (s *Store) DeleteHandlerByName(ctx context.Context, name string) error {
if name == "" {
return errors.New("must specify name of handler")
}
_, err := s.client.Delete(ctx, GetHandlersPath(ctx, name))
return err
}
// GetHandlers gets the list of handlers for a namespace.
func (s *Store) GetHandlers(ctx context.Context, pred *store.SelectionPredicate) ([]*types.Handler, error) {
handlers := []*types.Handler{}
err := List(ctx, s.client, GetHandlersPath, &handlers, pred)
return handlers, err
}
// GetHandlerByName gets a Handler by name.
func (s *Store) GetHandlerByName(ctx context.Context, name string) (*types.Handler, error) {
if name == "" {
return nil, errors.New("must specify name of handler")
}
resp, err := s.client.Get(ctx, GetHandlersPath(ctx, name))
if err != nil {
return nil, err
}
if len(resp.Kvs) == 0 {
return nil, nil
}
handlerBytes := resp.Kvs[0].Value
handler := &types.Handler{}
if err := unmarshal(handlerBytes, handler); err != nil {
return nil, err
}
return handler, nil
}
// UpdateHandler updates a Handler.
func (s *Store) UpdateHandler(ctx context.Context, handler *types.Handler) error {
if err := handler.Validate(); err != nil {
return err
}
handlerBytes, err := proto.Marshal(handler)
if err != nil {
return err
}
cmp := clientv3.Compare(clientv3.Version(getNamespacePath(handler.Namespace)), ">", 0)
req := clientv3.OpPut(getHandlerPath(handler), string(handlerBytes))
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 handler %s in namespace %s",
handler.Name,
handler.Namespace,
)
}
return nil
}