Skip to content

Commit 3177fcd

Browse files
authored
chore: several updates (#92)
* chore: use set for unordered collection * chore: update doc * fix: list all databases in the project * chore: update * fix: lint * chore: add databases for instance * chore: more error log * chore: update * chore: update
1 parent 2ded883 commit 3177fcd

28 files changed

+365
-121
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.13
1+
1.0.14

client/database.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"net/http"
77
"net/url"
88
"strings"
9+
"time"
910

1011
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
12+
"github.com/hashicorp/terraform-plugin-log/tflog"
1113
"google.golang.org/protobuf/encoding/protojson"
1214
)
1315

@@ -27,32 +29,44 @@ func (c *client) GetDatabase(ctx context.Context, databaseName string) (*v1pb.Da
2729
}
2830

2931
// ListDatabase list all databases.
30-
func (c *client) ListDatabase(ctx context.Context, instanceID, filter string) ([]*v1pb.Database, error) {
32+
func (c *client) ListDatabase(ctx context.Context, parent, filter string) ([]*v1pb.Database, error) {
3133
res := []*v1pb.Database{}
3234
pageToken := ""
35+
startTime := time.Now()
3336

3437
for {
35-
resp, err := c.listDatabasePerPage(ctx, instanceID, filter, pageToken, 500)
38+
startTimePerPage := time.Now()
39+
resp, err := c.listDatabasePerPage(ctx, parent, filter, pageToken, 500)
3640
if err != nil {
3741
return nil, err
3842
}
3943
res = append(res, resp.Databases...)
44+
tflog.Debug(ctx, "[list database per page]", map[string]interface{}{
45+
"count": len(resp.Databases),
46+
"ms": time.Since(startTimePerPage).Milliseconds(),
47+
})
48+
4049
pageToken = resp.NextPageToken
4150
if pageToken == "" {
4251
break
4352
}
4453
}
4554

55+
tflog.Debug(ctx, "[list database]", map[string]interface{}{
56+
"total": len(res),
57+
"ms": time.Since(startTime).Milliseconds(),
58+
})
59+
4660
return res, nil
4761
}
4862

4963
// listDatabasePerPage list the databases.
50-
func (c *client) listDatabasePerPage(ctx context.Context, instanceID, filter, pageToken string, pageSize int) (*v1pb.ListDatabasesResponse, error) {
64+
func (c *client) listDatabasePerPage(ctx context.Context, parent, filter, pageToken string, pageSize int) (*v1pb.ListDatabasesResponse, error) {
5165
requestURL := fmt.Sprintf(
52-
"%s/%s/instances/%s/databases?filter=%s&page_size=%d&page_token=%s",
66+
"%s/%s/%s/databases?filter=%s&page_size=%d&page_token=%s",
5367
c.url,
5468
c.version,
55-
instanceID,
69+
parent,
5670
url.QueryEscape(filter),
5771
pageSize,
5872
pageToken,

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.9"
5+
version = "1.0.14"
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.9"
4+
version = "1.0.14"
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.9"
5+
version = "1.0.14"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

examples/policies/main.tf

Lines changed: 9 additions & 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.9"
4+
version = "1.0.14"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}
@@ -25,3 +25,11 @@ data "bytebase_policy" "masking_exception_policy" {
2525
output "masking_exception_policy" {
2626
value = data.bytebase_policy.masking_exception_policy
2727
}
28+
29+
data "bytebase_policy" "global_masking_policy" {
30+
type = "MASKING_RULE"
31+
}
32+
33+
output "global_masking_policy" {
34+
value = data.bytebase_policy.global_masking_policy
35+
}

examples/projects/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.9"
5+
version = "1.0.14"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

examples/settings/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.9"
4+
version = "1.0.14"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

examples/setup/data_masking.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,28 @@ resource "bytebase_policy" "masking_exception_policy" {
9696
}
9797
}
9898
}
99+
100+
resource "bytebase_policy" "global_masking_policy" {
101+
depends_on = [
102+
bytebase_instance.prod,
103+
bytebase_environment.test
104+
]
105+
106+
parent = ""
107+
type = "MASKING_RULE"
108+
enforce = true
109+
inherit_from_parent = false
110+
111+
global_masking_policy {
112+
rules {
113+
condition = "environment_id in [\"test\"]"
114+
id = "69df1d15-abe5-4bc9-be38-f2a4bef3f7e0"
115+
semantic_type = "bb.default-partial"
116+
}
117+
rules {
118+
condition = "instance_id in [\"prod-sample-instance\"]"
119+
id = "90adb734-0808-4c9f-b281-1f76f7a1a29a"
120+
semantic_type = "bb.default"
121+
}
122+
}
123+
}

examples/setup/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.9"
4+
version = "1.0.14"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

examples/setup/project.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@ resource "bytebase_project" "sample_project" {
33
depends_on = [
44
bytebase_user.workspace_dba,
55
bytebase_user.project_developer,
6-
bytebase_group.developers
6+
bytebase_group.developers,
7+
bytebase_instance.prod
78
]
89

910
resource_id = local.project_id
1011
title = "Sample project"
1112
key = "SAMM"
1213

14+
dynamic "databases" {
15+
for_each = bytebase_instance.prod.databases
16+
content {
17+
name = databases.value.name
18+
}
19+
}
20+
1321
members {
1422
member = format("user:%s", bytebase_user.workspace_dba.email)
1523
role = "roles/projectOwner"

examples/setup/users.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ resource "bytebase_user" "workspace_dba" {
99

1010
# Create or update the user.
1111
resource "bytebase_user" "project_developer" {
12+
depends_on = [
13+
bytebase_user.workspace_dba
14+
]
15+
1216
title = "Developer"
1317
email = "developer@bytebase.com"
1418

examples/users/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.9"
4+
version = "1.0.14"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

examples/vcs/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.9"
4+
version = "1.0.14"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

provider/data_source_database_catalog.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,8 @@ func setDatabaseCatalog(d *schema.ResourceData, catalog *v1pb.DatabaseCatalog) d
152152
}
153153

154154
func columnHash(rawColumn interface{}) string {
155-
var buf bytes.Buffer
156155
column := rawColumn.(map[string]interface{})
157-
158-
if v, ok := column["name"].(string); ok {
159-
_, _ = buf.WriteString(fmt.Sprintf("%s-", v))
160-
}
161-
162-
return buf.String()
156+
return column["name"].(string)
163157
}
164158

165159
func tableHash(rawTable interface{}) string {

provider/data_source_instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313

1414
func dataSourceInstance() *schema.Resource {
1515
return &schema.Resource{
16-
Description: "The instance data source.",
17-
ReadContext: dataSourceInstanceRead,
16+
Description: "The instance data source.",
17+
ReadWithoutTimeout: dataSourceInstanceRead,
1818
Schema: map[string]*schema.Schema{
1919
"resource_id": {
2020
Type: schema.TypeString,

provider/data_source_instance_list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414

1515
func dataSourceInstanceList() *schema.Resource {
1616
return &schema.Resource{
17-
Description: "The instance data source list.",
18-
ReadContext: dataSourceInstanceListRead,
17+
Description: "The instance data source list.",
18+
ReadWithoutTimeout: dataSourceInstanceListRead,
1919
Schema: map[string]*schema.Schema{
2020
"show_deleted": {
2121
Type: schema.TypeBool,
@@ -182,7 +182,7 @@ func dataSourceInstanceListRead(ctx context.Context, d *schema.ResourceData, m i
182182
}
183183
ins["data_sources"] = schema.NewSet(dataSourceHash, dataSources)
184184

185-
databases, err := c.ListDatabase(ctx, instanceID, "")
185+
databases, err := c.ListDatabase(ctx, instance.Name, "")
186186
if err != nil {
187187
return diag.FromErr(err)
188188
}

provider/data_source_policy.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func dataSourcePolicy() *schema.Resource {
4646
Required: true,
4747
ValidateFunc: validation.StringInSlice([]string{
4848
v1pb.PolicyType_MASKING_EXCEPTION.String(),
49+
v1pb.PolicyType_MASKING_RULE.String(),
4950
}, false),
5051
Description: "The policy type.",
5152
},
@@ -65,6 +66,7 @@ func dataSourcePolicy() *schema.Resource {
6566
Description: "Decide if the policy is enforced.",
6667
},
6768
"masking_exception_policy": getMaskingExceptionPolicySchema(true),
69+
"global_masking_policy": getGlobalMaskingPolicySchema(true),
6870
},
6971
}
7072
}
@@ -142,6 +144,53 @@ func getMaskingExceptionPolicySchema(computed bool) *schema.Schema {
142144
}
143145
}
144146

147+
func getGlobalMaskingPolicySchema(computed bool) *schema.Schema {
148+
return &schema.Schema{
149+
Computed: computed,
150+
Optional: true,
151+
Default: nil,
152+
Type: schema.TypeList,
153+
MinItems: 0,
154+
MaxItems: 1,
155+
Elem: &schema.Resource{
156+
Schema: map[string]*schema.Schema{
157+
"rules": {
158+
Computed: computed,
159+
Optional: true,
160+
Default: nil,
161+
MinItems: 0,
162+
Type: schema.TypeList,
163+
Elem: &schema.Resource{
164+
Schema: map[string]*schema.Schema{
165+
"id": {
166+
Type: schema.TypeString,
167+
Computed: computed,
168+
Optional: true,
169+
ValidateFunc: validation.StringIsNotEmpty,
170+
Description: "The unique rule id",
171+
},
172+
"semantic_type": {
173+
Type: schema.TypeString,
174+
Computed: computed,
175+
Optional: true,
176+
ValidateFunc: validation.StringIsNotEmpty,
177+
Description: "The semantic type id",
178+
},
179+
"condition": {
180+
Type: schema.TypeString,
181+
Computed: computed,
182+
Optional: true,
183+
ValidateFunc: validation.StringIsNotEmpty,
184+
Description: "The condition expression",
185+
},
186+
},
187+
},
188+
},
189+
},
190+
},
191+
}
192+
}
193+
145194
func dataSourcePolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
146195
c := m.(api.Client)
147196

@@ -183,9 +232,40 @@ func setPolicyMessage(d *schema.ResourceData, policy *v1pb.Policy) diag.Diagnost
183232
}
184233
}
185234

235+
if p := policy.GetMaskingRulePolicy(); p != nil {
236+
maskingPolicy, err := flattenGlobalMaskingPolicy(p)
237+
if err != nil {
238+
return diag.FromErr(err)
239+
}
240+
if err := d.Set("global_masking_policy", maskingPolicy); err != nil {
241+
return diag.Errorf("cannot set global_masking_policy: %s", err.Error())
242+
}
243+
}
244+
186245
return nil
187246
}
188247

248+
func flattenGlobalMaskingPolicy(p *v1pb.MaskingRulePolicy) ([]interface{}, error) {
249+
ruleList := []interface{}{}
250+
251+
for _, rule := range p.Rules {
252+
if rule.Condition == nil || rule.Condition.Expression == "" {
253+
return nil, errors.Errorf("invalid global masking policy condition")
254+
}
255+
raw := map[string]interface{}{}
256+
raw["id"] = rule.Id
257+
raw["semantic_type"] = rule.SemanticType
258+
raw["condition"] = rule.Condition.Expression
259+
260+
ruleList = append(ruleList, raw)
261+
}
262+
263+
policy := map[string]interface{}{
264+
"rules": ruleList,
265+
}
266+
return []interface{}{policy}, nil
267+
}
268+
189269
func flattenMaskingExceptionPolicy(p *v1pb.MaskingExceptionPolicy) ([]interface{}, error) {
190270
exceptionList := []interface{}{}
191271
for _, exception := range p.MaskingExceptions {

0 commit comments

Comments
 (0)