github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_api_gateway_method_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 TestAccAWSAPIGatewayMethod_basic(t *testing.T) {
    15  	var conf apigateway.Method
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSAPIGatewayMethodDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSAPIGatewayMethodConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf),
    26  					testAccCheckAWSAPIGatewayMethodAttributes(&conf),
    27  					resource.TestCheckResourceAttr(
    28  						"aws_api_gateway_method.test", "http_method", "GET"),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_api_gateway_method.test", "authorization", "NONE"),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_api_gateway_method.test", "request_models.application/json", "Error"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckAWSAPIGatewayMethodAttributes(conf *apigateway.Method) resource.TestCheckFunc {
    40  	return func(s *terraform.State) error {
    41  		if *conf.HttpMethod != "GET" {
    42  			return fmt.Errorf("Wrong HttpMethod: %q", *conf.HttpMethod)
    43  		}
    44  		if *conf.AuthorizationType != "NONE" {
    45  			return fmt.Errorf("Wrong Authorization: %q", *conf.AuthorizationType)
    46  		}
    47  		return nil
    48  	}
    49  }
    50  
    51  func testAccCheckAWSAPIGatewayMethodExists(n string, res *apigateway.Method) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		rs, ok := s.RootModule().Resources[n]
    54  		if !ok {
    55  			return fmt.Errorf("Not found: %s", n)
    56  		}
    57  
    58  		if rs.Primary.ID == "" {
    59  			return fmt.Errorf("No API Gateway Method ID is set")
    60  		}
    61  
    62  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    63  
    64  		req := &apigateway.GetMethodInput{
    65  			HttpMethod: aws.String("GET"),
    66  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
    67  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
    68  		}
    69  		describe, err := conn.GetMethod(req)
    70  		if err != nil {
    71  			return err
    72  		}
    73  
    74  		*res = *describe
    75  
    76  		return nil
    77  	}
    78  }
    79  
    80  func testAccCheckAWSAPIGatewayMethodDestroy(s *terraform.State) error {
    81  	conn := testAccProvider.Meta().(*AWSClient).apigateway
    82  
    83  	for _, rs := range s.RootModule().Resources {
    84  		if rs.Type != "aws_api_gateway_method" {
    85  			continue
    86  		}
    87  
    88  		req := &apigateway.GetMethodInput{
    89  			HttpMethod: aws.String("GET"),
    90  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
    91  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
    92  		}
    93  		_, err := conn.GetMethod(req)
    94  
    95  		if err == nil {
    96  			return fmt.Errorf("API Gateway Method still exists")
    97  		}
    98  
    99  		aws2err, ok := err.(awserr.Error)
   100  		if !ok {
   101  			return err
   102  		}
   103  		if aws2err.Code() != "NotFoundException" {
   104  			return err
   105  		}
   106  
   107  		return nil
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  const testAccAWSAPIGatewayMethodConfig = `
   114  resource "aws_api_gateway_rest_api" "test" {
   115    name = "test"
   116  }
   117  
   118  resource "aws_api_gateway_resource" "test" {
   119    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   120    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   121    path_part = "test"
   122  }
   123  
   124  resource "aws_api_gateway_method" "test" {
   125    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   126    resource_id = "${aws_api_gateway_resource.test.id}"
   127    http_method = "GET"
   128    authorization = "NONE"
   129  
   130    request_models = {
   131      "application/json" = "Error"
   132    }
   133  }
   134  `