Update databases/retention policy link responses

pull/1029/head
Chris Goller 2017-03-23 13:16:02 -07:00
parent b0c57f9509
commit 8827093bc6
2 changed files with 27 additions and 9 deletions

View File

@ -11,7 +11,7 @@ import (
type dbLinks struct { type dbLinks struct {
Self string `json:"self"` // Self link mapping to this resource Self string `json:"self"` // Self link mapping to this resource
RPs string `json:"rps"` // URL for retention policies for this database RPs string `json:"retentionPolicies"` // URL for retention policies for this database
} }
type dbResponse struct { type dbResponse struct {
@ -22,6 +22,16 @@ type dbResponse struct {
Links dbLinks `json:"links"` // Links are URI locations related to the database Links dbLinks `json:"links"` // Links are URI locations related to the database
} }
func NewDBResponse(srcID int, name string) dbResponse {
base := "/chronograf/v1/sources"
return dbResponse{
Name: name,
Links: dbLinks{
Self: fmt.Sprintf("%s/%d/dbs/%s", base, srcID, name),
},
}
}
type dbsResponse struct { type dbsResponse struct {
Databases []dbResponse `json:"databases"` Databases []dbResponse `json:"databases"`
} }
@ -39,6 +49,13 @@ type rpResponse struct {
Links rpLinks `json:"links"` // Links are URI locations related to the database Links rpLinks `json:"links"` // Links are URI locations related to the database
} }
func (r *rpResponse) WithLinks(srcID int, dbName string) {
base := "/chronograf/v1/sources"
r.Links = rpLinks{
Self: fmt.Sprintf("%s/%d/dbs/%s/rps/%s", base, srcID, dbName, r.Name),
}
}
type rpsResponse struct { type rpsResponse struct {
RetentionPolicies []rpResponse `json:"retentionPolicies"` RetentionPolicies []rpResponse `json:"retentionPolicies"`
} }
@ -60,7 +77,6 @@ func (h *Service) GetDatabases(w http.ResponseWriter, r *http.Request) {
} }
db := h.Databases db := h.Databases
if err = db.Connect(ctx, &src); err != nil { if err = db.Connect(ctx, &src); err != nil {
msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err) msg := fmt.Sprintf("Unable to connect to source %d: %v", srcID, err)
Error(w, http.StatusBadRequest, msg, h.Logger) Error(w, http.StatusBadRequest, msg, h.Logger)
@ -75,9 +91,7 @@ func (h *Service) GetDatabases(w http.ResponseWriter, r *http.Request) {
dbs := make([]dbResponse, len(databases)) dbs := make([]dbResponse, len(databases))
for i, d := range databases { for i, d := range databases {
dbs[i] = dbResponse{ dbs[i] = NewDBResponse(srcID, d.Name)
Name: d.Name,
}
} }
res := dbsResponse{ res := dbsResponse{
@ -127,7 +141,7 @@ func (h *Service) NewDatabase(w http.ResponseWriter, r *http.Request) {
return return
} }
res := dbResponse{Name: database.Name} res := NewDBResponse(srcID, database.Name)
encodeJSON(w, http.StatusCreated, res, h.Logger) encodeJSON(w, http.StatusCreated, res, h.Logger)
} }
@ -198,13 +212,15 @@ func (h *Service) RetentionPolicies(w http.ResponseWriter, r *http.Request) {
rps := make([]rpResponse, len(allRP)) rps := make([]rpResponse, len(allRP))
for i, rp := range allRP { for i, rp := range allRP {
rps[i] = rpResponse{ rp := rpResponse{
Name: rp.Name, Name: rp.Name,
Duration: rp.Duration, Duration: rp.Duration,
Replication: rp.Replication, Replication: rp.Replication,
ShardDuration: rp.ShardDuration, ShardDuration: rp.ShardDuration,
Default: rp.Default, Default: rp.Default,
} }
rp.WithLinks(srcID, dbID)
rps[i] = rp
} }
res := rpsResponse{ res := rpsResponse{

View File

@ -18,6 +18,7 @@ type sourceLinks struct {
Permissions string `json:"permissions"` // URL for all allowed permissions for this source Permissions string `json:"permissions"` // URL for all allowed permissions for this source
Users string `json:"users"` // URL for all users associated with this source Users string `json:"users"` // URL for all users associated with this source
Roles string `json:"roles,omitempty"` // URL for all users associated with this source Roles string `json:"roles,omitempty"` // URL for all users associated with this source
Databases string `json:"databases"` // URL for the databases contained within this soure
} }
type sourceResponse struct { type sourceResponse struct {
@ -43,6 +44,7 @@ func newSourceResponse(src chronograf.Source) sourceResponse {
Kapacitors: fmt.Sprintf("%s/%d/kapacitors", httpAPISrcs, src.ID), Kapacitors: fmt.Sprintf("%s/%d/kapacitors", httpAPISrcs, src.ID),
Permissions: fmt.Sprintf("%s/%d/permissions", httpAPISrcs, src.ID), Permissions: fmt.Sprintf("%s/%d/permissions", httpAPISrcs, src.ID),
Users: fmt.Sprintf("%s/%d/users", httpAPISrcs, src.ID), Users: fmt.Sprintf("%s/%d/users", httpAPISrcs, src.ID),
Databases: fmt.Sprintf("%s/%d/dbs", httpAPISrcs, src.ID),
}, },
} }