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

Tech debt: Reduce tags boilerplate code - Plugin SDK resources n* (Phase 3c) #30491

Merged
merged 3 commits into from
Apr 6, 2023
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
41 changes: 7 additions & 34 deletions internal/service/neptune/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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 @@ -37,7 +38,8 @@ const (
ServerlessMaxNCUs = 128.0
)

// @SDKResource("aws_neptune_cluster")
// @SDKResource("aws_neptune_cluster", name="Cluster")
// @Tags(identifierAttribute="arn")
func ResourceCluster() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceClusterCreate,
Expand Down Expand Up @@ -278,8 +280,8 @@ func ResourceCluster() *schema.Resource {
ForceNew: true,
Default: false,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"vpc_security_group_ids": {
Type: schema.TypeSet,
Optional: true,
Expand All @@ -295,8 +297,6 @@ func ResourceCluster() *schema.Resource {
func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).NeptuneConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

// Check if any of the parameters that require a cluster modification after creation are set.
// See https://docs.aws.amazon.com/neptune/latest/userguide/backup-restore-restore-snapshot.html#backup-restore-restore-snapshot-considerations.
Expand All @@ -322,7 +322,7 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta int
Port: aws.Int64(int64(d.Get("port").(int))),
StorageEncrypted: aws.Bool(d.Get("storage_encrypted").(bool)),
DeletionProtection: aws.Bool(d.Get("deletion_protection").(bool)),
Tags: Tags(tags.IgnoreAWS()),
Tags: GetTagsIn(ctx),
ServerlessV2ScalingConfiguration: serverlessConfiguration,
}
inputR := &neptune.RestoreDBClusterFromSnapshotInput{
Expand All @@ -332,7 +332,7 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta int
Port: aws.Int64(int64(d.Get("port").(int))),
SnapshotIdentifier: aws.String(d.Get("snapshot_identifier").(string)),
DeletionProtection: aws.Bool(d.Get("deletion_protection").(bool)),
Tags: Tags(tags.IgnoreAWS()),
Tags: GetTagsIn(ctx),
ServerlessV2ScalingConfiguration: serverlessConfiguration,
}
inputM := &neptune.ModifyDBClusterInput{
Expand Down Expand Up @@ -482,8 +482,6 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta int
func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).NeptuneConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

dbc, err := FindClusterByID(ctx, conn, d.Id())

Expand Down Expand Up @@ -549,23 +547,6 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta inter
}
d.Set("vpc_security_group_ids", securityGroupIDs)

tags, err := ListTags(ctx, conn, arn)

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for Neptune Cluster (%s): %s", arn, err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

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

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

return diags
}

Expand Down Expand Up @@ -728,14 +709,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.Get("arn").(string), o, n); err != nil {
return sdkdiag.AppendErrorf(diags, "updating Neptune Cluster (%s) tags: %s", d.Get("arn").(string), err)
}
}

return append(diags, resourceClusterRead(ctx, d, meta)...)
}

Expand Down
50 changes: 9 additions & 41 deletions internal/service/neptune/cluster_endpoint.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_neptune_cluster_endpoint")
// @SDKResource("aws_neptune_cluster_endpoint", name="Cluster Endpoint")
// @Tags(identifierAttribute="arn")
func ResourceClusterEndpoint() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceClusterEndpointCreate,
Expand Down Expand Up @@ -68,8 +70,8 @@ func ResourceClusterEndpoint() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
Expand All @@ -79,13 +81,12 @@ func ResourceClusterEndpoint() *schema.Resource {
func resourceClusterEndpointCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).NeptuneConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &neptune.CreateDBClusterEndpointInput{
DBClusterEndpointIdentifier: aws.String(d.Get("cluster_endpoint_identifier").(string)),
DBClusterIdentifier: aws.String(d.Get("cluster_identifier").(string)),
EndpointType: aws.String(d.Get("endpoint_type").(string)),
Tags: GetTagsIn(ctx),
}

