github.com/kubeshop/testkube@v1.17.23/pkg/cloud/data/artifact/artifacts_storage.go (about) 1 package artifact 2 3 import ( 4 "context" 5 "encoding/json" 6 "io" 7 "net/http" 8 9 "github.com/pkg/errors" 10 "google.golang.org/grpc" 11 12 "github.com/kubeshop/testkube/pkg/api/v1/testkube" 13 "github.com/kubeshop/testkube/pkg/cloud" 14 "github.com/kubeshop/testkube/pkg/cloud/data/executor" 15 "github.com/kubeshop/testkube/pkg/storage" 16 ) 17 18 type CloudArtifactsStorage struct { 19 executor executor.Executor 20 } 21 22 var ErrOperationNotSupported = errors.New("operation not supported") 23 24 func NewCloudArtifactsStorage(cloudClient cloud.TestKubeCloudAPIClient, grpcConn *grpc.ClientConn, apiKey string) *CloudArtifactsStorage { 25 return &CloudArtifactsStorage{executor: executor.NewCloudGRPCExecutor(cloudClient, grpcConn, apiKey)} 26 } 27 28 func (c *CloudArtifactsStorage) ListFiles(ctx context.Context, executionID, testName, testSuiteName, testWorkflowName string) ([]testkube.Artifact, error) { 29 req := ListFilesRequest{ 30 ExecutionID: executionID, 31 TestName: testName, 32 TestSuiteName: testSuiteName, 33 TestWorkflowName: testWorkflowName, 34 } 35 response, err := c.executor.Execute(ctx, CmdArtifactsListFiles, req) 36 if err != nil { 37 return nil, err 38 } 39 var commandResponse ListFilesResponse 40 if err = json.Unmarshal(response, &commandResponse); err != nil { 41 return nil, err 42 } 43 44 return commandResponse.Artifacts, nil 45 } 46 47 func (c *CloudArtifactsStorage) DownloadFile(ctx context.Context, file, executionID, testName, testSuiteName, testWorkflowName string) (io.Reader, error) { 48 req := DownloadFileRequest{ 49 File: file, 50 ExecutionID: executionID, 51 TestName: testName, 52 TestSuiteName: testSuiteName, 53 TestWorkflowName: testWorkflowName, 54 } 55 response, err := c.executor.Execute(ctx, CmdArtifactsDownloadFile, req) 56 if err != nil { 57 return nil, err 58 } 59 var commandResponse DownloadFileResponse 60 if err = json.Unmarshal(response, &commandResponse); err != nil { 61 return nil, err 62 } 63 64 data, err := c.getObject(ctx, commandResponse.URL) 65 if err != nil { 66 return nil, err 67 } 68 69 return data, nil 70 } 71 72 func (c *CloudArtifactsStorage) DownloadArchive(ctx context.Context, executionID string, masks []string) (io.Reader, error) { 73 return nil, errors.WithStack(ErrOperationNotSupported) 74 } 75 76 func (c *CloudArtifactsStorage) getObject(ctx context.Context, url string) (io.Reader, error) { 77 req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) 78 if err != nil { 79 return nil, err 80 } 81 rsp, err := http.DefaultClient.Do(req) 82 if err != nil { 83 return nil, errors.Wrap(err, "failed to get file from cloud storage") 84 } 85 if rsp.StatusCode != http.StatusOK { 86 return nil, errors.Errorf("error getting file from presigned url: expected 200 OK response code, got %d", rsp.StatusCode) 87 } 88 89 return rsp.Body, nil 90 } 91 92 func (c *CloudArtifactsStorage) UploadFile(ctx context.Context, bucketFolder string, filePath string, reader io.Reader, objectSize int64) error { 93 return errors.WithStack(ErrOperationNotSupported) 94 } 95 96 func (c *CloudArtifactsStorage) PlaceFiles(ctx context.Context, bucketFolders []string, prefix string) error { 97 return errors.WithStack(ErrOperationNotSupported) 98 } 99 100 func (c *CloudArtifactsStorage) GetValidBucketName(parentType string, parentName string) string { 101 return "" 102 } 103 104 var _ storage.ArtifactsStorage = (*CloudArtifactsStorage)(nil)