Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: several updates #92

Merged
merged 13 commits into from
Feb 8, 2025
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.13
1.0.14
24 changes: 19 additions & 5 deletions client/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"net/http"
"net/url"
"strings"
"time"

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

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

// ListDatabase list all databases.
func (c *client) ListDatabase(ctx context.Context, instanceID, filter string) ([]*v1pb.Database, error) {
func (c *client) ListDatabase(ctx context.Context, parent, filter string) ([]*v1pb.Database, error) {
res := []*v1pb.Database{}
pageToken := ""
startTime := time.Now()

for {
resp, err := c.listDatabasePerPage(ctx, instanceID, filter, pageToken, 500)
startTimePerPage := time.Now()
resp, err := c.listDatabasePerPage(ctx, parent, filter, pageToken, 500)
if err != nil {
return nil, err
}
res = append(res, resp.Databases...)
tflog.Debug(ctx, "[list database per page]", map[string]interface{}{
"count": len(resp.Databases),
"ms": time.Since(startTimePerPage).Milliseconds(),
})

pageToken = resp.NextPageToken
if pageToken == "" {
break
}
}

tflog.Debug(ctx, "[list database]", map[string]interface{}{
"total": len(res),
"ms": time.Since(startTime).Milliseconds(),
})

return res, nil
}

// listDatabasePerPage list the databases.
func (c *client) listDatabasePerPage(ctx context.Context, instanceID, filter, pageToken string, pageSize int) (*v1pb.ListDatabasesResponse, error) {
func (c *client) listDatabasePerPage(ctx context.Context, parent, filter, pageToken string, pageSize int) (*v1pb.ListDatabasesResponse, error) {
requestURL := fmt.Sprintf(
"%s/%s/instances/%s/databases?filter=%s&page_size=%d&page_token=%s",
"%s/%s/%s/databases?filter=%s&page_size=%d&page_token=%s",
c.url,
c.version,
instanceID,
parent,
url.QueryEscape(filter),
pageSize,
pageToken,
Expand Down
2 changes: 1 addition & 1 deletion examples/environments/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.9"
version = "1.0.14"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/groups/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.9"
version = "1.0.14"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/instances/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.9"
version = "1.0.14"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
10 changes: 9 additions & 1 deletion examples/policies/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.9"
version = "1.0.14"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand All @@ -25,3 +25,11 @@ data "bytebase_policy" "masking_exception_policy" {
output "masking_exception_policy" {
value = data.bytebase_policy.masking_exception_policy
}

data "bytebase_policy" "global_masking_policy" {
type = "MASKING_RULE"
}

output "global_masking_policy" {
value = data.bytebase_policy.global_masking_policy
}
2 changes: 1 addition & 1 deletion examples/projects/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.9"
version = "1.0.14"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/settings/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.9"
version = "1.0.14"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
25 changes: 25 additions & 0 deletions examples/setup/data_masking.tf
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,28 @@ resource "bytebase_policy" "masking_exception_policy" {
}
}
}

resource "bytebase_policy" "global_masking_policy" {
depends_on = [
bytebase_instance.prod,
bytebase_environment.test
]

parent = ""
type = "MASKING_RULE"
enforce = true
inherit_from_parent = false

global_masking_policy {
rules {
condition = "environment_id in [\"test\"]"
id = "69df1d15-abe5-4bc9-be38-f2a4bef3f7e0"
semantic_type = "bb.default-partial"
}
rules {
condition = "instance_id in [\"prod-sample-instance\"]"
id = "90adb734-0808-4c9f-b281-1f76f7a1a29a"
semantic_type = "bb.default"
}
}
}
2 changes: 1 addition & 1 deletion examples/setup/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.9"
version = "1.0.14"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
10 changes: 9 additions & 1 deletion examples/setup/project.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ resource "bytebase_project" "sample_project" {
depends_on = [
bytebase_user.workspace_dba,
bytebase_user.project_developer,
bytebase_group.developers
bytebase_group.developers,
bytebase_instance.prod
]

resource_id = local.project_id
title = "Sample project"
key = "SAMM"

dynamic "databases" {
for_each = bytebase_instance.prod.databases
content {
name = databases.value.name
}
}

members {
member = format("user:%s", bytebase_user.workspace_dba.email)
role = "roles/projectOwner"
Expand Down
4 changes: 4 additions & 0 deletions examples/setup/users.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ resource "bytebase_user" "workspace_dba" {

# Create or update the user.
resource "bytebase_user" "project_developer" {
depends_on = [
bytebase_user.workspace_dba
]

title = "Developer"
email = "[email protected]"

Expand Down
2 changes: 1 addition & 1 deletion examples/users/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.9"
version = "1.0.14"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/vcs/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
bytebase = {
version = "1.0.9"
version = "1.0.14"
# For local development, please use "terraform.local/bytebase/bytebase" instead
source = "registry.terraform.io/bytebase/bytebase"
}
Expand Down
8 changes: 1 addition & 7 deletions provider/data_source_database_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,8 @@ func setDatabaseCatalog(d *schema.ResourceData, catalog *v1pb.DatabaseCatalog) d
}

func columnHash(rawColumn interface{}) string {
var buf bytes.Buffer
column := rawColumn.(map[string]interface{})

if v, ok := column["name"].(string); ok {
_, _ = buf.WriteString(fmt.Sprintf("%s-", v))
}

return buf.String()
return column["name"].(string)
}

func tableHash(rawTable interface{}) string {
Expand Down
4 changes: 2 additions & 2 deletions provider/data_source_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

func dataSourceInstance() *schema.Resource {
return &schema.Resource{
Description: "The instance data source.",
ReadContext: dataSourceInstanceRead,
Description: "The instance data source.",
ReadWithoutTimeout: dataSourceInstanceRead,
Schema: map[string]*schema.Schema{
"resource_id": {
Type: schema.TypeString,
Expand Down
6 changes: 3 additions & 3 deletions provider/data_source_instance_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

func dataSourceInstanceList() *schema.Resource {
return &schema.Resource{
Description: "The instance data source list.",
ReadContext: dataSourceInstanceListRead,
Description: "The instance data source list.",
ReadWithoutTimeout: dataSourceInstanceListRead,
Schema: map[string]*schema.Schema{
"show_deleted": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -182,7 +182,7 @@ func dataSourceInstanceListRead(ctx context.Context, d *schema.ResourceData, m i
}
ins["data_sources"] = schema.NewSet(dataSourceHash, dataSources)

databases, err := c.ListDatabase(ctx, instanceID, "")
databases, err := c.ListDatabase(ctx, instance.Name, "")
if err != nil {
return diag.FromErr(err)
}
Expand Down
80 changes: 80 additions & 0 deletions provider/data_source_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func dataSourcePolicy() *schema.Resource {
Required: true,
ValidateFunc: validation.StringInSlice([]string{
v1pb.PolicyType_MASKING_EXCEPTION.String(),
v1pb.PolicyType_MASKING_RULE.String(),
}, false),
Description: "The policy type.",
},
Expand All @@ -65,6 +66,7 @@ func dataSourcePolicy() *schema.Resource {
Description: "Decide if the policy is enforced.",
},
"masking_exception_policy": getMaskingExceptionPolicySchema(true),
"global_masking_policy": getGlobalMaskingPolicySchema(true),
},
}
}
Expand Down Expand Up @@ -142,6 +144,53 @@ func getMaskingExceptionPolicySchema(computed bool) *schema.Schema {
}
}

func getGlobalMaskingPolicySchema(computed bool) *schema.Schema {
return &schema.Schema{
Computed: computed,
Optional: true,
Default: nil,
Type: schema.TypeList,
MinItems: 0,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"rules": {
Computed: computed,
Optional: true,
Default: nil,
MinItems: 0,
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: computed,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The unique rule id",
},
"semantic_type": {
Type: schema.TypeString,
Computed: computed,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The semantic type id",
},
"condition": {
Type: schema.TypeString,
Computed: computed,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The condition expression",
},
},
},
},
},
},
}
}

func dataSourcePolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(api.Client)

Expand Down Expand Up @@ -183,9 +232,40 @@ func setPolicyMessage(d *schema.ResourceData, policy *v1pb.Policy) diag.Diagnost
}
}

if p := policy.GetMaskingRulePolicy(); p != nil {
maskingPolicy, err := flattenGlobalMaskingPolicy(p)
if err != nil {
return diag.FromErr(err)
}
if err := d.Set("global_masking_policy", maskingPolicy); err != nil {
return diag.Errorf("cannot set global_masking_policy: %s", err.Error())
}
}

return nil
}

func flattenGlobalMaskingPolicy(p *v1pb.MaskingRulePolicy) ([]interface{}, error) {
ruleList := []interface{}{}

for _, rule := range p.Rules {
if rule.Condition == nil || rule.Condition.Expression == "" {
return nil, errors.Errorf("invalid global masking policy condition")
}
raw := map[string]interface{}{}
raw["id"] = rule.Id
raw["semantic_type"] = rule.SemanticType
raw["condition"] = rule.Condition.Expression

ruleList = append(ruleList, raw)
}

policy := map[string]interface{}{
"rules": ruleList,
}
return []interface{}{policy}, nil
}

func flattenMaskingExceptionPolicy(p *v1pb.MaskingExceptionPolicy) ([]interface{}, error) {
exceptionList := []interface{}{}
for _, exception := range p.MaskingExceptions {
Expand Down
Loading