Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(http): create view #13448

Merged
merged 1 commit into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,16 @@ func SortDashboards(opts FindOptions, ds []*Dashboard) {

// Cell holds positional information about a cell on dashboard and a reference to a cell.
type Cell struct {
ID ID `json:"id,omitempty"`
X int32 `json:"x"`
Y int32 `json:"y"`
W int32 `json:"w"`
H int32 `json:"h"`
ID ID `json:"id,omitempty"`
CellProperty
}

// CellProperty contains the properties of a cell.
type CellProperty struct {
X int32 `json:"x"`
Y int32 `json:"y"`
W int32 `json:"w"`
H int32 `json:"h"`
}

// DashboardFilter is a filter for dashboards.
Expand Down
63 changes: 45 additions & 18 deletions http/dashboard_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path"

Expand Down Expand Up @@ -691,13 +690,13 @@ func (r *patchDashboardRequest) Valid() error {

type postDashboardCellRequest struct {
dashboardID platform.ID
cell *platform.Cell
opts platform.AddDashboardCellOptions
*platform.CellProperty
UsingView *platform.ID `json:"usingView"`
Name *string `json:"name"`
}

func decodePostDashboardCellRequest(ctx context.Context, r *http.Request) (*postDashboardCellRequest, error) {
req := &postDashboardCellRequest{}

params := httprouter.ParamsFromContext(ctx)
id := params.ByName("id")
if id == "" {
Expand All @@ -706,21 +705,21 @@ func decodePostDashboardCellRequest(ctx context.Context, r *http.Request) (*post
Msg: "url missing id",
}
}
if err := req.dashboardID.DecodeFromString(id); err != nil {
return nil, err
}

bs, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "bad request json body",
Err: err,
}
}

req.cell = &platform.Cell{}
if err := json.NewDecoder(bytes.NewReader(bs)).Decode(req.cell); err != nil {
return nil, err
}
if err := json.NewDecoder(bytes.NewReader(bs)).Decode(&req.opts); err != nil {
return nil, err
if err := req.dashboardID.DecodeFromString(id); err != nil {
return nil, &platform.Error{
Code: platform.EInvalid,
Msg: "invalid dashboard id",
Err: err,
}
}

return req, nil
Expand All @@ -735,12 +734,40 @@ func (h *DashboardHandler) handlePostDashboardCell(w http.ResponseWriter, r *htt
EncodeError(ctx, err, w)
return
}
if err := h.DashboardService.AddDashboardCell(ctx, req.dashboardID, req.cell, req.opts); err != nil {
cell := new(platform.Cell)

opts := new(platform.AddDashboardCellOptions)
if req.UsingView != nil || req.Name != nil {
opts.View = new(platform.View)
if req.UsingView != nil {
// load the view
opts.View, err = h.DashboardService.GetDashboardCellView(ctx, req.dashboardID, *req.UsingView)
if err != nil {
EncodeError(ctx, err, w)
return
}
}
if req.Name != nil {
opts.View.Name = *req.Name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the Name param supposed to describe the view name, or the dashboard name? The swagger says dashboard name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be the view's name, dashboard name is more like a parent thing like system dashboard. This name should be like cpu, memory, disk ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
} else if req.CellProperty == nil {
EncodeError(ctx, &platform.Error{
Code: platform.EInvalid,
Msg: "req body is empty",
}, w)
return
}

if req.CellProperty != nil {
cell.CellProperty = *req.CellProperty
}

if err := h.DashboardService.AddDashboardCell(ctx, req.dashboardID, cell, *opts); err != nil {
EncodeError(ctx, err, w)
return
}

if err := encodeResponse(ctx, w, http.StatusCreated, newDashboardCellResponse(req.dashboardID, req.cell)); err != nil {
if err := encodeResponse(ctx, w, http.StatusCreated, newDashboardCellResponse(req.dashboardID, cell)); err != nil {
logEncodingError(h.Logger, r, err)
return
}
Expand Down
142 changes: 109 additions & 33 deletions http/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ func TestService_handleGetDashboards(t *testing.T) {
Cells: []*platform.Cell{
{
ID: platformtesting.MustIDBase16("da7aba5e5d81e550"),
X: 1,
Y: 2,
W: 3,
H: 4,
CellProperty: platform.CellProperty{
X: 1,
Y: 2,
W: 3,
H: 4,
},
},
},
},
Expand Down Expand Up @@ -238,10 +240,12 @@ func TestService_handleGetDashboards(t *testing.T) {
Cells: []*platform.Cell{
{
ID: platformtesting.MustIDBase16("da7aba5e5d81e550"),
X: 1,
Y: 2,
W: 3,
H: 4,
CellProperty: platform.CellProperty{
X: 1,
Y: 2,
W: 3,
H: 4,
},
},
},
},
Expand Down Expand Up @@ -399,10 +403,12 @@ func TestService_handleGetDashboard(t *testing.T) {
Cells: []*platform.Cell{
{
ID: platformtesting.MustIDBase16("da7aba5e5d81e550"),
X: 1,
Y: 2,
W: 3,
H: 4,
CellProperty: platform.CellProperty{
X: 1,
Y: 2,
W: 3,
H: 4,
},
},
},
}, nil
Expand Down Expand Up @@ -557,10 +563,12 @@ func TestService_handlePostDashboard(t *testing.T) {
Cells: []*platform.Cell{
{
ID: platformtesting.MustIDBase16("da7aba5e5d81e550"),
X: 1,
Y: 2,
W: 3,
H: 4,
CellProperty: platform.CellProperty{
X: 1,
Y: 2,
W: 3,
H: 4,
},
},
},
},
Expand Down Expand Up @@ -776,10 +784,12 @@ func TestService_handlePatchDashboard(t *testing.T) {
Cells: []*platform.Cell{
{
ID: platformtesting.MustIDBase16("da7aba5e5d81e550"),
X: 1,
Y: 2,
W: 3,
H: 4,
CellProperty: platform.CellProperty{
X: 1,
Y: 2,
W: 3,
H: 4,
},
},
},
}
Expand Down Expand Up @@ -932,7 +942,7 @@ func TestService_handlePostDashboardCell(t *testing.T) {
}
type args struct {
id string
cell *platform.Cell
body string
}
type wants struct {
statusCode int
Expand All @@ -947,7 +957,7 @@ func TestService_handlePostDashboardCell(t *testing.T) {
wants wants
}{
{
name: "create a dashboard cell",
name: "empty body",
fields: fields{
&mock.DashboardService{
AddDashboardCellF: func(ctx context.Context, id platform.ID, c *platform.Cell, opt platform.AddDashboardCellOptions) error {
Expand All @@ -958,12 +968,82 @@ func TestService_handlePostDashboardCell(t *testing.T) {
},
args: args{
id: "020f755c3c082000",
cell: &platform.Cell{
ID: platformtesting.MustIDBase16("020f755c3c082000"),
X: 10,
Y: 11,
},
wants: wants{
statusCode: http.StatusBadRequest,
contentType: "application/json; charset=utf-8",
body: `{"code":"invalid","message":"bad request json body","error":"EOF"}`,
},
},
{
name: "no properties",
fields: fields{
&mock.DashboardService{
AddDashboardCellF: func(ctx context.Context, id platform.ID, c *platform.Cell, opt platform.AddDashboardCellOptions) error {
c.ID = platformtesting.MustIDBase16("020f755c3c082000")
return nil
},
},
},
args: args{
id: "020f755c3c082000",
body: `{"bad":1}`,
},
wants: wants{
statusCode: http.StatusBadRequest,
contentType: "application/json; charset=utf-8",
body: `
{
"code": "invalid",
"message": "req body is empty"
}`,
},
},
{
name: "bad dash id",
fields: fields{
&mock.DashboardService{
AddDashboardCellF: func(ctx context.Context, id platform.ID, c *platform.Cell, opt platform.AddDashboardCellOptions) error {
c.ID = platformtesting.MustIDBase16("020f755c3c082000")
return nil
},
},
},
args: args{
id: "fff",
body: `{}`,
},
wants: wants{
statusCode: http.StatusBadRequest,
contentType: "application/json; charset=utf-8",
body: `
{
"code": "invalid",
"error": "id must have a length of 16 bytes",
"message": "invalid dashboard id"
}`,
},
},
{
name: "general create a dashboard cell",
fields: fields{
&mock.DashboardService{
AddDashboardCellF: func(ctx context.Context, id platform.ID, c *platform.Cell, opt platform.AddDashboardCellOptions) error {
c.ID = platformtesting.MustIDBase16("020f755c3c082000")
return nil
},
GetDashboardCellViewF: func(ctx context.Context, id1, id2 platform.ID) (*platform.View, error) {
return &platform.View{
ViewContents: platform.ViewContents{
ID: platformtesting.MustIDBase16("020f755c3c082001"),
}}, nil
},
},
},
args: args{
id: "020f755c3c082000",
body: `{"x":10,"y":11,"name":"name1","usingView":"020f755c3c082001"}`,
},
wants: wants{
statusCode: http.StatusCreated,
contentType: "application/json; charset=utf-8",
Expand All @@ -989,13 +1069,9 @@ func TestService_handlePostDashboardCell(t *testing.T) {
dashboardBackend := NewMockDashboardBackend()
dashboardBackend.DashboardService = tt.fields.DashboardService
h := NewDashboardHandler(dashboardBackend)

b, err := json.Marshal(tt.args.cell)
if err != nil {
t.Fatalf("failed to unmarshal cell: %v", err)
}

r := httptest.NewRequest("GET", "http://any.url", bytes.NewReader(b))
buf := new(bytes.Buffer)
_, _ = buf.WriteString(tt.args.body)
r := httptest.NewRequest("POST", "http://any.url", buf)

r = r.WithContext(context.WithValue(
context.Background(),
Expand Down
3 changes: 0 additions & 3 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6749,9 +6749,6 @@ components:
h:
type: integer
format: int32
viewID:
type: string
description: uses the view provided in the request
usingView:
type: string
description: makes a copy of the provided view
Expand Down
Loading