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

Refactor r/aws_acmpca_certificate_authority to use keyvaluetags #10736

Merged
merged 6 commits into from
Nov 4, 2019
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
75 changes: 14 additions & 61 deletions aws/resource_aws_acmpca_certificate_authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsAcmpcaCertificateAuthority() *schema.Resource {
Expand Down Expand Up @@ -268,6 +269,7 @@ func resourceAwsAcmpcaCertificateAuthority() *schema.Resource {

func resourceAwsAcmpcaCertificateAuthorityCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).acmpcaconn
tags := keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().AcmpcaTags()

input := &acmpca.CreateCertificateAuthorityInput{
CertificateAuthorityConfiguration: expandAcmpcaCertificateAuthorityConfiguration(d.Get("certificate_authority_configuration").([]interface{})),
Expand All @@ -276,6 +278,10 @@ func resourceAwsAcmpcaCertificateAuthorityCreate(d *schema.ResourceData, meta in
RevocationConfiguration: expandAcmpcaRevocationConfiguration(d.Get("revocation_configuration").([]interface{})),
}

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

log.Printf("[DEBUG] Creating ACMPCA Certificate Authority: %s", input)
var output *acmpca.CreateCertificateAuthorityOutput
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
Expand All @@ -299,19 +305,6 @@ func resourceAwsAcmpcaCertificateAuthorityCreate(d *schema.ResourceData, meta in

d.SetId(aws.StringValue(output.CertificateAuthorityArn))

if v, ok := d.GetOk("tags"); ok {
input := &acmpca.TagCertificateAuthorityInput{
CertificateAuthorityArn: aws.String(d.Id()),
Tags: tagsFromMapACMPCA(v.(map[string]interface{})),
}

log.Printf("[DEBUG] Tagging ACMPCA Certificate Authority: %s", input)
_, err := conn.TagCertificateAuthority(input)
if err != nil {
return fmt.Errorf("error tagging ACMPCA Certificate Authority %q: %s", d.Id(), input)
}
}

stateConf := &resource.StateChangeConf{
Pending: []string{
"",
Expand Down Expand Up @@ -427,12 +420,13 @@ func resourceAwsAcmpcaCertificateAuthorityRead(d *schema.ResourceData, meta inte
d.Set("certificate_signing_request", getCertificateAuthorityCsrOutput.Csr)
}

tags, err := listAcmpcaTags(conn, d.Id())
tags, err := keyvaluetags.AcmpcaListTags(conn, d.Id())

if err != nil {
return fmt.Errorf("error reading ACMPCA Certificate Authority %q tags: %s", d.Id(), err)
return fmt.Errorf("error listing tags for ACMPCA Certificate Authority (%s): %s", d.Id(), err)
}

if err := d.Set("tags", tagsToMapACMPCA(tags)); err != nil {
if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

Expand Down Expand Up @@ -469,30 +463,10 @@ func resourceAwsAcmpcaCertificateAuthorityUpdate(d *schema.ResourceData, meta in
}

if d.HasChange("tags") {
oraw, nraw := d.GetChange("tags")
o := oraw.(map[string]interface{})
n := nraw.(map[string]interface{})
create, remove := diffTagsACMPCA(tagsFromMapACMPCA(o), tagsFromMapACMPCA(n))

if len(remove) > 0 {
log.Printf("[DEBUG] Removing ACMPCA Certificate Authority %q tags: %#v", d.Id(), remove)
_, err := conn.UntagCertificateAuthority(&acmpca.UntagCertificateAuthorityInput{
CertificateAuthorityArn: aws.String(d.Id()),
Tags: remove,
})
if err != nil {
return fmt.Errorf("error updating ACMPCA Certificate Authority %q tags: %s", d.Id(), err)
}
}
if len(create) > 0 {
log.Printf("[DEBUG] Creating ACMPCA Certificate Authority %q tags: %#v", d.Id(), create)
_, err := conn.TagCertificateAuthority(&acmpca.TagCertificateAuthorityInput{
CertificateAuthorityArn: aws.String(d.Id()),
Tags: create,
})
if err != nil {
return fmt.Errorf("error updating ACMPCA Certificate Authority %q tags: %s", d.Id(), err)
}
o, n := d.GetChange("tags")

if err := keyvaluetags.AcmpcaUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating ACMPCA Certificate Authority (%s) tags: %s", d.Id(), err)
}
}

Expand Down Expand Up @@ -714,24 +688,3 @@ func flattenAcmpcaRevocationConfiguration(config *acmpca.RevocationConfiguration

return []interface{}{m}
}

func listAcmpcaTags(conn *acmpca.ACMPCA, certificateAuthorityArn string) ([]*acmpca.Tag, error) {
tags := []*acmpca.Tag{}
input := &acmpca.ListTagsInput{
CertificateAuthorityArn: aws.String(certificateAuthorityArn),
}

for {
output, err := conn.ListTags(input)
if err != nil {
return tags, err
}
tags = append(tags, output.Tags...)
if output.NextToken == nil {
break
}
input.NextToken = output.NextToken
}

return tags, nil
}
2 changes: 1 addition & 1 deletion aws/resource_aws_acmpca_certificate_authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestAccAwsAcmpcaCertificateAuthority_Basic(t *testing.T) {
Config: testAccAwsAcmpcaCertificateAuthorityConfig_Required,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAcmpcaCertificateAuthorityExists(resourceName, &certificateAuthority),
resource.TestMatchResourceAttr(resourceName, "arn", regexp.MustCompile(`^arn:[^:]+:acm-pca:[^:]+:[^:]+:certificate-authority/.+$`)),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "acm-pca", regexp.MustCompile(`certificate-authority/.+`)),
resource.TestCheckResourceAttr(resourceName, "certificate_authority_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "certificate_authority_configuration.0.key_algorithm", "RSA_4096"),
resource.TestCheckResourceAttr(resourceName, "certificate_authority_configuration.0.signing_algorithm", "SHA512WITHRSA"),
Expand Down
50 changes: 0 additions & 50 deletions aws/tagsACMPCA.go

This file was deleted.

57 changes: 0 additions & 57 deletions aws/tagsACMPCA_test.go

This file was deleted.