Skip to content

Commit 9449992

Browse files
artifactregistry: accept all valid durations (#12667)
Co-authored-by: Chris Hawk <[email protected]>
1 parent f4a1b93 commit 9449992

File tree

4 files changed

+145
-32
lines changed

4 files changed

+145
-32
lines changed

mmv1/products/artifactregistry/Repository.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ properties:
368368
key_name: 'id'
369369
key_description: |-
370370
The policy ID. Must be unique within a repository.
371+
set_hash_func: 'mapHashID'
371372
value_type:
372373
name: cleanupPolicies
373374
type: NestedObject
@@ -416,12 +417,14 @@ properties:
416417
type: String
417418
description: |-
418419
Match versions older than a duration.
419-
diff_suppress_func: 'tpgresource.DurationDiffSuppress'
420+
custom_expand: 'templates/terraform/custom_expand/duration_to_seconds.go.tmpl'
421+
diff_suppress_func: 'durationDiffSuppress'
420422
- name: 'newerThan'
421423
type: String
422424
description: |-
423425
Match versions newer than a duration.
424-
diff_suppress_func: 'tpgresource.DurationDiffSuppress'
426+
custom_expand: 'templates/terraform/custom_expand/duration_to_seconds.go.tmpl'
427+
diff_suppress_func: 'durationDiffSuppress'
425428
- name: 'mostRecentVersions'
426429
type: NestedObject
427430
description: |-

mmv1/templates/terraform/constants/artifact_registry_repository.go.tmpl

Lines changed: 81 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,87 @@
1111
limitations under the License.
1212
*/ -}}
1313
func upstreamPoliciesDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
14-
o, n := d.GetChange("virtual_repository_config.0.upstream_policies")
15-
oldPolicies, ok := o.([]any)
16-
if !ok {
17-
return false
18-
}
19-
newPolicies, ok := n.([]any)
20-
if !ok {
21-
return false
22-
}
14+
o, n := d.GetChange("virtual_repository_config.0.upstream_policies")
15+
oldPolicies, ok := o.([]any)
16+
if !ok {
17+
return false
18+
}
19+
newPolicies, ok := n.([]any)
20+
if !ok {
21+
return false
22+
}
2323

24-
var oldHashes, newHashes []interface{}
25-
for _, policy := range oldPolicies {
26-
data, ok := policy.(map[string]any)
27-
if !ok {
28-
return false
29-
}
30-
hashStr := fmt.Sprintf("[id:%v priority:%v repository:%v]", data["id"], data["priority"], data["repository"])
31-
oldHashes = append(oldHashes, hashStr)
32-
}
33-
for _, policy := range newPolicies {
34-
data, ok := policy.(map[string]any)
35-
if !ok {
36-
return false
37-
}
38-
hashStr := fmt.Sprintf("[id:%v priority:%v repository:%v]", data["id"], data["priority"], data["repository"])
39-
newHashes = append(newHashes, hashStr)
40-
}
24+
var oldHashes, newHashes []interface{}
25+
for _, policy := range oldPolicies {
26+
data, ok := policy.(map[string]any)
27+
if !ok {
28+
return false
29+
}
30+
hashStr := fmt.Sprintf("[id:%v priority:%v repository:%v]", data["id"], data["priority"], data["repository"])
31+
oldHashes = append(oldHashes, hashStr)
32+
}
33+
for _, policy := range newPolicies {
34+
data, ok := policy.(map[string]any)
35+
if !ok {
36+
return false
37+
}
38+
hashStr := fmt.Sprintf("[id:%v priority:%v repository:%v]", data["id"], data["priority"], data["repository"])
39+
newHashes = append(newHashes, hashStr)
40+
}
4141

