-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathdatabases.go
More file actions
232 lines (196 loc) · 5.74 KB
/
Copy pathdatabases.go
File metadata and controls
232 lines (196 loc) · 5.74 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package influx
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/influxdata/chronograf"
)
// AllDB returns all databases from within Influx
func (c *Client) AllDB(ctx context.Context) ([]chronograf.Database, error) {
return c.showDatabases(ctx)
}
// CreateDB creates a database within Influx
func (c *Client) CreateDB(ctx context.Context, db *chronograf.Database) (*chronograf.Database, error) {
_, err := c.Query(ctx, chronograf.Query{
Command: fmt.Sprintf(`CREATE DATABASE "%s"`, db.Name),
})
if err != nil {
return nil, err
}
res := &chronograf.Database{Name: db.Name}
return res, nil
}
// DropDB drops a database within Influx
func (c *Client) DropDB(ctx context.Context, db string) error {
_, err := c.Query(ctx, chronograf.Query{
Command: fmt.Sprintf(`DROP DATABASE "%s"`, db),
DB: db,
})
if err != nil {
return err
}
return nil
}
// AllRP returns all the retention policies for a specific database
func (c *Client) AllRP(ctx context.Context, db string) ([]chronograf.RetentionPolicy, error) {
return c.showRetentionPolicies(ctx, db)
}
func (c *Client) getRP(ctx context.Context, db, rp string) (chronograf.RetentionPolicy, error) {
rs, err := c.AllRP(ctx, db)
if err != nil {
return chronograf.RetentionPolicy{}, err
}
for _, r := range rs {
if r.Name == rp {
return r, nil
}
}
return chronograf.RetentionPolicy{}, fmt.Errorf("unknown retention policy")
}
// CreateRP creates a retention policy for a specific database
func (c *Client) CreateRP(ctx context.Context, db string, rp *chronograf.RetentionPolicy) (*chronograf.RetentionPolicy, error) {
query := fmt.Sprintf(`CREATE RETENTION POLICY "%s" ON "%s" DURATION %s REPLICATION %d`, rp.Name, db, rp.Duration, rp.Replication)
if len(rp.ShardDuration) != 0 {
query = fmt.Sprintf(`%s SHARD DURATION %s`, query, rp.ShardDuration)
}
if rp.Default {
query = fmt.Sprintf(`%s DEFAULT`, query)
}
_, err := c.Query(ctx, chronograf.Query{
Command: query,
DB: db,
})
if err != nil {
return nil, err
}
res, err := c.getRP(ctx, db, rp.Name)
if err != nil {
return nil, err
}
return &res, nil
}
// UpdateRP updates a specific retention policy for a specific database
func (c *Client) UpdateRP(ctx context.Context, db string, rp string, upd *chronograf.RetentionPolicy) (*chronograf.RetentionPolicy, error) {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf(`ALTER RETENTION POLICY "%s" ON "%s"`, rp, db))
if len(upd.Duration) > 0 {
buffer.WriteString(" DURATION " + upd.Duration)
}
if upd.Replication > 0 {
buffer.WriteString(" REPLICATION " + fmt.Sprint(upd.Replication))
}
if len(upd.ShardDuration) > 0 {
buffer.WriteString(" SHARD DURATION " + upd.ShardDuration)
}
if upd.Default == true {
buffer.WriteString(" DEFAULT")
}
queryRes, err := c.Query(ctx, chronograf.Query{
Command: buffer.String(),
DB: db,
RP: rp,
})
if err != nil {
return nil, err
}
// The ALTER RETENTION POLICIES statements puts the error within the results itself
// So, we have to crack open the results to see what happens
octets, err := queryRes.MarshalJSON()
if err != nil {
return nil, err
}
results := make([]struct{ Error string }, 0)
if err := json.Unmarshal(octets, &results); err != nil {
return nil, err
}
// At last, we can check if there are any error strings
for _, r := range results {
if r.Error != "" {
return nil, fmt.Errorf("%s", r.Error)
}
}
res, err := c.getRP(ctx, db, upd.Name)
if err != nil {
return nil, err
}
return &res, nil
}
// DropRP removes a specific retention policy for a specific database
func (c *Client) DropRP(ctx context.Context, db string, rp string) error {
_, err := c.Query(ctx, chronograf.Query{
Command: fmt.Sprintf(`DROP RETENTION POLICY "%s" ON "%s"`, rp, db),
DB: db,
RP: rp,
})
if err != nil {
return err
}
return nil
}
// GetMeasurements returns measurements in a specified database, paginated by
// optional limit and offset. If no limit or offset is provided, it defaults to
// a limit of 100 measurements with no offset.
func (c *Client) GetMeasurements(ctx context.Context, db string, limit, offset int) ([]chronograf.Measurement, error) {
return c.showMeasurements(ctx, db, limit, offset)
}
func (c *Client) showDatabases(ctx context.Context) ([]chronograf.Database, error) {
res, err := c.Query(ctx, chronograf.Query{
Command: `SHOW DATABASES`,
})
if err != nil {
return nil, err
}
octets, err := res.MarshalJSON()
if err != nil {
return nil, err
}
results := showResults{}
if err := json.Unmarshal(octets, &results); err != nil {
return nil, err
}
return results.Databases(), nil
}
func (c *Client) showRetentionPolicies(ctx context.Context, db string) ([]chronograf.RetentionPolicy, error) {
retentionPolicies, err := c.Query(ctx, chronograf.Query{
Command: fmt.Sprintf(`SHOW RETENTION POLICIES ON "%s"`, db),
DB: db,
})
if err != nil {
return nil, err
}
octets, err := retentionPolicies.MarshalJSON()
if err != nil {
return nil, err
}
results := showResults{}
if err := json.Unmarshal(octets, &results); err != nil {
return nil, err
}
return results.RetentionPolicies(c.Logger), nil
}
func (c *Client) showMeasurements(ctx context.Context, db string, limit, offset int) ([]chronograf.Measurement, error) {
show := fmt.Sprintf(`SHOW MEASUREMENTS ON "%s"`, db)
if limit > 0 {
show += fmt.Sprintf(" LIMIT %d", limit)
}
if offset > 0 {
show += fmt.Sprintf(" OFFSET %d", offset)
}
measurements, err := c.Query(ctx, chronograf.Query{
Command: show,
DB: db,
})
if err != nil {
return nil, err
}
octets, err := measurements.MarshalJSON()
if err != nil {
return nil, err
}
results := showResults{}
if err := json.Unmarshal(octets, &results); err != nil {
return nil, err
}
return results.Measurements(), nil
}