github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 = {
   189  		"method.response.header.Content-Type" = true
   190  	}
   191  }
   192  
   193  resource "aws_api_gateway_integration" "test" {
   194    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   195    resource_id = "${aws_api_gateway_resource.test.id}"
   196    http_method = "${aws_api_gateway_method.test.http_method}"
   197  
   198    request_templates = {
   199      "application/json" = ""
   200      "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
   201    }
   202  
   203    type = "MOCK"
   204  }
   205  
   206  resource "aws_api_gateway_integration_response" "test" {
   207    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   208    resource_id = "${aws_api_gateway_resource.test.id}"
   209    http_method = "${aws_api_gateway_method.test.http_method}"
   210    status_code = "${aws_api_gateway_method_response.error.status_code}"
   211    selection_pattern = ".*"
   212  
   213    response_templates = {
   214      "application/json" = ""
   215      "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
   216    }
   217  
   218  	response_parameters = {
   219  		"method.response.header.Content-Type" = "integration.response.body.type"
   220  	}
   221  }
   222  `
   223  
   224  const testAccAWSAPIGatewayIntegrationResponseConfigUpdate = `
   225  resource "aws_api_gateway_rest_api" "test" {
   226    name = "test"
   227  }
   228  
   229  resource "aws_api_gateway_resource" "test" {
   230    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   231    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   232    path_part = "test"
   233  }
   234  
   235  resource "aws_api_gateway_method" "test" {
   236    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   237    resource_id = "${aws_api_gateway_resource.test.id}"
   238    http_method = "GET"
   239    authorization = "NONE"
   240  
   241    request_models = {
   242      "application/json" = "Error"
   243    }
   244  }
   245  
   246  resource "aws_api_gateway_method_response" "error" {
   247    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   248    resource_id = "${aws_api_gateway_resource.test.id}"
   249    http_method = "${aws_api_gateway_method.test.http_method}"
   250    status_code = "400"
   251  
   252    response_models = {
   253      "application/json" = "Error"
   254    }
   255  
   256  	response_parameters = {
   257  		"method.response.header.Content-Type" = true
   258  	}
   259  }
   260  
   261  resource "aws_api_gateway_integration" "test" {
   262    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   263    resource_id = "${aws_api_gateway_resource.test.id}"
   264    http_method = "${aws_api_gateway_method.test.http_method}"
   265  
   266    request_templates = {
   267      "application/json" = ""
   268      "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
   269    }
   270  
   271    type = "MOCK"
   272  }
   273  
   274  resource "aws_api_gateway_integration_response" "test" {
   275    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   276    resource_id = "${aws_api_gateway_resource.test.id}"
   277    http_method = "${aws_api_gateway_method.test.http_method}"
   278    status_code = "${aws_api_gateway_method_response.error.status_code}"
   279  
   280    response_templates = {
   281      "application/json" = "$input.path('$')"
   282      "application/xml" = ""
   283    }
   284  
   285  }
   286  `