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

provider/aws: Improve error handling in IAM Server Certificates #6442

Merged
merged 2 commits into from
May 2, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion builtin/providers/aws/resource_aws_iam_server_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ func resourceAwsIAMServerCertificateRead(d *schema.ResourceData, meta interface{

if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "NoSuchEntity" {
log.Printf("[WARN] IAM Server Cert (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("[WARN] Error reading IAM Server Certificate: %s: %s", awsErr.Code(), awsErr.Message())
}
return fmt.Errorf("[WARN] Error reading IAM Server Certificate: %s", err)
Expand All @@ -161,7 +166,7 @@ func resourceAwsIAMServerCertificateRead(d *schema.ResourceData, meta interface{
func resourceAwsIAMServerCertificateDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iamconn
log.Printf("[INFO] Deleting IAM Server Certificate: %s", d.Id())
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
err := resource.Retry(3*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteServerCertificate(&iam.DeleteServerCertificateInput{
ServerCertificateName: aws.String(d.Get("name").(string)),
})
Expand All @@ -172,6 +177,11 @@ func resourceAwsIAMServerCertificateDelete(d *schema.ResourceData, meta interfac
log.Printf("[WARN] Conflict deleting server certificate: %s, retrying", awsErr.Message())
return resource.RetryableError(err)
}
if awsErr.Code() == "NoSuchEntity" {
log.Printf("[WARN] IAM Server Certificate (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
}
return resource.NonRetryableError(err)
}
Expand Down
35 changes: 35 additions & 0 deletions builtin/providers/aws/resource_aws_iam_server_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,41 @@ func TestAccAWSIAMServerCertificate_name_prefix(t *testing.T) {
})
}

func TestAccAWSIAMServerCertificate_recreate(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conventionally I've been calling these _disappears tests.

var cert iam.ServerCertificate

testDestroyCert := func(*terraform.State) error {
// reach out and DELETE the Cert
conn := testAccProvider.Meta().(*AWSClient).iamconn
_, err := conn.DeleteServerCertificate(&iam.DeleteServerCertificateInput{
ServerCertificateName: cert.ServerCertificateMetadata.ServerCertificateName,
})

if err != nil {
return fmt.Errorf("Error destorying cert in test: %s", err)
}

return nil
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckIAMServerCertificateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccIAMServerCertConfig_random,
Check: resource.ComposeTestCheckFunc(
testAccCheckCertExists("aws_iam_server_certificate.test_cert", &cert),
testAccCheckAWSServerCertAttributes(&cert),
testDestroyCert,
),
ExpectNonEmptyPlan: true,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other _disappears tests I add a second step w/ a blank config and expect an empty plan, which is a nice extra check that the state is actually removed.

},
})
}

func testAccCheckCertExists(n string, cert *iam.ServerCertificate) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down