From b378dd7c27ff077d8b03254941f581a8a63f5fbd Mon Sep 17 00:00:00 2001 From: Jade McGough Date: Thu, 23 Mar 2017 04:27:53 -0700 Subject: [PATCH] createRP --- chronograf.go | 1 + influx/databases.go | 27 ++++++++++++++++--- server/databases.go | 63 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/chronograf.go b/chronograf.go index 1e32234bd..94c159a56 100644 --- a/chronograf.go +++ b/chronograf.go @@ -316,6 +316,7 @@ type Databases interface { CreateDB(context.Context, *Database) (*Database, error) DropDB(context.Context, string) error AllRP(context.Context, string) ([]RetentionPolicy, error) + CreateRP(context.Context, string, *RetentionPolicy) (*RetentionPolicy, error) } // DashboardID is the dashboard ID diff --git a/influx/databases.go b/influx/databases.go index b014f6536..b77d3ed45 100644 --- a/influx/databases.go +++ b/influx/databases.go @@ -30,9 +30,10 @@ func (c *Client) CreateDB(ctx context.Context, db *chronograf.Database) (*chrono return res, nil } -func (c *Client) DropDB(ctx context.Context, name string) error { +func (c *Client) DropDB(ctx context.Context, database string) error { _, err := c.Query(ctx, chronograf.Query{ - Command: fmt.Sprintf(`DROP DATABASE "%s"`, name), + Command: fmt.Sprintf(`DROP DATABASE`), + DB: database, }) if err != nil { return err @@ -40,8 +41,8 @@ func (c *Client) DropDB(ctx context.Context, name string) error { return nil } -func (c *Client) AllRP(ctx context.Context, name string) ([]chronograf.RetentionPolicy, error) { - retentionPolicies, err := c.showRetentionPolicies(ctx, name) +func (c *Client) AllRP(ctx context.Context, database string) ([]chronograf.RetentionPolicy, error) { + retentionPolicies, err := c.showRetentionPolicies(ctx, database) if err != nil { return nil, err } @@ -49,6 +50,24 @@ func (c *Client) AllRP(ctx context.Context, name string) ([]chronograf.Retention 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 + } + + res := &chronograf.RetentionPolicy{ + Name: rp.Name, + Duration: rp.Duration, + Replication: rp.Replication, + } + + return res, nil +} + func (c *Client) showDatabases(ctx context.Context) ([]chronograf.Database, error) { res, err := c.Query(ctx, chronograf.Query{ Command: `SHOW DATABASES`, diff --git a/server/databases.go b/server/databases.go index d0a1ef9ba..6064b629b 100644 --- a/server/databases.go +++ b/server/databases.go @@ -222,9 +222,72 @@ func (h *Service) RetentionPolicies(w http.ResponseWriter, r *http.Request) { encodeJSON(w, http.StatusOK, res, h.Logger) } +func (h *Service) NewRetentionPolicy(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + srcID, err := paramID("id", r) + if err != nil { + Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger) + return + } + + src, err := h.SourcesStore.Get(ctx, srcID) + if err != nil { + notFound(w, srcID, h.Logger) + return + } + + db := h.Databases + + if err = db.Connect(ctx, &src); err != nil { + msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err) + Error(w, http.StatusBadRequest, msg, h.Logger) + return + } + + postedRP := &chronograf.RetentionPolicy{} + if err := json.NewDecoder(r.Body).Decode(postedRP); err != nil { + invalidJSON(w, h.Logger) + return + } + if err := ValidRetentionPolicyRequest(postedRP); err != nil { + invalidData(w, err, h.Logger) + return + } + + dbID := httprouter.GetParamFromContext(ctx, "dbid") + if err != nil { + Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger) + return + } + + database, err := db.CreateRP(ctx, dbID, postedRP) + if err != nil { + Error(w, http.StatusBadRequest, err.Error(), h.Logger) + return + } + + res := dbResponse{Name: database.Name} + encodeJSON(w, http.StatusCreated, res, h.Logger) +} + func ValidDatabaseRequest(d *chronograf.Database) error { if len(d.Name) == 0 { return fmt.Errorf("name is required") } return nil } + +func ValidRetentionPolicyRequest(rp *chronograf.RetentionPolicy) error { + if len(rp.Name) == 0 { + return fmt.Errorf("name is required") + } + if len(rp.Duration) == 0 { + return fmt.Errorf("duration is required") + } + if rp.Replication == 0 { + return fmt.Errorf("replication factor is invalid") + } + + return nil +}