Skip to content

Commit

Permalink
fixed artifact_registry_repository permadiff in unordered array (Goog…
Browse files Browse the repository at this point in the history
…leCloudPlatform#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)
  • Loading branch information
Subserial authored Dec 1, 2023
1 parent 1f7bac7 commit 9988136
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
2 changes: 2 additions & 0 deletions mmv1/products/artifactregistry/Repository.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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"
}

Expand All @@ -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
}
}
}

0 comments on commit 9988136

Please sign in to comment.