diff --git a/aws/resource_aws_lb_listener_certificate.go b/aws/resource_aws_lb_listener_certificate.go index 855aa37a848..a659b87dc5c 100644 --- a/aws/resource_aws_lb_listener_certificate.go +++ b/aws/resource_aws_lb_listener_certificate.go @@ -33,6 +33,7 @@ func resourceAwsLbListenerCertificate() *schema.Resource { func resourceAwsLbListenerCertificateCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).elbv2conn + params := &elbv2.AddListenerCertificatesInput{ ListenerArn: aws.String(d.Get("listener_arn").(string)), Certificates: []*elbv2.Certificate{ @@ -42,6 +43,7 @@ func resourceAwsLbListenerCertificateCreate(d *schema.ResourceData, meta interfa }, } + log.Printf("[DEBUG] Adding certificate: %s of listener: %s", d.Get("certificate_arn").(string), d.Get("listener_arn").(string)) resp, err := conn.AddListenerCertificates(params) if err != nil { return errwrap.Wrapf("Error creating LB Listener Certificate: {{err}}", err) @@ -58,6 +60,8 @@ func resourceAwsLbListenerCertificateCreate(d *schema.ResourceData, meta interfa func resourceAwsLbListenerCertificateRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).elbv2conn + log.Printf("[DEBUG] Reading certificate: %s of listener: %s", d.Get("certificate_arn").(string), d.Get("listener_arn").(string)) + params := &elbv2.DescribeListenerCertificatesInput{ ListenerArn: aws.String(d.Get("listener_arn").(string)), PageSize: aws.Int64(400), @@ -68,7 +72,7 @@ func resourceAwsLbListenerCertificateRead(d *schema.ResourceData, meta interface for morePages && !found { resp, err := conn.DescribeListenerCertificates(params) if err != nil { - return errwrap.Wrapf("Error describing LB Listener Certificates: {{err}}", err) + return err } for _, cert := range resp.Certificates { @@ -82,7 +86,7 @@ func resourceAwsLbListenerCertificateRead(d *schema.ResourceData, meta interface } } - if *resp.NextMarker != "" { + if resp.NextMarker != nil { params.Marker = resp.NextMarker } else { morePages = false @@ -100,6 +104,8 @@ func resourceAwsLbListenerCertificateRead(d *schema.ResourceData, meta interface func resourceAwsLbListenerCertificateDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).elbv2conn + log.Printf("[DEBUG] Deleting certificate: %s of listener: %s", d.Get("certificate_arn").(string), d.Get("listener_arn").(string)) + params := &elbv2.RemoveListenerCertificatesInput{ ListenerArn: aws.String(d.Get("listener_arn").(string)), Certificates: []*elbv2.Certificate{ @@ -109,7 +115,6 @@ func resourceAwsLbListenerCertificateDelete(d *schema.ResourceData, meta interfa }, } - // Returns no useful response. _, err := conn.RemoveListenerCertificates(params) if err != nil { return errwrap.Wrapf("Error removing LB Listener Certificate: {{err}}", err) diff --git a/aws/resource_aws_lb_listener_certificate_test.go b/aws/resource_aws_lb_listener_certificate_test.go index 120e5506a56..ee564cd165e 100644 --- a/aws/resource_aws_lb_listener_certificate_test.go +++ b/aws/resource_aws_lb_listener_certificate_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/elbv2" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" @@ -15,7 +16,7 @@ import ( func TestAccAwsLbListenerCertificate_basic(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProvidersWithTLS, + Providers: testAccProviders, CheckDestroy: testAccCheckAwsLbListenerCertificateDestroy, Steps: []resource.TestStep{ { @@ -36,6 +37,64 @@ func TestAccAwsLbListenerCertificate_basic(t *testing.T) { }) } +func TestAccAwsLbListenerCertificate_cycle(t *testing.T) { + rName := acctest.RandString(5) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAwsLbListenerCertificateDestroy, + Steps: []resource.TestStep{ + { + Config: testAccLbListenerCertificateConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLbListenerCertificateExists("aws_lb_listener_certificate.default"), + testAccCheckAwsLbListenerCertificateExists("aws_lb_listener_certificate.additional_1"), + testAccCheckAwsLbListenerCertificateExists("aws_lb_listener_certificate.additional_2"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.default", "listener_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.default", "certificate_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_1", "listener_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_1", "certificate_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_2", "listener_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_2", "certificate_arn"), + ), + }, + { + Config: testAccLbListenerCertificateAddNew(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLbListenerCertificateExists("aws_lb_listener_certificate.default"), + testAccCheckAwsLbListenerCertificateExists("aws_lb_listener_certificate.additional_1"), + testAccCheckAwsLbListenerCertificateExists("aws_lb_listener_certificate.additional_2"), + testAccCheckAwsLbListenerCertificateExists("aws_lb_listener_certificate.additional_3"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.default", "listener_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.default", "certificate_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_1", "listener_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_1", "certificate_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_2", "listener_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_2", "certificate_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_3", "listener_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_3", "certificate_arn"), + ), + }, + { + Config: testAccLbListenerCertificateConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLbListenerCertificateExists("aws_lb_listener_certificate.default"), + testAccCheckAwsLbListenerCertificateExists("aws_lb_listener_certificate.additional_1"), + testAccCheckAwsLbListenerCertificateExists("aws_lb_listener_certificate.additional_2"), + testAccCheckAwsLbListenerCertificateNotExists("aws_lb_listener_certificate.additional_3"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.default", "listener_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.default", "certificate_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_1", "listener_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_1", "certificate_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_2", "listener_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_certificate.additional_2", "certificate_arn"), + ), + }, + }, + }) +} + func testAccCheckAwsLbListenerCertificateDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).elbv2conn @@ -51,6 +110,9 @@ func testAccCheckAwsLbListenerCertificateDestroy(s *terraform.State) error { resp, err := conn.DescribeListenerCertificates(input) if err != nil { + if wserr, ok := err.(awserr.Error); ok && wserr.Code() == "ListenerNotFound" { + return nil + } return err } @@ -80,6 +142,17 @@ func testAccCheckAwsLbListenerCertificateExists(name string) resource.TestCheckF } } +func testAccCheckAwsLbListenerCertificateNotExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + _, ok := s.RootModule().Resources[name] + if !ok { + return nil + } + + return fmt.Errorf("Not expecting but found: %s", name) + } +} + func testAccLbListenerCertificateConfig(rName string) string { return fmt.Sprintf(` resource "aws_lb_listener_certificate" "default" { @@ -124,86 +197,226 @@ resource "aws_lb_listener" "test" { resource "aws_iam_server_certificate" "default" { name = "terraform-default-cert" - certificate_body = "${tls_self_signed_cert.default.cert_pem}" - private_key = "${tls_private_key.default.private_key_pem}" -} - -resource "tls_private_key" "default" { - algorithm = "RSA" -} - -resource "tls_self_signed_cert" "default" { - key_algorithm = "RSA" - private_key_pem = "${tls_private_key.default.private_key_pem}" - - subject { - common_name = "example.com" - organization = "ACME Examples, Inc" + certificate_body = <