influxdb/influx/databases.go

178 lines
4.3 KiB
Go
Raw Normal View History

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
)
// AllDB returns all databases from within Influx
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
}
// CreateDB creates a database within Influx
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
}
// DropDB drops a database within Influx
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
}
// AllRP returns all the retention policies for a specific database
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
}
func (c *Client) getRP(ctx context.Context, db, name string) (chronograf.RetentionPolicy, error) {
rps, err := c.AllRP(ctx, db)
if err != nil {
return chronograf.RetentionPolicy{}, err
}
for _, rp := range rps {
if rp.Name == name {
return rp, nil
}
}
return chronograf.RetentionPolicy{}, fmt.Errorf("unknown retention policy")
}
// CreateRP creates a retention policy for a specific database
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{
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, err := c.getRP(ctx, database, rp.Name)
if err != nil {
return nil, err
2017-03-23 11:56:36 +00:00
}
return &res, nil
2017-03-23 11:27:53 +00:00
}
// UpdateRP updates a specific retention policy for a specific database
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(fmt.Sprintf(`ALTER RETENTION POLICY "%s" ON "%s"`, name, database))
2017-03-23 13:24:26 +00:00
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
}
res, err := c.getRP(ctx, database, rp.Name)
if err != nil {
return nil, err
2017-03-23 13:24:26 +00:00
}
return &res, nil
2017-03-23 13:13:41 +00:00
}
// DropRP removes a specific retention policy for a specific database
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{
2017-03-23 20:59:28 +00:00
Command: fmt.Sprintf(`DROP RETENTION POLICY "%s" ON "%s"`, rp, database),
2017-03-23 11:56:36 +00:00
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 ON "%s"`, name),
2017-03-23 11:56:36 +00:00
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
}