Skip to content

Commit

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

Tech debt: Reduce `tags` boilerplate code - Plugin SDK resources `a*` (Phase 3c)
  • Loading branch information
ewbankkit authored Apr 4, 2023
2 parents 3bde4e9 + 7ac07ab commit c718518
Show file tree
Hide file tree
Showing 65 changed files with 493 additions and 1,473 deletions.
2 changes: 1 addition & 1 deletion internal/service/accessanalyzer/accessanalyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAccAccessAnalyzer_serial(t *testing.T) {
"Analyzer": {
"basic": testAccAnalyzer_basic,
"disappears": testAccAnalyzer_disappears,
"Tags": testAccAnalyzer_Tags,
"tags": testAccAnalyzer_tags,
"Type_Organization": testAccAnalyzer_Type_Organization,
},
"ArchiveRule": {
Expand Down
35 changes: 9 additions & 26 deletions internal/service/accessanalyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 @@ -29,7 +30,8 @@ const (
organizationCreationTimeout = 10 * time.Minute
)

// @SDKResource("aws_accessanalyzer_analyzer")
// @SDKResource("aws_accessanalyzer_analyzer", name="Analyzer")
// @Tags(identifierAttribute="arn")
func ResourceAnalyzer() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAnalyzerCreate,
Expand All @@ -54,8 +56,8 @@ func ResourceAnalyzer() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"type": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -75,14 +77,12 @@ func ResourceAnalyzer() *schema.Resource {
func resourceAnalyzerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AccessAnalyzerConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))
analyzerName := d.Get("analyzer_name").(string)

analyzerName := d.Get("analyzer_name").(string)
input := &accessanalyzer.CreateAnalyzerInput{
AnalyzerName: aws.String(analyzerName),
ClientToken: aws.String(id.UniqueId()),
Tags: Tags(tags.IgnoreAWS()),
Tags: GetTagsIn(ctx),
Type: aws.String(d.Get("type").(string)),
}

Expand Down Expand Up @@ -117,8 +117,6 @@ func resourceAnalyzerCreate(ctx context.Context, d *schema.ResourceData, meta in
func resourceAnalyzerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AccessAnalyzerConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

input := &accessanalyzer.GetAnalyzerInput{
AnalyzerName: aws.String(d.Id()),
Expand All @@ -143,16 +141,7 @@ func resourceAnalyzerRead(ctx context.Context, d *schema.ResourceData, meta inte
d.Set("analyzer_name", output.Analyzer.Name)
d.Set("arn", output.Analyzer.Arn)

tags := KeyValueTags(ctx, output.Analyzer.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)
}
SetTagsOut(ctx, output.Analyzer.Tags)

d.Set("type", output.Analyzer.Type)

Expand All @@ -161,14 +150,8 @@ func resourceAnalyzerRead(ctx context.Context, d *schema.ResourceData, meta inte

func resourceAnalyzerUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).AccessAnalyzerConn()

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 Access Analyzer Analyzer (%s) tags: %s", d.Id(), err)
}
}
// Tags only.

return append(diags, resourceAnalyzerRead(ctx, d, meta)...)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/service/accessanalyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func testAccAnalyzer_disappears(t *testing.T) {
})
}

