-
Notifications
You must be signed in to change notification settings - Fork 6
/
ekey.go
71 lines (59 loc) · 1.69 KB
/
ekey.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
package kv
import (
"net/url"
"time"
"github.com/cerana/cerana/acomm"
"github.com/cerana/cerana/pkg/errors"
)
var eKeys = newEKeyMap()
// EphemeralSetArgs specifies the arguments to the "kv-ephemeral-set" endpoint.
type EphemeralSetArgs struct {
Key string `json:"key"`
Value string `json:"value"`
TTL time.Duration `json:"ttl"`
}
// EphemeralDestroyArgs specifies the arguments to the "kv-ephemeral-destroy" endpoint.
type EphemeralDestroyArgs struct {
Key string `json:"key"`
}
func (k *KV) eset(req *acomm.Request) (interface{}, *url.URL, error) {
args := EphemeralSetArgs{}
err := req.UnmarshalArgs(&args)
if err != nil {
return nil, nil, err
}
if args.Key == "" {
return nil, nil, errors.Newv("missing arg: key", map[string]interface{}{"args": args})
}
if args.Value == "" {
return nil, nil, errors.Newv("missing arg: value", map[string]interface{}{"args": args})
}
if args.TTL == 0 {
return nil, nil, errors.Newv("missing arg: ttl", map[string]interface{}{"args": args})
}
if k.kvDown() {
return nil, nil, errors.Wrap(errorKVDown)
}
eKey := eKeys.Get(args.Key)
if eKey == nil || eKey.Renew() != nil {
eKey, err = k.kv.EphemeralKey(args.Key, args.TTL)
if err != nil {
return nil, nil, err
}
}
if err = eKey.Set(args.Value); err != nil {
return nil, nil, err
}
eKeys.Add(args.Key, eKey)
return nil, nil, nil
}
func (k *KV) edestroy(req *acomm.Request) (interface{}, *url.URL, error) {
args := EphemeralDestroyArgs{}
if err := req.UnmarshalArgs(&args); err != nil {
return nil, nil, err
}
if args.Key == "" {
return nil, nil, errors.Newv("missing arg: key", map[string]interface{}{"args": args})
}
return nil, nil, eKeys.Destroy(args.Key)
}