From 302be83b2f9bb131380b1abe1b63dba131cab731 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Wed, 29 Jul 2020 20:34:55 +0000 Subject: [PATCH] Fix bug: diff being detected for source_repo_repository even when there are no changes (#3786) This patch fixes a bug in source_repo_repository where a diff was always being generated for the TypeSet field `pubsub_configs` since its Set hashing function was not accounting for the fact that `pubsub_configs[].topic` can contain either a topic's name or relative path. Signed-off-by: Modular Magician --- .changelog/3786.txt | 3 +++ google/resource_source_repo_repository.go | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 .changelog/3786.txt diff --git a/.changelog/3786.txt b/.changelog/3786.txt new file mode 100644 index 00000000000..94427bbbacf --- /dev/null +++ b/.changelog/3786.txt @@ -0,0 +1,3 @@ +```release-note:bug +sourcerepo: fixed perma-diff in `google_sourcerepo_repository` +``` diff --git a/google/resource_source_repo_repository.go b/google/resource_source_repo_repository.go index 9f963d36823..721eff7983a 100644 --- a/google/resource_source_repo_repository.go +++ b/google/resource_source_repo_repository.go @@ -15,6 +15,7 @@ package google import ( + "bytes" "fmt" "log" "reflect" @@ -22,10 +23,28 @@ import ( "strings" "time" + "github.com/hashicorp/terraform-plugin-sdk/helper/hashcode" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/helper/validation" ) +func resourceSourceRepoRepositoryPubSubConfigsHash(v interface{}) int { + if v == nil { + return 0 + } + + var buf bytes.Buffer + m := v.(map[string]interface{}) + + buf.WriteString(fmt.Sprintf("%s-", GetResourceNameFromSelfLink(m["topic"].(string)))) + buf.WriteString(fmt.Sprintf("%s-", m["message_format"].(string))) + if v, ok := m["service_account_email"]; ok { + buf.WriteString(fmt.Sprintf("%s-", v.(string))) + } + + return hashcode.String(buf.String()) +} + func resourceSourceRepoRepository() *schema.Resource { return &schema.Resource{ Create: resourceSourceRepoRepositoryCreate, @@ -81,6 +100,7 @@ If unspecified, it defaults to the compute engine default service account.`, }, }, }, + Set: resourceSourceRepoRepositoryPubSubConfigsHash, }, "size": { Type: schema.TypeInt,