func testAccAnalyzer_Tags(t *testing.T) {
func testAccAnalyzer_tags(t *testing.T) {
ctx := acctest.Context(t)
var analyzer accessanalyzer.AnalyzerSummary

Expand Down
4 changes: 4 additions & 0 deletions internal/service/accessanalyzer/service_package_gen.go

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

47 changes: 7 additions & 40 deletions internal/service/acm/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/types/duration"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

const (
Expand All @@ -45,7 +46,8 @@ const (
certificateValidationMethodNone = "NONE"
)

// @SDKResource("aws_acm_certificate")
// @SDKResource("aws_acm_certificate", name="Certificate")
// @Tags(identifierAttribute="id")
func ResourceCertificate() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceCertificateCreate,
Expand Down Expand Up @@ -205,8 +207,8 @@ func ResourceCertificate() *schema.Resource {
},
ConflictsWith: []string{"certificate_body", "certificate_chain", "private_key"},
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"type": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -321,8 +323,6 @@ func ResourceCertificate() *schema.Resource {

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

if _, ok := d.GetOk("domain_name"); ok {
_, v1 := d.GetOk("certificate_authority_arn")
Expand All @@ -336,6 +336,7 @@ func resourceCertificateCreate(ctx context.Context, d *schema.ResourceData, meta
input := &acm.RequestCertificateInput{
DomainName: aws.String(domainName),
IdempotencyToken: aws.String(id.PrefixedUniqueId("tf")), // 32 character limit
Tags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("certificate_authority_arn"); ok {
Expand Down Expand Up @@ -364,10 +365,6 @@ func resourceCertificateCreate(ctx context.Context, d *schema.ResourceData, meta
input.DomainValidationOptions = expandDomainValidationOptions(v.(*schema.Set).List())
}

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

output, err := conn.RequestCertificateWithContext(ctx, input)

if err != nil {
Expand All @@ -379,16 +376,13 @@ func resourceCertificateCreate(ctx context.Context, d *schema.ResourceData, meta
input := &acm.ImportCertificateInput{
Certificate: []byte(d.Get("certificate_body").(string)),
PrivateKey: []byte(d.Get("private_key").(string)),
Tags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("certificate_chain"); ok {
input.CertificateChain = []byte(v.(string))
}

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

output, err := conn.ImportCertificateWithContext(ctx, input)

if err != nil {
Expand All @@ -407,8 +401,6 @@ func resourceCertificateCreate(ctx context.Context, d *schema.ResourceData, meta

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

certificate, err := FindCertificateByARN(ctx, conn, d.Id())

Expand Down Expand Up @@ -475,23 +467,6 @@ func resourceCertificateRead(ctx context.Context, d *schema.ResourceData, meta i
d.Set("validation_emails", validationEmails)
d.Set("validation_method", certificateValidationMethod(certificate))

tags, err := ListTags(ctx, conn, d.Id())

if err != nil {
return diag.Errorf("listing tags for ACM Certificate (%s): %s", d.Id(), 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 @@ -548,14 +523,6 @@ func resourceCertificateUpdate(ctx context.Context, d *schema.ResourceData, meta
}
}

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 ACM Certificate (%s) tags: %s", d.Id(), err)
}
}

return resourceCertificateRead(ctx, d, meta)
}

Expand Down
4 changes: 4 additions & 0 deletions internal/service/acm/service_package_gen.go

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

42 changes: 6 additions & 36 deletions internal/service/acmpca/certificate_authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 @@ -27,7 +28,8 @@ const (
certificateAuthorityPermanentDeletionTimeInDaysDefault = certificateAuthorityPermanentDeletionTimeInDaysMax
)

// @SDKResource("aws_acmpca_certificate_authority")
// @SDKResource("aws_acmpca_certificate_authority", name="Certificate Authority")
// @Tags(identifierAttribute="id")
func ResourceCertificateAuthority() *schema.Resource {
//lintignore:R011
return &schema.Resource{
Expand Down Expand Up @@ -324,8 +326,8 @@ func ResourceCertificateAuthority() *schema.Resource {
Computed: true,
Deprecated: "The reported value of the \"status\" attribute is often inaccurate. Use the resource's \"enabled\" attribute to explicitly set status.",
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"type": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -348,24 +350,19 @@ func ResourceCertificateAuthority() *schema.Resource {
func resourceCertificateAuthorityCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ACMPCAConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &acmpca.CreateCertificateAuthorityInput{
CertificateAuthorityConfiguration: expandCertificateAuthorityConfiguration(d.Get("certificate_authority_configuration").([]interface{})),
CertificateAuthorityType: aws.String(d.Get("type").(string)),
IdempotencyToken: aws.String(id.UniqueId()),
RevocationConfiguration: expandRevocationConfiguration(d.Get("revocation_configuration").([]interface{})),
Tags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("usage_mode"); ok {
input.UsageMode = aws.String(v.(string))
}

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

// ValidationException: The ACM Private CA service account 'acm-pca-prod-pdx' requires getBucketAcl permissions for your S3 bucket 'tf-acc-test-5224996536060125340'. Check your S3 bucket permissions and try again.
outputRaw, err := tfresource.RetryWhenAWSErrMessageContains(ctx, 1*time.Minute, func() (interface{}, error) {
return conn.CreateCertificateAuthorityWithContext(ctx, input)
Expand All @@ -387,8 +384,6 @@ func resourceCertificateAuthorityCreate(ctx context.Context, d *schema.ResourceD
func resourceCertificateAuthorityRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ACMPCAConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

certificateAuthority, err := FindCertificateAuthorityByARN(ctx, conn, d.Id())

Expand Down Expand Up @@ -467,23 +462,6 @@ func resourceCertificateAuthorityRead(ctx context.Context, d *schema.ResourceDat
d.Set("certificate_signing_request", getCertificateAuthorityCsrOutput.Csr)
}

tags, err := ListTags(ctx, conn, d.Id())

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for ACM PCA Certificate Authority (%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 @@ -514,14 +492,6 @@ func resourceCertificateAuthorityUpdate(ctx context.Context, d *schema.ResourceD
}
}

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

if err := UpdateTags(ctx, conn, d.Id(), o, n); err != nil {
return sdkdiag.AppendErrorf(diags, "updating ACM PCA Certificate Authority (%s) tags: %s", d.Id(), err)
}
}

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

Expand Down
4 changes: 4 additions & 0 deletions internal/service/acmpca/service_package_gen.go

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

4 changes: 4 additions & 0 deletions internal/service/amp/service_package_gen.go

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

Loading

0 comments on commit c718518

Please sign in to comment.