drop db API

pull/10616/head
Jade McGough 2017-03-23 01:04:35 -07:00
parent 0b521f3a27
commit 6c418bb323
4 changed files with 57 additions and 7 deletions

View File

@ -306,6 +306,7 @@ type Databases interface {
AllDB(context.Context) ([]Database, error)
Connect(context.Context, *Source) error
CreateDB(context.Context, *Database) (*Database, error)
DropDB(context.Context, string) error
}
// DashboardID is the dashboard ID

View File

@ -30,6 +30,16 @@ func (c *Client) CreateDB(ctx context.Context, db *chronograf.Database) (*chrono
return res, nil
}
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
}
func (c *Client) showDatabases(ctx context.Context) ([]chronograf.Database, error) {
res, err := c.Query(ctx, chronograf.Query{
Command: `SHOW DATABASES`,

View File

@ -5,6 +5,7 @@ import (
"net/http"
"encoding/json"
"github.com/bouk/httprouter"
"github.com/influxdata/chronograf"
)
@ -113,6 +114,44 @@ func (h *Service) NewDatabase(w http.ResponseWriter, r *http.Request) {
encodeJSON(w, http.StatusCreated, res, h.Logger)
}
func (h *Service) DropDatabase(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
}
dbID := httprouter.GetParamFromContext(ctx, "dbid")
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
return
}
dropErr := db.DropDB(ctx, dbID)
if dropErr != nil {
Error(w, http.StatusBadRequest, dropErr.Error(), h.Logger)
return
}
w.WriteHeader(http.StatusNoContent)
}
func ValidDatabaseRequest(d *chronograf.Database) error {
if len(d.Name) == 0 {
return fmt.Errorf("name is required")

View File

@ -134,15 +134,15 @@ func NewMux(opts MuxOpts, service Service) http.Handler {
// Databases
router.GET("/chronograf/v1/sources/:id/dbs", service.GetDatabases)
router.POST("/chronograf/v1/sources/:id/dbs", service.NewDatabase)
//
// router.DELETE("/chronograf/v1/sources/:id/dbs/:did", service.DropDatabase)
//
router.DELETE("/chronograf/v1/sources/:id/dbs/:dbid", service.DropDatabase)
// // Retention Policies
// router.GET("/chronograf/v1/sources/:id/dbs/:did/rps", service.RetentionPolicies)
// router.POST("/chronograf/v1/sources/:id/dbs/:did/rps", service.NewRetentionPolicy)
// router.GET("/chronograf/v1/sources/:id/dbs/:dbid/rps", service.RetentionPolicies)
// router.POST("/chronograf/v1/sources/:id/dbs/:dbid/rps", service.NewRetentionPolicy)
//
// router.PATCH("/chronograf/v1/sources/:id/dbs/:did/rps/:rpid", service.UpdateRetentionPolicy)
// router.DELETE("/chronograf/v1/sources/:id/dbs/:did/rps/:rpid", service.DropRetentionPolicy)
// router.PATCH("/chronograf/v1/sources/:id/dbs/:dbid/rps/:rpid", service.UpdateRetentionPolicy)
// router.DELETE("/chronograf/v1/sources/:id/dbs/:dbid/rps/:rpid", service.DropRetentionPolicy)
var authRoutes AuthRoutes