|
| 1 | +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License-AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package service |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + |
| 10 | + "github.com/opentracing/opentracing-go" |
| 11 | + "google.golang.org/grpc/codes" |
| 12 | + "google.golang.org/grpc/status" |
| 13 | + |
| 14 | + "github.com/gitpod-io/gitpod/common-go/log" |
| 15 | + "github.com/gitpod-io/gitpod/common-go/tracing" |
| 16 | + "github.com/gitpod-io/gitpod/content-service/api" |
| 17 | + "github.com/gitpod-io/gitpod/content-service/api/config" |
| 18 | + "github.com/gitpod-io/gitpod/content-service/pkg/storage" |
| 19 | +) |
| 20 | + |
| 21 | +// UsageReportService implements UsageReportServiceServer |
| 22 | +type UsageReportService struct { |
| 23 | + cfg config.StorageConfig |
| 24 | + s storage.PresignedAccess |
| 25 | + |
| 26 | + api.UnimplementedUsageReportServiceServer |
| 27 | +} |
| 28 | + |
| 29 | +// NewUsageReportService create a new usagereport service |
| 30 | +func NewUsageReportService(cfg config.StorageConfig) (res *UsageReportService, err error) { |
| 31 | + s, err := storage.NewPresignedAccess(&cfg) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + return &UsageReportService{cfg: cfg, s: s}, nil |
| 36 | +} |
| 37 | + |
| 38 | +// UploadURL provides a URL to which clients can upload the content via HTTP PUT. |
| 39 | +func (us *UsageReportService) UploadURL(ctx context.Context, req *api.UsageReportUploadURLRequest) (resp *api.UsageReportUploadURLResponse, err error) { |
| 40 | + span, ctx := opentracing.StartSpanFromContext(ctx, "UsageReport.UploadURL") |
| 41 | + span.SetTag("name", req.Name) |
| 42 | + defer tracing.FinishSpan(span, &err) |
| 43 | + |
| 44 | + err = us.s.EnsureExists(ctx, req.Bucket) |
| 45 | + if err != nil { |
| 46 | + return nil, status.Error(codes.NotFound, err.Error()) |
| 47 | + } |
| 48 | + |
| 49 | + info, err := us.s.SignUpload(ctx, req.Bucket, req.Name, &storage.SignedURLOptions{ |
| 50 | + ContentType: "*/*", |
| 51 | + }) |
| 52 | + if err != nil { |
| 53 | + log.WithField("name", req.Name). |
| 54 | + WithField("bucket", req.Bucket). |
| 55 | + WithError(err). |
| 56 | + Error("error getting UsageReport SignUpload URL") |
| 57 | + if err == storage.ErrNotFound { |
| 58 | + return nil, status.Error(codes.NotFound, err.Error()) |
| 59 | + } |
| 60 | + return nil, status.Error(codes.Unknown, err.Error()) |
| 61 | + } |
| 62 | + return &api.UsageReportUploadURLResponse{Url: info.URL}, nil |
| 63 | +} |
0 commit comments