Skip to content

Commit

Permalink
Merge pull request #39658 from hashicorp/td-tftags-settagsdiff
Browse files Browse the repository at this point in the history
provider: Converts `conns.AWSClient` `DefaultTagsConfig` to function and adds transparent tagging to several data sources
  • Loading branch information
gdavison authored Oct 15, 2024
2 parents 68730b2 + a252a62 commit 4691dea
Show file tree
Hide file tree
Showing 266 changed files with 55,480 additions and 1,557 deletions.
12 changes: 6 additions & 6 deletions docs/resource-tagging.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ implement the logic to convert the configuration tags into the service tags, e.g
=== "Terraform Plugin SDK V2"
```go
// Typically declared near conn := /*...*/
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig(ctx)
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &eks.CreateClusterInput{
Expand All @@ -349,7 +349,7 @@ If the service API does not allow passing an empty list, the logic can be adjust
=== "Terraform Plugin SDK V2"
```go
// Typically declared near conn := /*...*/
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig(ctx)
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &eks.CreateClusterInput{
Expand All @@ -367,7 +367,7 @@ implement the logic to convert the configuration tags into the service API call
=== "Terraform Plugin SDK V2"
```go
// Typically declared near conn := /*...*/
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig(ctx)
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

/* ... creation steps ... */
Expand All @@ -386,7 +386,7 @@ This example shows using `TagSpecifications`:
=== "Terraform Plugin SDK V2"
```go
// Typically declared near conn := /*...*/
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig(ctx)
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &ec2.CreateFleetInput{
Expand All @@ -402,7 +402,7 @@ In the resource `Read` operation, implement the logic to convert the service tag
=== "Terraform Plugin SDK V2"
```go
// Typically declared near conn := /*...*/
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig(ctx)
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

/* ... other d.Set(...) logic ... */
Expand All @@ -424,7 +424,7 @@ use the generated `listTags` function, e.g., with Athena Workgroups:
=== "Terraform Plugin SDK V2"
```go
// Typically declared near conn := /*...*/
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig(ctx)
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

/* ... other d.Set(...) logic ... */
Expand Down
6 changes: 5 additions & 1 deletion internal/conns/awsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

type AWSClient struct {
AccountID string
DefaultTagsConfig *tftags.DefaultConfig
defaultTagsConfig *tftags.DefaultConfig
IgnoreTagsConfig *tftags.IgnoreConfig
Partition string
Region string
Expand Down Expand Up @@ -55,6 +55,10 @@ func (c *AWSClient) CredentialsProvider(context.Context) aws_sdkv2.CredentialsPr
return c.awsConfig.Credentials
}

func (c *AWSClient) DefaultTagsConfig(context.Context) *tftags.DefaultConfig {
return c.defaultTagsConfig
}

func (c *AWSClient) AwsConfig(context.Context) aws_sdkv2.Config { // nosemgrep:ci.aws-in-func-name
return c.awsConfig.Copy()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/conns/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (c *Config) ConfigureProvider(ctx context.Context, client *AWSClient) (*AWS
}

client.AccountID = accountID
client.DefaultTagsConfig = c.DefaultTagsConfig
client.defaultTagsConfig = c.DefaultTagsConfig
client.dnsSuffix = dnsSuffix
client.IgnoreTagsConfig = c.IgnoreTagsConfig
client.Partition = partition
Expand Down
13 changes: 13 additions & 0 deletions internal/conns/testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package conns

import (
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

// SetDefaultTagsConfig is only intended for use in tests
func SetDefaultTagsConfig(client *AWSClient, d *tftags.DefaultConfig) {
client.defaultTagsConfig = d
}
2 changes: 1 addition & 1 deletion internal/framework/resource_with_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (r *ResourceWithConfigure) SetTagsAll(ctx context.Context, request resource
return
}

defaultTagsConfig := r.Meta().DefaultTagsConfig
defaultTagsConfig := r.Meta().DefaultTagsConfig(ctx)
ignoreTagsConfig := r.Meta().IgnoreTagsConfig

var planTags tftags.Map
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/fwprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (p *fwprovider) DataSources(ctx context.Context) []func() datasource.DataSo
bootstrapContext := func(ctx context.Context, meta *conns.AWSClient) context.Context {
ctx = conns.NewDataSourceContext(ctx, servicePackageName, v.Name)
if meta != nil {
ctx = tftags.NewContext(ctx, meta.DefaultTagsConfig, meta.IgnoreTagsConfig)
ctx = tftags.NewContext(ctx, meta.DefaultTagsConfig(ctx), meta.IgnoreTagsConfig)
ctx = meta.RegisterLogger(ctx)
}

Expand Down Expand Up @@ -405,7 +405,7 @@ func (p *fwprovider) Resources(ctx context.Context) []func() resource.Resource {
bootstrapContext := func(ctx context.Context, meta *conns.AWSClient) context.Context {
ctx = conns.NewResourceContext(ctx, servicePackageName, v.Name)
if meta != nil {
ctx = tftags.NewContext(ctx, meta.DefaultTagsConfig, meta.IgnoreTagsConfig)
ctx = tftags.NewContext(ctx, meta.DefaultTagsConfig(ctx), meta.IgnoreTagsConfig)
ctx = meta.RegisterLogger(ctx)
ctx = flex.RegisterLogger(ctx)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func New(ctx context.Context) (*schema.Provider, error) {
bootstrapContext := func(ctx context.Context, meta any) context.Context {
ctx = conns.NewDataSourceContext(ctx, servicePackageName, v.Name)
if v, ok := meta.(*conns.AWSClient); ok {
ctx = tftags.NewContext(ctx, v.DefaultTagsConfig, v.IgnoreTagsConfig)
ctx = tftags.NewContext(ctx, v.DefaultTagsConfig(ctx), v.IgnoreTagsConfig)
ctx = v.RegisterLogger(ctx)
}

Expand Down Expand Up @@ -371,7 +371,7 @@ func New(ctx context.Context) (*schema.Provider, error) {
bootstrapContext := func(ctx context.Context, meta any) context.Context {
ctx = conns.NewResourceContext(ctx, servicePackageName, v.Name)
if v, ok := meta.(*conns.AWSClient); ok {
ctx = tftags.NewContext(ctx, v.DefaultTagsConfig, v.IgnoreTagsConfig)
ctx = tftags.NewContext(ctx, v.DefaultTagsConfig(ctx), v.IgnoreTagsConfig)
ctx = v.RegisterLogger(ctx)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/provider_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ func testAccCheckProviderDefaultTags_Tags(ctx context.Context, t *testing.T, p *
}

providerClient := (*p).Meta().(*conns.AWSClient)
defaultTagsConfig := providerClient.DefaultTagsConfig
defaultTagsConfig := providerClient.DefaultTagsConfig(ctx)

if defaultTagsConfig == nil || len(defaultTagsConfig.Tags) == 0 {
if len(expectedTags) != 0 {
Expand Down
8 changes: 4 additions & 4 deletions internal/provider/tags_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ func TestTagsResourceInterceptor(t *testing.T) {
ServicePackages: map[string]conns.ServicePackage{
"Test": &mockService{},
},
DefaultTagsConfig: expandDefaultTags(context.Background(), map[string]interface{}{
"tag": "",
}),
IgnoreTagsConfig: expandIgnoreTags(context.Background(), map[string]interface{}{
"tag2": "tag",
}),
}
conns.SetDefaultTagsConfig(conn, expandDefaultTags(context.Background(), map[string]interface{}{
"tag": "",
}))

bootstrapContext := func(ctx context.Context, meta any) context.Context {
ctx = conns.NewResourceContext(ctx, "Test", "aws_test")
if v, ok := meta.(*conns.AWSClient); ok {
ctx = tftags.NewContext(ctx, v.DefaultTagsConfig, v.IgnoreTagsConfig)
ctx = tftags.NewContext(ctx, v.DefaultTagsConfig(ctx), v.IgnoreTagsConfig)
}

return ctx
Expand Down
9 changes: 9 additions & 0 deletions internal/service/datapipeline/exports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package datapipeline

var (
FindPipeline = findPipeline
WaitForDeletion = waitForDeletion
)
1 change: 1 addition & 0 deletions internal/service/datapipeline/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

//go:generate go run ../../generate/tags/main.go -ListTagsInIDElem=PipelineId -ServiceTagsSlice -TagOp=AddTags -TagInIDElem=PipelineId -UntagOp=RemoveTags -UpdateTags
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.

package datapipeline
13 changes: 7 additions & 6 deletions internal/service/datapipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (
)

// @SDKResource("aws_datapipeline_pipeline", name="Pipeline")
// @Tags(identifierAttribute="id")
func ResourcePipeline() *schema.Resource {
// @Tags(identifierAttribute="id", resourceType="Pipeline")
// @Testing(existsType="github.com/aws/aws-sdk-go-v2/service/datapipeline/types;awstypes;awstypes.PipelineDescription")
func resourcePipeline() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourcePipelineCreate,
ReadWithoutTimeout: resourcePipelineRead,
Expand Down Expand Up @@ -87,7 +88,7 @@ func resourcePipelineRead(ctx context.Context, d *schema.ResourceData, meta inte
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).DataPipelineClient(ctx)

v, err := PipelineRetrieve(ctx, d.Id(), conn)
v, err := findPipeline(ctx, conn, d.Id())
if errs.IsA[*awstypes.PipelineNotFoundException](err) || errs.IsA[*awstypes.PipelineDeletedException](err) || v == nil {
log.Printf("[WARN] DataPipeline (%s) not found, removing from state", d.Id())
d.SetId("")
Expand Down Expand Up @@ -129,13 +130,13 @@ func resourcePipelineDelete(ctx context.Context, d *schema.ResourceData, meta in
return sdkdiag.AppendErrorf(diags, "deleting Data Pipeline %s: %s", d.Id(), err)
}

if err := WaitForDeletion(ctx, conn, d.Id()); err != nil {
if err := waitForDeletion(ctx, conn, d.Id()); err != nil {
return sdkdiag.AppendErrorf(diags, "deleting Data Pipeline %s: %s", d.Id(), err)
}
return diags
}

func PipelineRetrieve(ctx context.Context, id string, conn *datapipeline.Client) (*awstypes.PipelineDescription, error) {
func findPipeline(ctx context.Context, conn *datapipeline.Client, id string) (*awstypes.PipelineDescription, error) {
opts := datapipeline.DescribePipelinesInput{
PipelineIds: []string{id},
}
Expand All @@ -157,7 +158,7 @@ func PipelineRetrieve(ctx context.Context, id string, conn *datapipeline.Client)
return &pipeline, nil
}

func WaitForDeletion(ctx context.Context, conn *datapipeline.Client, pipelineID string) error {
func waitForDeletion(ctx context.Context, conn *datapipeline.Client, pipelineID string) error {
params := &datapipeline.DescribePipelinesInput{
PipelineIds: []string{pipelineID},
}
Expand Down
16 changes: 5 additions & 11 deletions internal/service/datapipeline/pipeline_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

// @SDKDataSource("aws_datapipeline_pipeline")
func DataSourcePipeline() *schema.Resource {
// @Tags
func dataSourcePipeline() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourcePipelineRead,

Expand All @@ -41,26 +42,19 @@ func dataSourcePipelineRead(ctx context.Context, d *schema.ResourceData, meta in
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).DataPipelineClient(ctx)
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

pipelineId := d.Get("pipeline_id").(string)

v, err := PipelineRetrieve(ctx, pipelineId, conn)
v, err := findPipeline(ctx, conn, pipelineId)
if err != nil {
return sdkdiag.AppendErrorf(diags, "describing DataPipeline Pipeline (%s): %s", pipelineId, err)
}

d.SetId(pipelineId)
d.Set(names.AttrName, v.Name)
d.Set(names.AttrDescription, v.Description)

tags := KeyValueTags(ctx, v.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

if err := d.Set(names.AttrTags, tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return sdkdiag.AppendErrorf(diags, "setting tags: %s", err)
}

d.SetId(pipelineId)
setTagsOut(ctx, v.Tags)

return diags
}

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

6 changes: 6 additions & 0 deletions internal/service/datapipeline/pipeline_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand All @@ -32,6 +35,9 @@ func TestAccDataPipelinePipelineDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrName, resourceName, names.AttrName),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrDescription, resourceName, names.AttrDescription),
),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(dataSourceName, tfjsonpath.New(names.AttrTags), knownvalue.MapExact(map[string]knownvalue.Check{})),
},
},
},
})
Expand Down
Loading

0 comments on commit 4691dea

Please sign in to comment.