Skip to content

Commit

Permalink
fixed artifact_registry_repository permadiff in unordered array (#9376)…
Browse files Browse the repository at this point in the history
… (#16646)

* 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)
[upstream:9988136d1c8e3ef6dfb0506fe78fc07cd4a0097f]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Dec 1, 2023
1 parent 8aba280 commit 6b59c2a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .changelog/9376.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
artifactregistry: fixed permadiff due to unsorted `virtual_repository_config` array in `google_artifact_registry_repository`
```
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,40 @@ import (
"github.com/hashicorp/terraform-provider-google/google/verify"
)

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)
}

func ResourceArtifactRegistryRepository() *schema.Resource {
return &schema.Resource{
Create: resourceArtifactRegistryRepositoryCreate,
Expand Down Expand Up @@ -375,8 +409,9 @@ remote repository. Must be in the format of
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"upstream_policies": {
Type: schema.TypeList,
Optional: true,
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: upstreamPoliciesDiffSuppress,
Description: `Policies that configure the upstream artifacts distributed by the Virtual
Repository. Upstream policies cannot be set on a standard repository.`,
Elem: &schema.Resource{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,17 @@ func TestAccArtifactRegistryRepository_artifactRegistryRepositoryVirtualExample(

func testAccArtifactRegistryRepository_artifactRegistryRepositoryVirtualExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_artifact_registry_repository" "my-repo-upstream" {
resource "google_artifact_registry_repository" "my-repo-upstream-1" {
location = "us-central1"
repository_id = "tf-test-my-repository-upstream%{random_suffix}"
description = "example docker repository (upstream source)%{random_suffix}"
repository_id = "tf-test-my-repository-upstream%{random_suffix}-1"
description = "example docker repository (upstream source)%{random_suffix} 1"
format = "DOCKER"
}
resource "google_artifact_registry_repository" "my-repo-upstream-2" {
location = "us-central1"
repository_id = "tf-test-my-repository-upstream%{random_suffix}-2"
description = "example docker repository (upstream source)%{random_suffix} 2"
format = "DOCKER"
}
Expand All @@ -198,9 +205,14 @@ resource "google_artifact_registry_repository" "my-repo" {
mode = "VIRTUAL_REPOSITORY"
virtual_repository_config {
upstream_policies {
id = "tf-test-my-repository-upstream%{random_suffix}"
repository = google_artifact_registry_repository.my-repo-upstream.id
priority = 1
id = "tf-test-my-repository-upstream%{random_suffix}-1"
repository = google_artifact_registry_repository.my-repo-upstream-1.id
priority = 20
}
upstream_policies {
id = "tf-test-my-repository-upstream%{random_suffix}-2"
repository = google_artifact_registry_repository.my-repo-upstream-2.id
priority = 10
}
}
}
Expand Down
24 changes: 18 additions & 6 deletions website/docs/r/artifact_registry_repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,17 @@ data "google_project" "project" {}


```hcl
resource "google_artifact_registry_repository" "my-repo-upstream" {
resource "google_artifact_registry_repository" "my-repo-upstream-1" {
location = "us-central1"
repository_id = "my-repository-upstream"
description = "example docker repository (upstream source)"
repository_id = "my-repository-upstream-1"
description = "example docker repository (upstream source) 1"
format = "DOCKER"
}
resource "google_artifact_registry_repository" "my-repo-upstream-2" {
location = "us-central1"
repository_id = "my-repository-upstream-2"
description = "example docker repository (upstream source) 2"
format = "DOCKER"
}
Expand All @@ -117,9 +124,14 @@ resource "google_artifact_registry_repository" "my-repo" {
mode = "VIRTUAL_REPOSITORY"
virtual_repository_config {
upstream_policies {
id = "my-repository-upstream"
repository = google_artifact_registry_repository.my-repo-upstream.id
priority = 1
id = "my-repository-upstream-1"
repository = google_artifact_registry_repository.my-repo-upstream-1.id
priority = 20
}
upstream_policies {
id = "my-repository-upstream-2"
repository = google_artifact_registry_repository.my-repo-upstream-2.id
priority = 10
}
}
}
Expand Down

0 comments on commit 6b59c2a

Please sign in to comment.