Skip to content

Commit

Permalink
Merge pull request #30516 from hashicorp/td-transparent-tagging-phase…
Browse files Browse the repository at this point in the history
…3c-k

Tech debt: Reduce `tags` boilerplate code - Plugin SDK resources `k*` (Phase 3c)
  • Loading branch information
ewbankkit authored Apr 7, 2023
2 parents 433d308 + a9ea5f0 commit 8e11a9d
Show file tree
Hide file tree
Showing 20 changed files with 142 additions and 469 deletions.
33 changes: 7 additions & 26 deletions internal/service/kafka/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_msk_cluster")
// @SDKResource("aws_msk_cluster", name="Cluster")
// @Tags(identifierAttribute="id")
func ResourceCluster() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceClusterCreate,
Expand Down Expand Up @@ -466,8 +468,8 @@ func ResourceCluster() *schema.Resource {
Computed: true,
ValidateFunc: validation.StringInSlice(kafka.StorageMode_Values(), true),
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"zookeeper_connect_string": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -482,15 +484,13 @@ func ResourceCluster() *schema.Resource {

func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KafkaConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

name := d.Get("cluster_name").(string)
input := &kafka.CreateClusterInput{
ClusterName: aws.String(name),
KafkaVersion: aws.String(d.Get("kafka_version").(string)),
NumberOfBrokerNodes: aws.Int64(int64(d.Get("number_of_broker_nodes").(int))),
Tags: Tags(tags.IgnoreAWS()),
Tags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("broker_node_group_info"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
Expand Down Expand Up @@ -544,8 +544,6 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta int

func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KafkaConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

cluster, err := FindClusterByARN(ctx, conn, d.Id())

Expand Down Expand Up @@ -638,16 +636,7 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta inter
d.Set("zookeeper_connect_string", SortEndpointsString(aws.StringValue(cluster.ZookeeperConnectString)))
d.Set("zookeeper_connect_string_tls", SortEndpointsString(aws.StringValue(cluster.ZookeeperConnectStringTls)))

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

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.Errorf("setting tags: %s", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return diag.Errorf("setting tags_all: %s", err)
}
SetTagsOut(ctx, cluster.Tags)

return nil
}
Expand Down Expand Up @@ -935,14 +924,6 @@ func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, meta int
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTags(ctx, conn, d.Id(), o, n); err != nil {
return diag.Errorf("updating MSK Cluster (%s) tags: %s", d.Id(), err)
}
}

return resourceClusterRead(ctx, d, meta)
}

Expand Down
36 changes: 8 additions & 28 deletions internal/service/kafka/serverless_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_msk_serverless_cluster")
// @SDKResource("aws_msk_serverless_cluster", name="Serverless Cluster")
// @Tags(identifierAttribute="id")
func ResourceServerlessCluster() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceServerlessClusterCreate,
Expand Down Expand Up @@ -82,8 +84,8 @@ func ResourceServerlessCluster() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 64),
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"vpc_config": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -116,8 +118,6 @@ func ResourceServerlessCluster() *schema.Resource {

func resourceServerlessClusterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KafkaConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

name := d.Get("cluster_name").(string)
input := &kafka.CreateClusterV2Input{
Expand All @@ -126,7 +126,7 @@ func resourceServerlessClusterCreate(ctx context.Context, d *schema.ResourceData
ClientAuthentication: expandServerlessClientAuthentication(d.Get("client_authentication").([]interface{})[0].(map[string]interface{})),
VpcConfigs: expandVpcConfigs(d.Get("vpc_config").([]interface{})),
},
Tags: Tags(tags.IgnoreAWS()),
Tags: GetTagsIn(ctx),
}

log.Printf("[DEBUG] Creating MSK Serverless Cluster: %s", input)
Expand All @@ -149,8 +149,6 @@ func resourceServerlessClusterCreate(ctx context.Context, d *schema.ResourceData

func resourceServerlessClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KafkaConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

cluster, err := FindServerlessClusterByARN(ctx, conn, d.Id())

Expand All @@ -177,31 +175,13 @@ func resourceServerlessClusterRead(ctx context.Context, d *schema.ResourceData,
return diag.Errorf("setting vpc_config: %s", err)
}

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

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.Errorf("setting tags: %s", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return diag.Errorf("setting tags_all: %s", err)
}
SetTagsOut(ctx, cluster.Tags)

return nil
}

func resourceServerlessClusterUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KafkaConn()

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTags(ctx, conn, d.Id(), o, n); err != nil {
return diag.Errorf("updating MSK Serverless Cluster (%s) tags: %s", d.Id(), err)
}
}

// Tags only.
return resourceServerlessClusterRead(ctx, d, meta)
}

Expand Down
8 changes: 8 additions & 0 deletions internal/service/kafka/service_package_gen.go

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

43 changes: 6 additions & 37 deletions internal/service/kendra/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

