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

Fixes timeout when creating an aws_kms_key with a tag set with the tag's key in ignore_tags #37818

Merged
merged 17 commits into from
Jun 4, 2024
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
15 changes: 15 additions & 0 deletions .changelog/37818.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```release-note:bug
resource/aws_kms_key: Fixes timeout error on creation when `ignore_tags` matches tag assigned to resource
```

```release-note:bug
resource/aws_kms_external_key: Fixes timeout error on creation when `ignore_tags` matches tag assigned to resource
```

```release-note:bug
resource/aws_kms_replica_key: Fixes timeout error on creation when `ignore_tags` matches tag assigned to resource
```

```release-note:bug
resource/aws_kms_replica_external_key: Fixes timeout error on creation when `ignore_tags` matches tag assigned to resource
```
60 changes: 60 additions & 0 deletions internal/acctest/const/consts_or_quote_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 0 additions & 55 deletions internal/acctest/consts_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions internal/acctest/statecheck/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package statecheck

import (
"fmt"

tfjson "github.com/hashicorp/terraform-json"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
)

type Base struct {
resourceAddress string
}

func NewBase(resourceAddress string) Base {
return Base{
resourceAddress: resourceAddress,
}
}

func (b Base) ResourceFromState(req statecheck.CheckStateRequest, resp *statecheck.CheckStateResponse) (*tfjson.StateResource, bool) {
var resource *tfjson.StateResource

if req.State == nil {
resp.Error = fmt.Errorf("state is nil")

return nil, false
}

if req.State.Values == nil {
resp.Error = fmt.Errorf("state does not contain any state values")

return nil, false
}

if req.State.Values.RootModule == nil {
resp.Error = fmt.Errorf("state does not contain a root module")

return nil, false
}

for _, r := range req.State.Values.RootModule.Resources {
if b.resourceAddress == r.Address {
resource = r

break
}
}

if resource == nil {
resp.Error = fmt.Errorf("%s - Resource not found in state", b.resourceAddress)

return nil, false
}

return resource, true
}

func (b Base) ResourceAddress() string {
return b.resourceAddress
}
112 changes: 112 additions & 0 deletions internal/acctest/statecheck/full_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package statecheck

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfmaps "github.com/hashicorp/terraform-provider-aws/internal/maps"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/types"
)

var _ statecheck.StateCheck = expectFullTagsCheck{}

type expectFullTagsCheck struct {
base Base
knownValue knownvalue.Check
servicePackage conns.ServicePackage
}

func (e expectFullTagsCheck) CheckState(ctx context.Context, req statecheck.CheckStateRequest, resp *statecheck.CheckStateResponse) {
res, ok := e.base.ResourceFromState(req, resp)
if !ok {
return
}

var tagsSpec *types.ServicePackageResourceTags
sp := e.servicePackage
for _, r := range sp.FrameworkResources(ctx) {
foo, _ := r.Factory(ctx)
var metadata resource.MetadataResponse
foo.Metadata(ctx, resource.MetadataRequest{}, &metadata)
if res.Type == metadata.TypeName {
tagsSpec = r.Tags
break
}
}
if tagsSpec == nil {
for _, r := range sp.SDKResources(ctx) {
if res.Type == r.TypeName {
tagsSpec = r.Tags
break
}
}
}

if tagsSpec == nil {
resp.Error = fmt.Errorf("no tagging specification found for resource type %s", res.Type)
return
}

identifierAttr := tagsSpec.IdentifierAttribute

identifier, ok := res.AttributeValues[identifierAttr]
if !ok {
resp.Error = fmt.Errorf("attribute %q not found in resource %s", identifierAttr, e.base.ResourceAddress())
return
}

ctx = tftags.NewContext(ctx, nil, nil)

var err error
if v, ok := sp.(tftags.ServiceTagLister); ok {
err = v.ListTags(ctx, acctest.Provider.Meta(), identifier.(string)) // Sets tags in Context
} else if v, ok := sp.(tftags.ResourceTypeTagLister); ok && tagsSpec.ResourceType != "" {
err = v.ListTags(ctx, acctest.Provider.Meta(), identifier.(string), tagsSpec.ResourceType) // Sets tags in Context
} else {
err = fmt.Errorf("no ListTags method found for service %s", sp.ServicePackageName())
}
if err != nil {
resp.Error = fmt.Errorf("listing tags for %s: %s", e.base.ResourceAddress(), err)
return
}

tagsInContext, ok := tftags.FromContext(ctx)
if !ok {
resp.Error = fmt.Errorf("Unable to retrieve tags from context")
return
}

var tags tftags.KeyValueTags
if tagsInContext.TagsOut.IsSome() {
tags, _ = tagsInContext.TagsOut.Unwrap()
} else {
resp.Error = fmt.Errorf("No output tags found in context")
return
}

tagsMap := tfmaps.ApplyToAllValues(tags.Map(), func(s string) any {
return s
})

if err := e.knownValue.CheckValue(tagsMap); err != nil {
resp.Error = fmt.Errorf("error checking remote tags for %s: %s", e.base.ResourceAddress(), err) // nosemgrep:ci.semgrep.errors.no-fmt.Errorf-leading-error
return
}
}

func ExpectFullTags(servicePackage conns.ServicePackage, resourceAddress string, knownValue knownvalue.Check) expectFullTagsCheck {
return expectFullTagsCheck{
base: NewBase(resourceAddress),
knownValue: knownValue,
servicePackage: servicePackage,
}
}
11 changes: 11 additions & 0 deletions internal/generate/acctestconsts/consts.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// Code generated by internal/generate/acctestconsts/main.go; DO NOT EDIT.

package acctest

const (
{{- range .Constants }}
Ct{{ .Constant }} = "{{ .Literal }}"
{{- end }}
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import (
"fmt"
)

const (
{{- range .Constants }}
Ct{{ .Constant }} = "{{ .Literal }}"
{{- end }}
)

// ConstOrQuote returns the constant name for the given attribute if it exists.
// Otherwise, it returns the attribute quoted. This is intended for use in
// generated code and templates.
Expand Down
Loading
Loading