github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_api_gateway_base_path_mapping_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 TestAccAWSAPIGatewayBasePath_basic(t *testing.T) {
    15  	var conf apigateway.BasePathMapping
    16  
    17  	// Our test cert is for a wildcard on this domain
    18  	name := fmt.Sprintf("%s.tf-acc.invalid", resource.UniqueId())
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckAWSAPIGatewayBasePathDestroy(name),
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccAWSAPIGatewayBasePathConfig(name),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckAWSAPIGatewayBasePathExists("aws_api_gateway_base_path_mapping.test", name, &conf),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func testAccCheckAWSAPIGatewayBasePathExists(n string, name string, res *apigateway.BasePathMapping) resource.TestCheckFunc {
    36  	return func(s *terraform.State) error {
    37  		rs, ok := s.RootModule().Resources[n]
    38  		if !ok {
    39  			return fmt.Errorf("Not found: %s", n)
    40  		}
    41  
    42  		if rs.Primary.ID == "" {
    43  			return fmt.Errorf("No API Gateway ID is set")
    44  		}
    45  
    46  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    47  
    48  		req := &apigateway.GetBasePathMappingInput{
    49  			DomainName: aws.String(name),
    50  			BasePath:   aws.String("tf-acc"),
    51  		}
    52  		describe, err := conn.GetBasePathMapping(req)
    53  		if err != nil {
    54  			return err
    55  		}
    56  
    57  		if *describe.BasePath != "tf-acc" {
    58  			return fmt.Errorf("base path mapping not found")
    59  		}
    60  
    61  		*res = *describe
    62  
    63  		return nil
    64  	}
    65  }
    66  
    67  func testAccCheckAWSAPIGatewayBasePathDestroy(name string) resource.TestCheckFunc {
    68  	return func(s *terraform.State) error {
    69  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    70  
    71  		for _, rs := range s.RootModule().Resources {
    72  			if rs.Type != "aws_api_gateway_rest_api" {
    73  				continue
    74  			}
    75  
    76  			req := &apigateway.GetBasePathMappingsInput{
    77  				DomainName: aws.String(name),
    78  			}
    79  			_, err := conn.GetBasePathMappings(req)
    80  
    81  			if err != nil {
    82  				if err, ok := err.(awserr.Error); ok && err.Code() == "NotFoundException" {
    83  					return nil
    84  				}
    85  				return err
    86  			}
    87  
    88  			return fmt.Errorf("expected error reading deleted base path, but got success")
    89  		}
    90  
    91  		return nil
    92  	}
    93  }
    94  
    95  func testAccAWSAPIGatewayBasePathConfig(name string) string {
    96  	return fmt.Sprintf(`
    97  resource "aws_api_gateway_rest_api" "test" {
    98    name = "tf-acc-apigateway-base-path-mapping"
    99    description = "Terraform Acceptance Tests"
   100  }
   101  # API gateway won't let us deploy an empty API
   102  resource "aws_api_gateway_resource" "test" {
   103    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   104    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   105    path_part = "tf-acc"
   106  }
   107  resource "aws_api_gateway_method" "test" {
   108    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   109    resource_id = "${aws_api_gateway_resource.test.id}"
   110    http_method = "GET"
   111    authorization = "NONE"
   112  }
   113  resource "aws_api_gateway_integration" "test" {
   114    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   115    resource_id = "${aws_api_gateway_resource.test.id}"
   116    http_method = "${aws_api_gateway_method.test.http_method}"
   117    type = "MOCK"
   118  }
   119  resource "aws_api_gateway_deployment" "test" {
   120    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   121    stage_name = "test"
   122    depends_on = ["aws_api_gateway_integration.test"]
   123  }
   124  resource "aws_api_gateway_base_path_mapping" "test" {
   125    api_id = "${aws_api_gateway_rest_api.test.id}"
   126    base_path = "tf-acc"
   127    stage_name = "${aws_api_gateway_deployment.test.stage_name}"
   128    domain_name = "${aws_api_gateway_domain_name.test.domain_name}"
   129  }
   130  resource "aws_api_gateway_domain_name" "test" {
   131    domain_name = "%s"
   132    certificate_name = "tf-apigateway-base-path-mapping-test"
   133    certificate_body = "%s"
   134    certificate_chain = "%s"
   135    certificate_private_key = "%s"
   136  }
   137  `, name, testAccAWSAPIGatewayCertBody, testAccAWSAPIGatewayCertChain, testAccAWSAPIGatewayCertPrivateKey)
   138  }