Skip to content

[public-api] Bind unimplemented WorkspacesServiceHandler using Connect #13595

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
Oct 6, 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
3 changes: 2 additions & 1 deletion components/public-api-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ go 1.19

require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20220708163326-82d177caec6e
github.com/bufbuild/connect-go v1.0.0
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/gitpod-protocol v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/public-api v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/usage-api v0.0.0-00010101000000-000000000000
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.8
github.com/google/go-cmp v0.5.9
github.com/gorilla/handlers v1.5.1
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/prometheus/client_golang v1.13.0
Expand Down
6 changes: 4 additions & 2 deletions components/public-api-server/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 57 additions & 5 deletions components/public-api-server/pkg/server/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ package server

import (
"context"
"net/http"
"net/url"
"testing"

"github.com/bufbuild/connect-go"
"github.com/gitpod-io/gitpod/common-go/baseserver"
v1 "github.com/gitpod-io/gitpod/public-api/v1"
"github.com/prometheus/client_golang/prometheus"
"github.com/gitpod-io/gitpod/public-api/v1/v1connect"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand All @@ -25,12 +27,11 @@ func TestPublicAPIServer_v1_WorkspaceService(t *testing.T) {
srv := baseserver.NewForTests(t,
baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)),
)
registry := prometheus.NewRegistry()

gitpodAPI, err := url.Parse("wss://main.preview.gitpod-dev.com/api/v1")
require.NoError(t, err)

require.NoError(t, register(srv, gitpodAPI, registry))
require.NoError(t, register(srv, gitpodAPI))
baseserver.StartServerForTests(t, srv)

conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
Expand Down Expand Up @@ -69,12 +70,11 @@ func TestPublicAPIServer_v1_WorkspaceService(t *testing.T) {
func TestPublicAPIServer_v1_PrebuildService(t *testing.T) {
ctx := context.Background()
srv := baseserver.NewForTests(t, baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)))
registry := prometheus.NewRegistry()

gitpodAPI, err := url.Parse("wss://main.preview.gitpod-dev.com/api/v1")
require.NoError(t, err)

require.NoError(t, register(srv, gitpodAPI, registry))
require.NoError(t, register(srv, gitpodAPI))

baseserver.StartServerForTests(t, srv)

Expand All @@ -100,6 +100,58 @@ func TestPublicAPIServer_v1_PrebuildService(t *testing.T) {
requireErrorStatusCode(t, codes.Unimplemented, err)
}

func TestPublicAPIServer_WorkspaceServiceHandler(t *testing.T) {
ctx := context.Background()
srv := baseserver.NewForTests(t,
baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)),
baseserver.WithHTTP(baseserver.MustUseRandomLocalAddress(t)),
)

gitpodAPI, err := url.Parse("wss://main.preview.gitpod-dev.com/api/v1")
require.NoError(t, err)

require.NoError(t, register(srv, gitpodAPI))
baseserver.StartServerForTests(t, srv)

client := v1connect.NewWorkspacesServiceClient(http.DefaultClient, srv.HTTPAddress())

_, err = client.ListWorkspaces(ctx, connect.NewRequest(&v1.ListWorkspacesRequest{}))
require.Equal(t, connect.CodeUnimplemented.String(), connect.CodeOf(err).String())

_, err = client.GetWorkspace(ctx, connect.NewRequest(&v1.GetWorkspaceRequest{}))
require.Equal(t, connect.CodeUnimplemented.String(), connect.CodeOf(err).String())

_, err = client.GetOwnerToken(ctx, connect.NewRequest(&v1.GetOwnerTokenRequest{}))
require.Equal(t, connect.CodeUnimplemented.String(), connect.CodeOf(err).String())

_, err = client.CreateAndStartWorkspace(ctx, connect.NewRequest(&v1.CreateAndStartWorkspaceRequest{}))
require.Equal(t, connect.CodeUnimplemented.String(), connect.CodeOf(err).String())

