Skip to content

Commit b6dff1a

Browse files
committed
[gpctl] Switch "gpctl api ws ls/get" to use v1/v1connect
1 parent c51a06b commit b6dff1a

File tree

6 files changed

+69
-20
lines changed

6 files changed

+69
-20
lines changed

dev/gpctl/cmd/public-api-workspace.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
)
1010

1111
var publicApiWorkspacesCmd = &cobra.Command{
12-
Use: "workspaces",
13-
Short: "Interact with workspaces through the public-API",
12+
Use: "workspaces",
13+
Aliases: []string{"ws"},
14+
Short: "Interact with workspaces through the public-API",
1415
}
1516

1617
func init() {

dev/gpctl/cmd/public-api-workspaces-get.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ package cmd
66

77
import (
88
"github.com/gitpod-io/gitpod/common-go/log"
9-
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
9+
v1 "github.com/gitpod-io/gitpod/components/public-api/go/v1"
10+
v1connect "github.com/gitpod-io/gitpod/components/public-api/go/v1/v1connect"
1011
"github.com/spf13/cobra"
1112
)
1213

@@ -17,25 +18,26 @@ var publicApiWorkspacesGetCmd = &cobra.Command{
1718
Run: func(cmd *cobra.Command, args []string) {
1819
workspaceID := args[0]
1920

20-
conn, err := newPublicAPIConn()
21+
httpClient, address, opts, err := newConnectHttpClient()
2122
if err != nil {
2223
log.Log.WithError(err).Fatal()
2324
}
2425

25-
service := v1.NewWorkspacesServiceClient(conn)
26+
service := v1connect.NewWorkspaceServiceClient(httpClient, address, opts...)
2627

2728
log.Log.Debugf("Retrieving workspace ID: %s", workspaceID)
28-
resp, err := service.GetWorkspace(cmd.Context(), &v1.GetWorkspaceRequest{WorkspaceId: workspaceID})
29+
cResp, err := service.GetWorkspace(cmd.Context(), wrapReq(&v1.GetWorkspaceRequest{WorkspaceId: workspaceID}))
2930
if err != nil {
3031
log.WithError(err).Fatalf("failed to retrieve workspace (ID: %s)", workspaceID)
3132
return
3233
}
34+
resp := cResp.Msg
3335

34-
tpl := `ID: {{ .Result.WorkspaceId }}
35-
Owner: {{ .Result.OwnerId }}
36-
ContextURL: {{ .Result.Context.ContextUrl }}
37-
InstanceID: {{ .Result.Status.Instance.InstanceId }}
38-
InstanceStatus: {{ .Result.Status.Instance.Status.Phase }}
36+
tpl := `ID: {{ .Workspace.Id }}
37+
OrganizationID: {{ .Workspace.OrganizationId }}
38+
ContextURL: {{ .Workspace.ContextUrl }}
39+
InstanceID: {{ .Workspace.Status.InstanceId }}
40+
InstanceStatus: {{ .Workspace.Status.Phase.Name }}
3941
`
4042
err = getOutputFormat(tpl, "{..result.workspace_id}").Print(resp)
4143
if err != nil {

dev/gpctl/cmd/public-api-workspaces-list.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,36 @@ package cmd
66

77
import (
88
"github.com/gitpod-io/gitpod/common-go/log"
9-
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
9+
v1 "github.com/gitpod-io/gitpod/components/public-api/go/v1"
10+
v1connect "github.com/gitpod-io/gitpod/components/public-api/go/v1/v1connect"
1011
"github.com/spf13/cobra"
1112
)
1213

1314
var publicApiWorkspacesListCmd = &cobra.Command{
14-
Use: "list",
15-
Short: "List your workspaces",
15+
Use: "list",
16+
Aliases: []string{"ls"},
17+
Short: "List your workspaces",
18+
Args: cobra.ExactArgs(1),
1619
Run: func(cmd *cobra.Command, args []string) {
17-
conn, err := newPublicAPIConn()
20+
organizationID := args[0]
21+
22+
httpClient, address, opts, err := newConnectHttpClient()
1823
if err != nil {
1924
log.Log.WithError(err).Fatal()
2025
}
2126

22-
service := v1.NewWorkspacesServiceClient(conn)
27+
service := v1connect.NewWorkspaceServiceClient(httpClient, address, opts...)
2328

24-
resp, err := service.ListWorkspaces(cmd.Context(), &v1.ListWorkspacesRequest{})
29+
cResp, err := service.ListWorkspaces(cmd.Context(), wrapReq(&v1.ListWorkspacesRequest{OrganizationId: organizationID}))
2530
if err != nil {
2631
log.WithError(err).Fatal("failed to retrieve workspace list")
2732
return
2833
}
34+
resp := cResp.Msg
2935

30-
tpl := `ID Owner ContextURL InstanceID InstanceStatus
31-
{{- range .Result }}
32-
{{ .WorkspaceId }} {{ .OwnerId }} {{ .Context.ContextUrl }} {{ .Status.Instance.InstanceId}} {{ .Status.Instance.Status.Phase}}
36+
tpl := `ID OrganizationID ContextURL InstanceID InstanceStatus
37+
{{- range .Workspaces }}
38+
{{ .Id }} {{ .OrganizationId }} {{ .ContextUrl }} {{ .Status.InstanceId }} {{ .Status.Phase.Name }}
3339
{{ end }}
3440
`
3541
err = getOutputFormat(tpl, "{..id}").Print(resp)

dev/gpctl/cmd/public-api.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import (
88
"context"
99
"crypto/tls"
1010
"fmt"
11+
"net/http"
1112
"os"
13+
"strings"
1214

15+
connect "github.com/bufbuild/connect-go"
1316
"github.com/gitpod-io/gitpod/common-go/log"
1417
"github.com/spf13/cobra"
1518
"google.golang.org/grpc"
@@ -69,3 +72,36 @@ func newPublicAPIConn() (*grpc.ClientConn, error) {
6972

7073
return conn, nil
7174
}
75+
76+
type connectHttpTransport struct {
77+
token string
78+
}
79+
80+
func (t *connectHttpTransport) RoundTrip(req *http.Request) (*http.Response, error) {
81+
req.URL.Path = "/public-api" + req.URL.Path
82+
req.Header.Add("authorization", fmt.Sprintf("Bearer %s", t.token))
83+
return http.DefaultTransport.RoundTrip(req)
84+
}
85+
86+
func newConnectHttpClient() (client connect.HTTPClient, address string, opts []connect.ClientOption, err error) {
87+
if publicApiCmdOpts.address == "" {
88+
return nil, "", nil, fmt.Errorf("empty connection address")
89+
}
90+
address = publicApiCmdOpts.address
91+
if !strings.Contains(publicApiCmdOpts.address, "://") {
92+
address = "https://" + address
93+
}
94+
95+
if publicApiCmdOpts.token == "" {
96+
return nil, "", nil, fmt.Errorf("empty connection token. Use --token or GPCTL_PUBLICAPI_TOKEN to provide one.")
97+
}
98+
opts = []connect.ClientOption{
99+
connect.WithProtoJSON(),
100+
}
101+
client = &http.Client{Transport: &connectHttpTransport{token: publicApiCmdOpts.token}}
102+
return client, address, opts, nil
103+
}
104+
105+
func wrapReq[T any](req *T) *connect.Request[T] {
106+
return &connect.Request[T]{Msg: req}
107+
}

dev/gpctl/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ require (
2727
k8s.io/client-go v0.27.3
2828
)
2929

30+
require github.com/bufbuild/connect-go v1.10.0
31+
3032
require (
3133
github.com/beorn7/perks v1.0.1 // indirect
3234
github.com/cenkalti/backoff/v4 v4.1.3 // indirect

dev/gpctl/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)