github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_api_gateway_integration_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 TestAccAWSAPIGatewayIntegration_basic(t *testing.T) {
    15  	var conf apigateway.Integration
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSAPIGatewayIntegrationDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSAPIGatewayIntegrationConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSAPIGatewayIntegrationExists("aws_api_gateway_integration.test", &conf),
    26  					testAccCheckAWSAPIGatewayIntegrationAttributes(&conf),
    27  					resource.TestCheckResourceAttr(
    28  						"aws_api_gateway_integration.test", "type", "HTTP"),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_api_gateway_integration.test", "integration_http_method", "GET"),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_api_gateway_integration.test", "uri", "https://www.google.de"),
    33  					resource.TestCheckResourceAttr(
    34  						"aws_api_gateway_integration.test", "request_templates.application/json", ""),
    35  					resource.TestCheckResourceAttr(
    36  						"aws_api_gateway_integration.test", "request_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"),
    37  					resource.TestCheckResourceAttr(
    38  						"aws_api_gateway_integration.test", "passthrough_behavior", "WHEN_NO_MATCH"),
    39  				),
    40  			},
    41  
    42  			resource.TestStep{
    43  				Config: testAccAWSAPIGatewayIntegrationConfigUpdate,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckAWSAPIGatewayIntegrationExists("aws_api_gateway_integration.test", &conf),
    46  					testAccCheckAWSAPIGatewayMockIntegrationAttributes(&conf),
    47  					resource.TestCheckResourceAttr(
    48  						"aws_api_gateway_integration.test", "type", "MOCK"),
    49  					resource.TestCheckResourceAttr(
    50  						"aws_api_gateway_integration.test", "integration_http_method", ""),
    51  					resource.TestCheckResourceAttr(
    52  						"aws_api_gateway_integration.test", "uri", ""),
    53  					resource.TestCheckResourceAttr(
    54  						"aws_api_gateway_integration.test", "passthrough_behavior", "NEVER"),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func testAccCheckAWSAPIGatewayMockIntegrationAttributes(conf *apigateway.Integration) resource.TestCheckFunc {
    62  	return func(s *terraform.State) error {
    63  		if *conf.Type != "MOCK" {
    64  			return fmt.Errorf("Wrong Type: %q", *conf.Type)
    65  		}
    66  		if *conf.RequestParameters["integration.request.header.X-Authorization"] != "'updated'" {
    67  			return fmt.Errorf("wrong updated RequestParameters for header.X-Authorization")
    68  		}
    69  		return nil
    70  	}
    71  }
    72  
    73  func testAccCheckAWSAPIGatewayIntegrationAttributes(conf *apigateway.Integration) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		if *conf.HttpMethod == "" {
    76  			return fmt.Errorf("empty HttpMethod")
    77  		}
    78  		if *conf.Uri != "https://www.google.de" {
    79  			return fmt.Errorf("wrong Uri")
    80  		}
    81  		if *conf.Type != "HTTP" {
    82  			return fmt.Errorf("wrong Type")
    83  		}
    84  		if conf.RequestTemplates["application/json"] != nil {
    85  			return fmt.Errorf("wrong RequestTemplate for application/json")
    86  		}
    87  		if *conf.RequestTemplates["application/xml"] != "#set($inputRoot = $input.path('$'))\n{ }" {
    88  			return fmt.Errorf("wrong RequestTemplate for application/xml")
    89  		}
    90  		if *conf.RequestParameters["integration.request.header.X-Authorization"] != "'static'" {
    91  			return fmt.Errorf("wrong RequestParameters for header.X-Authorization")
    92  		}
    93  		return nil
    94  	}
    95  }
    96  
    97  func testAccCheckAWSAPIGatewayIntegrationExists(n string, res *apigateway.Integration) resource.TestCheckFunc {
    98  	return func(s *terraform.State) error {
    99  		rs, ok := s.RootModule().Resources[n]
   100  		if !ok {
   101  			return fmt.Errorf("Not found: %s", n)
   102  		}
   103  
   104  		if rs.Primary.ID == "" {
   105  			return fmt.Errorf("No API Gateway Method ID is set")
   106  		}
   107  
   108  		conn := testAccProvider.Meta().(*AWSClient).apigateway
   109  
   110  		req := &apigateway.GetIntegrationInput{
   111  			HttpMethod: aws.String("GET"),
   112  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
   113  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
   114  		}
   115  		describe, err := conn.GetIntegration(req)
   116  		if err != nil {
   117  			return err
   118  		}
   119  
   120  		*res = *describe
   121  
   122  		return nil
   123  	}
   124  }
   125  
   126  func testAccCheckAWSAPIGatewayIntegrationDestroy(s *terraform.State) error {
   127  	conn := testAccProvider.Meta().(*AWSClient).apigateway
   128  
   129  	for _, rs := range s.RootModule().Resources {
   130  		if rs.Type != "aws_api_gateway_integration" {
   131  			continue
   132  		}
   133  
   134  		req := &apigateway.GetIntegrationInput{
   135  			HttpMethod: aws.String("GET"),
   136  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
   137  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
   138  		}
   139  		_, err := conn.GetIntegration(req)
   140  
   141  		if err == nil {
   142  			return fmt.Errorf("API Gateway Method still exists")
   143  		}
   144  
   145  		aws2err, ok := err.(awserr.Error)
   146  		if !ok {
   147  			return err
   148  		}
   149  		if aws2err.Code() != "NotFoundException" {
   150  			return err
   151  		}
   152  
   153  		return nil
   154  	}
   155  
   156  	return nil
   157  }
   158  
   159  const testAccAWSAPIGatewayIntegrationConfig = `
   160  resource "aws_api_gateway_rest_api" "test" {
   161    name = "test"
   162  }
   163  
   164  resource "aws_api_gateway_resource" "test" {
   165    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   166    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   167    path_part = "test"
   168  }
   169  
   170  resource "aws_api_gateway_method" "test" {
   171    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   172    resource_id = "${aws_api_gateway_resource.test.id}"
   173    http_method = "GET"
   174    authorization = "NONE"
   175  
   176    request_models = {
   177      "application/json" = "Error"
   178    }
   179  }
   180  
   181  resource "aws_api_gateway_integration" "test" {
   182    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   183    resource_id = "${aws_api_gateway_resource.test.id}"
   184    http_method = "${aws_api_gateway_method.test.http_method}"
   185  
   186    request_templates = {
   187      "application/json" = ""
   188      "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
   189    }
   190  
   191    request_parameters = {
   192  	  "integration.request.header.X-Authorization" = "'static'"
   193    }
   194  
   195    type = "HTTP"
   196    uri = "https://www.google.de"
   197    integration_http_method = "GET"
   198    passthrough_behavior = "WHEN_NO_MATCH"
   199  }
   200  `
   201  
   202  const testAccAWSAPIGatewayIntegrationConfigUpdate = `
   203  resource "aws_api_gateway_rest_api" "test" {
   204    name = "test"
   205  }
   206  
   207  resource "aws_api_gateway_resource" "test" {
   208    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   209    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   210    path_part = "test"
   211  }
   212  
   213  resource "aws_api_gateway_method" "test" {
   214    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   215    resource_id = "${aws_api_gateway_resource.test.id}"
   216    http_method = "GET"
   217    authorization = "NONE"
   218  
   219    request_models = {
   220      "application/json" = "Error"
   221    }
   222  }
   223  
   224  resource "aws_api_gateway_integration" "test" {
   225    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   226    resource_id = "${aws_api_gateway_resource.test.id}"
   227    http_method = "${aws_api_gateway_method.test.http_method}"
   228  
   229    request_parameters = {
   230  	  "integration.request.header.X-Authorization" = "'updated'"
   231    }
   232  
   233    type = "MOCK"
   234    passthrough_behavior = "NEVER"
   235  
   236  }
   237  `