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 #16646

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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