Skip to content

Commit 5f92f81

Browse files
google_artifact_registry_repository: Fix perma-diff in maven_config (#12190) (#9109)
[upstream:c5023de0d6808cba61407f190cc4f2bbce00bb62] Signed-off-by: Modular Magician <[email protected]>
1 parent 658b638 commit 5f92f81

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

.changelog/12190.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
artifactregistry: fixed perma-diff in `google_artifact_registry_repository`
3+
```

google-beta/services/artifactregistry/resource_artifact_registry_repository.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,25 @@ func mapHashID(v any) int {
120120
return schema.HashString(obj["id"])
121121
}
122122

123+
func isDefaultEnum(val any) bool {
124+
s, ok := val.(string)
125+
if !ok {
126+
return false
127+
}
128+
return s == "" || strings.HasSuffix(s, "_UNSPECIFIED")
129+
}
130+
131+
// emptyMavenConfigDiffSuppress generates a config from defaults if it or any
132+
// properties are unset. Missing, empty and default configs are all equivalent.
133+
func emptyMavenConfigDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
134+
oSnap, nSnap := d.GetChange("maven_config.0.allow_snapshot_overwrites")
135+
if oSnap.(bool) != nSnap.(bool) {
136+
return false
137+
}
138+
oPolicy, nPolicy := d.GetChange("maven_config.0.version_policy")
139+
return isDefaultEnum(oPolicy) && isDefaultEnum(nPolicy)
140+
}
141+
123142
func ResourceArtifactRegistryRepository() *schema.Resource {
124143
return &schema.Resource{
125144
Create: resourceArtifactRegistryRepositoryCreate,
@@ -322,8 +341,9 @@ or use the
322341
data source for possible values.`,
323342
},
324343
"maven_config": {
325-
Type: schema.TypeList,
326-
Optional: true,
344+
Type: schema.TypeList,
345+
Optional: true,
346+
DiffSuppressFunc: emptyMavenConfigDiffSuppress,
327347
Description: `MavenRepositoryConfig is maven related repository details.
328348
Provides additional configuration details for repositories of the maven
329349
format type.`,

google-beta/services/artifactregistry/resource_artifact_registry_repository_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,38 @@ func TestAccArtifactRegistryRepository_createMvnRelease(t *testing.T) {
8686
})
8787
}
8888

89+
func TestAccArtifactRegistryRepository_updateEmptyMvn(t *testing.T) {
90+
t.Parallel()
91+
92+
repositoryID := fmt.Sprintf("tf-test-%d", acctest.RandInt(t))
93+
94+
acctest.VcrTest(t, resource.TestCase{
95+
PreCheck: func() { acctest.AccTestPreCheck(t) },
96+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
97+
CheckDestroy: testAccCheckArtifactRegistryRepositoryDestroyProducer(t),
98+
Steps: []resource.TestStep{
99+
{
100+
Config: testAccArtifactRegistryRepository_createMvnEmpty1(repositoryID),
101+
},
102+
{
103+
ResourceName: "google_artifact_registry_repository.test",
104+
ImportState: true,
105+
ImportStateVerify: true,
106+
},
107+
{
108+
Config: testAccArtifactRegistryRepository_createMvnEmpty2(repositoryID),
109+
PlanOnly: true,
110+
ExpectNonEmptyPlan: false,
111+
},
112+
{
113+
Config: testAccArtifactRegistryRepository_createMvnEmpty3(repositoryID),
114+
PlanOnly: true,
115+
ExpectNonEmptyPlan: false,
116+
},
117+
},
118+
})
119+
}
120+
89121
func TestAccArtifactRegistryRepository_kfp(t *testing.T) {
90122
t.Parallel()
91123

@@ -154,6 +186,43 @@ resource "google_artifact_registry_repository" "test" {
154186
`, repositoryID, versionPolicy)
155187
}
156188

189+
func testAccArtifactRegistryRepository_createMvnEmpty1(repositoryID string) string {
190+
return fmt.Sprintf(`
191+
resource "google_artifact_registry_repository" "test" {
192+
repository_id = "%s"
193+
location = "us-central1"
194+
description = "maven repository with empty maven_config"
195+
format = "MAVEN"
196+
}
197+
`, repositoryID)
198+
}
199+
200+
func testAccArtifactRegistryRepository_createMvnEmpty2(repositoryID string) string {
201+
return fmt.Sprintf(`
202+
resource "google_artifact_registry_repository" "test" {
203+
repository_id = "%s"
204+
location = "us-central1"
205+
description = "maven repository with empty maven_config"
206+
format = "MAVEN"
207+
maven_config { }
208+
}
209+
`, repositoryID)
210+
}
211+
212+
func testAccArtifactRegistryRepository_createMvnEmpty3(repositoryID string) string {
213+
return fmt.Sprintf(`
214+
resource "google_artifact_registry_repository" "test" {
215+
repository_id = "%s"
216+
location = "us-central1"
217+
description = "maven repository with empty maven_config"
218+
format = "MAVEN"
219+
maven_config {
220+
allow_snapshot_overwrites = false
221+
}
222+
}
223+
`, repositoryID)
224+
}
225+
157226
func testAccArtifactRegistryRepository_kfp(repositoryID string) string {
158227
return fmt.Sprintf(`
159228
resource "google_artifact_registry_repository" "test" {

0 commit comments

Comments
 (0)