From d283ae50e880594342eff1de4a2dc563459ff051 Mon Sep 17 00:00:00 2001 From: William Perron <37561740+wperron@users.noreply.github.com> Date: Mon, 11 May 2020 21:37:45 -0400 Subject: [PATCH] added `operation_name` attribute to API G. method This commit adds the `operation_name` attribute to the `aws_api_gateway_method` resource. This attribute allows users to specify a custom function name to be used when using API Gateway to generate an SDK. The attribute is already supported by the AWS API and SDKs so there was really no reason not to include it. It also made it a pretty straight forward change. This commit also includes: * test cases for the new attribute * updated documentation for the resource Closes #13232 --- aws/resource_aws_api_gateway_method.go | 25 ++++ aws/resource_aws_api_gateway_method_test.go | 107 ++++++++++++++++++ .../docs/r/api_gateway_method.html.markdown | 1 + 3 files changed, 133 insertions(+) diff --git a/aws/resource_aws_api_gateway_method.go b/aws/resource_aws_api_gateway_method.go index bfbe5af1dcb5..040d87fd7ddd 100644 --- a/aws/resource_aws_api_gateway_method.go +++ b/aws/resource_aws_api_gateway_method.go @@ -100,6 +100,11 @@ func resourceAwsApiGatewayMethod() *schema.Resource { Type: schema.TypeString, Optional: true, }, + + "operation_name": { + Type: schema.TypeString, + Optional: true, + }, }, } } @@ -147,6 +152,10 @@ func resourceAwsApiGatewayMethodCreate(d *schema.ResourceData, meta interface{}) input.RequestValidatorId = aws.String(v.(string)) } + if v, ok := d.GetOk("operation_name"); ok { + input.OperationName = aws.String(v.(string)) + } + _, err := conn.PutMethod(&input) if err != nil { return fmt.Errorf("Error creating API Gateway Method: %s", err) @@ -196,6 +205,8 @@ func resourceAwsApiGatewayMethodRead(d *schema.ResourceData, meta interface{}) e d.Set("request_validator_id", out.RequestValidatorId) + d.Set("operation_name", out.OperationName) + return nil } @@ -304,6 +315,20 @@ func resourceAwsApiGatewayMethodUpdate(d *schema.ResourceData, meta interface{}) }) } + if d.HasChange("operation_name") { + var operation_name *string + if v, ok := d.GetOk("operation_name"); ok { + if s := v.(string); len(s) > 0 { + operation_name = &s + } + } + operations = append(operations, &apigateway.PatchOperation{ + Op: aws.String("replace"), + Path: aws.String("/operationName"), + Value: operation_name, + }) + } + method, err := conn.UpdateMethod(&apigateway.UpdateMethodInput{ HttpMethod: aws.String(d.Get("http_method").(string)), ResourceId: aws.String(d.Get("resource_id").(string)), diff --git a/aws/resource_aws_api_gateway_method_test.go b/aws/resource_aws_api_gateway_method_test.go index 1d7a59232294..e5cf48f2879c 100644 --- a/aws/resource_aws_api_gateway_method_test.go +++ b/aws/resource_aws_api_gateway_method_test.go @@ -195,6 +195,50 @@ func TestAccAWSAPIGatewayMethod_customrequestvalidator(t *testing.T) { }) } +func TestAccAWSAPIGatewayMethod_customoperationname(t *testing.T) { + var conf apigateway.Method + rInt := acctest.RandInt() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayMethodDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGatewayMethodConfigWithCustomOperationName(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf), + testAccCheckAWSAPIGatewayMethodAttributes(&conf), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "http_method", "GET"), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "authorization", "NONE"), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "request_models.application/json", "Error"), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "operation_name", "getTest"), + ), + }, + { + ResourceName: "aws_api_gateway_method.test", + ImportState: true, + ImportStateIdFunc: testAccAWSAPIGatewayMethodImportStateIdFunc("aws_api_gateway_method.test"), + ImportStateVerify: true, + }, + + { + Config: testAccAWSAPIGatewayMethodConfigWithCustomOperationNameUpdate(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf), + testAccCheckAWSAPIGatewayMethodAttributesUpdate(&conf), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "operation_name", "describeTest"), + ), + }, + }, + }) +} + func testAccCheckAWSAPIGatewayMethodAttributes(conf *apigateway.Method) resource.TestCheckFunc { return func(s *terraform.State) error { if *conf.HttpMethod != "GET" { @@ -722,3 +766,66 @@ resource "aws_api_gateway_method" "test" { } `, rInt) } + +func testAccAWSAPIGatewayMethodConfigWithCustomOperationName(rInt int) string { + return fmt.Sprintf(` +resource "aws_api_gateway_rest_api" "test" { + name = "tf-acc-test-apig-method-custom-op-name-%d" +} + +resource "aws_api_gateway_resource" "test" { + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" + path_part = "test" +} + +resource "aws_api_gateway_method" "test" { + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + resource_id = "${aws_api_gateway_resource.test.id}" + http_method = "GET" + authorization = "NONE" + + request_models = { + "application/json" = "Error" + } + + request_parameters = { + "method.request.header.Content-Type" = false + "method.request.querystring.page" = true + } + + operation_name = "getTest" +} +`, rInt) +} + +func testAccAWSAPIGatewayMethodConfigWithCustomOperationNameUpdate(rInt int) string { + return fmt.Sprintf(` +resource "aws_api_gateway_rest_api" "test" { + name = "tf-acc-test-apig-method-custom-op-name-%d" +} + +resource "aws_api_gateway_resource" "test" { + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" + path_part = "test" +} + +resource "aws_api_gateway_method" "test" { + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + resource_id = "${aws_api_gateway_resource.test.id}" + http_method = "GET" + authorization = "NONE" + + request_models = { + "application/json" = "Error" + } + + request_parameters = { + "method.request.querystring.page" = false + } + + operation_name = "describeTest" +} +`, rInt) +} diff --git a/website/docs/r/api_gateway_method.html.markdown b/website/docs/r/api_gateway_method.html.markdown index 5405933f397f..f61cd412c9a6 100644 --- a/website/docs/r/api_gateway_method.html.markdown +++ b/website/docs/r/api_gateway_method.html.markdown @@ -88,6 +88,7 @@ The following arguments are supported: * `request_validator_id` - (Optional) The ID of a `aws_api_gateway_request_validator` * `request_parameters` - (Optional) A map of request parameters (from the path, query string and headers) that should be passed to the integration. The boolean value indicates whether the parameter is required (`true`) or optional (`false`). For example: `request_parameters = {"method.request.header.X-Some-Header" = true "method.request.querystring.some-query-param" = true}` would define that the header `X-Some-Header` and the query string `some-query-param` must be provided in the request. +* `operation_name` - (Optional) The function name that will be given to the method when generating an SDK through API Gateway. If omitted, API Gateway will generate a function name based on the resource path and HTTP verb. ## Import