-
Notifications
You must be signed in to change notification settings - Fork 0
/
havaultlight.go
162 lines (126 loc) · 3.87 KB
/
havaultlight.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package havaultlight
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
const supportedEngineVersion = 2
var (
ErrKeyNotFound = errors.New("key not found")
ErrEngineUnsupported = errors.New("engine unsupported")
)
type Client interface {
Get(ctx context.Context, key string) (map[string]any, error)
Set(ctx context.Context, key string, value map[string]any) error
Delete(ctx context.Context, key string) error
Ping(ctx context.Context) error
}
type Config struct {
Addr string
Engine string
Token string
}
type h struct {
c *Config
}
func New(ctx context.Context, c *Config) (Client, error) {
s := h{c: c}
res, body, err := s.doReq(ctx, http.MethodGet, fmt.Sprintf("/sys/mounts/%s", c.Engine), nil)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response status '%d' received", res.StatusCode)
}
d, ok := body["data"].(map[string]any)
if !ok {
return nil, ErrEngineUnsupported
}
if d["type"] != "kv" {
return nil, ErrEngineUnsupported
}
d, ok = d["options"].(map[string]any)
if !ok {
return nil, ErrEngineUnsupported
}
if d["version"] != fmt.Sprint(supportedEngineVersion) {
return nil, ErrEngineUnsupported
}
return &s, nil
}
func (h *h) doReq(ctx context.Context, method, path string, body map[string]any) (*http.Response, map[string]any, error) {
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(body); err != nil {
return nil, nil, fmt.Errorf("failed to encode request body: %w", err)
}
req, err := http.NewRequestWithContext(ctx, method, fmt.Sprintf("%s/v1%s", h.c.Addr, path), &b)
if err != nil {
return nil, nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", h.c.Token))
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("failed to request: %w", err)
}
defer res.Body.Close()
var m map[string]any
if err = json.NewDecoder(res.Body).Decode(&m); err != nil {
return nil, nil, fmt.Errorf("failed to decode response body: %w", err)
}
return res, m, nil
}
func (h *h) Get(ctx context.Context, key string) (map[string]any, error) {
res, body, err := h.doReq(ctx, http.MethodGet, fmt.Sprintf("/%s/data/%s", h.c.Engine, key), nil)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
if res.StatusCode == http.StatusNotFound {
return nil, ErrKeyNotFound
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response status '%d' received", res.StatusCode)
}
d, ok := body["data"].(map[string]any)
if !ok {
return nil, ErrKeyNotFound
}
d, ok = d["data"].(map[string]any)
if !ok {
return nil, ErrKeyNotFound
}
return d, nil
}
func (h *h) Set(ctx context.Context, key string, value map[string]any) error {
res, _, err := h.doReq(ctx, http.MethodPost, fmt.Sprintf("/%s/data/%s", h.c.Engine, key), map[string]any{"data": value})
if err != nil {
return fmt.Errorf("failed to request: %w", err)
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected response status '%d' received", res.StatusCode)
}
return nil
}
func (h *h) Delete(ctx context.Context, key string) error {
res, _, err := h.doReq(ctx, http.MethodDelete, fmt.Sprintf("/%s/data/%s", h.c.Engine, key), nil)
if err != nil {
return fmt.Errorf("failed to request: %w", err)
}
if res.StatusCode != http.StatusNoContent {
return fmt.Errorf("unexpected response status '%d' received", res.StatusCode)
}
return nil
}
func (h *h) Ping(ctx context.Context) error {
res, _, err := h.doReq(ctx, http.MethodGet, "/sys/health", nil)
if err != nil {
return fmt.Errorf("failed to request: %w", err)
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected response status '%d' received", res.StatusCode)
}
return nil
}