github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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  		return nil
    49  	}
    50  }
    51  
    52  func testAccCheckAWSAPIGatewayIntegrationResponseExists(n string, res *apigateway.IntegrationResponse) resource.TestCheckFunc {
    53  	return func(s *terraform.State) error {
    54  		rs, ok := s.RootModule().Resources[n]
    55  		if !ok {
    56  			return fmt.Errorf("Not found: %s", n)
    57  		}
    58  
    59  		if rs.Primary.ID == "" {
    60  			return fmt.Errorf("No API Gateway Method ID is set")
    61  		}
    62  
    63  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    64  
    65  		req := &apigateway.GetIntegrationResponseInput{
    66  			HttpMethod: aws.String("GET"),
    67  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
    68  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
    69  			StatusCode: aws.String(rs.Primary.Attributes["status_code"]),
    70  		}
    71  		describe, err := conn.GetIntegrationResponse(req)
    72  		if err != nil {
    73  			return err
    74  		}
    75  
    76  		*res = *describe
    77  
    78  		return nil
    79  	}
    80  }
    81  
    82  func testAccCheckAWSAPIGatewayIntegrationResponseDestroy(s *terraform.State) error {
    83  	conn := testAccProvider.Meta().(*AWSClient).apigateway
    84  
    85  	for _, rs := range s.RootModule().Resources {
    86  		if rs.Type != "aws_api_gateway_integration_response" {
    87  			continue
    88  		}
    89  
    90  		req := &apigateway.GetIntegrationResponseInput{
    91  			HttpMethod: aws.String("GET"),
    92  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
    93  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
    94  			StatusCode: aws.String(rs.Primary.Attributes["status_code"]),
    95  		}
    96  		_, err := conn.GetIntegrationResponse(req)
    97  
    98  		if err == nil {
    99  			return fmt.Errorf("API Gateway Method still exists")
   100  		}
   101  
   102  		aws2err, ok := err.(awserr.Error)
   103  		if !ok {
   104  			return err
   105  		}
   106  		if aws2err.Code() != "NotFoundException" {
   107  			return err
   108  		}
   109  
   110  		return nil
   111  	}
   112  
   113  	return nil
   114  }
   115  
   116  const testAccAWSAPIGatewayIntegrationResponseConfig = `
   117  resource "aws_api_gateway_rest_api" "test" {
   118    name = "test"
   119  }
   120  
   121  resource "aws_api_gateway_resource" "test" {
   122    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   123    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   124    path_part = "test"
   125  }
   126  
   127  resource "aws_api_gateway_method" "test" {
   128    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   129    resource_id = "${aws_api_gateway_resource.test.id}"
   130    http_method = "GET"
   131    authorization = "NONE"
   132  
   133    request_models = {
   134      "application/json" = "Error"
   135    }
   136  }
   137  
   138  resource "aws_api_gateway_method_response" "error" {
   139    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   140    resource_id = "${aws_api_gateway_resource.test.id}"
   141    http_method = "${aws_api_gateway_method.test.http_method}"
   142    status_code = "400"
   143  
   144    response_models = {
   145      "application/json" = "Error"
   146    }
   147  }
   148  
   149  resource "aws_api_gateway_integration" "test" {
   150    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   151    resource_id = "${aws_api_gateway_resource.test.id}"
   152    http_method = "${aws_api_gateway_method.test.http_method}"
   153  
   154    request_templates = {
   155      "application/json" = ""
   156      "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
   157    }
   158  
   159    type = "MOCK"
   160  }
   161  
   162  resource "aws_api_gateway_integration_response" "test" {
   163    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   164    resource_id = "${aws_api_gateway_resource.test.id}"
   165    http_method = "${aws_api_gateway_method.test.http_method}"
   166    status_code = "${aws_api_gateway_method_response.error.status_code}"
   167  
   168    response_templates = {
   169      "application/json" = ""
   170      "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
   171    }
   172  }
   173  `