From 7ee33f8a7f43cc33ae36d620eab7c6b875879b22 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Thu, 26 Sep 2024 13:20:22 -0500 Subject: [PATCH] generate separate checkFunc --- internal/generate/tags/main.go | 3 -- .../tags/templates/v2/update_tags_body.gtpl | 24 +-------- .../v2/update_tags_for_resource_body.gtpl | 6 +-- .../v2/wait_tags_propagated_body.gtpl | 28 +++++++++- internal/service/dynamodb/tags_gen.go | 53 ++++++++++--------- internal/service/kms/external_key.go | 3 +- internal/service/kms/key.go | 3 +- internal/service/kms/replica_external_key.go | 3 +- internal/service/kms/replica_key.go | 3 +- internal/service/kms/tags_gen.go | 47 ++++++++-------- 10 files changed, 91 insertions(+), 82 deletions(-) diff --git a/internal/generate/tags/main.go b/internal/generate/tags/main.go index 55d124ac7cf..256fac08b9e 100644 --- a/internal/generate/tags/main.go +++ b/internal/generate/tags/main.go @@ -1,9 +1,6 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -//go:build generate -// +build generate - package main import ( diff --git a/internal/generate/tags/templates/v2/update_tags_body.gtpl b/internal/generate/tags/templates/v2/update_tags_body.gtpl index 36935f861d1..91db91dd85d 100644 --- a/internal/generate/tags/templates/v2/update_tags_body.gtpl +++ b/internal/generate/tags/templates/v2/update_tags_body.gtpl @@ -164,28 +164,8 @@ func {{ .UpdateTagsFunc }}(ctx context.Context, conn {{ .ClientType }}, identifi {{ if .WaitForPropagation }} if len(removedTags) > 0 || len(updatedTags) > 0 { - checkFunc := func(tags tftags.KeyValueTags) func() (bool, error) { - return func() (bool, error) { - output, err := listTags(ctx, conn, identifier, optFns...) - - if tfresource.NotFound(err) { - return false, nil - } - - if err != nil { - return false, err - } - - if inContext, ok := tftags.FromContext(ctx); ok { - tags = tags.IgnoreConfig(inContext.IgnoreConfig) - output = output.IgnoreConfig(inContext.IgnoreConfig) - } - - return output.Equal(tags), nil - } - } - - if err := {{ .WaitTagsPropagatedFunc }}(ctx, conn, identifier, newTags, checkFunc(newTags), optFns...); err != nil { + check := checkFunc(ctx, conn, newTags, identifier, optFns...) + if err := {{ .WaitTagsPropagatedFunc }}(ctx, newTags, check); err != nil { return fmt.Errorf("waiting for resource (%s) tag propagation: %w", identifier, err) } } diff --git a/internal/generate/tags/templates/v2/update_tags_for_resource_body.gtpl b/internal/generate/tags/templates/v2/update_tags_for_resource_body.gtpl index 7f34ec3b244..40242dab2ce 100644 --- a/internal/generate/tags/templates/v2/update_tags_for_resource_body.gtpl +++ b/internal/generate/tags/templates/v2/update_tags_for_resource_body.gtpl @@ -164,9 +164,9 @@ func {{ .UpdateTagsForResourceFunc }}(ctx context.Context, conn {{ .ClientType } {{ if .WaitForPropagation }} if len(removedTags) > 0 || len(updatedTags) > 0 { - checkFunc := func(tags tftags.KeyValueTags) func() (bool, error) { + checkFunc := func(ctx context.Context, conn {{ .ClientType }}, tags tftags.KeyValueTags, id string, optFns ...func(*{{ .AWSService }}.Options)) func() (bool, error) { return func() (bool, error) { - output, err := listTags(ctx, conn, identifier, optFns...) + output, err := listTags(ctx, conn, id, optFns...) if tfresource.NotFound(err) { return false, nil @@ -185,7 +185,7 @@ func {{ .UpdateTagsForResourceFunc }}(ctx context.Context, conn {{ .ClientType } } } - if err := {{ .WaitTagsPropagatedFunc }}(ctx, conn, identifier, newTags, checkFunc(newTags), optFns...); err != nil { + if err := {{ .WaitTagsPropagatedFunc }}(ctx, newTags, checkFunc(ctx, conn, newTags, identifier, optFns...)); err != nil { return fmt.Errorf("waiting for resource (%s) tag propagation: %w", identifier, err) } } diff --git a/internal/generate/tags/templates/v2/wait_tags_propagated_body.gtpl b/internal/generate/tags/templates/v2/wait_tags_propagated_body.gtpl index 82ad677aae6..35f3887209e 100644 --- a/internal/generate/tags/templates/v2/wait_tags_propagated_body.gtpl +++ b/internal/generate/tags/templates/v2/wait_tags_propagated_body.gtpl @@ -1,7 +1,7 @@ // {{ .WaitTagsPropagatedFunc }} waits for {{ .ServicePackage }} service tags to be propagated. // The identifier is typically the Amazon Resource Name (ARN), although // it may also be a different identifier depending on the service. -func {{ .WaitTagsPropagatedFunc }}(ctx context.Context, conn {{ .ClientType }}, id string, tags tftags.KeyValueTags, checkFunc func() (bool, error), optFns ...func(*{{ .AWSService }}.Options)) error { +func {{ .WaitTagsPropagatedFunc }}(ctx context.Context, tags tftags.KeyValueTags, checkFunc func() (bool, error)) error { tflog.Debug(ctx, "Waiting for tag propagation", map[string]any{ names.AttrTags: tags, }) @@ -23,3 +23,29 @@ func {{ .WaitTagsPropagatedFunc }}(ctx context.Context, conn {{ .ClientType }}, return tfresource.WaitUntil(ctx, {{ .WaitTimeout }}, checkFunc, opts) } + +// checkFunc returns a function that checks if the tags are propagated. +func checkFunc(ctx context.Context, conn {{ .ClientType }}, tags tftags.KeyValueTags, id string, optFns ...func(*{{ .AWSService }}.Options)) func() (bool, error) { + return func() (bool, error) { + output, err := listTags(ctx, conn, id, optFns...) + + if tfresource.NotFound(err) { + return false, nil + } + + if err != nil { + return false, err + } + + if inContext, ok := tftags.FromContext(ctx); ok { + tags = tags.IgnoreConfig(inContext.IgnoreConfig) + output = output.IgnoreConfig(inContext.IgnoreConfig) + } + + {{- if .UpdateTagsForResource }} + return output.ContainsAll(tags), nil + {{- else }} + return output.Equal(tags), nil + {{- end }} + } +} diff --git a/internal/service/dynamodb/tags_gen.go b/internal/service/dynamodb/tags_gen.go index a12db943650..8d38c0088f5 100644 --- a/internal/service/dynamodb/tags_gen.go +++ b/internal/service/dynamodb/tags_gen.go @@ -167,28 +167,8 @@ func updateTags(ctx context.Context, conn *dynamodb.Client, identifier string, o } if len(removedTags) > 0 || len(updatedTags) > 0 { - checkFunc := func(tags tftags.KeyValueTags) func() (bool, error) { - return func() (bool, error) { - output, err := listTags(ctx, conn, identifier, optFns...) - - if tfresource.NotFound(err) { - return false, nil - } - - if err != nil { - return false, err - } - - if inContext, ok := tftags.FromContext(ctx); ok { - tags = tags.IgnoreConfig(inContext.IgnoreConfig) - output = output.IgnoreConfig(inContext.IgnoreConfig) - } - - return output.Equal(tags), nil - } - } - - if err := waitTagsPropagated(ctx, conn, identifier, newTags, checkFunc(newTags), optFns...); err != nil { + check := checkFunc(ctx, conn, newTags, identifier, optFns...) + if err := waitTagsPropagated(ctx, newTags, check); err != nil { return fmt.Errorf("waiting for resource (%s) tag propagation: %w", identifier, err) } } @@ -242,9 +222,9 @@ func updateTagsForResource(ctx context.Context, conn *dynamodb.Client, identifie } if len(removedTags) > 0 || len(updatedTags) > 0 { - checkFunc := func(tags tftags.KeyValueTags) func() (bool, error) { + checkFunc := func(ctx context.Context, conn *dynamodb.Client, tags tftags.KeyValueTags, id string, optFns ...func(*dynamodb.Options)) func() (bool, error) { return func() (bool, error) { - output, err := listTags(ctx, conn, identifier, optFns...) + output, err := listTags(ctx, conn, id, optFns...) if tfresource.NotFound(err) { return false, nil @@ -263,7 +243,7 @@ func updateTagsForResource(ctx context.Context, conn *dynamodb.Client, identifie } } - if err := waitTagsPropagated(ctx, conn, identifier, newTags, checkFunc(newTags), optFns...); err != nil { + if err := waitTagsPropagated(ctx, newTags, checkFunc(ctx, conn, newTags, identifier, optFns...)); err != nil { return fmt.Errorf("waiting for resource (%s) tag propagation: %w", identifier, err) } } @@ -274,7 +254,7 @@ func updateTagsForResource(ctx context.Context, conn *dynamodb.Client, identifie // waitTagsPropagated waits for dynamodb service tags to be propagated. // The identifier is typically the Amazon Resource Name (ARN), although // it may also be a different identifier depending on the service. -func waitTagsPropagated(ctx context.Context, conn *dynamodb.Client, id string, tags tftags.KeyValueTags, checkFunc func() (bool, error), optFns ...func(*dynamodb.Options)) error { +func waitTagsPropagated(ctx context.Context, tags tftags.KeyValueTags, checkFunc func() (bool, error)) error { tflog.Debug(ctx, "Waiting for tag propagation", map[string]any{ names.AttrTags: tags, }) @@ -286,3 +266,24 @@ func waitTagsPropagated(ctx context.Context, conn *dynamodb.Client, id string, t return tfresource.WaitUntil(ctx, 2*time.Minute, checkFunc, opts) } + +// checkFunc returns a function that checks if the tags are propagated. +func checkFunc(ctx context.Context, conn *dynamodb.Client, tags tftags.KeyValueTags, id string, optFns ...func(*dynamodb.Options)) func() (bool, error) { + return func() (bool, error) { + output, err := listTags(ctx, conn, id, optFns...) + + if tfresource.NotFound(err) { + return false, nil + } + + if err != nil { + return false, err + } + + if inContext, ok := tftags.FromContext(ctx); ok { + tags = tags.IgnoreConfig(inContext.IgnoreConfig) + output = output.IgnoreConfig(inContext.IgnoreConfig) + } + return output.Equal(tags), nil + } +} diff --git a/internal/service/kms/external_key.go b/internal/service/kms/external_key.go index a9410123236..8409b4b1b81 100644 --- a/internal/service/kms/external_key.go +++ b/internal/service/kms/external_key.go @@ -204,7 +204,8 @@ func resourceExternalKeyCreate(ctx context.Context, d *schema.ResourceData, meta } if tags := KeyValueTags(ctx, getTagsIn(ctx)); len(tags) > 0 { - if err := waitTagsPropagated(ctx, conn, d.Id(), tags); err != nil { + check := checkFunc(ctx, conn, tags, d.Id()) + if err := waitTagsPropagated(ctx, tags, check); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for KMS External Key (%s) tag update: %s", d.Id(), err) } } diff --git a/internal/service/kms/key.go b/internal/service/kms/key.go index 2acb5a9851a..457c91dfbdd 100644 --- a/internal/service/kms/key.go +++ b/internal/service/kms/key.go @@ -218,7 +218,8 @@ func resourceKeyCreate(ctx context.Context, d *schema.ResourceData, meta interfa } if tags := KeyValueTags(ctx, getTagsIn(ctx)); len(tags) > 0 { - if err := waitTagsPropagated(ctx, conn, d.Id(), tags); err != nil { + check := checkFunc(ctx, conn, tags, d.Id()) + if err := waitTagsPropagated(ctx, tags, check); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for KMS Key (%s) tag update: %s", d.Id(), err) } } diff --git a/internal/service/kms/replica_external_key.go b/internal/service/kms/replica_external_key.go index 19f2228ebac..2ea9de2a6bb 100644 --- a/internal/service/kms/replica_external_key.go +++ b/internal/service/kms/replica_external_key.go @@ -199,7 +199,8 @@ func resourceReplicaExternalKeyCreate(ctx context.Context, d *schema.ResourceDat } if tags := KeyValueTags(ctx, getTagsIn(ctx)); len(tags) > 0 { - if err := waitTagsPropagated(ctx, conn, d.Id(), tags); err != nil { + check := checkFunc(ctx, conn, tags, d.Id()) + if err := waitTagsPropagated(ctx, tags, check); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for KMS Replica External Key (%s) tag update: %s", d.Id(), err) } } diff --git a/internal/service/kms/replica_key.go b/internal/service/kms/replica_key.go index 6b9f7d5f9e6..7fbe81eff0a 100644 --- a/internal/service/kms/replica_key.go +++ b/internal/service/kms/replica_key.go @@ -172,7 +172,8 @@ func resourceReplicaKeyCreate(ctx context.Context, d *schema.ResourceData, meta } if tags := KeyValueTags(ctx, getTagsIn(ctx)); len(tags) > 0 { - if err := waitTagsPropagated(ctx, conn, d.Id(), tags); err != nil { + check := checkFunc(ctx, conn, tags, d.Id()) + if err := waitTagsPropagated(ctx, tags, check); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for KMS Replica Key (%s) tag update: %s", d.Id(), err) } } diff --git a/internal/service/kms/tags_gen.go b/internal/service/kms/tags_gen.go index b656d52196e..23f72cc2e66 100644 --- a/internal/service/kms/tags_gen.go +++ b/internal/service/kms/tags_gen.go @@ -156,28 +156,8 @@ func updateTags(ctx context.Context, conn *kms.Client, identifier string, oldTag } if len(removedTags) > 0 || len(updatedTags) > 0 { - checkFunc := func(tags tftags.KeyValueTags) func() (bool, error) { - return func() (bool, error) { - output, err := listTags(ctx, conn, identifier, optFns...) - - if tfresource.NotFound(err) { - return false, nil - } - - if err != nil { - return false, err - } - - if inContext, ok := tftags.FromContext(ctx); ok { - tags = tags.IgnoreConfig(inContext.IgnoreConfig) - output = output.IgnoreConfig(inContext.IgnoreConfig) - } - - return output.Equal(tags), nil - } - } - - if err := waitTagsPropagated(ctx, conn, identifier, newTags, checkFunc(newTags), optFns...); err != nil { + check := checkFunc(ctx, conn, newTags, identifier, optFns...) + if err := waitTagsPropagated(ctx, newTags, check); err != nil { return fmt.Errorf("waiting for resource (%s) tag propagation: %w", identifier, err) } } @@ -194,7 +174,7 @@ func (p *servicePackage) UpdateTags(ctx context.Context, meta any, identifier st // waitTagsPropagated waits for kms service tags to be propagated. // The identifier is typically the Amazon Resource Name (ARN), although // it may also be a different identifier depending on the service. -func waitTagsPropagated(ctx context.Context, conn *kms.Client, id string, tags tftags.KeyValueTags, checkFunc func() (bool, error), optFns ...func(*kms.Options)) error { +func waitTagsPropagated(ctx context.Context, tags tftags.KeyValueTags, checkFunc func() (bool, error)) error { tflog.Debug(ctx, "Waiting for tag propagation", map[string]any{ names.AttrTags: tags, }) @@ -206,3 +186,24 @@ func waitTagsPropagated(ctx context.Context, conn *kms.Client, id string, tags t return tfresource.WaitUntil(ctx, 10*time.Minute, checkFunc, opts) } + +// checkFunc returns a function that checks if the tags are propagated. +func checkFunc(ctx context.Context, conn *kms.Client, tags tftags.KeyValueTags, id string, optFns ...func(*kms.Options)) func() (bool, error) { + return func() (bool, error) { + output, err := listTags(ctx, conn, id, optFns...) + + if tfresource.NotFound(err) { + return false, nil + } + + if err != nil { + return false, err + } + + if inContext, ok := tftags.FromContext(ctx); ok { + tags = tags.IgnoreConfig(inContext.IgnoreConfig) + output = output.IgnoreConfig(inContext.IgnoreConfig) + } + return output.Equal(tags), nil + } +}