42-
oldSet := schema.NewSet(schema.HashString, oldHashes)
43-
newSet := schema.NewSet(schema.HashString, newHashes)
44-
return oldSet.Equal(newSet)
42+
oldSet := schema.NewSet(schema.HashString, oldHashes)
43+
newSet := schema.NewSet(schema.HashString, newHashes)
44+
return oldSet.Equal(newSet)
45+
}
46+
47+
func parseDurationAsSeconds(v string) (int, bool) {
48+
if len(v) == 0 {
49+
return 0, false
50+
}
51+
n, err := strconv.Atoi(v[:len(v)-1])
52+
if err != nil {
53+
return 0, false
54+
}
55+
switch v[len(v)-1] {
56+
case 's':
57+
return n, true
58+
case 'm':
59+
return n * 60, true
60+
case 'h':
61+
return n * 3600, true
62+
case 'd':
63+
return n * 86400, true
64+
default:
65+
return 0, false
66+
}
67+
}
68+
69+
// Like tpgresource.DurationDiffSuppress, but supports 'd'
70+
func durationDiffSuppress(k, oldr, newr string, d *schema.ResourceData) bool {
71+
o, n := d.GetChange(k)
72+
old, ok := o.(string)
73+
if !ok {
74+
return false
75+
}
76+
new, ok := n.(string)
77+
if !ok {
78+
return false
79+
}
80+
if old == new {
81+
return true
82+
}
83+
oldSeconds, ok := parseDurationAsSeconds(old)
84+
if !ok {
85+
return false
86+
}
87+
newSeconds, ok := parseDurationAsSeconds(new)
88+
if !ok {
89+
return false
90+
}
91+
return oldSeconds == newSeconds
92+
}
93+
94+
func mapHashID(v any) int {
95+
obj := v.(map[string]any)
96+
return schema.HashString(obj["id"])
4597
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{{/*
2+
The license inside this block applies to this file
3+
Copyright 2024 Google Inc.
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/ -}}
13+
func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
14+
if v == nil {
15+
return nil, nil
16+
}
17+
val, ok := v.(string)
18+
if !ok {
19+
return nil, fmt.Errorf("unexpected value is not string: %v", v)
20+
}
21+
if len(val) == 0 {
22+
return nil, nil
23+
}
24+
n, err := strconv.Atoi(val[:len(val)-1])
25+
if err != nil {
26+
return nil, fmt.Errorf("unexpected value is not duration: %v", v)
27+
}
28+
// time.ParseDuration does not support 'd'
29+
var seconds int
30+
switch val[len(val)-1] {
31+
case 's':
32+
seconds = n
33+
case 'm':
34+
seconds = n * 60
35+
case 'h':
36+
seconds = n * 3600
37+
case 'd':
38+
seconds = n * 86400
39+
default:
40+
return nil, fmt.Errorf("unexpected duration has unknown unit: %c", val[len(val)-1])
41+
}
42+
return fmt.Sprintf("%ds", seconds), nil
43+
}

mmv1/templates/terraform/examples/artifact_registry_repository_cleanup.tf.tmpl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@ resource "google_artifact_registry_repository" "{{$.PrimaryResourceId}}" {
44
description = "{{index $.Vars "desc"}}"
55
format = "DOCKER"
66
cleanup_policy_dry_run = false
7+
cleanup_policies {
8+
id = "delete-untagged"
9+
action = "DELETE"
10+
condition {
11+
tag_state = "UNTAGGED"
12+
}
13+
}
14+
cleanup_policies {
15+
id = "keep-new-untagged"
16+
action = "KEEP"
17+
condition {
18+
tag_state = "UNTAGGED"
19+
newer_than = "7d"
20+
}
21+
}
722
cleanup_policies {
823
id = "delete-prerelease"
924
action = "DELETE"
1025
condition {
1126
tag_state = "TAGGED"
1227
tag_prefixes = ["alpha", "v0"]
13-
older_than = "2592000s"
28+
older_than = "30d"
1429
}
1530
}
1631
cleanup_policies {

0 commit comments

Comments
 (0)