github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_api_gateway_client_certificate_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/apigateway"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSAPIGatewayClientCertificate_basic(t *testing.T) {
    15  	var conf apigateway.ClientCertificate
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSAPIGatewayClientCertificateDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSAPIGatewayClientCertificateConfig_basic,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSAPIGatewayClientCertificateExists("aws_api_gateway_client_certificate.cow", &conf),
    26  					resource.TestCheckResourceAttr("aws_api_gateway_client_certificate.cow", "description", "Hello from TF acceptance test"),
    27  				),
    28  			},
    29  			resource.TestStep{
    30  				Config: testAccAWSAPIGatewayClientCertificateConfig_basic_updated,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckAWSAPIGatewayClientCertificateExists("aws_api_gateway_client_certificate.cow", &conf),
    33  					resource.TestCheckResourceAttr("aws_api_gateway_client_certificate.cow", "description", "Hello from TF acceptance test - updated"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func TestAccAWSAPIGatewayClientCertificate_importBasic(t *testing.T) {
    41  	resourceName := "aws_api_gateway_client_certificate.cow"
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckAWSAPIGatewayClientCertificateDestroy,
    47  		Steps: []resource.TestStep{
    48  			resource.TestStep{
    49  				Config: testAccAWSAPIGatewayClientCertificateConfig_basic,
    50  			},
    51  
    52  			resource.TestStep{
    53  				ResourceName:      resourceName,
    54  				ImportState:       true,
    55  				ImportStateVerify: true,
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func testAccCheckAWSAPIGatewayClientCertificateExists(n string, res *apigateway.ClientCertificate) resource.TestCheckFunc {
    62  	return func(s *terraform.State) error {
    63  		rs, ok := s.RootModule().Resources[n]
    64  		if !ok {
    65  			return fmt.Errorf("Not found: %s", n)
    66  		}
    67  
    68  		if rs.Primary.ID == "" {
    69  			return fmt.Errorf("No API Gateway Client Certificate ID is set")
    70  		}
    71  
    72  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    73  
    74  		req := &apigateway.GetClientCertificateInput{
    75  			ClientCertificateId: aws.String(rs.Primary.ID),
    76  		}
    77  		out, err := conn.GetClientCertificate(req)
    78  		if err != nil {
    79  			return err
    80  		}
    81  
    82  		*res = *out
    83  
    84  		return nil
    85  	}
    86  }
    87  
    88  func testAccCheckAWSAPIGatewayClientCertificateDestroy(s *terraform.State) error {
    89  	conn := testAccProvider.Meta().(*AWSClient).apigateway
    90  
    91  	for _, rs := range s.RootModule().Resources {
    92  		if rs.Type != "aws_api_gateway_client_certificate" {
    93  			continue
    94  		}
    95  
    96  		req := &apigateway.GetClientCertificateInput{
    97  			ClientCertificateId: aws.String(rs.Primary.ID),
    98  		}
    99  		out, err := conn.GetClientCertificate(req)
   100  		if err == nil {
   101  			return fmt.Errorf("API Gateway Client Certificate still exists: %s", out)
   102  		}
   103  
   104  		awsErr, ok := err.(awserr.Error)
   105  		if !ok {
   106  			return err
   107  		}
   108  		if awsErr.Code() != "NotFoundException" {
   109  			return err
   110  		}
   111  
   112  		return nil
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  const testAccAWSAPIGatewayClientCertificateConfig_basic = `
   119  resource "aws_api_gateway_client_certificate" "cow" {
   120    description = "Hello from TF acceptance test"
   121  }
   122  `
   123  
   124  const testAccAWSAPIGatewayClientCertificateConfig_basic_updated = `
   125  resource "aws_api_gateway_client_certificate" "cow" {
   126    description = "Hello from TF acceptance test - updated"
   127  }
   128  `