pull/1022/head
Jade McGough 2017-03-23 04:27:53 -07:00
parent 073c03303d
commit b378dd7c27
3 changed files with 87 additions and 4 deletions

View File

@ -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

View File

@ -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`,

View File

@ -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
}