Skip to content

Commit

Permalink
artifactregistry: accept all valid durations (#12667)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Hawk <chrishawk+github@gmail.com>
  • Loading branch information
Subserial and SirGitsalot authored Jan 13, 2025
1 parent f4a1b93 commit 9449992
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 32 deletions.
7 changes: 5 additions & 2 deletions mmv1/products/artifactregistry/Repository.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ properties:
key_name: 'id'
key_description: |-
The policy ID. Must be unique within a repository.
set_hash_func: 'mapHashID'
value_type:
name: cleanupPolicies
type: NestedObject
Expand Down Expand Up @@ -416,12 +417,14 @@ properties:
type: String
description: |-
Match versions older than a duration.
diff_suppress_func: 'tpgresource.DurationDiffSuppress'
custom_expand: 'templates/terraform/custom_expand/duration_to_seconds.go.tmpl'
diff_suppress_func: 'durationDiffSuppress'
- name: 'newerThan'
type: String
description: |-
Match versions newer than a duration.
diff_suppress_func: 'tpgresource.DurationDiffSuppress'
custom_expand: 'templates/terraform/custom_expand/duration_to_seconds.go.tmpl'
diff_suppress_func: 'durationDiffSuppress'
- name: 'mostRecentVersions'
type: NestedObject
description: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,87 @@
limitations under the License.
*/ -}}
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
}
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)
}
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)
oldSet := schema.NewSet(schema.HashString, oldHashes)
newSet := schema.NewSet(schema.HashString, newHashes)
return oldSet.Equal(newSet)
}

func parseDurationAsSeconds(v string) (int, bool) {
if len(v) == 0 {
return 0, false
}
n, err := strconv.Atoi(v[:len(v)-1])
if err != nil {
return 0, false
}
switch v[len(v)-1] {
case 's':
return n, true
case 'm':
return n * 60, true
case 'h':
return n * 3600, true
case 'd':
return n * 86400, true
default:
return 0, false
}
}

// Like tpgresource.DurationDiffSuppress, but supports 'd'
func durationDiffSuppress(k, oldr, newr string, d *schema.ResourceData) bool {
o, n := d.GetChange(k)
old, ok := o.(string)
if !ok {
return false
}
new, ok := n.(string)
if !ok {
return false
}
if old == new {
return true
}
oldSeconds, ok := parseDurationAsSeconds(old)
if !ok {
return false
}
newSeconds, ok := parseDurationAsSeconds(new)
if !ok {
return false
}
return oldSeconds == newSeconds
}

func mapHashID(v any) int {
obj := v.(map[string]any)
return schema.HashString(obj["id"])
}
43 changes: 43 additions & 0 deletions mmv1/templates/terraform/custom_expand/duration_to_seconds.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{{/*
The license inside this block applies to this file
Copyright 2024 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ -}}
func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
if v == nil {
return nil, nil
}
val, ok := v.(string)
if !ok {
return nil, fmt.Errorf("unexpected value is not string: %v", v)
}
if len(val) == 0 {
return nil, nil
}
n, err := strconv.Atoi(val[:len(val)-1])
if err != nil {
return nil, fmt.Errorf("unexpected value is not duration: %v", v)
}
// time.ParseDuration does not support 'd'
var seconds int
switch val[len(val)-1] {
case 's':
seconds = n
case 'm':
seconds = n * 60
case 'h':
seconds = n * 3600
case 'd':
seconds = n * 86400
default:
return nil, fmt.Errorf("unexpected duration has unknown unit: %c", val[len(val)-1])
}
return fmt.Sprintf("%ds", seconds), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ resource "google_artifact_registry_repository" "{{$.PrimaryResourceId}}" {
description = "{{index $.Vars "desc"}}"
format = "DOCKER"
cleanup_policy_dry_run = false
cleanup_policies {
id = "delete-untagged"
action = "DELETE"
condition {
tag_state = "UNTAGGED"
}
}
cleanup_policies {
id = "keep-new-untagged"
action = "KEEP"
condition {
tag_state = "UNTAGGED"
newer_than = "7d"
}
}
cleanup_policies {
id = "delete-prerelease"
action = "DELETE"
condition {
tag_state = "TAGGED"
tag_prefixes = ["alpha", "v0"]
older_than = "2592000s"
older_than = "30d"
}
}
cleanup_policies {
Expand Down

0 comments on commit 9449992

Please sign in to comment.