Skip to content

Commit

Permalink
Merge pull request #2228 from influxdata/more-label-responses
Browse files Browse the repository at this point in the history
add label responses to telegraf and buckets
  • Loading branch information
imogenkinsman authored Jan 4, 2019
2 parents 97cafe8 + 6a2554d commit 4ca3b83
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 46 deletions.
44 changes: 33 additions & 11 deletions http/bucket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,28 +188,38 @@ func newBucketUpdate(pb *platform.BucketUpdate) *bucketUpdate {
type bucketResponse struct {
Links map[string]string `json:"links"`
bucket
Labels []platform.Label `json:"labels"`
}

func newBucketResponse(b *platform.Bucket) *bucketResponse {
return &bucketResponse{
func newBucketResponse(b *platform.Bucket, labels []*platform.Label) *bucketResponse {
res := &bucketResponse{
Links: map[string]string{
"self": fmt.Sprintf("/api/v2/buckets/%s", b.ID),
"log": fmt.Sprintf("/api/v2/buckets/%s/log", b.ID),
"org": fmt.Sprintf("/api/v2/orgs/%s", b.OrganizationID),
"self": fmt.Sprintf("/api/v2/buckets/%s", b.ID),
"log": fmt.Sprintf("/api/v2/buckets/%s/log", b.ID),
"labels": fmt.Sprintf("/api/v2/buckets/%s/labels", b.ID),
"org": fmt.Sprintf("/api/v2/orgs/%s", b.OrganizationID),
},
bucket: *newBucket(b),
Labels: []platform.Label{},
}

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

return res
}

type bucketsResponse struct {
Links *platform.PagingLinks `json:"links"`
Buckets []*bucketResponse `json:"buckets"`
}

func newBucketsResponse(opts platform.FindOptions, f platform.BucketFilter, bs []*platform.Bucket) *bucketsResponse {
func newBucketsResponse(ctx context.Context, opts platform.FindOptions, f platform.BucketFilter, bs []*platform.Bucket, labelService platform.LabelService) *bucketsResponse {
rs := make([]*bucketResponse, 0, len(bs))
for _, b := range bs {
rs = append(rs, newBucketResponse(b))
labels, _ := labelService.FindLabels(ctx, platform.LabelFilter{ResourceID: b.ID})
rs = append(rs, newBucketResponse(b, labels))
}
return &bucketsResponse{
Links: newPagingLinks(bucketsPath, opts, f, len(bs)),
Expand All @@ -232,7 +242,7 @@ func (h *BucketHandler) handlePostBucket(w http.ResponseWriter, r *http.Request)
return
}

if err := encodeResponse(ctx, w, http.StatusCreated, newBucketResponse(req.Bucket)); err != nil {
if err := encodeResponse(ctx, w, http.StatusCreated, newBucketResponse(req.Bucket, []*platform.Label{})); err != nil {
logEncodingError(h.Logger, r, err)
return
}
Expand Down Expand Up @@ -283,7 +293,13 @@ func (h *BucketHandler) handleGetBucket(w http.ResponseWriter, r *http.Request)
return
}

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

if err := encodeResponse(ctx, w, http.StatusOK, newBucketResponse(b, labels)); err != nil {
logEncodingError(h.Logger, r, err)
return
}
Expand Down Expand Up @@ -367,7 +383,7 @@ func (h *BucketHandler) handleGetBuckets(w http.ResponseWriter, r *http.Request)
return
}

if err := encodeResponse(ctx, w, http.StatusOK, newBucketsResponse(req.opts, req.filter, bs)); err != nil {
if err := encodeResponse(ctx, w, http.StatusOK, newBucketsResponse(ctx, req.opts, req.filter, bs, h.LabelService)); err != nil {
logEncodingError(h.Logger, r, err)
return
}
Expand Down Expand Up @@ -424,7 +440,13 @@ func (h *BucketHandler) handlePatchBucket(w http.ResponseWriter, r *http.Request
return
}

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

if err := encodeResponse(ctx, w, http.StatusOK, newBucketResponse(b, labels)); err != nil {
logEncodingError(h.Logger, r, err)
return
}
Expand Down
80 changes: 63 additions & 17 deletions http/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
func TestService_handleGetBuckets(t *testing.T) {
type fields struct {
BucketService platform.BucketService
LabelService platform.LabelService
}
type args struct {
queryParams map[string][]string
Expand Down Expand Up @@ -58,6 +59,20 @@ func TestService_handleGetBuckets(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{
map[string][]string{
Expand All @@ -78,23 +93,43 @@ func TestService_handleGetBuckets(t *testing.T) {
"links": {
"org": "/api/v2/orgs/50f7ba1150f7ba11",
"self": "/api/v2/buckets/0b501e7e557ab1ed",
"log": "/api/v2/buckets/0b501e7e557ab1ed/log"
"log": "/api/v2/buckets/0b501e7e557ab1ed/log",
"labels": "/api/v2/buckets/0b501e7e557ab1ed/labels"
},
"id": "0b501e7e557ab1ed",
"organizationID": "50f7ba1150f7ba11",
"name": "hello",
"retentionRules": [{"type": "expire", "everySeconds": 2}]
"retentionRules": [{"type": "expire", "everySeconds": 2}],
"labels": [
{
"resourceID": "0b501e7e557ab1ed",
"name": "label",
"properties": {
"color": "fff000"
}
}
]
},
{
"links": {
"org": "/api/v2/orgs/7e55e118dbabb1ed",
"self": "/api/v2/buckets/c0175f0077a77005",
"log": "/api/v2/buckets/c0175f0077a77005/log"
"log": "/api/v2/buckets/c0175f0077a77005/log",
"labels": "/api/v2/buckets/c0175f0077a77005/labels"
},
"id": "c0175f0077a77005",
"organizationID": "7e55e118dbabb1ed",
"name": "example",
"retentionRules": [{"type": "expire", "everySeconds": 86400}]
"retentionRules": [{"type": "expire", "everySeconds": 86400}],
"labels": [
{
"resourceID": "c0175f0077a77005",
"name": "label",
"properties": {
"color": "fff000"
}
}
]
}
]
}
Expand All @@ -109,6 +144,7 @@ func TestService_handleGetBuckets(t *testing.T) {
return []*platform.Bucket{}, 0, nil
},
},
&mock.LabelService{},
},
args: args{
map[string][]string{
Expand All @@ -132,7 +168,7 @@ func TestService_handleGetBuckets(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 := NewBucketHandler(mappingService, labelService, userService)
h.BucketService = tt.fields.BucketService
Expand Down Expand Up @@ -161,8 +197,8 @@ func TestService_handleGetBuckets(t *testing.T) {
if tt.wants.contentType != "" && content != tt.wants.contentType {
t.Errorf("%q. handleGetBuckets() = %v, want %v", tt.name, content, tt.wants.contentType)
}
if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq {
t.Errorf("%q. handleGetBuckets() = ***%s***", tt.name, diff)
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil || tt.wants.body != "" && !eq {
t.Errorf("%q. handleGetBuckets() = ***%v***", tt.name, diff)
}
})
}
Expand Down Expand Up @@ -216,12 +252,14 @@ func TestService_handleGetBucket(t *testing.T) {
"links": {
"org": "/api/v2/orgs/020f755c3c082000",
"self": "/api/v2/buckets/020f755c3c082000",
"log": "/api/v2/buckets/020f755c3c082000/log"
"log": "/api/v2/buckets/020f755c3c082000/log",
"labels": "/api/v2/buckets/020f755c3c082000/labels"
},
"id": "020f755c3c082000",
"organizationID": "020f755c3c082000",
"name": "hello",
"retentionRules": [{"type": "expire", "everySeconds": 30}]
"retentionRules": [{"type": "expire", "everySeconds": 30}],
"labels": []
}
`,
},
Expand Down Expand Up @@ -332,12 +370,14 @@ func TestService_handlePostBucket(t *testing.T) {
"links": {
"org": "/api/v2/orgs/6f626f7274697320",
"self": "/api/v2/buckets/020f755c3c082000",
"log": "/api/v2/buckets/020f755c3c082000/log"
"log": "/api/v2/buckets/020f755c3c082000/log",
"labels": "/api/v2/buckets/020f755c3c082000/labels"
},
"id": "020f755c3c082000",
"organizationID": "6f626f7274697320",
"name": "hello",
"retentionRules": []
"retentionRules": [],
"labels": []
}
`,
},
Expand Down Expand Up @@ -542,12 +582,14 @@ func TestService_handlePatchBucket(t *testing.T) {
"links": {
"org": "/api/v2/orgs/020f755c3c082000",
"self": "/api/v2/buckets/020f755c3c082000",
"log": "/api/v2/buckets/020f755c3c082000/log"
"log": "/api/v2/buckets/020f755c3c082000/log",
"labels": "/api/v2/buckets/020f755c3c082000/labels"
},
"id": "020f755c3c082000",
"organizationID": "020f755c3c082000",
"name": "example",
"retentionRules": [{"type": "expire", "everySeconds": 2}]
"retentionRules": [{"type": "expire", "everySeconds": 2}],
"labels": []
}
`,
},
Expand Down Expand Up @@ -613,12 +655,14 @@ func TestService_handlePatchBucket(t *testing.T) {
"links": {
"org": "/api/v2/orgs/020f755c3c082000",
"self": "/api/v2/buckets/020f755c3c082000",
"log": "/api/v2/buckets/020f755c3c082000/log"
"log": "/api/v2/buckets/020f755c3c082000/log",
"labels": "/api/v2/buckets/020f755c3c082000/labels"
},
"id": "020f755c3c082000",
"organizationID": "020f755c3c082000",
"name": "bucket with no retention",
"retentionRules": []
"retentionRules": [],
"labels": []
}
`,
},
Expand Down Expand Up @@ -665,12 +709,14 @@ func TestService_handlePatchBucket(t *testing.T) {
"links": {
"org": "/api/v2/orgs/020f755c3c082000",
"self": "/api/v2/buckets/020f755c3c082000",
"log": "/api/v2/buckets/020f755c3c082000/log"
"log": "/api/v2/buckets/020f755c3c082000/log",
"labels": "/api/v2/buckets/020f755c3c082000/labels"
},
"id": "020f755c3c082000",
"organizationID": "020f755c3c082000",
"name": "b1",
"retentionRules": []
"retentionRules": [],
"labels": []
}
`,
},
Expand Down
2 changes: 1 addition & 1 deletion http/onboarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func newOnboardingResponse(results *platform.OnboardingResults) *onboardingRespo
}
return &onboardingResponse{
User: newUserResponse(results.User),
Bucket: newBucketResponse(results.Bucket),
Bucket: newBucketResponse(results.Bucket, []*platform.Label{}),
Organization: newOrgResponse(results.Org),
Auth: newAuthResponse(results.Auth, results.Org, results.User, ps),
}
Expand Down
8 changes: 6 additions & 2 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4770,6 +4770,8 @@ components:
example: 86400
minimum: 1
required: [type, everySeconds]
labels:
$ref: "#/components/schemas/Labels"
required: [name, retentionRules]
Buckets:
type: object
Expand Down Expand Up @@ -6600,8 +6602,10 @@ components:
properties:
self:
type: string
owners:
$ref: "#/components/schemas/Owners"
labels:
type: string
labels:
$ref: "#/components/schemas/Labels"
Telegrafs:
type: object
properties:
Expand Down
Loading

0 comments on commit 4ca3b83

Please sign in to comment.