github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/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  			resource.TestStep{
    35  				Config: testAccAWSAPIGatewayIntegrationResponseConfigUpdate,
    36  				Check: resource.ComposeTestCheckFunc(
    37  					testAccCheckAWSAPIGatewayIntegrationResponseExists("aws_api_gateway_integration_response.test", &conf),
    38  					testAccCheckAWSAPIGatewayIntegrationResponseAttributesUpdate(&conf),
    39  					resource.TestCheckResourceAttr(
    40  						"aws_api_gateway_integration_response.test", "response_templates.application/json", "$input.path('$')"),
    41  					resource.TestCheckResourceAttr(
    42  						"aws_api_gateway_integration_response.test", "response_templates.application/xml", ""),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func testAccCheckAWSAPIGatewayIntegrationResponseAttributes(conf *apigateway.IntegrationResponse) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		if *conf.StatusCode != "400" {
    52  			return fmt.Errorf("wrong StatusCode: %q", *conf.StatusCode)
    53  		}
    54  		if conf.ResponseTemplates["application/json"] != nil {
    55  			return fmt.Errorf("wrong ResponseTemplate for application/json")
    56  		}
    57  		if *conf.ResponseTemplates["application/xml"] != "#set($inputRoot = $input.path('$'))\n{ }" {
    58  			return fmt.Errorf("wrong ResponseTemplate for application/xml")
    59  		}
    60  		if conf.SelectionPattern == nil || *conf.SelectionPattern != ".*" {
    61  			return fmt.Errorf("wrong SelectionPattern (expected .*)")
    62  		}
    63  		if *conf.ResponseParameters["method.response.header.Content-Type"] != "integration.response.body.type" {
    64  			return fmt.Errorf("wrong ResponseParameters for header.Content-Type")
    65  		}
    66  		return nil
    67  	}
    68  }
    69  
    70  func testAccCheckAWSAPIGatewayIntegrationResponseAttributesUpdate(conf *apigateway.IntegrationResponse) resource.TestCheckFunc {
    71  	return func(s *terraform.State) error {
    72  		if *conf.StatusCode != "400" {
    73  			return fmt.Errorf("wrong StatusCode: %q", *conf.StatusCode)
    74  		}
    75  		if *conf.ResponseTemplates["application/json"] != "$input.path('$')" {
    76  			return fmt.Errorf("wrong ResponseTemplate for application/json")
    77  		}
    78  		if conf.ResponseTemplates["application/xml"] != nil {
    79  			return fmt.Errorf("wrong ResponseTemplate for application/xml")
    80  		}
    81  		if conf.SelectionPattern != nil {
    82  			return fmt.Errorf("wrong SelectionPattern (expected nil)")
    83  		}
    84  		if conf.ResponseParameters["method.response.header.Content-Type"] != nil {
    85  			return fmt.Errorf("ResponseParameters for header.Content-Type shouldnt exist")
    86  		}
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  func testAccCheckAWSAPIGatewayIntegrationResponseExists(n string, res *apigateway.IntegrationResponse) resource.TestCheckFunc {
    93  	return func(s *terraform.State) error {
    94  		rs, ok := s.RootModule().Resources[n]
    95  		if !ok {
    96  			return fmt.Errorf("Not found: %s", n)
    97  		}
    98  
    99  		if rs.Primary.ID == "" {
   100  			return fmt.Errorf("No API Gateway Method ID is set")
   101  		}
   102  
   103  		conn := testAccProvider.Meta().(*AWSClient).apigateway
   104  
   105  		req := &apigateway.GetIntegrationResponseInput{
   106  			HttpMethod: aws.String("GET"),
   107  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
   108  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
   109  			StatusCode: aws.String(rs.Primary.Attributes["status_code"]),
   110  		}
   111  		describe, err := conn.GetIntegrationResponse(req)
   112  		if err != nil {
   113  			return err
   114  		}
   115  
   116  		*res = *describe
   117  
   118  		return nil
   119  	}
   120  }
   121  
   122  func testAccCheckAWSAPIGatewayIntegrationResponseDestroy(s *terraform.State) error {
   123  	conn := testAccProvider.Meta().(*AWSClient).apigateway
   124  
   125  	for _, rs := range s.RootModule().Resources {
   126  		if rs.Type != "aws_api_gateway_integration_response" {
   127  			continue
   128  		}
   129  
   130  		req := &apigateway.GetIntegrationResponseInput{
   131  			HttpMethod: aws.String("GET"),
   132  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
   133  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
   134  			StatusCode: aws.String(rs.Primary.Attributes["status_code"]),
   135  		}
   136  		_, err := conn.GetIntegrationResponse(req)
   137  
   138  		if err == nil {
   139  			return fmt.Errorf("API Gateway Method still exists")
   140  		}
   141  
   142  		aws2err, ok := err.(awserr.Error)
   143  		if !ok {
   144  			return err
   145  		}
   146  		if aws2err.Code() != "NotFoundException" {
   147  			return err
   148  		}
   149  
   150  		return nil
   151  	}
   152  
   153  	return nil
   154  }
   155  
   156  const testAccAWSAPIGatewayIntegrationResponseConfig = `
   157  resource "aws_api_gateway_rest_api" "test" {
   158    name = "test"
   159  }
   160  
   161  resource "aws_api_gateway_resource" "test" {
   162    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   163    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   164    path_part = "test"
   165  }
   166  
   167  resource "aws_api_gateway_method" "test" {
   168    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   169    resource_id = "${aws_api_gateway_resource.test.id}"
   170    http_method = "GET"
   171    authorization = "NONE"
   172  
   173    request_models = {
   174      "application/json" = "Error"
   175    }
   176  }
   177  
   178  resource "aws_api_gateway_method_response" "error" {
   179    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   180    resource_id = "${aws_api_gateway_resource.test.id}"
   181    http_method = "${aws_api_gateway_method.test.http_method}"
   182    status_code = "400"
   183  
   184    response_models = {
   185      "application/json" = "Error"
   186    }
   187  
   188  	response_parameters_in_json = <<PARAMS
   189  	{
   190  		"method.response.header.Content-Type": true
   191  	}
   192  	PARAMS
   193  }
   194  
   195  resource "aws_api_gateway_integration" "test" {
   196    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   197    resource_id = "${aws_api_gateway_resource.test.id}"
   198    http_method = "${aws_api_gateway_method.test.http_method}"
   199  
   200    request_templates = {
   201      "application/json" = ""
   202      "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
   203    }
   204  
   205    type = "MOCK"
   206  }
   207  
   208  resource "aws_api_gateway_integration_response" "test" {
   209    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   210    resource_id = "${aws_api_gateway_resource.test.id}"
   211    http_method = "${aws_api_gateway_method.test.http_method}"
   212    status_code = "${aws_api_gateway_method_response.error.status_code}"
   213    selection_pattern = ".*"
   214  
   215    response_templates = {
   216      "application/json" = ""
   217      "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
   218    }
   219  
   220  	response_parameters_in_json = <<PARAMS
   221  	{
   222  		"method.response.header.Content-Type": "integration.response.body.type"
   223  	}
   224  	PARAMS
   225  }
   226  `
   227  
   228  const testAccAWSAPIGatewayIntegrationResponseConfigUpdate = `
   229  resource "aws_api_gateway_rest_api" "test" {
   230    name = "test"
   231  }
   232  
   233  resource "aws_api_gateway_resource" "test" {
   234    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   235    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   236    path_part = "test"
   237  }
   238  
   239  resource "aws_api_gateway_method" "test" {
   240    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   241    resource_id = "${aws_api_gateway_resource.test.id}"
   242    http_method = "GET"
   243    authorization = "NONE"
   244  
   245    request_models = {
   246      "application/json" = "Error"
   247    }
   248  }
   249  
   250  resource "aws_api_gateway_method_response" "error" {
   251    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   252    resource_id = "${aws_api_gateway_resource.test.id}"
   253    http_method = "${aws_api_gateway_method.test.http_method}"
   254    status_code = "400"
   255  
   256    response_models = {
   257      "application/json" = "Error"
   258    }
   259  
   260  	response_parameters_in_json = <<PARAMS
   261  	{
   262  		"method.response.header.Content-Type": true
   263  	}
   264  	PARAMS
   265  }
   266  
   267  resource "aws_api_gateway_integration" "test" {
   268    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   269    resource_id = "${aws_api_gateway_resource.test.id}"
   270    http_method = "${aws_api_gateway_method.test.http_method}"
   271  
   272    request_templates = {
   273      "application/json" = ""
   274      "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
   275    }
   276  
   277    type = "MOCK"
   278  }
   279  
   280  resource "aws_api_gateway_integration_response" "test" {
   281    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   282    resource_id = "${aws_api_gateway_resource.test.id}"
   283    http_method = "${aws_api_gateway_method.test.http_method}"
   284    status_code = "${aws_api_gateway_method_response.error.status_code}"
   285  
   286    response_templates = {
   287      "application/json" = "$input.path('$')"
   288      "application/xml" = ""
   289    }
   290  
   291  }
   292  `