From e3bbcb66d13a8788d0c514c3ff742d9c0fa064aa Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Thu, 27 Feb 2020 15:04:43 +0800 Subject: [PATCH] Set the "Link" and "location" header Set the "Link" and "location" header Signed-off-by: Wenkai Yin --- api/v2.0/swagger.yaml | 3 ++ src/server/v2.0/handler/artifact.go | 17 ++++---- src/server/v2.0/handler/base.go | 32 ++++++++++++++ src/server/v2.0/handler/base_test.go | 62 +++++++++++++++++++++++++++ src/server/v2.0/handler/repository.go | 6 ++- 5 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 src/server/v2.0/handler/base_test.go diff --git a/api/v2.0/swagger.yaml b/api/v2.0/swagger.yaml index acea581b0..2557e7c3d 100644 --- a/api/v2.0/swagger.yaml +++ b/api/v2.0/swagger.yaml @@ -560,6 +560,9 @@ responses: X-Request-Id: description: The ID of the corresponding request for the response type: string + Location: + description: The location of the resource + type: string '202': description: Accepted headers: diff --git a/src/server/v2.0/handler/artifact.go b/src/server/v2.0/handler/artifact.go index 6a8596a45..320633a36 100644 --- a/src/server/v2.0/handler/artifact.go +++ b/src/server/v2.0/handler/artifact.go @@ -107,8 +107,10 @@ func (a *artifactAPI) ListArtifacts(ctx context.Context, params operation.ListAr assembler.NewVulAssembler(boolValue(params.WithScanOverview)).WithArtifacts(artifacts...).Assemble(ctx) - // TODO add link header - return operation.NewListArtifactsOK().WithXTotalCount(total).WithLink("").WithPayload(artifacts) + return operation.NewListArtifactsOK(). + WithXTotalCount(total). + WithLink(a.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String()). + WithPayload(artifacts) } func (a *artifactAPI) GetArtifact(ctx context.Context, params operation.GetArtifactParams) middleware.Responder { @@ -163,18 +165,17 @@ func (a *artifactAPI) CopyArtifact(ctx context.Context, params operation.CopyArt } dstRepo := fmt.Sprintf("%s/%s", params.ProjectName, params.RepositoryName) - _, id, err := a.repoCtl.Ensure(ctx, dstRepo) + _, _, err = a.repoCtl.Ensure(ctx, dstRepo) if err != nil { return a.SendError(ctx, err) } - id, err = a.artCtl.Copy(ctx, srcRepo, ref, dstRepo) + _, err = a.artCtl.Copy(ctx, srcRepo, ref, dstRepo) if err != nil { return a.SendError(ctx, err) } - // TODO set location header - _ = id - return operation.NewCopyArtifactCreated() + location := strings.TrimSuffix(params.HTTPRequest.URL.Path, "/") + "/" + ref + return operation.NewCopyArtifactCreated().WithLocation(location) } func (a *artifactAPI) ScanArtifact(ctx context.Context, params operation.ScanArtifactParams) middleware.Responder { @@ -240,7 +241,7 @@ func (a *artifactAPI) CreateTag(ctx context.Context, params operation.CreateTagP if _, err = a.artCtl.CreateTag(ctx, tag); err != nil { return a.SendError(ctx, err) } - // TODO set location header? + // TODO as we provide no API for get the single tag, ignore setting the location header here return operation.NewCreateTagCreated() } diff --git a/src/server/v2.0/handler/base.go b/src/server/v2.0/handler/base.go index 8b1057b64..0b50cfe12 100644 --- a/src/server/v2.0/handler/base.go +++ b/src/server/v2.0/handler/base.go @@ -19,7 +19,10 @@ package handler import ( "context" "errors" + "github.com/goharbor/harbor/src/internal" ierror "github.com/goharbor/harbor/src/internal/error" + "net/url" + "strconv" "github.com/go-openapi/runtime/middleware" "github.com/goharbor/harbor/src/common/rbac" @@ -95,3 +98,32 @@ func (b *BaseAPI) RequireProjectAccess(ctx context.Context, projectIDOrName inte } return ierror.ForbiddenError(nil) } + +// Links return Links based on the provided pagination information +func (b *BaseAPI) Links(ctx context.Context, u *url.URL, total, pageNumber, pageSize int64) internal.Links { + url := *u + var links internal.Links + // prev + if pageNumber > 1 && (pageNumber-1)*pageSize < total { + q := url.Query() + q.Set("page", strconv.FormatInt(pageNumber-1, 10)) + url.RawQuery = q.Encode() + link := &internal.Link{ + URL: url.String(), + Rel: "prev", + } + links = append(links, link) + } + // next + if pageSize*pageNumber < total { + q := url.Query() + q.Set("page", strconv.FormatInt(pageNumber+1, 10)) + url.RawQuery = q.Encode() + link := &internal.Link{ + URL: url.String(), + Rel: "next", + } + links = append(links, link) + } + return links +} diff --git a/src/server/v2.0/handler/base_test.go b/src/server/v2.0/handler/base_test.go new file mode 100644 index 000000000..c9bba03e0 --- /dev/null +++ b/src/server/v2.0/handler/base_test.go @@ -0,0 +1,62 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package handler + +import ( + "github.com/stretchr/testify/suite" + "net/url" + "testing" +) + +type baseHandlerTestSuite struct { + suite.Suite + base *BaseAPI +} + +func (b *baseHandlerTestSuite) SetupSuite() { + b.base = &BaseAPI{} +} + +func (b *baseHandlerTestSuite) TestLinks() { + // request first page, response contains only "next" link + url, err := url.Parse("http://localhost/api/artifacts?page=1&page_size=1") + b.Require().Nil(err) + links := b.base.Links(nil, url, 3, 1, 1) + b.Require().Len(links, 1) + b.Equal("next", links[0].Rel) + b.Equal("http://localhost/api/artifacts?page=2&page_size=1", links[0].URL) + + // request last page, response contains only "prev" link + url, err = url.Parse("http://localhost/api/artifacts?page=3&page_size=1") + b.Require().Nil(err) + links = b.base.Links(nil, url, 3, 3, 1) + b.Require().Len(links, 1) + b.Equal("prev", links[0].Rel) + b.Equal("http://localhost/api/artifacts?page=2&page_size=1", links[0].URL) + + // request the second page, response contains both "prev" and "next" links + url, err = url.Parse("http://localhost/api/artifacts?page=2&page_size=1") + b.Require().Nil(err) + links = b.base.Links(nil, url, 3, 2, 1) + b.Require().Len(links, 2) + b.Equal("prev", links[0].Rel) + b.Equal("http://localhost/api/artifacts?page=1&page_size=1", links[0].URL) + b.Equal("next", links[1].Rel) + b.Equal("http://localhost/api/artifacts?page=3&page_size=1", links[1].URL) +} + +func TestBaseHandler(t *testing.T) { + suite.Run(t, &baseHandlerTestSuite{}) +} diff --git a/src/server/v2.0/handler/repository.go b/src/server/v2.0/handler/repository.go index 9f784ef0e..b178c3e54 100644 --- a/src/server/v2.0/handler/repository.go +++ b/src/server/v2.0/handler/repository.go @@ -81,8 +81,10 @@ func (r *repositoryAPI) ListRepositories(ctx context.Context, params operation.L for _, repository := range repositories { repos = append(repos, r.assembleRepository(ctx, repository)) } - // TODO add link header - return operation.NewListRepositoriesOK().WithXTotalCount(total).WithLink("").WithPayload(repos) + return operation.NewListRepositoriesOK(). + WithXTotalCount(total). + WithLink(r.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String()). + WithPayload(repos) } func (r *repositoryAPI) GetRepository(ctx context.Context, params operation.GetRepositoryParams) middleware.Responder {