From 04c81d73db7d496a76ce3d611d32c9b645830a37 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Mon, 13 Jan 2025 22:35:34 +0000 Subject: [PATCH] artifactregistry: accept all valid durations (#12667) Co-authored-by: Chris Hawk [upstream:94499923e80f3168760b9a75e0b3d3a5c07e67e3] Signed-off-by: Modular Magician --- go.mod | 2 +- go.sum | 4 +- .../artifactregistry_repository.go | 113 +++++++++++++++++- 3 files changed, 114 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index bcc1985e7..b8768922d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index a7e17e19b..b9690c85d 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/tfplan2cai/converters/google/resources/services/artifactregistry/artifactregistry_repository.go b/tfplan2cai/converters/google/resources/services/artifactregistry/artifactregistry_repository.go index cbe816d27..f868a06de 100644 --- a/tfplan2cai/converters/google/resources/services/artifactregistry/artifactregistry_repository.go +++ b/tfplan2cai/converters/google/resources/services/artifactregistry/artifactregistry_repository.go @@ -17,6 +17,7 @@ package artifactregistry import ( "fmt" "reflect" + "strconv" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -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 { @@ -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) {