_, err = client.StartWorkspace(ctx, connect.NewRequest(&v1.StartWorkspaceRequest{}))
require.Equal(t, connect.CodeUnimplemented.String(), connect.CodeOf(err).String())

_, err = client.GetActiveWorkspaceInstance(ctx, connect.NewRequest(&v1.GetActiveWorkspaceInstanceRequest{}))
require.Equal(t, connect.CodeUnimplemented.String(), connect.CodeOf(err).String())

_, err = client.GetWorkspaceInstanceOwnerToken(ctx, connect.NewRequest(&v1.GetWorkspaceInstanceOwnerTokenRequest{}))
require.Equal(t, connect.CodeUnimplemented.String(), connect.CodeOf(err).String())

stream, err := client.ListenToWorkspaceInstance(ctx, connect.NewRequest(&v1.ListenToWorkspaceInstanceRequest{}))
require.NoError(t, err)
stream.Receive()
require.Equal(t, connect.CodeUnimplemented.String(), connect.CodeOf(stream.Err()).String())

logsStream, err := client.ListenToImageBuildLogs(ctx, connect.NewRequest(&v1.ListenToImageBuildLogsRequest{}))
require.NoError(t, err)
logsStream.Receive()
require.Equal(t, connect.CodeUnimplemented.String(), connect.CodeOf(logsStream.Err()).String())

stopStream, err := client.StopWorkspace(ctx, connect.NewRequest(&v1.StopWorkspaceRequest{}))
require.NoError(t, err)
stopStream.Receive()
require.Equal(t, connect.CodeUnimplemented.String(), connect.CodeOf(stopStream.Err()).String())
}

func requireErrorStatusCode(t *testing.T, expected codes.Code, err error) {
require.Error(t, err)
st, ok := status.FromError(err)
Expand Down
14 changes: 7 additions & 7 deletions components/public-api-server/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gitpod-io/gitpod/common-go/log"

"github.com/gitpod-io/gitpod/public-api/config"
"github.com/gitpod-io/gitpod/public-api/v1/v1connect"
"github.com/gorilla/handlers"

"github.com/gitpod-io/gitpod/common-go/baseserver"
Expand All @@ -22,7 +23,6 @@ import (
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
"github.com/gitpod-io/gitpod/public-api-server/pkg/webhooks"
v1 "github.com/gitpod-io/gitpod/public-api/v1"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -62,12 +62,9 @@ func Start(logger *logrus.Entry, version string, cfg *config.Configuration) erro
log.Info("No stripe webhook secret is configured, endpoints will return NotImplemented")
}

srv.HTTPMux().Handle("/test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`test`))
}))
srv.HTTPMux().Handle("/stripe/invoices/webhook", handlers.ContentTypeHandler(stripeWebhookHandler, "application/json"))

if registerErr := register(srv, gitpodAPI, srv.MetricsRegistry()); registerErr != nil {
if registerErr := register(srv, gitpodAPI); registerErr != nil {
return fmt.Errorf("failed to register services: %w", registerErr)
}

Expand All @@ -78,14 +75,17 @@ func Start(logger *logrus.Entry, version string, cfg *config.Configuration) erro
return nil
}

func register(srv *baseserver.Server, serverAPIURL *url.URL, registry *prometheus.Registry) error {
proxy.RegisterMetrics(registry)
func register(srv *baseserver.Server, serverAPIURL *url.URL) error {
proxy.RegisterMetrics(srv.MetricsRegistry())

connPool := &proxy.NoConnectionPool{ServerAPI: serverAPIURL}

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

workspacesRoute, workspacesServiceHandler := v1connect.NewWorkspacesServiceHandler(&v1connect.UnimplementedWorkspacesServiceHandler{})
srv.HTTPMux().Handle(workspacesRoute, workspacesServiceHandler)

return nil
}

Expand Down