2017-03-20 22:26:48 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-03-22 20:27:36 +00:00
|
|
|
"fmt"
|
2017-03-21 19:59:27 +00:00
|
|
|
"net/http"
|
2017-03-23 06:21:21 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
2017-03-20 22:26:48 +00:00
|
|
|
)
|
|
|
|
|
2017-03-21 19:59:27 +00:00
|
|
|
type dbLinks struct {
|
|
|
|
Self string `json:"self"` // Self link mapping to this resource
|
|
|
|
RPs string `json:"rps"` // URL for retention policies for this database
|
|
|
|
}
|
|
|
|
|
2017-03-22 08:40:30 +00:00
|
|
|
type dbResponse struct {
|
2017-03-21 19:59:27 +00:00
|
|
|
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)
|
2017-03-22 20:27:36 +00:00
|
|
|
ShardDuration string `json:"shardDuration,omitempty"` // the shard duration (when creating a default retention policy)
|
|
|
|
Links dbLinks `json:"links"` // Links are URI locations related to the database
|
2017-03-21 19:59:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 08:40:30 +00:00
|
|
|
type dbsResponse struct {
|
|
|
|
Databases []dbResponse `json:"databases"`
|
2017-03-20 22:26:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 08:40:30 +00:00
|
|
|
// Databases queries the list of all databases for a source
|
2017-03-22 20:27:36 +00:00
|
|
|
func (h *Service) GetDatabases(w http.ResponseWriter, r *http.Request) {
|
2017-03-22 08:40:30 +00:00
|
|
|
ctx := r.Context()
|
2017-03-21 19:59:27 +00:00
|
|
|
|
2017-03-22 20:27:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
databases, err := db.AllDB(ctx)
|
2017-03-22 08:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), h.Logger)
|
|
|
|
return
|
|
|
|
}
|
2017-03-21 19:59:27 +00:00
|
|
|
|
2017-03-22 08:40:30 +00:00
|
|
|
dbs := make([]dbResponse, len(databases))
|
2017-03-22 20:46:30 +00:00
|
|
|
for i, d := range databases {
|
|
|
|
dbs[i] = dbResponse{
|
|
|
|
Name: d.Name,
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 19:59:27 +00:00
|
|
|
|
2017-03-22 08:40:30 +00:00
|
|
|
res := dbsResponse{
|
|
|
|
Databases: dbs,
|
2017-03-21 19:59:27 +00:00
|
|
|
}
|
2017-03-20 22:26:48 +00:00
|
|
|
|
2017-03-21 19:59:27 +00:00
|
|
|
encodeJSON(w, http.StatusOK, res, h.Logger)
|
2017-03-20 22:26:48 +00:00
|
|
|
}
|
2017-03-23 05:21:25 +00:00
|
|
|
|
|
|
|
func (h *Service) NewDatabase(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
|
|
|
|
}
|
|
|
|
|
2017-03-23 06:21:21 +00:00
|
|
|
postedDB := &chronograf.Database{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(postedDB); err != nil {
|
|
|
|
invalidJSON(w, h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ValidDatabaseRequest(postedDB); err != nil {
|
|
|
|
invalidData(w, err, h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
database, err := db.CreateDB(ctx, postedDB)
|
2017-03-23 05:21:25 +00:00
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := dbResponse{Name: database.Name}
|
2017-03-23 06:21:21 +00:00
|
|
|
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
|
2017-03-23 05:21:25 +00:00
|
|
|
}
|