From 48b76966680265b545f4dacfdb605e0d0fd0a8ad Mon Sep 17 00:00:00 2001 From: ecmadao Date: Wed, 25 Jun 2025 09:46:40 +0800 Subject: [PATCH] fix: hash --- provider/data_source_database.go | 21 +++++++++++++- provider/resource_instance.go | 44 +++++++++++++++++++++--------- provider/resource_review_config.go | 6 ++++ 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/provider/data_source_database.go b/provider/data_source_database.go index 36eb09e..d419cfe 100644 --- a/provider/data_source_database.go +++ b/provider/data_source_database.go @@ -144,8 +144,24 @@ func dataSourceDatabaseRead(ctx context.Context, d *schema.ResourceData, m inter } func columnHash(rawColumn interface{}) string { + var buf bytes.Buffer column := rawColumn.(map[string]interface{}) - return column["name"].(string) + + if v, ok := column["name"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := column["semantic_type"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := column["classification"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := column["classification"].(map[string]interface{}); ok { + for key, val := range v { + _, _ = buf.WriteString(fmt.Sprintf("[%s:%s]-", key, val.(string))) + } + } + return buf.String() } func tableHash(rawTable interface{}) string { @@ -155,6 +171,9 @@ func tableHash(rawTable interface{}) string { if v, ok := table["name"].(string); ok { _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) } + if v, ok := table["classification"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) + } if columns, ok := table["columns"].(*schema.Set); ok { for _, column := range columns.List() { rawColumn := column.(map[string]interface{}) diff --git a/provider/resource_instance.go b/provider/resource_instance.go index ad76bcc..dfbfd32 100644 --- a/provider/resource_instance.go +++ b/provider/resource_instance.go @@ -1,6 +1,7 @@ package provider import ( + "bytes" "context" "fmt" "regexp" @@ -743,27 +744,44 @@ func flattenDataSourceList(d *schema.ResourceData, dataSourceList []*v1pb.DataSo } func dataSourceHash(rawDataSource interface{}) int { - dataSource := rawDataSource.(map[string]interface{}) - // Include id and SSL-related field presence to detect configuration changes - hashStr := dataSource["id"].(string) + var buf bytes.Buffer + raw := rawDataSource.(map[string]interface{}) - // Include use_ssl in hash to detect SSL enablement changes - if v, ok := dataSource["use_ssl"].(bool); ok { - hashStr = fmt.Sprintf("%s-ssl_%t", hashStr, v) + if v, ok := raw["id"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := raw["username"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := raw["password"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := raw["host"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := raw["port"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := raw["database"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) } + // Include use_ssl in hash to detect SSL enablement changes + if v, ok := raw["use_ssl"].(bool); ok { + _, _ = buf.WriteString(fmt.Sprintf("ssl_%v-", v)) + } // Include whether SSL certificates are present (not the values themselves) - if v, ok := dataSource["ssl_ca"].(string); ok && v != "" { - hashStr = fmt.Sprintf("%s-ca_present", hashStr) + if v, ok := raw["ssl_ca"].(string); ok && v != "" { + _, _ = buf.WriteString(fmt.Sprintf("ca_present_%s-", v)) } - if v, ok := dataSource["ssl_cert"].(string); ok && v != "" { - hashStr = fmt.Sprintf("%s-cert_present", hashStr) + if v, ok := raw["ssl_cert"].(string); ok && v != "" { + _, _ = buf.WriteString(fmt.Sprintf("cert_present_%s-", v)) } - if v, ok := dataSource["ssl_key"].(string); ok && v != "" { - hashStr = fmt.Sprintf("%s-key_present", hashStr) + if v, ok := raw["ssl_key"].(string); ok && v != "" { + _, _ = buf.WriteString(fmt.Sprintf("key_present_%s-", v)) } - return internal.ToHashcodeInt(hashStr) + return internal.ToHashcodeInt(buf.String()) } func convertDataSourceCreateList(d *schema.ResourceData, validate bool) ([]*v1pb.DataSource, error) { diff --git a/provider/resource_review_config.go b/provider/resource_review_config.go index 6df8c2b..3125d5c 100644 --- a/provider/resource_review_config.go +++ b/provider/resource_review_config.go @@ -239,6 +239,12 @@ func reviewRuleHash(rawRule interface{}) int { if v, ok := raw["engine"].(string); ok { _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) } + if v, ok := raw["payload"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := raw["level"].(string); ok { + _, _ = buf.WriteString(fmt.Sprintf("%s-", v)) + } return internal.ToHashcodeInt(buf.String()) }