@@ -22,6 +22,7 @@ import (
22
22
"log"
23
23
"net/http"
24
24
"reflect"
25
+ "strconv"
25
26
"strings"
26
27
"time"
27
28
@@ -67,6 +68,58 @@ func upstreamPoliciesDiffSuppress(k, old, new string, d *schema.ResourceData) bo
67
68
return oldSet .Equal (newSet )
68
69
}
69
70
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
+
70
123
func ResourceArtifactRegistryRepository () * schema.Resource {
71
124
return & schema.Resource {
72
125
Create : resourceArtifactRegistryRepositoryCreate ,
@@ -136,13 +189,13 @@ unique within a repository and be under 128 characters in length.`,
136
189
"newer_than" : {
137
190
Type : schema .TypeString ,
138
191
Optional : true ,
139
- DiffSuppressFunc : tpgresource . DurationDiffSuppress ,
192
+ DiffSuppressFunc : durationDiffSuppress ,
140
193
Description : `Match versions newer than a duration.` ,
141
194
},
142
195
"older_than" : {
143
196
Type : schema .TypeString ,
144
197
Optional : true ,
145
- DiffSuppressFunc : tpgresource . DurationDiffSuppress ,
198
+ DiffSuppressFunc : durationDiffSuppress ,
146
199
Description : `Match versions older than a duration.` ,
147
200
},
148
201
"package_name_prefixes" : {
@@ -205,6 +258,7 @@ specified with a Keep action.`,
205
258
},
206
259
},
207
260
},
261
+ Set : mapHashID ,
208
262
},
209
263
"cleanup_policy_dry_run" : {
210
264
Type : schema .TypeBool ,
@@ -2095,11 +2149,67 @@ func expandArtifactRegistryRepositoryCleanupPoliciesConditionPackageNamePrefixes
2095
2149
}
2096
2150
2097
2151
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
2099
2181
}
2100
2182
2101
2183
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
2103
2213
}
2104
2214
2105
2215
func expandArtifactRegistryRepositoryCleanupPoliciesMostRecentVersions (v interface {}, d tpgresource.TerraformResourceData , config * transport_tpg.Config ) (interface {}, error ) {
0 commit comments