Skip to content

Commit

Permalink
Label responses (#2165)
Browse files Browse the repository at this point in the history
* add labels to task links

* add labels to task responses

* add failing test

* fix label POST

* make fmt

* fix dashboard tests

* update swagger

* fix task service test labels

* add labels test for dashboards http service

* pull params out of newTaskResponse

* pull params out of newDashboardResponse

* make fmt

* add labels to dashboard response in swagger

* make context first argument

* fix test formatting
  • Loading branch information
imogenkinsman authored Jan 2, 2019
1 parent be1ba8a commit 0ddf6b3
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 67 deletions.
37 changes: 28 additions & 9 deletions http/dashboard_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ type dashboardLinks struct {

type dashboardResponse struct {
platform.Dashboard
Cells []dashboardCellResponse `json:"cells"`
Links dashboardLinks `json:"links"`
Cells []dashboardCellResponse `json:"cells"`
Labels []platform.Label `json:"labels"`
Links dashboardLinks `json:"links"`
}

func (d dashboardResponse) toPlatform() *platform.Dashboard {
Expand All @@ -110,7 +111,7 @@ func (d dashboardResponse) toPlatform() *platform.Dashboard {
}
}

func newDashboardResponse(d *platform.Dashboard) dashboardResponse {
func newDashboardResponse(d *platform.Dashboard, labels []*platform.Label) dashboardResponse {
res := dashboardResponse{
Links: dashboardLinks{
Self: fmt.Sprintf("/api/v2/dashboards/%s", d.ID),
Expand All @@ -119,9 +120,14 @@ func newDashboardResponse(d *platform.Dashboard) dashboardResponse {
Labels: fmt.Sprintf("/api/v2/dashboards/%s/labels", d.ID),
},
Dashboard: *d,
Labels: []platform.Label{},
Cells: []dashboardCellResponse{},
}

for _, l := range labels {
res.Labels = append(res.Labels, *l)
}

for _, cell := range d.Cells {
res.Cells = append(res.Cells, newDashboardCellResponse(d.ID, cell))
}
Expand Down Expand Up @@ -235,7 +241,7 @@ func (h *DashboardHandler) handleGetDashboards(w http.ResponseWriter, r *http.Re
return
}

if err := encodeResponse(ctx, w, http.StatusOK, newGetDashboardsResponse(dashboards)); err != nil {
if err := encodeResponse(ctx, w, http.StatusOK, newGetDashboardsResponse(ctx, dashboards, h.LabelService)); err != nil {
logEncodingError(h.Logger, r, err)
return
}
Expand Down Expand Up @@ -293,7 +299,7 @@ func (d getDashboardsResponse) toPlatform() []*platform.Dashboard {
return res
}

func newGetDashboardsResponse(dashboards []*platform.Dashboard) getDashboardsResponse {
func newGetDashboardsResponse(ctx context.Context, dashboards []*platform.Dashboard, labelService platform.LabelService) getDashboardsResponse {
res := getDashboardsResponse{
Links: getDashboardsLinks{
Self: "/api/v2/dashboards",
Expand All @@ -303,7 +309,8 @@ func newGetDashboardsResponse(dashboards []*platform.Dashboard) getDashboardsRes

for _, dashboard := range dashboards {
if dashboard != nil {
res.Dashboards = append(res.Dashboards, newDashboardResponse(dashboard))
labels, _ := labelService.FindLabels(ctx, platform.LabelFilter{ResourceID: dashboard.ID})
res.Dashboards = append(res.Dashboards, newDashboardResponse(dashboard, labels))
}
}

Expand All @@ -324,7 +331,7 @@ func (h *DashboardHandler) handlePostDashboard(w http.ResponseWriter, r *http.Re
return
}

if err := encodeResponse(ctx, w, http.StatusCreated, newDashboardResponse(req.Dashboard)); err != nil {
if err := encodeResponse(ctx, w, http.StatusCreated, newDashboardResponse(req.Dashboard, []*platform.Label{})); err != nil {
logEncodingError(h.Logger, r, err)
return
}
Expand Down Expand Up @@ -360,7 +367,13 @@ func (h *DashboardHandler) handleGetDashboard(w http.ResponseWriter, r *http.Req
return
}

if err := encodeResponse(ctx, w, http.StatusOK, newDashboardResponse(dashboard)); err != nil {
labels, err := h.LabelService.FindLabels(ctx, platform.LabelFilter{ResourceID: dashboard.ID})
if err != nil {
EncodeError(ctx, err, w)
return
}

if err := encodeResponse(ctx, w, http.StatusOK, newDashboardResponse(dashboard, labels)); err != nil {
logEncodingError(h.Logger, r, err)
return
}
Expand Down Expand Up @@ -506,7 +519,13 @@ func (h *DashboardHandler) handlePatchDashboard(w http.ResponseWriter, r *http.R
return
}

if err := encodeResponse(ctx, w, http.StatusOK, newDashboardResponse(dashboard)); err != nil {
labels, err := h.LabelService.FindLabels(ctx, platform.LabelFilter{ResourceID: dashboard.ID})
if err != nil {
EncodeError(ctx, err, w)
return
}

if err := encodeResponse(ctx, w, http.StatusOK, newDashboardResponse(dashboard, labels)); err != nil {
logEncodingError(h.Logger, r, err)
return
}
Expand Down
47 changes: 44 additions & 3 deletions http/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
func TestService_handleGetDashboards(t *testing.T) {
type fields struct {
DashboardService platform.DashboardService
LabelService platform.LabelService
}
type args struct {
queryParams map[string][]string
Expand Down Expand Up @@ -73,6 +74,20 @@ func TestService_handleGetDashboards(t *testing.T) {
}, 2, nil
},
},
&mock.LabelService{
FindLabelsFn: func(ctx context.Context, f platform.LabelFilter) ([]*platform.Label, error) {
labels := []*platform.Label{
{
ResourceID: f.ResourceID,
Name: "label",
Properties: map[string]string{
"color": "fff000",
},
},
}
return labels, nil
},
},
},
args: args{},
wants: wants{
Expand All @@ -88,6 +103,15 @@ func TestService_handleGetDashboards(t *testing.T) {
"id": "da7aba5e5d81e550",
"name": "hello",
"description": "oh hello there!",
"labels": [
{
"resourceID": "da7aba5e5d81e550",
"name": "label",
"properties": {
"color": "fff000"
}
}
],
"meta": {
"createdAt": "2009-11-10T23:00:00Z",
"updatedAt": "2009-11-11T00:00:00Z"
Expand All @@ -110,13 +134,22 @@ func TestService_handleGetDashboards(t *testing.T) {
"self": "/api/v2/dashboards/da7aba5e5d81e550",
"cells": "/api/v2/dashboards/da7aba5e5d81e550/cells",
"log": "/api/v2/dashboards/da7aba5e5d81e550/log",
"labels": "/api/v2/dashboards/da7aba5e5d81e550/labels"
"labels": "/api/v2/dashboards/da7aba5e5d81e550/labels"
}
},
{
"id": "0ca2204eca2204e0",
"name": "example",
"description": "",
"labels": [
{
"resourceID": "0ca2204eca2204e0",
"name": "label",
"properties": {
"color": "fff000"
}
}
],
"meta": {
"createdAt": "2012-11-10T23:00:00Z",
"updatedAt": "2012-11-11T00:00:00Z"
Expand All @@ -126,7 +159,7 @@ func TestService_handleGetDashboards(t *testing.T) {
"self": "/api/v2/dashboards/0ca2204eca2204e0",
"log": "/api/v2/dashboards/0ca2204eca2204e0/log",
"cells": "/api/v2/dashboards/0ca2204eca2204e0/cells",
"labels": "/api/v2/dashboards/0ca2204eca2204e0/labels"
"labels": "/api/v2/dashboards/0ca2204eca2204e0/labels"
}
}
]
Expand All @@ -142,6 +175,11 @@ func TestService_handleGetDashboards(t *testing.T) {
return []*platform.Dashboard{}, 0, nil
},
},
&mock.LabelService{
FindLabelsFn: func(ctx context.Context, f platform.LabelFilter) ([]*platform.Label, error) {
return []*platform.Label{}, nil
},
},
},
args: args{},
wants: wants{
Expand All @@ -161,7 +199,7 @@ func TestService_handleGetDashboards(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mappingService := mock.NewUserResourceMappingService()
labelService := mock.NewLabelService()
labelService := tt.fields.LabelService
userService := mock.NewUserService()
h := NewDashboardHandler(mappingService, labelService, userService)
h.DashboardService = tt.fields.DashboardService
Expand Down Expand Up @@ -258,6 +296,7 @@ func TestService_handleGetDashboard(t *testing.T) {
"id": "020f755c3c082000",
"name": "hello",
"description": "",
"labels": [],
"meta": {
"createdAt": "2012-11-10T23:00:00Z",
"updatedAt": "2012-11-11T00:00:00Z"
Expand Down Expand Up @@ -406,6 +445,7 @@ func TestService_handlePostDashboard(t *testing.T) {
"id": "020f755c3c082000",
"name": "hello",
"description": "howdy there",
"labels": [],
"meta": {
"createdAt": "2012-11-10T23:00:00Z",
"updatedAt": "2012-11-11T00:00:00Z"
Expand Down Expand Up @@ -640,6 +680,7 @@ func TestService_handlePatchDashboard(t *testing.T) {
"id": "020f755c3c082000",
"name": "example",
"description": "",
"labels": [],
"meta": {
"createdAt": "2012-11-10T23:00:00Z",
"updatedAt": "2012-11-11T01:00:00Z"
Expand Down
37 changes: 18 additions & 19 deletions http/label_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,17 @@ func newPostLabelHandler(s plat.LabelService) http.HandlerFunc {
return
}

label := &plat.Label{
ResourceID: req.ResourceID,
Name: req.Name,
}

if err := label.Validate(); err != nil {
if err := req.Label.Validate(); err != nil {
EncodeError(ctx, err, w)
return
}

if err := s.CreateLabel(ctx, label); err != nil {
if err := s.CreateLabel(ctx, &req.Label); err != nil {
EncodeError(ctx, err, w)
return
}

if err := encodeResponse(ctx, w, http.StatusCreated, newLabelResponse(label)); err != nil {
if err := encodeResponse(ctx, w, http.StatusCreated, newLabelResponse(&req.Label)); err != nil {
// TODO: this can potentially result in calling w.WriteHeader multiple times, we need to pass a logger in here
// some how. This isn't as simple as simply passing in a logger to this function since the time that this function
// is called is distinct from the time that a potential logger is set.
Expand All @@ -142,8 +137,7 @@ func newPostLabelHandler(s plat.LabelService) http.HandlerFunc {
}

type postLabelRequest struct {
ResourceID plat.ID
Name string
Label plat.Label
}

func decodePostLabelRequest(ctx context.Context, r *http.Request) (*postLabelRequest, error) {
Expand All @@ -153,22 +147,27 @@ func decodePostLabelRequest(ctx context.Context, r *http.Request) (*postLabelReq
return nil, kerrors.InvalidDataf("url missing id")
}

name := params.ByName("name")
if name == "" {
return nil, kerrors.InvalidDataf("url missing label name")
}

var rid plat.ID
if err := rid.DecodeFromString(id); err != nil {
return nil, err
}

label := &postLabelRequest{
ResourceID: rid,
Name: name,
label := &plat.Label{}
if err := json.NewDecoder(r.Body).Decode(label); err != nil {
return nil, err
}

label.ResourceID = rid

if err := label.Validate(); err != nil {
return nil, err
}

return label, nil
req := &postLabelRequest{
Label: *label,
}

return req, nil
}

type patchLabelRequest struct {
Expand Down
36 changes: 11 additions & 25 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,7 @@ paths:
content:
application/json:
schema:
type: object
properties:
label:
type: string
$ref: "#/components/schemas/Label"
responses:
'200':
description: a list of all labels for a telegraf config
Expand Down Expand Up @@ -1258,10 +1255,7 @@ paths:
content:
application/json:
schema:
type: object
properties:
label:
type: string
$ref: "#/components/schemas/Label"
responses:
'200':
description: a list of all labels for a view
Expand Down Expand Up @@ -1905,15 +1899,12 @@ paths:
required: true
description: ID of the dashboard
requestBody:
description: tag to add
description: label to add
required: true
content:
application/json:
schema:
type: object
properties:
label:
type: string
$ref: "#/components/schemas/Label"
responses:
'200':
description: a list of all labels for a dashboard
Expand Down Expand Up @@ -2770,10 +2761,7 @@ paths:
content:
application/json:
schema:
type: object
properties:
label:
type: string
$ref: "#/components/schemas/Label"
responses:
'200':
description: a list of all labels for a bucket
Expand Down Expand Up @@ -3222,10 +3210,7 @@ paths:
content:
application/json:
schema:
type: object
properties:
label:
type: string
$ref: "#/components/schemas/Label"
responses:
'200':
description: a list of all labels for an organization
Expand Down Expand Up @@ -3991,10 +3976,7 @@ paths:
content:
application/json:
schema:
type: object
properties:
label:
type: string
$ref: "#/components/schemas/Label"
responses:
'200':
description: a list of all labels for a task
Expand Down Expand Up @@ -4991,6 +4973,8 @@ components:
enum:
- active
- inactive
labels:
$ref: "#/components/schemas/Labels"
flux:
description: The Flux script to run for this task.
type: string
Expand Down Expand Up @@ -5991,6 +5975,8 @@ components:
format: date
cells:
$ref: "#/components/schemas/Cells"
labels:
$ref: "#/components/schemas/Labels"
Dashboards:
type: object
properties:
Expand Down
Loading

0 comments on commit 0ddf6b3

Please sign in to comment.