Skip to content

Commit be8a135

Browse files
artifactregistry: accept all valid durations (#12667) (#20902)
[upstream:94499923e80f3168760b9a75e0b3d3a5c07e67e3] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 41490be commit be8a135

File tree

4 files changed

+149
-6
lines changed

4 files changed

+149
-6
lines changed

.changelog/12667.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: fix `artifact_registry_repository` not accepting durations with 'm', 'h' or 'd'
3+
```

google/services/artifactregistry/resource_artifact_registry_repository.go

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"log"
2323
"net/http"
2424
"reflect"
25+
"strconv"
2526
"strings"
2627
"time"
2728

@@ -67,6 +68,58 @@ func upstreamPoliciesDiffSuppress(k, old, new string, d *schema.ResourceData) bo
6768
return oldSet.Equal(newSet)
6869
}
6970

71+
func parseDurationAsSeconds(v string) (int, bool) {
72+
if len(v) == 0 {
73+
return 0, false
74+
}
75+
n, err := strconv.Atoi(v[:len(v)-1])
76+
if err != nil {
77+
return 0, false
78+
}
79+
switch v[len(v)-1] {
80+
case 's':
81+
return n, true
82+
case 'm':
83+
return n * 60, true
84+
case 'h':
85+
return n * 3600, true
86+
case 'd':
87+
return n * 86400, true
88+
default:
89+
return 0, false
90+
}
91+
}
92+
93+
// Like tpgresource.DurationDiffSuppress, but supports 'd'
94+
func durationDiffSuppress(k, oldr, newr string, d *schema.ResourceData) bool {
95+
o, n := d.GetChange(k)
96+
old, ok := o.(string)
97+
if !ok {
98+
return false
99+
}
100+
new, ok := n.(string)
101+
if !ok {
102+
return false
103+
}
104+
if old == new {
105+
return true
106+
}
107+
oldSeconds, ok := parseDurationAsSeconds(old)
108+
if !ok {
109+
return false
110+
}
111+
newSeconds, ok := parseDurationAsSeconds(new)
112+
if !ok {
113+
return false
114+
}
115+
return oldSeconds == newSeconds
116+
}
117+
118+
func mapHashID(v any) int {
119+
obj := v.(map[string]any)
120+
return schema.HashString(obj["id"])
121+
}
122+
70123
func ResourceArtifactRegistryRepository() *schema.Resource {
71124
return &schema.Resource{
72125
Create: resourceArtifactRegistryRepositoryCreate,
@@ -136,13 +189,13 @@ unique within a repository and be under 128 characters in length.`,
136189
"newer_than": {
137190
Type: schema.TypeString,
138191
Optional: true,
139-
DiffSuppressFunc: tpgresource.DurationDiffSuppress,
192+
DiffSuppressFunc: durationDiffSuppress,
140193
Description: `Match versions newer than a duration.`,
141194
},
142195
"older_than": {
143196
Type: schema.TypeString,
144197
Optional: true,
145-
DiffSuppressFunc: tpgresource.DurationDiffSuppress,
198+
DiffSuppressFunc: durationDiffSuppress,
146199
Description: `Match versions older than a duration.`,
147200
},
148201
"package_name_prefixes": {
@@ -205,6 +258,7 @@ specified with a Keep action.`,
205258
},
206259
},
207260
},
261+
Set: mapHashID,
208262
},
209263
"cleanup_policy_dry_run": {
210264
Type: schema.TypeBool,
@@ -2095,11 +2149,67 @@ func expandArtifactRegistryRepositoryCleanupPoliciesConditionPackageNamePrefixes
20952149
}
20962150

20972151
func expandArtifactRegistryRepositoryCleanupPoliciesConditionOlderThan(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
2098-
return v, nil
2152+
if v == nil {
2153+
return nil, nil
2154+
}
2155+
val, ok := v.(string)
2156+
if !ok {
2157+
return nil, fmt.Errorf("unexpected value is not string: %v", v)
2158+
}
2159+
if len(val) == 0 {
2160+
return nil, nil
2161+
}
2162+
n, err := strconv.Atoi(val[:len(val)-1])
2163+
if err != nil {
2164+
return nil, fmt.Errorf("unexpected value is not duration: %v", v)
2165+
}
2166+
// time.ParseDuration does not support 'd'
2167+
var seconds int
2168+
switch val[len(val)-1] {
2169+
case 's':
2170+
seconds = n
2171+
case 'm':
2172+
seconds = n * 60
2173+
case 'h':
2174+
seconds = n * 3600
2175+
case 'd':
2176+
seconds = n * 86400
2177+
default:
2178+
return nil, fmt.Errorf("unexpected duration has unknown unit: %c", val[len(val)-1])
2179+
}
2180+
return fmt.Sprintf("%ds", seconds), nil
20992181
}
21002182

21012183
func expandArtifactRegistryRepositoryCleanupPoliciesConditionNewerThan(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
2102-
return v, nil
2184+
if v == nil {
2185+
return nil, nil
2186+
}
2187+
val, ok := v.(string)
2188+
if !ok {
2189+
return nil, fmt.Errorf("unexpected value is not string: %v", v)
2190+
}
2191+
if len(val) == 0 {
2192+
return nil, nil
2193+
}
2194+
n, err := strconv.Atoi(val[:len(val)-1])
2195+
if err != nil {
2196+
return nil, fmt.Errorf("unexpected value is not duration: %v", v)
2197+
}
2198+
// time.ParseDuration does not support 'd'
2199+
var seconds int
2200+
switch val[len(val)-1] {
2201+
case 's':
2202+
seconds = n
2203+
case 'm':
2204+
seconds = n * 60
2205+
case 'h':
2206+
seconds = n * 3600
2207+
case 'd':
2208+
seconds = n * 86400
2209+
default:
2210+
return nil, fmt.Errorf("unexpected duration has unknown unit: %c", val[len(val)-1])
2211+
}
2212+
return fmt.Sprintf("%ds", seconds), nil
21032213
}
21042214

21052215
func expandArtifactRegistryRepositoryCleanupPoliciesMostRecentVersions(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {

google/services/artifactregistry/resource_artifact_registry_repository_generated_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,28 @@ resource "google_artifact_registry_repository" "my-repo" {
423423
description = "example docker repository with cleanup policies%{random_suffix}"
424424
format = "DOCKER"
425425
cleanup_policy_dry_run = false
426+
cleanup_policies {
427+
id = "delete-untagged"
428+
action = "DELETE"
429+
condition {
430+
tag_state = "UNTAGGED"
431+
}
432+
}
433+
cleanup_policies {
434+
id = "keep-new-untagged"
435+
action = "KEEP"
436+
condition {
437+
tag_state = "UNTAGGED"
438+
newer_than = "7d"
439+
}
440+
}
426441
cleanup_policies {
427442
id = "delete-prerelease"
428443
action = "DELETE"
429444
condition {
430445
tag_state = "TAGGED"
431446
tag_prefixes = ["alpha", "v0"]
432-
older_than = "2592000s"
447+
older_than = "30d"
433448
}
434449
}
435450
cleanup_policies {

website/docs/r/artifact_registry_repository.html.markdown

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,28 @@ resource "google_artifact_registry_repository" "my-repo" {
242242
description = "example docker repository with cleanup policies"
243243
format = "DOCKER"
244244
cleanup_policy_dry_run = false
245+
cleanup_policies {
246+
id = "delete-untagged"
247+
action = "DELETE"
248+
condition {
249+
tag_state = "UNTAGGED"
250+
}
251+
}
252+
cleanup_policies {
253+
id = "keep-new-untagged"
254+
action = "KEEP"
255+
condition {
256+
tag_state = "UNTAGGED"
257+
newer_than = "7d"
258+
}
259+
}
245260
cleanup_policies {
246261
id = "delete-prerelease"
247262
action = "DELETE"
248263
condition {
249264
tag_state = "TAGGED"
250265
tag_prefixes = ["alpha", "v0"]
251-
older_than = "2592000s"
266+
older_than = "30d"
252267
}
253268
}
254269
cleanup_policies {

0 commit comments

Comments
 (0)