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

fixed artifact_registry_repository permadiff in unordered array #9376

Merged
merged 9 commits into from
Dec 1, 2023
2 changes: 2 additions & 0 deletions mmv1/products/artifactregistry/Repository.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ examples:
repository_id: 'my-repository'
description: 'example docker repository with cleanup policies'
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
Expand Down Expand Up @@ -221,6 +222,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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<%# 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 {
log.Printf("%v, %v, %v\n", k, old, new)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless we have a reason to do this by hand, it would probably be better to use the sdk provided set methods such as schema.NewSet and Set.Equal to ensure we don't miss anything with casting the types around.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! Switched over to using schema.Set.

o, n := d.GetChange("virtual_repository_config.0.upstream_policies")
oMap, ok := o.([]any)
if !ok {
return false
}
nMap, ok := n.([]any)
if !ok {
return false
}
hashFunc := func(val any) string {
x := val.(map[string]any)
return fmt.Sprintf("[id:%v priority:%v repository:%v]", x["id"], x["priority"], x["repository"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is equivalent to

return fmt.Sprintf("%v", x)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if golang is consistent with map key order within the same process. This was to prevent two maps from outputting different strings with the same information.

}

return arrayDiffSuppress(oMap, nMap, hashFunc)
}

func arrayDiffSuppress(old, new []any, hashFunc func(x any) string) bool {
oldMap := map[string]bool{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're just making a set here we can use map[string]struct{}, if we need a boolean value that's distinct from omitting the key, use map[string]bool.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced with schema.Set.

newMap := map[string]bool{}
for _, x := range old {
key := hashFunc(x)
oldMap[key] = true
}
for _, y := range new {
key := hashFunc(y)
newMap[key] = true
}
if len(oldMap) != len(newMap) {
return false
}
for x := range oldMap {
if !newMap[x] {
return false
}
}
return true
}