github.com/anfernee/terraform@v0.6.16-0.20160430000239-06e5085a92f2/builtin/providers/aws/resource_aws_api_gateway_integration_response_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 TestAccAWSAPIGatewayIntegrationResponse_basic(t *testing.T) {
    15  	var conf apigateway.IntegrationResponse
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSAPIGatewayIntegrationResponseDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSAPIGatewayIntegrationResponseConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSAPIGatewayIntegrationResponseExists("aws_api_gateway_integration_response.test", &conf),
    26  					testAccCheckAWSAPIGatewayIntegrationResponseAttributes(&conf),
    27  					resource.TestCheckResourceAttr(
    28  						"aws_api_gateway_integration_response.test", "response_templates.application/json", ""),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_api_gateway_integration_response.test", "response_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func testAccCheckAWSAPIGatewayIntegrationResponseAttributes(conf *apigateway.IntegrationResponse) resource.TestCheckFunc {
    38  	return func(s *terraform.State) error {
    39  		if *conf.StatusCode != "400" {
    40  			return fmt.Errorf("wrong StatusCode: %q", *conf.StatusCode)
    41  		}
    42  		if conf.ResponseTemplates["application/json"] != nil {
    43  			return fmt.Errorf("wrong ResponseTemplate for application/json")
    44  		}
    45  		if *conf.ResponseTemplates["application/xml"] != "#set($inputRoot = $input.path('$'))\n{ }" {
    46  			return fmt.Errorf("wrong ResponseTemplate for application/xml")
    47  		}
    48  		if conf.SelectionPattern == nil || *conf.SelectionPattern != ".*" {
    49  			return fmt.Errorf("wrong SelectionPattern (expected .*)")
    50  		}
    51  		return nil
    52  	}
    53  }
    54  
    55  func testAccCheckAWSAPIGatewayIntegrationResponseExists(n string, res *apigateway.IntegrationResponse) resource.TestCheckFunc {
    56  	return func(s *terraform.State) error {
    57  		rs, ok := s.RootModule().Resources[n]
    58  		if !ok {
    59  			return fmt.Errorf("Not found: %s", n)
    60  		}
    61  
    62  		if rs.Primary.ID == "" {
    63  			return fmt.Errorf("No API Gateway Method ID is set")
    64  		}
    65  
    66  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    67  
    68  		req := &apigateway.GetIntegrationResponseInput{
    69  			HttpMethod: aws.String("GET"),
    70  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
    71  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
    72  			StatusCode: aws.String(rs.Primary.Attributes["status_code"]),
    73  		}
    74  		describe, err := conn.GetIntegrationResponse(req)
    75  		if err != nil {
    76  			return err
    77  		}
    78  
    79  		*res = *describe
    80  
    81  		return nil
    82  	}
    83  }
    84  
    85  func testAccCheckAWSAPIGatewayIntegrationResponseDestroy(s *terraform.State) error {
    86  	conn := testAccProvider.Meta().(*AWSClient).apigateway
    87  
    88  	for _, rs := range s.RootModule().Resources {
    89  		if rs.Type != "aws_api_gateway_integration_response" {
    90  			continue
    91  		}
    92  
    93  		req := &apigateway.GetIntegrationResponseInput{
    94  			HttpMethod: aws.String("GET"),
    95  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
    96  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
    97  			StatusCode: aws.String(rs.Primary.Attributes["status_code"]),
    98  		}
    99  		_, err := conn.GetIntegrationResponse(req)
   100  
   101  		if err == nil {
   102  			return fmt.Errorf("API Gateway Method still exists")
   103  		}
   104  
   105  		aws2err, ok := err.(awserr.Error)
   106  		if !ok {
   107  			return err
   108  		}
   109  		if aws2err.Code() != "NotFoundException" {
   110  			return err
   111  		}
   112  
   113  		return nil
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  const testAccAWSAPIGatewayIntegrationResponseConfig = `
   120  resource "aws_api_gateway_rest_api" "test" {
   121    name = "test"
   122  }
   123  
   124  resource "aws_api_gateway_resource" "test" {
   125    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   126    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   127    path_part = "test"
   128  }
   129  
   130  resource "aws_api_gateway_method" "test" {
   131    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   132    resource_id = "${aws_api_gateway_resource.test.id}"
   133    http_method = "GET"
   134    authorization = "NONE"
   135  
   136    request_models = {
   137      "application/json" = "Error"
   138    }
   139  }
   140  
   141  resource "aws_api_gateway_method_response" "error" {
   142    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   143    resource_id = "${aws_api_gateway_resource.test.id}"
   144    http_method = "${aws_api_gateway_method.test.http_method}"
   145    status_code = "400"
   146  
   147    response_models = {
   148      "application/json" = "Error"
   149    }
   150  }
   151  
   152  resource "aws_api_gateway_integration" "test" {
   153    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   154    resource_id = "${aws_api_gateway_resource.test.id}"
   155    http_method = "${aws_api_gateway_method.test.http_method}"
   156  
   157    request_templates = {
   158      "application/json" = ""
   159      "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
   160    }
   161  
   162    type = "MOCK"
   163  }
   164  
   165  resource "aws_api_gateway_integration_response" "test" {
   166    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   167    resource_id = "${aws_api_gateway_resource.test.id}"
   168    http_method = "${aws_api_gateway_method.test.http_method}"
   169    status_code = "${aws_api_gateway_method_response.error.status_code}"
   170    selection_pattern = ".*"
   171  
   172    response_templates = {
   173      "application/json" = ""
   174      "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
   175    }
   176  }
   177  `