From 9988136d1c8e3ef6dfb0506fe78fc07cd4a0097f Mon Sep 17 00:00:00 2001 From: Jared Date: Thu, 30 Nov 2023 17:58:48 -0800 Subject: [PATCH] fixed artifact_registry_repository permadiff in unordered array (#9376) * fixed permadiff in unordered array * changed permadiff fix strategy for upstream policies * removed no-op field * change diff semantics to use schema.Set * updated virtual example with intentional out-of-order priorities * update virtual policy comparison to not panic on bad cast (if possible) --- .../products/artifactregistry/Repository.yaml | 2 + .../artifact_registry_repository.go.erb | 47 +++++++++++++++++++ ...rtifact_registry_repository_virtual.tf.erb | 24 +++++++--- 3 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 mmv1/templates/terraform/constants/artifact_registry_repository.go.erb diff --git a/mmv1/products/artifactregistry/Repository.yaml b/mmv1/products/artifactregistry/Repository.yaml index 607387e85bf7..aaac06a984c4 100644 --- a/mmv1/products/artifactregistry/Repository.yaml +++ b/mmv1/products/artifactregistry/Repository.yaml @@ -112,6 +112,7 @@ examples: username: 'remote-username' secret_data: 'remote-password' custom_code: !ruby/object:Provider::Terraform::CustomCode + constants: templates/terraform/constants/artifact_registry_repository.go.erb encoder: templates/terraform/encoders/location_from_region.go.erb properties: - !ruby/object:Api::Type::String @@ -231,6 +232,7 @@ properties: properties: - !ruby/object:Api::Type::Array name: 'upstreamPolicies' + diff_suppress_func: 'upstreamPoliciesDiffSuppress' description: |- Policies that configure the upstream artifacts distributed by the Virtual Repository. Upstream policies cannot be set on a standard repository. diff --git a/mmv1/templates/terraform/constants/artifact_registry_repository.go.erb b/mmv1/templates/terraform/constants/artifact_registry_repository.go.erb new file mode 100644 index 000000000000..52e5b587d59c --- /dev/null +++ b/mmv1/templates/terraform/constants/artifact_registry_repository.go.erb @@ -0,0 +1,47 @@ +<%# The license inside this block applies to this file + # Copyright 2023 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 upstreamPoliciesDiffSuppress(k, old, new string, d *schema.ResourceData) bool { + o, n := d.GetChange("virtual_repository_config.0.upstream_policies") + oldPolicies, ok := o.([]any) + if !ok { + return false + } + newPolicies, ok := n.([]any) + if !ok { + return false + } + + var oldHashes, newHashes []interface{} + for _, policy := range oldPolicies { + data, ok := policy.(map[string]any) + if !ok { + return false + } + hashStr := fmt.Sprintf("[id:%v priority:%v repository:%v]", data["id"], data["priority"], data["repository"]) + oldHashes = append(oldHashes, hashStr) + } + for _, policy := range newPolicies { + data, ok := policy.(map[string]any) + if !ok { + return false + } + hashStr := fmt.Sprintf("[id:%v priority:%v repository:%v]", data["id"], data["priority"], data["repository"]) + newHashes = append(newHashes, hashStr) + } + + oldSet := schema.NewSet(schema.HashString, oldHashes) + newSet := schema.NewSet(schema.HashString, newHashes) + return oldSet.Equal(newSet) +} diff --git a/mmv1/templates/terraform/examples/artifact_registry_repository_virtual.tf.erb b/mmv1/templates/terraform/examples/artifact_registry_repository_virtual.tf.erb index b197d33709bc..4d20c408b80b 100644 --- a/mmv1/templates/terraform/examples/artifact_registry_repository_virtual.tf.erb +++ b/mmv1/templates/terraform/examples/artifact_registry_repository_virtual.tf.erb @@ -1,7 +1,14 @@ -resource "google_artifact_registry_repository" "<%= ctx[:primary_resource_id] %>-upstream" { +resource "google_artifact_registry_repository" "<%= ctx[:primary_resource_id] %>-upstream-1" { location = "us-central1" - repository_id = "<%= ctx[:vars]['upstream_repository_id'] %>" - description = "<%= ctx[:vars]['upstream_description'] %>" + repository_id = "<%= ctx[:vars]['upstream_repository_id'] %>-1" + description = "<%= ctx[:vars]['upstream_description'] %> 1" + format = "DOCKER" +} + +resource "google_artifact_registry_repository" "<%= ctx[:primary_resource_id] %>-upstream-2" { + location = "us-central1" + repository_id = "<%= ctx[:vars]['upstream_repository_id'] %>-2" + description = "<%= ctx[:vars]['upstream_description'] %> 2" format = "DOCKER" } @@ -14,9 +21,14 @@ resource "google_artifact_registry_repository" "<%= ctx[:primary_resource_id] %> mode = "VIRTUAL_REPOSITORY" virtual_repository_config { upstream_policies { - id = "<%= ctx[:vars]['upstream_policy_id'] %>" - repository = google_artifact_registry_repository.<%= ctx[:primary_resource_id] %>-upstream.id - priority = 1 + id = "<%= ctx[:vars]['upstream_policy_id'] %>-1" + repository = google_artifact_registry_repository.<%= ctx[:primary_resource_id] %>-upstream-1.id + priority = 20 + } + upstream_policies { + id = "<%= ctx[:vars]['upstream_policy_id'] %>-2" + repository = google_artifact_registry_repository.<%= ctx[:primary_resource_id] %>-upstream-2.id + priority = 10 } } }