Skip to content

Commit 4d14d0e

Browse files
committed
feat: support custom roles
1 parent 77c25b1 commit 4d14d0e

File tree

24 files changed

+724
-11
lines changed

24 files changed

+724
-11
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.17
1+
1.0.18

api/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ type Client interface {
134134
// UndeleteUser undeletes the user by name.
135135
UndeleteUser(ctx context.Context, userName string) (*v1pb.User, error)
136136

137+
// Role
138+
// ListRole will returns all roles.
139+
ListRole(ctx context.Context) (*v1pb.ListRolesResponse, error)
140+
// DeleteRole deletes the role by name.
141+
DeleteRole(ctx context.Context, name string) error
142+
// CreateRole creates the role.
143+
CreateRole(ctx context.Context, roleID string, role *v1pb.Role) (*v1pb.Role, error)
144+
// GetRole gets the role by full name.
145+
GetRole(ctx context.Context, name string) (*v1pb.Role, error)
146+
// UpdateRole updates the role.
147+
UpdateRole(ctx context.Context, patch *v1pb.Role, updateMasks []string) (*v1pb.Role, error)
148+
137149
// Group
138150
// ListGroup list all groups.
139151
ListGroup(ctx context.Context) (*v1pb.ListGroupsResponse, error)

client/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (c *client) listDatabasePerPage(ctx context.Context, parent, filter, pageTo
6969
parent,
7070
url.QueryEscape(filter),
7171
pageSize,
72-
pageToken,
72+
url.QueryEscape(pageToken),
7373
)
7474

7575
req, err := http.NewRequestWithContext(ctx, "GET", requestURL, nil)

client/role.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
9+
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
10+
"google.golang.org/protobuf/encoding/protojson"
11+
)
12+
13+
// GetRole gets the role by full name.
14+
func (c *client) GetRole(ctx context.Context, name string) (*v1pb.Role, error) {
15+
body, err := c.getResource(ctx, name)
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
var res v1pb.Role
21+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
22+
return nil, err
23+
}
24+
25+
return &res, nil
26+
}
27+
28+
// CreateRole creates the role.
29+
func (c *client) CreateRole(ctx context.Context, roleID string, role *v1pb.Role) (*v1pb.Role, error) {
30+
payload, err := protojson.Marshal(role)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/roles?roleId=%s", c.url, c.version, roleID), strings.NewReader(string(payload)))
36+
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
body, err := c.doRequest(req)
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
var res v1pb.Role
47+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
48+
return nil, err
49+
}
50+
51+
return &res, nil
52+
}
53+
54+
// DeleteRole deletes the role by name.
55+
func (c *client) DeleteRole(ctx context.Context, name string) error {
56+
return c.deleteResource(ctx, name)
57+
}
58+
59+
// UpdateRole updates the role.
60+
func (c *client) UpdateRole(ctx context.Context, patch *v1pb.Role, updateMasks []string) (*v1pb.Role, error) {
61+
body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, false /* allow missing = false*/)
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
var res v1pb.Role
67+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
68+
return nil, err
69+
}
70+
71+
return &res, nil
72+
}
73+
74+
// ListRole will returns all roles.
75+
func (c *client) ListRole(ctx context.Context) (*v1pb.ListRolesResponse, error) {
76+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/roles", c.url, c.version), nil)
77+
if err != nil {
78+
return nil, err
79+
}
80+
81+
body, err := c.doRequest(req)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
var res v1pb.ListRolesResponse
87+
if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil {
88+
return nil, err
89+
}
90+
91+
return &res, nil
92+
}

docs/data-sources/role.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "bytebase_role Data Source - terraform-provider-bytebase"
4+
subcategory: ""
5+
description: |-
6+
The role data source.
7+
---
8+
9+
# bytebase_role (Data Source)
10+
11+
The role data source.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `resource_id` (String) The role unique resource id.
21+
22+
### Read-Only
23+
24+
- `description` (String) The role description.
25+
- `id` (String) The ID of this resource.
26+
- `name` (String) The role full name in roles/{resource id} format.
27+
- `permissions` (Set of String) The role permissions.
28+
- `title` (String) The role title.
29+
- `type` (String) The role type.
30+
31+

docs/data-sources/role_list.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "bytebase_role_list Data Source - terraform-provider-bytebase"
4+
subcategory: ""
5+
description: |-
6+
The role data source list.
7+
---
8+
9+
# bytebase_role_list (Data Source)
10+
11+
The role data source list.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Read-Only
19+
20+
- `id` (String) The ID of this resource.
21+
- `roles` (List of Object) (see [below for nested schema](#nestedatt--roles))
22+
23+
<a id="nestedatt--roles"></a>
24+
### Nested Schema for `roles`
25+
26+
Read-Only:
27+
28+
- `description` (String)
29+
- `name` (String)
30+
- `permissions` (Set of String)
31+
- `resource_id` (String)
32+
- `title` (String)
33+
- `type` (String)
34+
35+

docs/resources/role.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "bytebase_role Resource - terraform-provider-bytebase"
4+
subcategory: ""
5+
description: |-
6+
The role resource. Require ENTERPRISE subscription. Check the docs https://www.bytebase.com/docs/administration/custom-roles/?source=terraform for more information.
7+
---
8+
9+
# bytebase_role (Resource)
10+
11+
The role resource. Require ENTERPRISE subscription. Check the docs https://www.bytebase.com/docs/administration/custom-roles/?source=terraform for more information.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `permissions` (Set of String) The role permissions. All permissions should start with "bb." prefix.
21+
- `resource_id` (String) The role unique resource id.
22+
- `title` (String) The role title.
23+
24+
### Optional
25+
26+
- `description` (String) The role description.
27+
28+
### Read-Only
29+
30+
- `id` (String) The ID of this resource.
31+
- `name` (String) The role full name in roles/{resource id} format.
32+
- `type` (String) The role type.
33+
34+

examples/environments/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
terraform {
33
required_providers {
44
bytebase = {
5-
version = "1.0.17"
5+
version = "1.0.18"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

examples/groups/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
bytebase = {
4-
version = "1.0.17"
4+
version = "1.0.18"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

examples/instances/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
terraform {
33
required_providers {
44
bytebase = {
5-
version = "1.0.17"
5+
version = "1.0.18"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

0 commit comments

Comments
 (0)