Skip to content

Commit be77d5c

Browse files
authored
chore: support users and project iam (#81)
* refactor: use protocol api * fix: go mod * chore: update * chore: update * chore: go version * fix: test * chore: golang lint * chore: support approval setting * chore: support approval flow * chore: update * fix: go mod tidy * fix: lint * chore: update * chore: update * chore: update * fix: lint * chore: policy and setting * chore: update * chore: update docs * chore: support vcs provider and vcs connector * chore: update * fix: golang lint * chore: update docs * chore: support users and project iam * fix: lint * chore: update * chore: update * chore: check expire time
1 parent 7a63ad5 commit be77d5c

29 files changed

+1554
-200
lines changed

api/client.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import (
99

1010
// Client is the API message for Bytebase OpenAPI client.
1111
type Client interface {
12-
// Auth
13-
// Login will login the user and get the response.
14-
Login() (*v1pb.LoginResponse, error)
12+
// GetCaller returns the API caller.
13+
GetCaller() *v1pb.User
1514

1615
// Environment
1716
// CreateEnvironment creates the environment.
1817
CreateEnvironment(ctx context.Context, environmentID string, create *v1pb.Environment) (*v1pb.Environment, error)
19-
// GetEnvironment gets the environment by id.
18+
// GetEnvironment gets the environment by full name.
2019
GetEnvironment(ctx context.Context, environmentName string) (*v1pb.Environment, error)
2120
// ListEnvironment finds all environments.
2221
ListEnvironment(ctx context.Context, showDeleted bool) (*v1pb.ListEnvironmentsResponse, error)
@@ -30,7 +29,7 @@ type Client interface {
3029
// Instance
3130
// ListInstance will return instances.
3231
ListInstance(ctx context.Context, showDeleted bool) (*v1pb.ListInstancesResponse, error)
33-
// GetInstance gets the instance by id.
32+
// GetInstance gets the instance by full name.
3433
GetInstance(ctx context.Context, instanceName string) (*v1pb.Instance, error)
3534
// CreateInstance creates the instance.
3635
CreateInstance(ctx context.Context, instanceID string, instance *v1pb.Instance) (*v1pb.Instance, error)
@@ -62,7 +61,7 @@ type Client interface {
6261
UpdateDatabase(ctx context.Context, patch *v1pb.Database, updateMasks []string) (*v1pb.Database, error)
6362

6463
// Project
65-
// GetProject gets the project by resource id.
64+
// GetProject gets the project by project full name.
6665
GetProject(ctx context.Context, projectName string) (*v1pb.Project, error)
6766
// ListProject list the projects,
6867
ListProject(ctx context.Context, showDeleted bool) (*v1pb.ListProjectsResponse, error)
@@ -74,6 +73,10 @@ type Client interface {
7473
DeleteProject(ctx context.Context, projectName string) error
7574
// UndeleteProject undeletes the project.
7675
UndeleteProject(ctx context.Context, projectName string) (*v1pb.Project, error)
76+
// GetProjectIAMPolicy gets the project IAM policy by project full name.
77+
GetProjectIAMPolicy(ctx context.Context, projectName string) (*v1pb.IamPolicy, error)
78+
// SetProjectIAMPolicy sets the project IAM policy.
79+
SetProjectIAMPolicy(ctx context.Context, projectName string, iamPolicy *v1pb.IamPolicy) (*v1pb.IamPolicy, error)
7780

7881
// Setting
7982
// ListSettings lists all settings.
@@ -90,7 +93,7 @@ type Client interface {
9093
// VCS Provider
9194
// ListVCSProvider will returns all vcs providers.
9295
ListVCSProvider(ctx context.Context) (*v1pb.ListVCSProvidersResponse, error)
93-
// GetVCSProvider gets the vcs by id.
96+
// GetVCSProvider gets the vcs by full name.
9497
GetVCSProvider(ctx context.Context, name string) (*v1pb.VCSProvider, error)
9598
// CreateVCSProvider creates the vcs provider.
9699
CreateVCSProvider(ctx context.Context, vcsID string, vcs *v1pb.VCSProvider) (*v1pb.VCSProvider, error)
@@ -102,12 +105,26 @@ type Client interface {
102105
// VCS Connector
103106
// ListVCSConnector will returns all vcs connector in a project.
104107
ListVCSConnector(ctx context.Context, projectName string) (*v1pb.ListVCSConnectorsResponse, error)
105-
// GetVCSConnector gets the vcs connector by id.
108+
// GetVCSConnector gets the vcs connector by full name.
106109
GetVCSConnector(ctx context.Context, name string) (*v1pb.VCSConnector, error)
107110
// CreateVCSConnector creates the vcs connector in a project.
108111
CreateVCSConnector(ctx context.Context, projectName, connectorID string, connector *v1pb.VCSConnector) (*v1pb.VCSConnector, error)
109112
// UpdateVCSConnector updates the vcs connector.
110113
UpdateVCSConnector(ctx context.Context, patch *v1pb.VCSConnector, updateMasks []string) (*v1pb.VCSConnector, error)
111114
// DeleteVCSConnector deletes the vcs provider.
112115
DeleteVCSConnector(ctx context.Context, name string) error
116+
117+
// User
118+
// ListUser list all users.
119+
ListUser(ctx context.Context, showDeleted bool) (*v1pb.ListUsersResponse, error)
120+
// CreateUser creates the user.
121+
CreateUser(ctx context.Context, user *v1pb.User) (*v1pb.User, error)
122+
// GetUser gets the user by name.
123+
GetUser(ctx context.Context, userName string) (*v1pb.User, error)
124+
// UpdateUser updates the user.
125+
UpdateUser(ctx context.Context, patch *v1pb.User, updateMasks []string) (*v1pb.User, error)
126+
// DeleteUser deletes the user by name.
127+
DeleteUser(ctx context.Context, userName string) error
128+
// UndeleteUser undeletes the user by name.
129+
UndeleteUser(ctx context.Context, userName string) (*v1pb.User, error)
113130
}

client/auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
)
1313

1414
// Login will login the user and get the response.
15-
func (c *client) Login() (*v1pb.LoginResponse, error) {
16-
if c.auth.Email == "" || c.auth.Password == "" {
15+
func (c *client) login(request *v1pb.LoginRequest) (*v1pb.LoginResponse, error) {
16+
if request.Email == "" || request.Password == "" {
1717
return nil, errors.Errorf("define username and password")
1818
}
19-
rb, err := protojson.Marshal(c.auth)
19+
rb, err := protojson.Marshal(request)
2020
if err != nil {
2121
return nil, err
2222
}

client/client.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type client struct {
2020
version string
2121
client *http.Client
2222
token string
23-
auth *v1pb.LoginRequest
23+
caller *v1pb.User
2424
}
2525

2626
// NewClient returns the new Bytebase API client.
@@ -31,17 +31,16 @@ func NewClient(url, version, email, password string) (api.Client, error) {
3131
version: version,
3232
}
3333

34-
c.auth = &v1pb.LoginRequest{
34+
response, err := c.login(&v1pb.LoginRequest{
3535
Email: email,
3636
Password: password,
37-
}
38-
39-
ar, err := c.Login()
37+
})
4038
if err != nil {
4139
return nil, err
4240
}
4341

44-
c.token = ar.Token
42+
c.token = response.Token
43+
c.caller = response.User
4544

4645
return &c, nil
4746
}
@@ -68,3 +67,8 @@ func (c *client) doRequest(req *http.Request) ([]byte, error) {
6867

6968
return body, err
7069
}
70+
71+
// GetCaller returns the API caller.
72+
func (c *client) GetCaller() *v1pb.User {
73+
return c.caller
74+
}

client/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
1010
)
1111

12-
// GetDatabase gets the database by the database name.
12+
// GetDatabase gets the database by the database full name.
1313
func (c *client) GetDatabase(ctx context.Context, databaseName string) (*v1pb.Database, error) {
1414
body, err := c.getResource(ctx, databaseName)
1515
if err != nil {

client/environment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (c *client) CreateEnvironment(ctx context.Context, environmentID string, cr
3535
return &env, nil
3636
}
3737

38-
// GetEnvironment gets the environment by id.
38+
// GetEnvironment gets the environment by full name.
3939
func (c *client) GetEnvironment(ctx context.Context, environmentName string) (*v1pb.Environment, error) {
4040
body, err := c.getResource(ctx, environmentName)
4141
if err != nil {

client/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (c *client) ListInstance(ctx context.Context, showDeleted bool) (*v1pb.List
3030
return &res, nil
3131
}
3232

33-
// GetInstance gets the instance by id.
33+
// GetInstance gets the instance by full name.
3434
func (c *client) GetInstance(ctx context.Context, instanceName string) (*v1pb.Instance, error) {
3535
body, err := c.getResource(ctx, instanceName)
3636
if err != nil {

client/project.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"google.golang.org/protobuf/encoding/protojson"
1111
)
1212

13-
// GetProject gets the project by resource id.
13+
// GetProject gets the project by project full name.
1414
func (c *client) GetProject(ctx context.Context, projectName string) (*v1pb.Project, error) {
1515
body, err := c.getResource(ctx, projectName)
1616
if err != nil {
@@ -25,6 +25,49 @@ func (c *client) GetProject(ctx context.Context, projectName string) (*v1pb.Proj
2525
return &res, nil
2626
}
2727

28+
// GetProjectIAMPolicy gets the project IAM policy by project full name.
29+
func (c *client) GetProjectIAMPolicy(ctx context.Context, projectName string) (*v1pb.IamPolicy, error) {
30+
body, err := c.getResource(ctx, fmt.Sprintf("%s:getIamPolicy", projectName))
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
var res v1pb.IamPolicy
36+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
37+
return nil, err
38+
}
39+
40+
return &res, nil
41+
}
42+
43+
// SetProjectIAMPolicy sets the project IAM policy.
44+
func (c *client) SetProjectIAMPolicy(ctx context.Context, projectName string, iamPolicy *v1pb.IamPolicy) (*v1pb.IamPolicy, error) {
45+
payload, err := protojson.Marshal(&v1pb.SetIamPolicyRequest{
46+
Policy: iamPolicy,
47+
})
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/%s:setIamPolicy", c.url, c.version, projectName), strings.NewReader(string(payload)))
53+
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
body, err := c.doRequest(req)
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
var res v1pb.IamPolicy
64+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
65+
return nil, err
66+
}
67+
68+
return &res, nil
69+
}
70+
2871
// ListProject list the projects.
2972
func (c *client) ListProject(ctx context.Context, showDeleted bool) (*v1pb.ListProjectsResponse, error) {
3073
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/projects?showDeleted=%v", c.url, c.version, showDeleted), nil)

client/vcs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (c *client) ListVCSProvider(ctx context.Context) (*v1pb.ListVCSProvidersRes
3030
return &res, nil
3131
}
3232

33-
// GetVCSProvider gets the vcs by id.
33+
// GetVCSProvider gets the vcs by full name.
3434
func (c *client) GetVCSProvider(ctx context.Context, name string) (*v1pb.VCSProvider, error) {
3535
body, err := c.getResource(ctx, name)
3636
if err != nil {
@@ -111,7 +111,7 @@ func (c *client) ListVCSConnector(ctx context.Context, projectName string) (*v1p
111111
return &res, nil
112112
}
113113

114-
// GetVCSConnector gets the vcs connector by id.
114+
// GetVCSConnector gets the vcs connector by full name.
115115
func (c *client) GetVCSConnector(ctx context.Context, name string) (*v1pb.VCSConnector, error) {
116116
body, err := c.getResource(ctx, name)
117117
if err != nil {

docs/data-sources/policy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Optional:
4949
- `database` (String) The database full name in instances/{instance resource id}/databases/{database name} format
5050
- `expire_timestamp` (String) The expiration timestamp in YYYY-MM-DDThh:mm:ss.000Z format
5151
- `masking_level` (String)
52-
- `member` (String) The member in user:{email} format.
52+
- `member` (String) The member in user:{email} or group:{email} format.
5353
- `schema` (String)
5454
- `table` (String)
5555

docs/data-sources/project.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ The project data source.
2121

2222
### Read-Only
2323

24+
- `allow_modify_statement` (Boolean) Allow modifying statement after issue is created.
25+
- `auto_enable_backup` (Boolean) Whether to automatically enable backup.
26+
- `auto_resolve_issue` (Boolean) Enable auto resolve issue.
2427
- `databases` (List of Object) The databases in the project. (see [below for nested schema](#nestedatt--databases))
28+
- `enforce_issue_title` (Boolean) Enforce issue title created by user instead of generated by Bytebase.
2529
- `id` (String) The ID of this resource.
2630
- `key` (String) The project key.
31+
- `members` (Set of Object) The members in the project. (see [below for nested schema](#nestedatt--members))
2732
- `name` (String) The project full name in projects/{resource id} format.
33+
- `postgres_database_tenant_mode` (Boolean) Whether to enable the database tenant mode for PostgreSQL. If enabled, the issue will be created with the pre-appended "set role <db_owner>" statement.
34+
- `skip_backup_errors` (Boolean) Whether to skip backup errors and continue the data migration.
2835
- `title` (String) The project title.
2936
- `workflow` (String) The project workflow.
3037

@@ -41,3 +48,24 @@ Read-Only:
4148
- `sync_state` (String)
4249

4350

51+
<a id="nestedatt--members"></a>
52+
### Nested Schema for `members`
53+
54+
Read-Only:
55+
56+
- `condition` (Set of Object) (see [below for nested schema](#nestedobjatt--members--condition))
57+
- `member` (String)
58+
- `role` (String)
59+
60+
<a id="nestedobjatt--members--condition"></a>
61+
### Nested Schema for `members.condition`
62+
63+
Read-Only:
64+
65+
- `database` (String)
66+
- `expire_timestamp` (String)
67+
- `row_limit` (Number)
68+
- `schema` (String)
69+
- `tables` (Set of String)
70+
71+

0 commit comments

Comments
 (0)