Skip to content
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

[Terrarium - Providers]: Implementation of Storage Feature - 2 #71

Merged
merged 13 commits into from
Apr 30, 2024
95 changes: 95 additions & 0 deletions internal/provider/services/mocks/mock_clients.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package mocks

import (
"context"

providerServices "github.com/terrariumcloud/terrarium/internal/provider/services"
terrariumProvider "github.com/terrariumcloud/terrarium/pkg/terrarium/provider"

"google.golang.org/grpc"
)

type MockProviderStorageClient struct {
providerServices.StorageClient
DownloadSourceZipInvocations int
DownloadSourceZipClient providerServices.Storage_DownloadProviderSourceZipClient
DownloadSourceZipError error
DownloadShasumInvocations int
DownloadShasumClient providerServices.Storage_DownloadShasumClient
DownloadShasumError error
DownloadShasumSignatureInvocations int
DownloadShasumSignatureClient providerServices.Storage_DownloadShasumSignatureClient
DownloadShasumSignatureError error
}

func (m *MockProviderStorageClient) DownloadProviderSourceZip(ctx context.Context, in *terrariumProvider.DownloadSourceZipRequest, opts ...grpc.CallOption) (providerServices.Storage_DownloadProviderSourceZipClient, error) {
m.DownloadSourceZipInvocations++
return m.DownloadSourceZipClient, m.DownloadSourceZipError
}

func (m *MockProviderStorageClient) DownloadShasum(ctx context.Context, in *terrariumProvider.DownloadShasumRequest, opts ...grpc.CallOption) (providerServices.Storage_DownloadShasumClient, error) {
m.DownloadShasumInvocations++
return m.DownloadShasumClient, m.DownloadShasumError
}

func (m *MockProviderStorageClient) DownloadShasumSignature(ctx context.Context, in *terrariumProvider.DownloadShasumRequest, opts ...grpc.CallOption) (providerServices.Storage_DownloadShasumSignatureClient, error) {
m.DownloadShasumSignatureInvocations++
return m.DownloadShasumSignatureClient, m.DownloadShasumSignatureError
}

type MockStorage_DownloadProviderSourceZipClient struct {
providerServices.Storage_DownloadProviderSourceZipClient
RecvInvocations int
RecvResponse *terrariumProvider.SourceZipResponse
RecvError error
CloseSendInvocations int
CloseSendError error
}

func (m *MockStorage_DownloadProviderSourceZipClient) Recv() (*terrariumProvider.SourceZipResponse, error) {
m.RecvInvocations++
return m.RecvResponse, m.RecvError
}

func (m *MockStorage_DownloadProviderSourceZipClient) CloseSend() error {
m.CloseSendInvocations++
return m.CloseSendError
}

type MockStorage_DownloadProviderShasumClient struct {
providerServices.Storage_DownloadShasumClient
RecvInvocations int
RecvResponse *terrariumProvider.DownloadShasumResponse
RecvError error
CloseSendInvocations int
CloseSendError error
}

func (m *MockStorage_DownloadProviderShasumClient) Recv() (*terrariumProvider.DownloadShasumResponse, error) {
m.RecvInvocations++
return m.RecvResponse, m.RecvError
}

func (m *MockStorage_DownloadProviderShasumClient) CloseSend() error {
m.CloseSendInvocations++
return m.CloseSendError
}

type MockStorage_DownloadProviderShasumSignatureClient struct {
providerServices.Storage_DownloadShasumSignatureClient
RecvInvocations int
RecvResponse *terrariumProvider.DownloadShasumResponse
RecvError error
CloseSendInvocations int
CloseSendError error
}

func (m *MockStorage_DownloadProviderShasumSignatureClient) Recv() (*terrariumProvider.DownloadShasumResponse, error) {
m.RecvInvocations++
return m.RecvResponse, m.RecvError
}

func (m *MockStorage_DownloadProviderShasumSignatureClient) CloseSend() error {
m.CloseSendInvocations++
return m.CloseSendError
}
59 changes: 59 additions & 0 deletions internal/provider/services/mocks/mock_servers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package mocks

import (
"context"

providerServices "github.com/terrariumcloud/terrarium/internal/provider/services"
terrariumProvider "github.com/terrariumcloud/terrarium/pkg/terrarium/provider"
)

type MockDownloadProviderSourceZipServer struct {
providerServices.Storage_DownloadProviderSourceZipServer
SendInvocations int
SendResponse *terrariumProvider.SourceZipResponse
SendError error
}

func (mds *MockDownloadProviderSourceZipServer) Context() context.Context {
return context.TODO()
}

func (mds *MockDownloadProviderSourceZipServer) Send(res *terrariumProvider.SourceZipResponse) error {
mds.SendInvocations++
mds.SendResponse = res
return mds.SendError
}

type MockDownloadProviderShasumServer struct {
providerServices.Storage_DownloadShasumServer
SendInvocations int
SendResponse *terrariumProvider.DownloadShasumResponse
SendError error
}

func (mds *MockDownloadProviderShasumServer) Context() context.Context {
return context.TODO()
}

func (mds *MockDownloadProviderShasumServer) Send(res *terrariumProvider.DownloadShasumResponse) error {
mds.SendInvocations++
mds.SendResponse = res
return mds.SendError
}

type MockDownloadProviderShasumSignatureServer struct {
providerServices.Storage_DownloadShasumSignatureServer
SendInvocations int
SendResponse *terrariumProvider.DownloadShasumResponse
SendError error
}

