Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

artifactregistry: accept all valid durations #3300

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/hashicorp/hcl/v2 v2.20.1
github.com/hashicorp/terraform-json v0.22.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20250113202309-3374bc878cc0
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20250113223125-b164e694c606
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0 h1:qHprzXy/As0rxedphECBEQAh
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A=
github.com/hashicorp/terraform-plugin-testing v1.5.1 h1:T4aQh9JAhmWo4+t1A7x+rnxAJHCDIYW9kXyo4sVO92c=
github.com/hashicorp/terraform-plugin-testing v1.5.1/go.mod h1:dg8clO6K59rZ8w9EshBmDp1CxTIPu3yA4iaDpX1h5u0=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20250113202309-3374bc878cc0 h1:RjZwsMKBS4JSMOTumumh5mr15VXpUu7UUKlU4ttbML4=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20250113202309-3374bc878cc0/go.mod h1:3oxffi8u7i0pihgQGidFrcv1eysjxO4kfEw/Zc7/YDU=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20250113223125-b164e694c606 h1:eoAeWtbnqocWSM/Cig1Te0NQULkHS8iWVQjmFRTSRcg=
github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20250113223125-b164e694c606/go.mod h1:3oxffi8u7i0pihgQGidFrcv1eysjxO4kfEw/Zc7/YDU=
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package artifactregistry
import (
"fmt"
"reflect"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

Expand Down Expand Up @@ -59,6 +60,58 @@ func upstreamPoliciesDiffSuppress(k, old, new string, d *schema.ResourceData) bo
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"])
}

const ArtifactRegistryRepositoryAssetType string = "artifactregistry.googleapis.com/Repository"

func ResourceConverterArtifactRegistryRepository() cai.ResourceConverter {
Expand Down Expand Up @@ -445,11 +498,67 @@ func expandArtifactRegistryRepositoryCleanupPoliciesConditionPackageNamePrefixes
}

func expandArtifactRegistryRepositoryCleanupPoliciesConditionOlderThan(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
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
}

func expandArtifactRegistryRepositoryCleanupPoliciesConditionNewerThan(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
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
}

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