const (
Expand All @@ -34,7 +35,8 @@ const (
validationExceptionMessageDataSourceSecrets = "Secrets Manager throws the exception"
)

// @SDKResource("aws_kendra_data_source")
// @SDKResource("aws_kendra_data_source", name="Data Source")
// @Tags(identifierAttribute="arn")
func ResourceDataSource() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceDataSourceCreate,
Expand Down Expand Up @@ -468,8 +470,8 @@ func ResourceDataSource() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},
}
}
Expand Down Expand Up @@ -587,15 +589,13 @@ func documentAttributeValueSchema() *schema.Schema {

func resourceDataSourceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KendraClient()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

name := d.Get("name").(string)

input := &kendra.CreateDataSourceInput{
ClientToken: aws.String(id.UniqueId()),
IndexId: aws.String(d.Get("index_id").(string)),
Name: aws.String(name),
Tags: GetTagsIn(ctx),
Type: types.DataSourceType(d.Get("type").(string)),
}

Expand Down Expand Up @@ -623,12 +623,6 @@ func resourceDataSourceCreate(ctx context.Context, d *schema.ResourceData, meta
input.Schedule = aws.String(v.(string))
}

if len(tags) > 0 {
input.Tags = Tags(tags.IgnoreAWS())
}

log.Printf("[DEBUG] Creating Kendra Data Source %#v", input)

outputRaw, err := tfresource.RetryWhen(ctx, propagationTimeout,
func() (interface{}, error) {
return conn.CreateDataSource(ctx, input)
Expand Down Expand Up @@ -668,8 +662,6 @@ func resourceDataSourceCreate(ctx context.Context, d *schema.ResourceData, meta

func resourceDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KendraClient()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

id, indexId, err := DataSourceParseResourceID(d.Id())
if err != nil {
Expand Down Expand Up @@ -718,21 +710,6 @@ func resourceDataSourceRead(ctx context.Context, d *schema.ResourceData, meta in
return diag.FromErr(err)
}

tags, err := ListTags(ctx, conn, arn)
if err != nil {
return diag.Errorf("listing tags for resource (%s): %s", arn, err)
}
tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.Errorf("setting tags: %s", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return diag.Errorf("setting tags_all: %s", err)
}

return nil
}

Expand Down Expand Up @@ -804,14 +781,6 @@ func resourceDataSourceUpdate(ctx context.Context, d *schema.ResourceData, meta
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTags(ctx, conn, d.Get("arn").(string), o, n); err != nil {
return diag.FromErr(fmt.Errorf("updating Kendra Data Source (%s) tags: %s", d.Id(), err))
}
}

return resourceDataSourceRead(ctx, d, meta)
}

Expand Down
46 changes: 7 additions & 39 deletions internal/service/kendra/faq.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_kendra_faq")
// @SDKResource("aws_kendra_faq", name="FAQ")
// @Tags(identifierAttribute="arn")
func ResourceFaq() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceFaqCreate,
Expand Down Expand Up @@ -137,25 +139,23 @@ func ResourceFaq() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},
}
}

func resourceFaqCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KendraClient()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

name := d.Get("name").(string)

input := &kendra.CreateFaqInput{
ClientToken: aws.String(id.UniqueId()),
IndexId: aws.String(d.Get("index_id").(string)),
Name: aws.String(name),
RoleArn: aws.String(d.Get("role_arn").(string)),
S3Path: expandS3Path(d.Get("s3_path").([]interface{})),
Tags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("description"); ok {
Expand All @@ -170,12 +170,6 @@ func resourceFaqCreate(ctx context.Context, d *schema.ResourceData, meta interfa
input.LanguageCode = aws.String(v.(string))
}

if len(tags) > 0 {
input.Tags = Tags(tags.IgnoreAWS())
}

log.Printf("[DEBUG] Creating Kendra Faq %#v", input)

outputRaw, err := tfresource.RetryWhen(ctx, propagationTimeout,
func() (interface{}, error) {
return conn.CreateFaq(ctx, input)
Expand Down Expand Up @@ -215,8 +209,6 @@ func resourceFaqCreate(ctx context.Context, d *schema.ResourceData, meta interfa

func resourceFaqRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KendraClient()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

id, indexId, err := FaqParseResourceID(d.Id())
if err != nil {
Expand Down Expand Up @@ -260,35 +252,11 @@ func resourceFaqRead(ctx context.Context, d *schema.ResourceData, meta interface
return diag.FromErr(err)
}

tags, err := ListTags(ctx, conn, arn)
if err != nil {
return diag.Errorf("listing tags for resource (%s): %s", arn, err)
}
tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.Errorf("setting tags: %s", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return diag.Errorf("setting tags_all: %s", err)
}

return nil
}

func resourceFaqUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).KendraClient()

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTags(ctx, conn, d.Get("arn").(string), o, n); err != nil {
return diag.FromErr(fmt.Errorf("updating Kendra Faq (%s) tags: %s", d.Id(), err))
}
}

// Tags only.
return resourceFaqRead(ctx, d, meta)
}

Expand Down
Loading

0 comments on commit 8e11a9d

Please sign in to comment.