func (mds *MockDownloadProviderShasumSignatureServer) Context() context.Context {
return context.TODO()
}

func (mds *MockDownloadProviderShasumSignatureServer) Send(res *terrariumProvider.DownloadShasumResponse) error {
mds.SendInvocations++
mds.SendResponse = res
return mds.SendError
}
172 changes: 172 additions & 0 deletions internal/provider/services/storage/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package storage

import (
"context"
"github.com/terrariumcloud/terrarium/internal/common/grpc_service"
"github.com/terrariumcloud/terrarium/internal/provider/services"
"github.com/terrariumcloud/terrarium/pkg/terrarium/provider"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"io"
)

type storageGrpcClient struct {
endpoint string
}

func NewStorageGrpcClient(endpoint string) services.StorageClient {
return &storageGrpcClient{endpoint: endpoint}
}

func (s storageGrpcClient) DownloadProviderSourceZip(ctx context.Context, in *provider.DownloadSourceZipRequest, opts ...grpc.CallOption) (services.Storage_DownloadProviderSourceZipClient, error) {
if conn, err := grpc_service.CreateGRPCConnection(s.endpoint); err != nil {
return nil, err
} else {
client := services.NewStorageClient(conn)
if download, err := client.DownloadProviderSourceZip(ctx, in, opts...); err == nil {
return &downloadSourceZipClient{conn: conn, client: download}, nil
} else {
_ = conn.Close()
return nil, err
}
}
}

func (s storageGrpcClient) DownloadShasum(ctx context.Context, in *provider.DownloadShasumRequest, opts ...grpc.CallOption) (services.Storage_DownloadShasumClient, error) {
if conn, err := grpc_service.CreateGRPCConnection(s.endpoint); err != nil {
return nil, err
} else {
client := services.NewStorageClient(conn)
if download, err := client.DownloadShasum(ctx, in, opts...); err == nil {
return &downloadShasumClient{conn: conn, client: download}, nil
} else {
_ = conn.Close()
return nil, err
}
}
}

func (s storageGrpcClient) DownloadShasumSignature(ctx context.Context, in *provider.DownloadShasumRequest, opts ...grpc.CallOption) (services.Storage_DownloadShasumSignatureClient, error) {
if conn, err := grpc_service.CreateGRPCConnection(s.endpoint); err != nil {
return nil, err
} else {
client := services.NewStorageClient(conn)
if download, err := client.DownloadShasumSignature(ctx, in, opts...); err == nil {
return &downloadShasumSignatureClient{conn: conn, client: download}, nil
} else {
_ = conn.Close()
return nil, err
}
}
}

type downloadSourceZipClient struct {
conn *grpc.ClientConn
client services.Storage_DownloadProviderSourceZipClient
}

func (d downloadSourceZipClient) Recv() (*provider.SourceZipResponse, error) {
result, err := d.client.Recv()
if err == io.EOF {
_ = d.conn.Close()
}
return result, err
}

func (d downloadSourceZipClient) Header() (metadata.MD, error) {
return d.client.Header()
}

func (d downloadSourceZipClient) Trailer() metadata.MD {
return d.client.Trailer()
}

func (d downloadSourceZipClient) CloseSend() error {
return d.client.CloseSend()
}

func (d downloadSourceZipClient) Context() context.Context {
return d.client.Context()
}

func (d downloadSourceZipClient) SendMsg(m any) error {
return d.client.SendMsg(m)
}

func (d downloadSourceZipClient) RecvMsg(m any) error {
return d.client.RecvMsg(m)
}

type downloadShasumClient struct {
conn *grpc.ClientConn
client services.Storage_DownloadShasumClient
}

func (d downloadShasumClient) Recv() (*provider.DownloadShasumResponse, error) {
result, err := d.client.Recv()
if err == io.EOF {
_ = d.conn.Close()
}
return result, err
}

func (d downloadShasumClient) Header() (metadata.MD, error) {
return d.client.Header()
}

func (d downloadShasumClient) Trailer() metadata.MD {
return d.client.Trailer()
}

func (d downloadShasumClient) CloseSend() error {
return d.client.CloseSend()
}

func (d downloadShasumClient) Context() context.Context {
return d.client.Context()
}

func (d downloadShasumClient) SendMsg(m any) error {
return d.client.SendMsg(m)
}

func (d downloadShasumClient) RecvMsg(m any) error {
return d.client.RecvMsg(m)
}

type downloadShasumSignatureClient struct {
conn *grpc.ClientConn
client services.Storage_DownloadShasumSignatureClient
}

func (d downloadShasumSignatureClient) Recv() (*provider.DownloadShasumResponse, error) {
result, err := d.client.Recv()
if err == io.EOF {
_ = d.conn.Close()
}
return result, err
}

func (d downloadShasumSignatureClient) Header() (metadata.MD, error) {
return d.client.Header()
}

func (d downloadShasumSignatureClient) Trailer() metadata.MD {
return d.client.Trailer()
}

func (d downloadShasumSignatureClient) CloseSend() error {
return d.client.CloseSend()
}

func (d downloadShasumSignatureClient) Context() context.Context {
return d.client.Context()
}

func (d downloadShasumSignatureClient) SendMsg(m any) error {
return d.client.SendMsg(m)
}

func (d downloadShasumSignatureClient) RecvMsg(m any) error {
return d.client.RecvMsg(m)
}
Loading