github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_api_gateway_method_settings_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/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSAPIGatewayMethodSettings_basic(t *testing.T) {
    16  	var stage apigateway.Stage
    17  	rInt := acctest.RandInt()
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckAWSAPIGatewayMethodSettingsDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: testAccAWSAPIGatewayMethodSettingsConfig(rInt),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckAWSAPIGatewayMethodSettingsExists("aws_api_gateway_method_settings.test", &stage),
    28  					testAccCheckAWSAPIGatewayMethodSettings_metricsEnabled(&stage, "test/GET", true),
    29  					testAccCheckAWSAPIGatewayMethodSettings_loggingLevel(&stage, "test/GET", "INFO"),
    30  					resource.TestCheckResourceAttr("aws_api_gateway_method_settings.test", "settings.#", "1"),
    31  					resource.TestCheckResourceAttr("aws_api_gateway_method_settings.test", "settings.0.metrics_enabled", "true"),
    32  					resource.TestCheckResourceAttr("aws_api_gateway_method_settings.test", "settings.0.logging_level", "INFO"),
    33  				),
    34  			},
    35  
    36  			{
    37  				Config: testAccAWSAPIGatewayMethodSettingsConfigUpdate(rInt),
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckAWSAPIGatewayMethodSettingsExists("aws_api_gateway_method_settings.test", &stage),
    40  					testAccCheckAWSAPIGatewayMethodSettings_metricsEnabled(&stage, "test/GET", false),
    41  					testAccCheckAWSAPIGatewayMethodSettings_loggingLevel(&stage, "test/GET", "OFF"),
    42  					resource.TestCheckResourceAttr("aws_api_gateway_method_settings.test", "settings.#", "1"),
    43  					resource.TestCheckResourceAttr("aws_api_gateway_method_settings.test", "settings.0.metrics_enabled", "false"),
    44  					resource.TestCheckResourceAttr("aws_api_gateway_method_settings.test", "settings.0.logging_level", "OFF"),
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func testAccCheckAWSAPIGatewayMethodSettings_metricsEnabled(conf *apigateway.Stage, path string, expected bool) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		settings, ok := conf.MethodSettings[path]
    54  		if !ok {
    55  			return fmt.Errorf("Expected to find method settings for %q", path)
    56  		}
    57  
    58  		if expected && *settings.MetricsEnabled != expected {
    59  			return fmt.Errorf("Expected metrics to be enabled, got %t", *settings.MetricsEnabled)
    60  		}
    61  		if !expected && *settings.MetricsEnabled != expected {
    62  			return fmt.Errorf("Expected metrics to be disabled, got %t", *settings.MetricsEnabled)
    63  		}
    64  
    65  		return nil
    66  	}
    67  }
    68  
    69  func testAccCheckAWSAPIGatewayMethodSettings_loggingLevel(conf *apigateway.Stage, path string, expectedLevel string) resource.TestCheckFunc {
    70  	return func(s *terraform.State) error {
    71  		settings, ok := conf.MethodSettings[path]
    72  		if !ok {
    73  			return fmt.Errorf("Expected to find method settings for %q", path)
    74  		}
    75  
    76  		if *settings.LoggingLevel != expectedLevel {
    77  			return fmt.Errorf("Expected logging level to match %q, got %q", expectedLevel, *settings.LoggingLevel)
    78  		}
    79  
    80  		return nil
    81  	}
    82  }
    83  
    84  func testAccCheckAWSAPIGatewayMethodSettingsExists(n string, res *apigateway.Stage) resource.TestCheckFunc {
    85  	return func(s *terraform.State) error {
    86  		rs, ok := s.RootModule().Resources[n]
    87  		if !ok {
    88  			return fmt.Errorf("Not found: %s", n)
    89  		}
    90  
    91  		if rs.Primary.ID == "" {
    92  			return fmt.Errorf("No API Gateway Stage ID is set")
    93  		}
    94  
    95  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    96  
    97  		req := &apigateway.GetStageInput{
    98  			StageName: aws.String(s.RootModule().Resources["aws_api_gateway_deployment.test"].Primary.Attributes["stage_name"]),
    99  			RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
   100  		}
   101  		out, err := conn.GetStage(req)
   102  		if err != nil {
   103  			return err
   104  		}
   105  
   106  		*res = *out
   107  
   108  		return nil
   109  	}
   110  }
   111  
   112  func testAccCheckAWSAPIGatewayMethodSettingsDestroy(s *terraform.State) error {
   113  	conn := testAccProvider.Meta().(*AWSClient).apigateway
   114  
   115  	for _, rs := range s.RootModule().Resources {
   116  		if rs.Type != "aws_api_gateway_method_settings" {
   117  			continue
   118  		}
   119  
   120  		req := &apigateway.GetStageInput{
   121  			StageName: aws.String(s.RootModule().Resources["aws_api_gateway_deployment.test"].Primary.Attributes["stage_name"]),
   122  			RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
   123  		}
   124  		out, err := conn.GetStage(req)
   125  		if err == nil {
   126  			return fmt.Errorf("API Gateway Stage still exists: %s", out)
   127  		}
   128  
   129  		awsErr, ok := err.(awserr.Error)
   130  		if !ok {
   131  			return err
   132  		}
   133  		if awsErr.Code() != "NotFoundException" {
   134  			return err
   135  		}
   136  
   137  		return nil
   138  	}
   139  
   140  	return nil
   141  }
   142  
   143  func testAccAWSAPIGatewayMethodSettingsConfig(rInt int) string {
   144  	return fmt.Sprintf(`
   145  resource "aws_api_gateway_rest_api" "test" {
   146    name = "tf-acc-test-apig-method-%d"
   147  }
   148  
   149  resource "aws_api_gateway_resource" "test" {
   150    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   151    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   152    path_part = "test"
   153  }
   154  
   155  resource "aws_api_gateway_method" "test" {
   156    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   157    resource_id = "${aws_api_gateway_resource.test.id}"
   158    http_method = "GET"
   159    authorization = "NONE"
   160  
   161    request_models = {
   162      "application/json" = "Error"
   163    }
   164  
   165    request_parameters = {
   166      "method.request.header.Content-Type" = false,
   167  	  "method.request.querystring.page" = true
   168    }
   169  }
   170  
   171  resource "aws_api_gateway_integration" "test" {
   172    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   173    resource_id = "${aws_api_gateway_resource.test.id}"
   174    http_method = "${aws_api_gateway_method.test.http_method}"
   175    type        = "MOCK"
   176  
   177    request_templates {
   178      "application/xml" = <<EOF
   179  {
   180     "body" : $input.json('$')
   181  }
   182  EOF
   183    }
   184  }
   185  
   186  resource "aws_api_gateway_deployment" "test" {
   187    depends_on = ["aws_api_gateway_integration.test"]
   188    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   189    stage_name = "dev"
   190  }
   191  
   192  resource "aws_api_gateway_method_settings" "test" {
   193    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   194    stage_name  = "${aws_api_gateway_deployment.test.stage_name}"
   195    method_path = "${aws_api_gateway_resource.test.path_part}/${aws_api_gateway_method.test.http_method}"
   196  
   197    settings {
   198    	metrics_enabled = true
   199  	logging_level = "INFO"
   200    }
   201  }
   202  `, rInt)
   203  }
   204  
   205  func testAccAWSAPIGatewayMethodSettingsConfigUpdate(rInt int) string {
   206  	return fmt.Sprintf(`
   207  resource "aws_api_gateway_rest_api" "test" {
   208    name = "tf-acc-test-apig-method-%d"
   209  }
   210  
   211  resource "aws_api_gateway_resource" "test" {
   212    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   213    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   214    path_part = "test"
   215  }
   216  
   217  resource "aws_api_gateway_method" "test" {
   218    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   219    resource_id = "${aws_api_gateway_resource.test.id}"
   220    http_method = "GET"
   221    authorization = "NONE"
   222  
   223    request_models = {
   224      "application/json" = "Error"
   225    }
   226  
   227    request_parameters = {
   228      "method.request.header.Content-Type" = false,
   229  	  "method.request.querystring.page" = true
   230    }
   231  }
   232  
   233  resource "aws_api_gateway_integration" "test" {
   234    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   235    resource_id = "${aws_api_gateway_resource.test.id}"
   236    http_method = "${aws_api_gateway_method.test.http_method}"
   237    type        = "MOCK"
   238  
   239    request_templates {
   240      "application/xml" = <<EOF
   241  {
   242     "body" : $input.json('$')
   243  }
   244  EOF
   245    }
   246  }
   247  
   248  resource "aws_api_gateway_deployment" "test" {
   249    depends_on = ["aws_api_gateway_integration.test"]
   250    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   251    stage_name = "dev"
   252  }
   253  
   254  resource "aws_api_gateway_method_settings" "test" {
   255    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   256    stage_name  = "${aws_api_gateway_deployment.test.stage_name}"
   257    method_path = "${aws_api_gateway_resource.test.path_part}/${aws_api_gateway_method.test.http_method}"
   258  
   259    settings {
   260    	metrics_enabled = false
   261  	logging_level = "OFF"
   262    }
   263  }
   264  `, rInt)
   265  }