influxdb/influx/databases.go

62 lines
1.2 KiB
Go
Raw Normal View History

2017-03-22 11:01:55 +00:00
package influx
import (
"encoding/json"
2017-03-22 18:29:38 +00:00
"context"
2017-03-23 05:21:25 +00:00
"fmt"
2017-03-22 18:29:38 +00:00
"github.com/influxdata/chronograf"
2017-03-22 11:01:55 +00:00
)
func (c *Client) AllDB(ctx context.Context) ([]chronograf.Database, error) {
databases, err := c.showDatabases(ctx)
if err != nil {
return nil, err
}
return databases, nil
}
2017-03-23 05:21:25 +00:00
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
}
2017-03-23 08:04:35 +00:00
func (c *Client) DropDB(ctx context.Context, name string) error {
_, err := c.Query(ctx, chronograf.Query{
Command: fmt.Sprintf(`DROP DATABASE "%s"`, name),
})
if err != nil {
return err
}
return nil
}
2017-03-22 11:01:55 +00:00
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
}