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