if attr := d.Get("static_members").(*schema.Set); attr.Len() > 0 {
Expand All @@ -97,8 +98,8 @@ func resourceClusterEndpointCreate(ctx context.Context, d *schema.ResourceData,
}

// Tags are currently only supported in AWS Commercial.
if len(tags) > 0 && meta.(*conns.AWSClient).Partition == endpoints.AwsPartitionID {
input.Tags = Tags(tags.IgnoreAWS())
if meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID {
input.Tags = nil
}

out, err := conn.CreateDBClusterEndpointWithContext(ctx, input)
Expand All @@ -121,10 +122,9 @@ func resourceClusterEndpointCreate(ctx context.Context, d *schema.ResourceData,
func resourceClusterEndpointRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).NeptuneConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

resp, err := FindEndpointByID(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
d.SetId("")
log.Printf("[DEBUG] Neptune Cluster Endpoint (%s) not found", d.Id())
Expand All @@ -145,29 +145,6 @@ func resourceClusterEndpointRead(ctx context.Context, d *schema.ResourceData, me
arn := aws.StringValue(resp.DBClusterEndpointArn)
d.Set("arn", arn)

// Tags are currently only supported in AWS Commercial.
if meta.(*conns.AWSClient).Partition == endpoints.AwsPartitionID {
tags, err := ListTags(ctx, conn, arn)

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for Neptune Cluster Endpoint (%s): %s", arn, err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

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

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

return diags
}

Expand Down Expand Up @@ -203,15 +180,6 @@ func resourceClusterEndpointUpdate(ctx context.Context, d *schema.ResourceData,
}
}

// Tags are currently only supported in AWS Commercial.
if d.HasChange("tags_all") && meta.(*conns.AWSClient).Partition == endpoints.AwsPartitionID {
o, n := d.GetChange("tags_all")

if err := UpdateTags(ctx, conn, d.Get("arn").(string), o, n); err != nil {
return sdkdiag.AppendErrorf(diags, "updating Neptune Cluster Endpoint (%s) tags: %s", d.Get("arn").(string), err)
}
}

return append(diags, resourceClusterEndpointRead(ctx, d, meta)...)
}

Expand Down
39 changes: 6 additions & 33 deletions internal/service/neptune/cluster_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,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_neptune_cluster_instance")
// @SDKResource("aws_neptune_cluster_instance", name="Cluster")
// @Tags(identifierAttribute="arn")
func ResourceClusterInstance() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceClusterInstanceCreate,
Expand Down Expand Up @@ -164,8 +166,8 @@ func ResourceClusterInstance() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"writer": {
Type: schema.TypeBool,
Computed: true,
Expand All @@ -179,8 +181,6 @@ func ResourceClusterInstance() *schema.Resource {
func resourceClusterInstanceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).NeptuneConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

var instanceID string
if v, ok := d.GetOk("identifier"); ok {
Expand All @@ -199,7 +199,7 @@ func resourceClusterInstanceCreate(ctx context.Context, d *schema.ResourceData,
Engine: aws.String(d.Get("engine").(string)),
PromotionTier: aws.Int64(int64(d.Get("promotion_tier").(int))),
PubliclyAccessible: aws.Bool(d.Get("publicly_accessible").(bool)),
Tags: Tags(tags.IgnoreAWS()),
Tags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("availability_zone"); ok {
Expand Down Expand Up @@ -246,8 +246,6 @@ func resourceClusterInstanceCreate(ctx context.Context, d *schema.ResourceData,
func resourceClusterInstanceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).NeptuneConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

db, err := FindClusterInstanceByID(ctx, conn, d.Id())

Expand Down Expand Up @@ -301,23 +299,6 @@ func resourceClusterInstanceRead(ctx context.Context, d *schema.ResourceData, me

d.Set("writer", m.IsClusterWriter)

tags, err := ListTags(ctx, conn, d.Get("arn").(string))

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for Neptune Cluster Instance (%s): %s", d.Get("arn").(string), err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

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

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

return diags
}

Expand Down Expand Up @@ -368,14 +349,6 @@ func resourceClusterInstanceUpdate(ctx context.Context, d *schema.ResourceData,
}
}

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 sdkdiag.AppendErrorf(diags, "updating Neptune Cluster Instance (%s) tags: %s", d.Get("arn").(string), err)
}
}

return append(diags, resourceClusterInstanceRead(ctx, d, meta)...)
}

Expand Down
40 changes: 6 additions & 34 deletions internal/service/neptune/cluster_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

const clusterParameterGroupMaxParamsBulkEdit = 20

// @SDKResource("aws_neptune_cluster_parameter_group")
// @SDKResource("aws_neptune_cluster_parameter_group", name="Cluster Parameter Group")
// @Tags(identifierAttribute="arn")
func ResourceClusterParameterGroup() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceClusterParameterGroupCreate,
Expand Down Expand Up @@ -87,9 +89,8 @@ func ResourceClusterParameterGroup() *schema.Resource {
},
},
},

"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
Expand All @@ -99,8 +100,6 @@ func ResourceClusterParameterGroup() *schema.Resource {
func resourceClusterParameterGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).NeptuneConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

var groupName string
if v, ok := d.GetOk("name"); ok {
Expand All @@ -115,7 +114,7 @@ func resourceClusterParameterGroupCreate(ctx context.Context, d *schema.Resource
DBClusterParameterGroupName: aws.String(groupName),
DBParameterGroupFamily: aws.String(d.Get("family").(string)),
Description: aws.String(d.Get("description").(string)),
Tags: Tags(tags.IgnoreAWS()),
Tags: GetTagsIn(ctx),
}

_, err := conn.CreateDBClusterParameterGroupWithContext(ctx, &createOpts)
Expand All @@ -138,8 +137,6 @@ func resourceClusterParameterGroupCreate(ctx context.Context, d *schema.Resource
func resourceClusterParameterGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).NeptuneConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

describeOpts := neptune.DescribeDBClusterParameterGroupsInput{
DBClusterParameterGroupName: aws.String(d.Id()),
Expand Down Expand Up @@ -187,23 +184,6 @@ func resourceClusterParameterGroupRead(ctx context.Context, d *schema.ResourceDa
return sdkdiag.AppendErrorf(diags, "setting neptune parameter: %s", err)
}

tags, err := ListTags(ctx, conn, d.Get("arn").(string))

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for Neptune Cluster Parameter Group (%s): %s", d.Id(), err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

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

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

return diags
}

Expand Down Expand Up @@ -233,14 +213,6 @@ func resourceClusterParameterGroupUpdate(ctx context.Context, d *schema.Resource
}
}

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 sdkdiag.AppendErrorf(diags, "updating Neptune Cluster Parameter Group (%s) tags: %s", d.Id(), err)
}
}

return append(diags, resourceClusterParameterGroupRead(ctx, d, meta)...)
}

Expand Down
Loading