go format
parent
b40a90b9d1
commit
ba8609d7b6
|
@ -295,23 +295,23 @@ type UsersStore interface {
|
|||
}
|
||||
|
||||
type Database struct {
|
||||
Name string `json:"name"` // a unique string identifier for the database
|
||||
Duration string `json:"duration,omitempty"` // the duration (when creating a default retention policy)
|
||||
Replication int32 `json:"replication,omitempty"` // the replication factor (when creating a default retention policy)
|
||||
ShardDuration string `json:"shardDuration,omitempty"` // the shard duration (when creating a default retention policy)
|
||||
Name string `json:"name"` // a unique string identifier for the database
|
||||
Duration string `json:"duration,omitempty"` // the duration (when creating a default retention policy)
|
||||
Replication int32 `json:"replication,omitempty"` // the replication factor (when creating a default retention policy)
|
||||
ShardDuration string `json:"shardDuration,omitempty"` // the shard duration (when creating a default retention policy)
|
||||
}
|
||||
|
||||
type RetentionPolicy struct {
|
||||
Name string `json:"name"` // a unique string identifier for the retention policy
|
||||
Duration string `json:"duration,omitempty"` // the duration
|
||||
Replication int32 `json:"replication,omitempty"` // the replication factor
|
||||
ShardDuration string `json:"shardDuration,omitempty"` // the shard duration
|
||||
Default bool `json:"default,omitempty"` // whether the RP should be the default
|
||||
Name string `json:"name"` // a unique string identifier for the retention policy
|
||||
Duration string `json:"duration,omitempty"` // the duration
|
||||
Replication int32 `json:"replication,omitempty"` // the replication factor
|
||||
ShardDuration string `json:"shardDuration,omitempty"` // the shard duration
|
||||
Default bool `json:"default,omitempty"` // whether the RP should be the default
|
||||
}
|
||||
|
||||
type Databases interface {
|
||||
// All lists all databases
|
||||
AllDB(context.Context) ([]Database, error)
|
||||
// All lists all databases
|
||||
AllDB(context.Context) ([]Database, error)
|
||||
Connect(context.Context, *Source) error
|
||||
CreateDB(context.Context, *Database) (*Database, error)
|
||||
DropDB(context.Context, string) error
|
||||
|
|
|
@ -1,123 +1,123 @@
|
|||
package influx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"context"
|
||||
"fmt"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/influxdata/chronograf"
|
||||
"github.com/influxdata/chronograf"
|
||||
)
|
||||
|
||||
func (c *Client) AllDB(ctx context.Context) ([]chronograf.Database, error) {
|
||||
databases, err := c.showDatabases(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
databases, err := c.showDatabases(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return databases, nil
|
||||
return databases, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
_, 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}
|
||||
res := &chronograf.Database{Name: db.Name}
|
||||
|
||||
return res, nil
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *Client) DropDB(ctx context.Context, database string) error {
|
||||
_, err := c.Query(ctx, chronograf.Query{
|
||||
Command: fmt.Sprintf(`DROP DATABASE`),
|
||||
DB: database,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
_, err := c.Query(ctx, chronograf.Query{
|
||||
Command: fmt.Sprintf(`DROP DATABASE`),
|
||||
DB: database,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) AllRP(ctx context.Context, database string) ([]chronograf.RetentionPolicy, error) {
|
||||
retentionPolicies, err := c.showRetentionPolicies(ctx, database)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
retentionPolicies, err := c.showRetentionPolicies(ctx, database)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return retentionPolicies, nil
|
||||
return retentionPolicies, nil
|
||||
}
|
||||
|
||||
func (c *Client) CreateRP(ctx context.Context, database string, rp *chronograf.RetentionPolicy) (*chronograf.RetentionPolicy, error) {
|
||||
_, err := c.Query(ctx, chronograf.Query{
|
||||
Command: fmt.Sprintf(`CREATE RETENTION POLICY "%s" DURATION "%s" REPLICATION "%s"`, rp.Name, rp.Duration, rp.Replication),
|
||||
DB: database,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err := c.Query(ctx, chronograf.Query{
|
||||
Command: fmt.Sprintf(`CREATE RETENTION POLICY "%s" DURATION "%s" REPLICATION "%s"`, rp.Name, rp.Duration, rp.Replication),
|
||||
DB: database,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := &chronograf.RetentionPolicy{
|
||||
Name: rp.Name,
|
||||
Duration: rp.Duration,
|
||||
Replication: rp.Replication,
|
||||
}
|
||||
res := &chronograf.RetentionPolicy{
|
||||
Name: rp.Name,
|
||||
Duration: rp.Duration,
|
||||
Replication: rp.Replication,
|
||||
}
|
||||
|
||||
return res, nil
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *Client) DropRP(ctx context.Context, database string, rp string) error {
|
||||
_, err := c.Query(ctx, chronograf.Query{
|
||||
Command: fmt.Sprintf(`DROP RETENTION POLICY`),
|
||||
DB: database,
|
||||
RP: rp,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
_, err := c.Query(ctx, chronograf.Query{
|
||||
Command: fmt.Sprintf(`DROP RETENTION POLICY`),
|
||||
DB: database,
|
||||
RP: rp,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
results := showResults{}
|
||||
if err := json.Unmarshal(octets, &results); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return results.Databases(), nil
|
||||
return results.Databases(), nil
|
||||
}
|
||||
|
||||
func (c *Client) showRetentionPolicies(ctx context.Context, name string) ([]chronograf.RetentionPolicy, error) {
|
||||
retentionPolicies, err := c.Query(ctx, chronograf.Query{
|
||||
Command: fmt.Sprintf(`SHOW RETENTION POLICIES`),
|
||||
DB: name,
|
||||
})
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
results := showResults{}
|
||||
if err := json.Unmarshal(octets, &results); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return results.RetentionPolicies(), nil
|
||||
return results.RetentionPolicies(), nil
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/bouk/httprouter"
|
||||
"github.com/bouk/httprouter"
|
||||
"github.com/influxdata/chronograf"
|
||||
)
|
||||
|
||||
|
@ -23,7 +23,7 @@ type dbResponse struct {
|
|||
}
|
||||
|
||||
type dbsResponse struct {
|
||||
Databases []dbResponse `json:"databases"`
|
||||
Databases []dbResponse `json:"databases"`
|
||||
}
|
||||
|
||||
type rpLinks struct {
|
||||
|
@ -40,14 +40,14 @@ type rpResponse struct {
|
|||
}
|
||||
|
||||
type rpsResponse struct {
|
||||
RetentionPolicies []rpResponse `json:"retentionPolicies"`
|
||||
RetentionPolicies []rpResponse `json:"retentionPolicies"`
|
||||
}
|
||||
|
||||
// Databases queries the list of all databases for a source
|
||||
func (h *Service) GetDatabases(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
ctx := r.Context()
|
||||
|
||||
srcID, err := paramID("id", r)
|
||||
srcID, err := paramID("id", r)
|
||||
if err != nil {
|
||||
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
|
||||
return
|
||||
|
@ -59,7 +59,7 @@ func (h *Service) GetDatabases(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
db := h.Databases
|
||||
db := h.Databases
|
||||
|
||||
if err = db.Connect(ctx, &src); err != nil {
|
||||
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
|
||||
|
@ -67,18 +67,18 @@ func (h *Service) GetDatabases(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
databases, err := db.AllDB(ctx)
|
||||
if err != nil {
|
||||
Error(w, http.StatusBadRequest, err.Error(), h.Logger)
|
||||
return
|
||||
}
|
||||
databases, err := db.AllDB(ctx)
|
||||
if err != nil {
|
||||
Error(w, http.StatusBadRequest, err.Error(), h.Logger)
|
||||
return
|
||||
}
|
||||
|
||||
dbs := make([]dbResponse, len(databases))
|
||||
for i, d := range databases {
|
||||
dbs[i] = dbResponse{
|
||||
Name: d.Name,
|
||||
}
|
||||
}
|
||||
dbs := make([]dbResponse, len(databases))
|
||||
for i, d := range databases {
|
||||
dbs[i] = dbResponse{
|
||||
Name: d.Name,
|
||||
}
|
||||
}
|
||||
|
||||
res := dbsResponse{
|
||||
Databases: dbs,
|
||||
|
@ -199,11 +199,11 @@ func (h *Service) RetentionPolicies(w http.ResponseWriter, r *http.Request) {
|
|||
rps := make([]rpResponse, len(allRP))
|
||||
for i, rp := range allRP {
|
||||
rps[i] = rpResponse{
|
||||
Name: rp.Name,
|
||||
Duration: rp.Duration,
|
||||
Replication: rp.Replication,
|
||||
Name: rp.Name,
|
||||
Duration: rp.Duration,
|
||||
Replication: rp.Replication,
|
||||
ShardDuration: rp.ShardDuration,
|
||||
Default: rp.Default,
|
||||
Default: rp.Default,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue