Skip to content

[content-service] Implement UsageReportService.DownloadURL #12335

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

Merged
merged 1 commit into from
Aug 26, 2022
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
60 changes: 52 additions & 8 deletions components/content-service/pkg/service/usage-report-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package service

import (
"context"

"errors"
"github.com/opentracing/opentracing-go"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -42,20 +42,64 @@ func (us *UsageReportService) UploadURL(ctx context.Context, req *api.UsageRepor
span.SetTag("name", req.Name)
defer tracing.FinishSpan(span, &err)

err = us.s.EnsureExists(ctx, us.bucketName)
if req.GetName() == "" {
return nil, status.Error(codes.InvalidArgument, "Name is required but got empty.")
}

logger := log.WithField("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.

usually we just name the variable log in cases like this.

Suggested change
logger := log.WithField("name", req.Name).
log := log.WithField("name", req.Name).

Copy link
Member Author

Choose a reason for hiding this comment

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

log is the package name where the logging is implemented. It's a name clash that we shouldn't allow.

WithField("bucket", us.bucketName)

err = us.ensureBucketExists(ctx)
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
return nil, err
}

info, err := us.s.SignUpload(ctx, us.bucketName, req.Name, &storage.SignedURLOptions{
ContentType: "application/json",
})
if err != nil {
log.WithField("name", req.Name).
WithField("bucket", us.bucketName).
WithError(err).
Error("Error getting UsageReport SignUpload URL")
return nil, status.Error(codes.Unknown, err.Error())
logger.WithError(err).Error("Error getting UsageReport SignUpload URL")
return nil, status.Error(codes.Internal, err.Error())
}
return &api.UsageReportUploadURLResponse{Url: info.URL}, nil
}

func (us *UsageReportService) DownloadURL(ctx context.Context, req *api.UsageReportDownloadURLRequest) (resp *api.UsageReportDownloadURLResponse, err error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "UsageReport.DownloadURL")
span.SetTag("name", req.Name)
defer tracing.FinishSpan(span, &err)

if req.GetName() == "" {
return nil, status.Error(codes.InvalidArgument, "Name is required but got empty.")
}

err = us.ensureBucketExists(ctx)
if err != nil {
return nil, err
}

download, err := us.s.SignDownload(ctx, us.bucketName, req.GetName(), &storage.SignedURLOptions{
ContentType: "application/json",
})
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return nil, status.Errorf(codes.NotFound, "Object %s does not exist.", req.GetName())
}

return nil, status.Errorf(codes.Internal, "Failed to generate download URL for usage report: %s", err.Error())
}

return &api.UsageReportDownloadURLResponse{
Url: download.URL,
}, nil
}

func (us *UsageReportService) ensureBucketExists(ctx context.Context) error {
err := us.s.EnsureExists(ctx, us.bucketName)
if err != nil {
log.WithError(err).Errorf("Bucket %s does not exist", us.bucketName)
return status.Error(codes.Internal, err.Error())
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,24 @@ func TestUploadURL(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "http://example.com/some-path", resp.Url)
}

func TestDownloadURL(t *testing.T) {
const (
fileName = "some-report-filename"
bucketName = "gitpod-usage-reports"
)

ctrl := gomock.NewController(t)
s := storagemock.NewMockPresignedAccess(ctrl)

s.EXPECT().EnsureExists(gomock.Any(), bucketName).
Return(nil)
s.EXPECT().SignDownload(gomock.Any(), bucketName, fileName, gomock.Any()).
Return(&storage.DownloadInfo{URL: "http://example.com/some-path"}, nil)

svc := &UsageReportService{cfg: config.StorageConfig{}, s: s, bucketName: bucketName}
resp, err := svc.DownloadURL(context.Background(), &api.UsageReportDownloadURLRequest{Name: fileName})

require.NoError(t, err)
require.Equal(t, "http://example.com/some-path", resp.Url)
}
2 changes: 1 addition & 1 deletion components/content-service/pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (

var (
// ErrNotFound is returned when an object is not found
ErrNotFound = xerrors.Errorf("not found")
ErrNotFound = fmt.Errorf("not found")
Copy link
Member Author

Choose a reason for hiding this comment

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

Drive-by: xerrors.Errorf is deperacted

)

// BucketNamer provides names for storage buckets
Expand Down