Skip to content

Commit

Permalink
yolo
Browse files Browse the repository at this point in the history
  • Loading branch information
easyCZ committed May 3, 2022
1 parent 1e1c229 commit 4ff236b
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 24 deletions.
13 changes: 12 additions & 1 deletion components/public-api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
package main

import (
"fmt"
"github.com/gitpod-io/gitpod/common-go/baseserver"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/public-api-server/middleware"
"github.com/gitpod-io/gitpod/public-api-server/pkg/apiv1"
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
v1 "github.com/gitpod-io/gitpod/public-api/v1"
"net/http"
"net/url"
)

func main() {
Expand Down Expand Up @@ -38,7 +41,15 @@ func register(srv *baseserver.Server) error {
m := middleware.NewLoggingMiddleware(logger)
srv.HTTPMux().Handle("/", m(http.HandlerFunc(HelloWorldHandler)))

v1.RegisterWorkspacesServiceServer(srv.GRPC(), apiv1.NewWorkspaceService())
// TODO(milan): Move to configuration
serverAPI, err := url.Parse("wss://gitpod.io/api/v1")
if err != nil {
return fmt.Errorf("failed to parse server API: %w", err)
}

serverConnPool := &proxy.NoConnectionPool{ServerAPI: serverAPI}

v1.RegisterWorkspacesServiceServer(srv.GRPC(), apiv1.NewWorkspaceService(serverConnPool))
v1.RegisterPrebuildsServiceServer(srv.GRPC(), v1.UnimplementedPrebuildsServiceServer{})

return nil
Expand Down
29 changes: 7 additions & 22 deletions components/public-api-server/pkg/apiv1/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@ package apiv1

import (
"context"
"encoding/json"
"fmt"
"github.com/gitpod-io/gitpod/common-go/log"
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
v1 "github.com/gitpod-io/gitpod/public-api/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

func NewWorkspaceService() *WorkspaceService {
func NewWorkspaceService(serverConnPool proxy.ServerConnectionPool) *WorkspaceService {
return &WorkspaceService{
connectionPool: serverConnPool,
UnimplementedWorkspacesServiceServer: &v1.UnimplementedWorkspacesServiceServer{},
}
}

type WorkspaceService struct {
serverEndpoint string
connectionPool proxy.ServerConnectionPool

*v1.UnimplementedWorkspacesServiceServer
}
Expand All @@ -33,31 +31,18 @@ func (w *WorkspaceService) GetWorkspace(ctx context.Context, r *v1.GetWorkspaceR
if err != nil {
return nil, err
}
fmt.Println(token)

endpoint := fmt.Sprintf("wss://%s/api/v1", "gitpod.io")
server, err := gitpod.ConnectToServer(endpoint, gitpod.ConnectToServerOpts{
Context: ctx,
Token: token,
Log: log.Log,
})
server, err := w.connectionPool.Get(ctx, token)
if err != nil {
return nil, status.Error(codes.Internal, "failed to establish connection to downstream services")
}

workspace, err := server.GetWorkspace(ctx, r.GetWorkspaceId())
// TODO(milan): Use resulting workspace and transform it to public API response
_, err = server.GetWorkspace(ctx, r.GetWorkspaceId())
if err != nil {
return nil, status.Error(codes.NotFound, "failed to get workspace")
}

log.Log.WithField("workspace", workspace).Infof("workspace %v", workspace)

b, err := json.Marshal(workspace)
if err != nil {
panic("failed to marshal")
}
fmt.Println(string(b))

return &v1.GetWorkspaceResponse{
ResponseStatus: nil,
Result: &v1.Workspace{
Expand Down
241 changes: 240 additions & 1 deletion components/public-api-server/pkg/apiv1/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package apiv1

import (
"context"
"fmt"
"github.com/gitpod-io/gitpod/common-go/baseserver"
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
v1 "github.com/gitpod-io/gitpod/public-api/v1"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
Expand All @@ -18,7 +20,9 @@ import (

func TestWorkspaceService_GetWorkspace(t *testing.T) {
srv := baseserver.NewForTests(t)
v1.RegisterWorkspacesServiceServer(srv.GRPC(), NewWorkspaceService())

connPool := &FakeServerConnPool{}
v1.RegisterWorkspacesServiceServer(srv.GRPC(), NewWorkspaceService(connPool))
baseserver.StartServerForTests(t, srv)

conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
Expand Down Expand Up @@ -47,3 +51,238 @@ func TestWorkspaceService_GetWorkspace(t *testing.T) {
},
}, resp))
}

type FakeServerConnPool struct{}

func (f *FakeServerConnPool) Get(ctx context.Context, token string) (gitpod.APIInterface, error) {
return &FakeGitpodAPI{}, nil
}

type FakeGitpodAPI struct {
workspaces map[string]*gitpod.WorkspaceInfo
}

func (f *FakeGitpodAPI) GetWorkspace(ctx context.Context, id string) (res *gitpod.WorkspaceInfo, err error) {
w, ok := f.workspaces[id]
if !ok {
return nil, fmt.Errorf("workspace not found")
}

return w, nil
}

func (f *FakeGitpodAPI) AdminBlockUser(ctx context.Context, req *gitpod.AdminBlockUserRequest) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetLoggedInUser(ctx context.Context) (res *gitpod.User, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) UpdateLoggedInUser(ctx context.Context, user *gitpod.User) (res *gitpod.User, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetAuthProviders(ctx context.Context) (res []*gitpod.AuthProviderInfo, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetOwnAuthProviders(ctx context.Context) (res []*gitpod.AuthProviderEntry, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) UpdateOwnAuthProvider(ctx context.Context, params *gitpod.UpdateOwnAuthProviderParams) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) DeleteOwnAuthProvider(ctx context.Context, params *gitpod.DeleteOwnAuthProviderParams) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetConfiguration(ctx context.Context) (res *gitpod.Configuration, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetGitpodTokenScopes(ctx context.Context, tokenHash string) (res []string, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetToken(ctx context.Context, query *gitpod.GetTokenSearchOptions) (res *gitpod.Token, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetPortAuthenticationToken(ctx context.Context, workspaceID string) (res *gitpod.Token, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) DeleteAccount(ctx context.Context) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetClientRegion(ctx context.Context) (res string, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) HasPermission(ctx context.Context, permission *gitpod.PermissionName) (res bool, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetWorkspaces(ctx context.Context, options *gitpod.GetWorkspacesOptions) (res []*gitpod.WorkspaceInfo, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetWorkspaceOwner(ctx context.Context, workspaceID string) (res *gitpod.UserInfo, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetWorkspaceUsers(ctx context.Context, workspaceID string) (res []*gitpod.WorkspaceInstanceUser, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetFeaturedRepositories(ctx context.Context) (res []*gitpod.WhitelistedRepository, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) IsWorkspaceOwner(ctx context.Context, workspaceID string) (res bool, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) CreateWorkspace(ctx context.Context, options *gitpod.CreateWorkspaceOptions) (res *gitpod.WorkspaceCreationResult, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) StartWorkspace(ctx context.Context, id string, options *gitpod.StartWorkspaceOptions) (res *gitpod.StartWorkspaceResult, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) StopWorkspace(ctx context.Context, id string) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) DeleteWorkspace(ctx context.Context, id string) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) SetWorkspaceDescription(ctx context.Context, id string, desc string) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) ControlAdmission(ctx context.Context, id string, level *gitpod.AdmissionLevel) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) UpdateWorkspaceUserPin(ctx context.Context, id string, action *gitpod.PinAction) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) SendHeartBeat(ctx context.Context, options *gitpod.SendHeartBeatOptions) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) WatchWorkspaceImageBuildLogs(ctx context.Context, workspaceID string) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) IsPrebuildDone(ctx context.Context, pwsid string) (res bool, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) SetWorkspaceTimeout(ctx context.Context, workspaceID string, duration *gitpod.WorkspaceTimeoutDuration) (res *gitpod.SetWorkspaceTimeoutResult, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetWorkspaceTimeout(ctx context.Context, workspaceID string) (res *gitpod.GetWorkspaceTimeoutResult, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetOpenPorts(ctx context.Context, workspaceID string) (res []*gitpod.WorkspaceInstancePort, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) OpenPort(ctx context.Context, workspaceID string, port *gitpod.WorkspaceInstancePort) (res *gitpod.WorkspaceInstancePort, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) ClosePort(ctx context.Context, workspaceID string, port float32) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetUserStorageResource(ctx context.Context, options *gitpod.GetUserStorageResourceOptions) (res string, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) UpdateUserStorageResource(ctx context.Context, options *gitpod.UpdateUserStorageResourceOptions) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetEnvVars(ctx context.Context) (res []*gitpod.UserEnvVarValue, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) SetEnvVar(ctx context.Context, variable *gitpod.UserEnvVarValue) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) DeleteEnvVar(ctx context.Context, variable *gitpod.UserEnvVarValue) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetContentBlobUploadURL(ctx context.Context, name string) (url string, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetContentBlobDownloadURL(ctx context.Context, name string) (url string, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetGitpodTokens(ctx context.Context) (res []*gitpod.APIToken, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GenerateNewGitpodToken(ctx context.Context, options *gitpod.GenerateNewGitpodTokenOptions) (res string, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) DeleteGitpodToken(ctx context.Context, tokenHash string) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) SendFeedback(ctx context.Context, feedback string) (res string, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) RegisterGithubApp(ctx context.Context, installationID string) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) TakeSnapshot(ctx context.Context, options *gitpod.TakeSnapshotOptions) (res string, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) WaitForSnapshot(ctx context.Context, snapshotId string) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetSnapshots(ctx context.Context, workspaceID string) (res []*string, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) StoreLayout(ctx context.Context, workspaceID string, layoutData string) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GetLayout(ctx context.Context, workspaceID string) (res string, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) GuessGitTokenScopes(ctx context.Context, params *gitpod.GuessGitTokenScopesParams) (res *gitpod.GuessedGitTokenScopes, err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) TrackEvent(ctx context.Context, event *gitpod.RemoteTrackMessage) (err error) {
panic("implement me")
}

func (f *FakeGitpodAPI) InstanceUpdates(ctx context.Context, instanceID string) (<-chan *gitpod.WorkspaceInstance, error) {
panic("implement me")
}
35 changes: 35 additions & 0 deletions components/public-api-server/pkg/proxy/server_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package proxy

import (
"context"
"fmt"
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"net/url"
)

type ServerConnectionPool interface {
// Get retrieves or creates a new connection for the specified token
// Connections must not be shared across tokens
Get(ctx context.Context, token string) (gitpod.APIInterface, error)
}

// NoConnectionPool is simple version of the ServerConnectionPool which always creates a new connection.
type NoConnectionPool struct {
ServerAPI *url.URL
}

func (p *NoConnectionPool) Get(ctx context.Context, token string) (gitpod.APIInterface, error) {
logger := ctxlogrus.Extract(ctx)

server, err := gitpod.ConnectToServer(p.ServerAPI.String(), gitpod.ConnectToServerOpts{
Context: ctx,
Token: token,
Log: logger,
})
if err != nil {
return nil, fmt.Errorf("failed to create new connection to server: %w", err)
}

return server, nil
}

0 comments on commit 4ff236b

Please sign in to comment.