Skip to content

Commit 806fee8

Browse files
authored
chore: dependency update (#146)
* chore: dependency update * fix: lint * fix: lint
1 parent cf6750c commit 806fee8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1958
-1761
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ using Terraform Bytebase Provider to prepare those instances ready for applicati
1717

1818
- [Go](https://golang.org/doc/install) (1.19 or later)
1919
- [Terraform](https://developer.hashicorp.com/terraform/downloads?product_intent=terraform) (1.3.5 or later)
20-
- [Bytebase](https://github.com/bytebase/bytebase) (3.8.0 or later)
20+
- [Bytebase](https://github.com/bytebase/bytebase) (3.9.2 or later)
2121

2222
> If you have problems running `terraform` in MacOS with Apple Silicon, you can following https://stackoverflow.com/questions/66281882/how-can-i-get-terraform-init-to-run-on-my-apple-silicon-macbook-pro-for-the-go and use the `tfenv`.
2323

api/client.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package api
33
import (
44
"context"
55

6-
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
6+
v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1"
77
v1alpha1 "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
88
)
99

@@ -51,15 +51,14 @@ type UserFilter struct {
5151
State v1pb.State
5252
}
5353

54+
// GroupFilter is the filter for list group API.
55+
type GroupFilter struct {
56+
Query string
57+
Project string
58+
}
59+
5460
// Client is the API message for Bytebase OpenAPI client.
5561
type Client interface {
56-
// GetCaller returns the API caller.
57-
GetCaller() *v1pb.User
58-
// CheckResourceExist check if the resource exists.
59-
CheckResourceExist(ctx context.Context, name string) error
60-
// DeleteResource force delete the resource by name.
61-
DeleteResource(ctx context.Context, name string) error
62-
6362
// Instance
6463
// ListInstance will return instances.
6564
ListInstance(ctx context.Context, filter *InstanceFilter) ([]*v1pb.Instance, error)
@@ -71,6 +70,8 @@ type Client interface {
7170
UpdateInstance(ctx context.Context, patch *v1pb.Instance, updateMasks []string) (*v1pb.Instance, error)
7271
// UndeleteInstance undeletes the instance.
7372
UndeleteInstance(ctx context.Context, instanceName string) (*v1pb.Instance, error)
73+
// DeleteInstance deletes the instance.
74+
DeleteInstance(ctx context.Context, instanceName string) error
7475
// SyncInstanceSchema will trigger the schema sync for an instance.
7576
SyncInstanceSchema(ctx context.Context, instanceName string) error
7677

@@ -109,6 +110,8 @@ type Client interface {
109110
UpdateProject(ctx context.Context, patch *v1pb.Project, updateMask []string) (*v1pb.Project, error)
110111
// UndeleteProject undeletes the project.
111112
UndeleteProject(ctx context.Context, projectName string) (*v1pb.Project, error)
113+
// DeleteProject deletes the project.
114+
DeleteProject(ctx context.Context, projectName string) error
112115
// GetProjectIAMPolicy gets the project IAM policy by project full name.
113116
GetProjectIAMPolicy(ctx context.Context, projectName string) (*v1pb.IamPolicy, error)
114117
// SetProjectIAMPolicy sets the project IAM policy.
@@ -143,6 +146,8 @@ type Client interface {
143146
UpdateUser(ctx context.Context, patch *v1pb.User, updateMasks []string) (*v1pb.User, error)
144147
// UndeleteUser undeletes the user by name.
145148
UndeleteUser(ctx context.Context, userName string) (*v1pb.User, error)
149+
// DeleteUser deletes the user.
150+
DeleteUser(ctx context.Context, userName string) error
146151

147152
// Role
148153
// ListRole will returns all roles.
@@ -153,16 +158,20 @@ type Client interface {
153158
GetRole(ctx context.Context, name string) (*v1pb.Role, error)
154159
// UpdateRole updates the role.
155160
UpdateRole(ctx context.Context, patch *v1pb.Role, updateMasks []string) (*v1pb.Role, error)
161+
// DeleteRole deletes the role.
162+
DeleteRole(ctx context.Context, roleName string) error
156163

157164
// Group
158165
// ListGroup list all groups.
159-
ListGroup(ctx context.Context) (*v1pb.ListGroupsResponse, error)
166+
ListGroup(ctx context.Context, filter *GroupFilter) ([]*v1pb.Group, error)
160167
// CreateGroup creates the group.
161168
CreateGroup(ctx context.Context, email string, group *v1pb.Group) (*v1pb.Group, error)
162169
// GetGroup gets the group by name.
163170
GetGroup(ctx context.Context, name string) (*v1pb.Group, error)
164171
// UpdateGroup updates the group.
165172
UpdateGroup(ctx context.Context, patch *v1pb.Group, updateMasks []string) (*v1pb.Group, error)
173+
// DeleteGroup deletes the group.
174+
DeleteGroup(ctx context.Context, groupName string) error
166175

167176
// Workspace
168177
// GetWorkspaceIAMPolicy gets the workspace IAM policy.
@@ -177,6 +186,8 @@ type Client interface {
177186
GetReviewConfig(ctx context.Context, reviewName string) (*v1pb.ReviewConfig, error)
178187
// UpsertReviewConfig updates or creates the review config.
179188
UpsertReviewConfig(ctx context.Context, patch *v1pb.ReviewConfig, updateMasks []string) (*v1pb.ReviewConfig, error)
189+
// DeleteReviewConfig deletes the review config.
190+
DeleteReviewConfig(ctx context.Context, reviewConfigName string) error
180191

181192
// Risk
182193
// ListRisk lists the risk.
@@ -187,6 +198,8 @@ type Client interface {
187198
CreateRisk(ctx context.Context, risk *v1pb.Risk) (*v1pb.Risk, error)
188199
// UpdateRisk updates the risk.
189200
UpdateRisk(ctx context.Context, patch *v1pb.Risk, updateMasks []string) (*v1pb.Risk, error)
201+
// DeleteRisk deletes the risk.
202+
DeleteRisk(ctx context.Context, riskName string) error
190203

191204
// ListDatabaseGroup list all database groups in a project.
192205
ListDatabaseGroup(ctx context.Context, project string) (*v1pb.ListDatabaseGroupsResponse, error)
@@ -196,4 +209,6 @@ type Client interface {
196209
GetDatabaseGroup(ctx context.Context, name string, view v1pb.DatabaseGroupView) (*v1pb.DatabaseGroup, error)
197210
// UpdateDatabaseGroup updates the database group.
198211
UpdateDatabaseGroup(ctx context.Context, patch *v1pb.DatabaseGroup, updateMasks []string) (*v1pb.DatabaseGroup, error)
212+
// DeleteDatabaseGroup deletes the database group.
213+
DeleteDatabaseGroup(ctx context.Context, databaseGroupName string) error
199214
}

client/auth.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
package client
22

33
import (
4+
"context"
45
"fmt"
5-
"net/http"
6-
"strings"
76

8-
"github.com/pkg/errors"
9-
"google.golang.org/protobuf/encoding/protojson"
10-
11-
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
7+
"connectrpc.com/connect"
128
)
139

14-
// Login will login the user and get the response.
15-
func (c *client) login(request *v1pb.LoginRequest) (*v1pb.LoginResponse, error) {
16-
if request.Email == "" || request.Password == "" {
17-
return nil, errors.Errorf("undefined login service account or key")
18-
}
19-
rb, err := protojson.Marshal(request)
20-
if err != nil {
21-
return nil, err
22-
}
23-
24-
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s/auth/login", c.url, c.version), strings.NewReader(string(rb)))
25-
if err != nil {
26-
return nil, err
27-
}
10+
// Note: The login method has been moved to client.go and now uses Connect RPC.
11+
// This file is kept for backward compatibility but the implementation
12+
// has been migrated to use the AuthServiceClient from Connect RPC.
13+
// authInterceptor implements connect.Interceptor to add authentication headers.
14+
type authInterceptor struct {
15+
token string
16+
}
2817

29-
body, err := c.doRequest(req)
30-
if err != nil {
31-
return nil, errors.Wrapf(err, "failed to login")
32-
}
18+
func (a *authInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
19+
return connect.UnaryFunc(func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
20+
if req.Spec().IsClient && a.token != "" {
21+
req.Header().Set("Authorization", fmt.Sprintf("Bearer %s", a.token))
22+
}
23+
return next(ctx, req)
24+
})
25+
}
3326

34-
ar := v1pb.LoginResponse{}
35-
if err := ProtojsonUnmarshaler.Unmarshal(body, &ar); err != nil {
36-
return nil, err
37-
}
27+
func (a *authInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
28+
return connect.StreamingClientFunc(func(ctx context.Context, spec connect.Spec) connect.StreamingClientConn {
29+
conn := next(ctx, spec)
30+
if a.token != "" {
31+
conn.RequestHeader().Set("Authorization", fmt.Sprintf("Bearer %s", a.token))
32+
}
33+
return conn
34+
})
35+
}
3836

39-
return &ar, nil
37+
func (*authInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
38+
return connect.StreamingHandlerFunc(func(ctx context.Context, conn connect.StreamingHandlerConn) error {
39+
return next(ctx, conn)
40+
})
4041
}

client/cel.go

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,31 @@ package client
22

33
import (
44
"context"
5-
"fmt"
6-
"net/http"
7-
"strings"
85

9-
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
6+
v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1"
7+
"connectrpc.com/connect"
108
"github.com/pkg/errors"
119
v1alpha1 "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
12-
"google.golang.org/protobuf/encoding/protojson"
1310
)
1411

15-
// ParseExpression parse the expression string.
12+
// ParseExpression parse the expression string using Connect RPC.
1613
func (c *client) ParseExpression(ctx context.Context, expression string) (*v1alpha1.Expr, error) {
17-
payload, err := protojson.Marshal(&v1pb.BatchParseRequest{
18-
Expressions: []string{expression},
19-
})
20-
if err != nil {
21-
return nil, err
14+
if c.celClient == nil {
15+
return nil, errors.New("cel service client not initialized")
2216
}
2317

24-
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/cel/batchParse", c.url, c.version), strings.NewReader(string(payload)))
25-
if err != nil {
26-
return nil, err
27-
}
18+
req := connect.NewRequest(&v1pb.BatchParseRequest{
19+
Expressions: []string{expression},
20+
})
2821

29-
body, err := c.doRequest(req)
22+
resp, err := c.celClient.BatchParse(ctx, req)
3023
if err != nil {
3124
return nil, err
3225
}
3326

34-
var res v1pb.BatchParseResponse
35-
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
36-
return nil, err
37-
}
38-
39-
if len(res.Expressions) != 1 {
27+
if len(resp.Msg.Expressions) != 1 {
4028
return nil, errors.Errorf("failed to parse the cel: %v", expression)
4129
}
4230

43-
return res.GetExpressions()[0], nil
31+
return resp.Msg.GetExpressions()[0], nil
4432
}

client/client.go

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,91 @@ package client
33

44
import (
55
"context"
6-
"fmt"
7-
"io"
86
"net/http"
7+
"strings"
98
"time"
109

1110
"github.com/pkg/errors"
1211

13-
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
12+
"buf.build/gen/go/bytebase/bytebase/connectrpc/go/v1/bytebasev1connect"
13+
v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1"
14+
"connectrpc.com/connect"
1415

1516
"github.com/bytebase/terraform-provider-bytebase/api"
1617
)
1718

1819
// client is the API message for Bytebase API client.
1920
type client struct {
20-
url string
21-
version string
22-
client *http.Client
23-
token string
24-
caller *v1pb.User
21+
url string
22+
client *http.Client
23+
24+
// Connect RPC clients
25+
authClient bytebasev1connect.AuthServiceClient
26+
workspaceClient bytebasev1connect.WorkspaceServiceClient
27+
instanceClient bytebasev1connect.InstanceServiceClient
28+
databaseClient bytebasev1connect.DatabaseServiceClient
29+
databaseCatalogClient bytebasev1connect.DatabaseCatalogServiceClient
30+
databaseGroupClient bytebasev1connect.DatabaseGroupServiceClient
31+
projectClient bytebasev1connect.ProjectServiceClient
32+
userClient bytebasev1connect.UserServiceClient
33+
roleClient bytebasev1connect.RoleServiceClient
34+
groupClient bytebasev1connect.GroupServiceClient
35+
settingClient bytebasev1connect.SettingServiceClient
36+
orgPolicyClient bytebasev1connect.OrgPolicyServiceClient
37+
reviewConfigClient bytebasev1connect.ReviewConfigServiceClient
38+
riskClient bytebasev1connect.RiskServiceClient
39+
celClient bytebasev1connect.CelServiceClient
2540
}
2641

2742
// NewClient returns the new Bytebase API client.
28-
func NewClient(url, version, email, password string) (api.Client, error) {
43+
func NewClient(url, email, password string) (api.Client, error) {
2944
c := client{
30-
client: &http.Client{Timeout: 10 * time.Second},
31-
url: url,
32-
version: version,
45+
url: strings.TrimSuffix(url, "/"),
3346
}
3447

35-
response, err := c.login(&v1pb.LoginRequest{
36-
Email: email,
37-
Password: password,
38-
})
39-
if err != nil {
40-
return nil, err
48+
// Use standard HTTP client that supports both HTTP/1.1 and HTTP/2
49+
c.client = &http.Client{
50+
Timeout: 30 * time.Second,
4151
}
4252

43-
c.token = response.Token
44-
c.caller = response.User
53+
authInt := &authInterceptor{}
54+
interceptors := connect.WithInterceptors(authInt)
4555

46-
return &c, nil
47-
}
56+
// Create auth client without token first
57+
// Try without WithGRPC first to see if it's a standard Connect/gRPC-Web service
58+
c.authClient = bytebasev1connect.NewAuthServiceClient(
59+
c.client,
60+
c.url,
61+
)
4862

49-
func (c *client) doRequest(req *http.Request) ([]byte, error) {
50-
if c.token != "" {
51-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
52-
}
53-
54-
res, err := c.client.Do(req)
55-
if err != nil {
56-
return nil, err
57-
}
58-
defer res.Body.Close()
63+
// Login to get token
64+
loginReq := connect.NewRequest(&v1pb.LoginRequest{
65+
Email: email,
66+
Password: password,
67+
})
5968

60-
body, err := io.ReadAll(res.Body)
69+
loginResp, err := c.authClient.Login(context.Background(), loginReq)
6170
if err != nil {
62-
return nil, err
63-
}
64-
65-
if res.StatusCode != http.StatusOK {
66-
return nil, errors.Errorf("status: %d, body: %s", res.StatusCode, body)
71+
return nil, errors.Wrapf(err, "failed to login")
6772
}
6873

69-
return body, err
70-
}
74+
authInt.token = loginResp.Msg.Token
75+
76+
// Initialize other clients with auth token
77+
c.workspaceClient = bytebasev1connect.NewWorkspaceServiceClient(c.client, c.url, interceptors)
78+
c.instanceClient = bytebasev1connect.NewInstanceServiceClient(c.client, c.url, interceptors)
79+
c.databaseClient = bytebasev1connect.NewDatabaseServiceClient(c.client, c.url, interceptors)
80+
c.databaseCatalogClient = bytebasev1connect.NewDatabaseCatalogServiceClient(c.client, c.url, interceptors)
81+
c.databaseGroupClient = bytebasev1connect.NewDatabaseGroupServiceClient(c.client, c.url, interceptors)
82+
c.projectClient = bytebasev1connect.NewProjectServiceClient(c.client, c.url, interceptors)
83+
c.userClient = bytebasev1connect.NewUserServiceClient(c.client, c.url, interceptors)
84+
c.roleClient = bytebasev1connect.NewRoleServiceClient(c.client, c.url, interceptors)
85+
c.groupClient = bytebasev1connect.NewGroupServiceClient(c.client, c.url, interceptors)
86+
c.settingClient = bytebasev1connect.NewSettingServiceClient(c.client, c.url, interceptors)
87+
c.orgPolicyClient = bytebasev1connect.NewOrgPolicyServiceClient(c.client, c.url, interceptors)
88+
c.reviewConfigClient = bytebasev1connect.NewReviewConfigServiceClient(c.client, c.url, interceptors)
89+
c.riskClient = bytebasev1connect.NewRiskServiceClient(c.client, c.url, interceptors)
90+
c.celClient = bytebasev1connect.NewCelServiceClient(c.client, c.url, interceptors)
7191

72-
// GetCaller returns the API caller.
73-
func (c *client) GetCaller() *v1pb.User {
74-
return c.caller
75-
}
76-
77-
// CheckResourceExist check if the resource exists.
78-
func (c *client) CheckResourceExist(ctx context.Context, name string) error {
79-
if _, err := c.getResource(ctx, name, ""); err != nil {
80-
return err
81-
}
82-
return nil
83-
}
84-
85-
// DeleteResource force delete the resource by name.
86-
func (c *client) DeleteResource(ctx context.Context, name string) error {
87-
return c.execDelete(ctx, name)
92+
return &c, nil
8893
}

0 commit comments

Comments
 (0)