Skip to content

Commit aecaa76

Browse files
Andrew Farriesroboquat
authored andcommitted
Add gpctl api workspaces ownertoken command
1 parent 40491e6 commit aecaa76

File tree

5 files changed

+147
-61
lines changed

5 files changed

+147
-61
lines changed

dev/gpctl/cmd/api/root.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewCommand() *cobra.Command {
3737
}
3838

3939
func newConn(connOpts *connectionOptions) (*grpc.ClientConn, error) {
40-
log.Log.Infof("Estabilishing gRPC connection against %s", connOpts.address)
40+
log.Log.Infof("Establishing gRPC connection against %s", connOpts.address)
4141

4242
opts := []grpc.DialOption{
4343
// attach token to requests to auth
@@ -61,13 +61,26 @@ func newConn(connOpts *connectionOptions) (*grpc.ClientConn, error) {
6161
return conn, nil
6262
}
6363

64+
func validateAndConnect(connOpts *connectionOptions) (*grpc.ClientConn, error) {
65+
if err := connOpts.validate(); err != nil {
66+
return nil, fmt.Errorf("invalid connection options: %w", err)
67+
}
68+
69+
conn, err := newConn(connOpts)
70+
if err != nil {
71+
return nil, fmt.Errorf("failed to establish gRPC connection: %w", err)
72+
}
73+
74+
return conn, nil
75+
}
76+
6477
type connectionOptions struct {
6578
address string
6679
insecure bool
6780
token string
6881
}
6982

70-
func (o *connectionOptions) Validate() error {
83+
func (o *connectionOptions) validate() error {
7184
if o.address == "" {
7285
return fmt.Errorf("empty connection address")
7386
}

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package api
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"os"
11+
12+
"github.com/gitpod-io/gitpod/common-go/log"
13+
v1 "github.com/gitpod-io/gitpod/public-api/v1"
14+
"github.com/spf13/cobra"
15+
"google.golang.org/grpc"
16+
)
17+
18+
func newWorkspacesGetCommand() *cobra.Command {
19+
var workspaceID string
20+
21+
cmd := &cobra.Command{
22+
Use: "get",
23+
Short: "Retrieve details about a workspace by ID",
24+
Example: " get --id 1234",
25+
Run: func(cmd *cobra.Command, args []string) {
26+
if workspaceID == "" {
27+
log.Log.Fatal("no workspace id specified, use --id to set it")
28+
}
29+
30+
conn, err := validateAndConnect(connOpts)
31+
if err != nil {
32+
log.Log.WithError(err).Fatal()
33+
}
34+
35+
workspace, err := getWorkspace(cmd.Context(), conn, workspaceID)
36+
37+
log.Log.WithError(err).WithField("workspace", workspace.String()).Debugf("Workspace response")
38+
if err != nil {
39+
log.Log.WithError(err).Fatal("Failed to retrieve workspace.")
40+
return
41+
}
42+
43+
if err := printProtoMsg(os.Stdout, workspace); err != nil {
44+
log.Log.WithError(err).Fatal("Failed to serialize proto message and print it.")
45+
}
46+
},
47+
}
48+
49+
cmd.Flags().StringVar(&workspaceID, "id", "", "Workspace ID")
50+
51+
return cmd
52+
}
53+
54+
func getWorkspace(ctx context.Context, conn *grpc.ClientConn, workspaceID string) (*v1.GetWorkspaceResponse, error) {
55+
service := v1.NewWorkspacesServiceClient(conn)
56+
57+
log.Log.Debugf("Retrieving workspace ID: %s", workspaceID)
58+
resp, err := service.GetWorkspace(ctx, &v1.GetWorkspaceRequest{WorkspaceId: workspaceID})
59+
if err != nil {
60+
return nil, fmt.Errorf("failed to retrieve workspace (ID: %s): %w", workspaceID, err)
61+
}
62+
63+
return resp, nil
64+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package api
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"os"
11+
12+
"github.com/gitpod-io/gitpod/common-go/log"
13+
v1 "github.com/gitpod-io/gitpod/public-api/v1"
14+
"github.com/spf13/cobra"
15+
"google.golang.org/grpc"
16+
)
17+
18+
func newWorkspacesOwnerTokenCommand() *cobra.Command {
19+
var workspaceID string
20+
21+
cmd := &cobra.Command{
22+
Use: "ownertoken",
23+
Short: "Retrieve the owner token for a given workspace ID",
24+
Example: " ownertoken --id 1234",
25+
Run: func(cmd *cobra.Command, args []string) {
26+
if workspaceID == "" {
27+
log.Log.Fatal("no workspace id specified, use --id to set it")
28+
}
29+
30+
conn, err := validateAndConnect(connOpts)
31+
if err != nil {
32+
log.Log.WithError(err).Fatal()
33+
}
34+
35+
ownerToken, err := getOwnerToken(cmd.Context(), conn, workspaceID)
36+
37+
log.Log.WithError(err).WithField("ownertoken", ownerToken.String()).Debugf("Owner token response")
38+
if err != nil {
39+
log.Log.WithError(err).Fatal("Failed to retrieve owner token.")
40+
return
41+
}
42+
43+
if err := printProtoMsg(os.Stdout, ownerToken); err != nil {
44+
log.Log.WithError(err).Fatal("Failed to serialize proto message and print it.")
45+
}
46+
},
47+
}
48+
49+
cmd.Flags().StringVar(&workspaceID, "id", "", "Workspace ID")
50+
51+
return cmd
52+
}
53+
54+
func getOwnerToken(ctx context.Context, conn *grpc.ClientConn, workspaceID string) (*v1.GetOwnerTokenResponse, error) {
55+
service := v1.NewWorkspacesServiceClient(conn)
56+
57+
log.Log.Debugf("Retrieving owner token from workspace ID: %s", workspaceID)
58+
resp, err := service.GetOwnerToken(ctx, &v1.GetOwnerTokenRequest{WorkspaceId: workspaceID})
59+
if err != nil {
60+
return nil, fmt.Errorf("failed to retrieve workspace (ID: %s): %w", workspaceID, err)
61+
}
62+
63+
return resp, nil
64+
}

dev/gpctl/cmd/api/workspaces.go

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
package api
66

77
import (
8-
"context"
98
"fmt"
10-
"github.com/gitpod-io/gitpod/common-go/log"
11-
v1 "github.com/gitpod-io/gitpod/public-api/v1"
9+
"io"
10+
1211
"github.com/spf13/cobra"
13-
"google.golang.org/grpc"
1412
"google.golang.org/protobuf/encoding/protojson"
1513
"google.golang.org/protobuf/proto"
16-
"io"
17-
"os"
1814
)
1915

2016
func newWorkspacesCommand() *cobra.Command {
@@ -24,62 +20,11 @@ func newWorkspacesCommand() *cobra.Command {
2420
}
2521

2622
cmd.AddCommand(newWorkspacesGetCommand())
23+
cmd.AddCommand(newWorkspacesOwnerTokenCommand())
2724

2825
return cmd
2926
}
3027

31-
func newWorkspacesGetCommand() *cobra.Command {
32-
var workspaceID string
33-
34-
cmd := &cobra.Command{
35-
Use: "get",
36-
Short: "Retrieve details about a workspace by ID",
37-
Example: " get --id 1234",
38-
Run: func(cmd *cobra.Command, args []string) {
39-
if workspaceID == "" {
40-
log.Log.Fatal("no workspace id specified, use --id to set it")
41-
}
42-
43-
if err := connOpts.Validate(); err != nil {
44-
log.Log.WithError(err).Fatal("Invalid connections options.")
45-
}
46-
47-
conn, err := newConn(connOpts)
48-
if err != nil {
49-
log.Log.WithError(err).Fatal("Failed to establish gRPC connection")
50-
}
51-
52-
workspace, err := getWorkspace(cmd.Context(), conn, workspaceID)
53-
54-
log.Log.WithError(err).WithField("workspace", workspace.String()).Debugf("Workspace response")
55-
if err != nil {
56-
log.Log.WithError(err).Fatal("Failed to retrieve workspace.")
57-
return
58-
}
59-
60-
if err := printProtoMsg(os.Stdout, workspace); err != nil {
61-
log.Log.WithError(err).Fatal("Failed to serialize proto message and print it.")
62-
}
63-
},
64-
}
65-
66-
cmd.Flags().StringVar(&workspaceID, "id", "", "Workspace ID")
67-
68-
return cmd
69-
}
70-
71-
func getWorkspace(ctx context.Context, conn *grpc.ClientConn, workspaceID string) (*v1.GetWorkspaceResponse, error) {
72-
service := v1.NewWorkspacesServiceClient(conn)
73-
74-
log.Log.Debugf("Retrieving workspace ID: %s", workspaceID)
75-
resp, err := service.GetWorkspace(ctx, &v1.GetWorkspaceRequest{WorkspaceId: workspaceID})
76-
if err != nil {
77-
return nil, fmt.Errorf("failed to retrieve workspace (ID: %s): %w", workspaceID, err)
78-
}
79-
80-
return resp, nil
81-
}
82-
8328
func printProtoMsg(w io.Writer, m proto.Message) error {
8429
b, err := protojson.Marshal(m)
8530
if err != nil {

scripts/protoc-generator.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ install_dependencies() {
2020
lint() {
2121
local PROTO_DIR=${1:-.}
2222

23-
docker run --volume "$PWD/$PROTO_DIR:/workspace" --workdir /workspace bufbuild/buf lint || exit 1
23+
docker run --rm --volume "$PWD/$PROTO_DIR:/workspace" --workdir /workspace bufbuild/buf lint || exit 1
2424
}
2525

2626
go_protoc() {

0 commit comments

Comments
 (0)