From acb73ca2ac7a60241cd5d5fb4c96ee741b102c89 Mon Sep 17 00:00:00 2001 From: Jazel Canseco Date: Wed, 29 Jul 2020 13:29:18 -0700 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. --- products/sourcerepo/terraform.yaml | 2 ++ .../constants/source_repo_repository.go.erb | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 templates/terraform/constants/source_repo_repository.go.erb diff --git a/products/sourcerepo/terraform.yaml b/products/sourcerepo/terraform.yaml index 688c25f64cb7..31aa0f98a20a 100644 --- a/products/sourcerepo/terraform.yaml +++ b/products/sourcerepo/terraform.yaml @@ -49,9 +49,11 @@ overrides: !ruby/object:Overrides::ResourceOverrides A Cloud Pub/Sub topic in this repo's project. Values are of the form `projects//topics/` or `` (where the topic will be inferred). + set_hash_func: 'resourceSourceRepoRepositoryPubSubConfigsHash' pubsubConfigs.serviceAccountEmail: !ruby/object:Overrides::Terraform::PropertyOverride default_from_api: true custom_code: !ruby/object:Provider::Terraform::CustomCode + constants: templates/terraform/constants/source_repo_repository.go.erb update_encoder: templates/terraform/update_encoder/source_repo_repository.erb post_create: templates/terraform/post_create/source_repo_repository_update.go.erb # This is for copying files over diff --git a/templates/terraform/constants/source_repo_repository.go.erb b/templates/terraform/constants/source_repo_repository.go.erb new file mode 100644 index 000000000000..d523a7869673 --- /dev/null +++ b/templates/terraform/constants/source_repo_repository.go.erb @@ -0,0 +1,30 @@ +<%- # the license inside this block applies to this file + # Copyright 2019 Google Inc. + # Licensed under the Apache License, Version 2.0 (the "License"); + # you may not use this file except in compliance with the License. + # You may obtain a copy of the License at + # + # http://www.apache.org/licenses/LICENSE-2.0 + # + # Unless required by applicable law or agreed to in writing, software + # distributed under the License is distributed on an "AS IS" BASIS, + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + # See the License for the specific language governing permissions and + # limitations under the License. +-%> +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()) +}