2017-03-22 11:01:55 +00:00
|
|
|
package influx
|
|
|
|
|
|
|
|
import (
|
2017-03-23 13:24:26 +00:00
|
|
|
"bytes"
|
2017-03-23 11:56:36 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-03-22 18:29:38 +00:00
|
|
|
|
2017-03-23 11:56:36 +00:00
|
|
|
"github.com/influxdata/chronograf"
|
2017-03-22 11:01:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Client) AllDB(ctx context.Context) ([]chronograf.Database, error) {
|
2017-03-23 11:56:36 +00:00
|
|
|
databases, err := c.showDatabases(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-22 11:01:55 +00:00
|
|
|
|
2017-03-23 11:56:36 +00:00
|
|
|
return databases, nil
|
2017-03-22 11:01:55 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 05:21:25 +00:00
|
|
|
func (c *Client) CreateDB(ctx context.Context, db *chronograf.Database) (*chronograf.Database, error) {
|
2017-03-23 11:56:36 +00:00
|
|
|
_, err := c.Query(ctx, chronograf.Query{
|
|
|
|
Command: fmt.Sprintf(`CREATE DATABASE "%s"`, db.Name),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-23 05:21:25 +00:00
|
|
|
|
2017-03-23 11:56:36 +00:00
|
|
|
res := &chronograf.Database{Name: db.Name}
|
2017-03-23 05:21:25 +00:00
|
|
|
|
2017-03-23 11:56:36 +00:00
|
|
|
return res, nil
|
2017-03-23 05:21:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 11:27:53 +00:00
|
|
|
func (c *Client) DropDB(ctx context.Context, database string) error {
|
2017-03-23 11:56:36 +00:00
|
|
|
_, err := c.Query(ctx, chronograf.Query{
|
2017-03-23 20:33:46 +00:00
|
|
|
Command: fmt.Sprintf(`DROP DATABASE "%s"`, database),
|
2017-03-23 11:56:36 +00:00
|
|
|
DB: database,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2017-03-23 08:04:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 11:27:53 +00:00
|
|
|
func (c *Client) AllRP(ctx context.Context, database string) ([]chronograf.RetentionPolicy, error) {
|
2017-03-23 11:56:36 +00:00
|
|
|
retentionPolicies, err := c.showRetentionPolicies(ctx, database)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-23 10:06:59 +00:00
|
|
|
|
2017-03-23 11:56:36 +00:00
|
|
|
return retentionPolicies, nil
|
2017-03-23 10:06:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 11:27:53 +00:00
|
|
|
func (c *Client) CreateRP(ctx context.Context, database string, rp *chronograf.RetentionPolicy) (*chronograf.RetentionPolicy, error) {
|
2017-03-23 11:56:36 +00:00
|
|
|
_, err := c.Query(ctx, chronograf.Query{
|
2017-03-23 20:15:39 +00:00
|
|
|
Command: fmt.Sprintf(`CREATE RETENTION POLICY "%s" ON "%s" DURATION %s REPLICATION %d`, rp.Name, database, rp.Duration, rp.Replication),
|
2017-03-23 11:56:36 +00:00
|
|
|
DB: database,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res := &chronograf.RetentionPolicy{
|
|
|
|
Name: rp.Name,
|
|
|
|
Duration: rp.Duration,
|
|
|
|
Replication: rp.Replication,
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2017-03-23 11:27:53 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 13:13:41 +00:00
|
|
|
func (c *Client) UpdateRP(ctx context.Context, database string, name string, rp *chronograf.RetentionPolicy) (*chronograf.RetentionPolicy, error) {
|
2017-03-23 13:24:26 +00:00
|
|
|
var buffer bytes.Buffer
|
|
|
|
buffer.WriteString("ALTER RETENTION POLICY")
|
|
|
|
if len(rp.Duration) > 0 {
|
|
|
|
buffer.WriteString(" DURATION " + rp.Duration)
|
|
|
|
}
|
|
|
|
if rp.Replication > 0 {
|
|
|
|
buffer.WriteString(" REPLICATION " + fmt.Sprint(rp.Replication))
|
|
|
|
}
|
|
|
|
if len(rp.ShardDuration) > 0 {
|
|
|
|
buffer.WriteString(" SHARD DURATION " + rp.ShardDuration)
|
|
|
|
}
|
|
|
|
if rp.Default == true {
|
|
|
|
buffer.WriteString(" DEFAULT")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := c.Query(ctx, chronograf.Query{
|
|
|
|
Command: buffer.String(),
|
|
|
|
DB: database,
|
|
|
|
RP: name,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use actual information here
|
|
|
|
res := &chronograf.RetentionPolicy{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2017-03-23 13:13:41 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 11:51:08 +00:00
|
|
|
func (c *Client) DropRP(ctx context.Context, database string, rp string) error {
|
2017-03-23 11:56:36 +00:00
|
|
|
_, err := c.Query(ctx, chronograf.Query{
|
|
|
|
Command: fmt.Sprintf(`DROP RETENTION POLICY`),
|
|
|
|
DB: database,
|
|
|
|
RP: rp,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2017-03-23 11:51:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 11:01:55 +00:00
|
|
|
func (c *Client) showDatabases(ctx context.Context) ([]chronograf.Database, error) {
|
2017-03-23 11:56:36 +00:00
|
|
|
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
|
2017-03-22 11:01:55 +00:00
|
|
|
}
|
2017-03-23 10:06:59 +00:00
|
|
|
|
|
|
|
func (c *Client) showRetentionPolicies(ctx context.Context, name string) ([]chronograf.RetentionPolicy, error) {
|
2017-03-23 11:56:36 +00:00
|
|
|
retentionPolicies, err := c.Query(ctx, chronograf.Query{
|
|
|
|
Command: fmt.Sprintf(`SHOW RETENTION POLICIES`),
|
|
|
|
DB: name,
|
|
|
|
})
|
|
|
|
|
|
|
|
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(), nil
|
2017-03-23 10:06:59 +00:00
|
|
|
}
|