Skip to content

Commit

Permalink
added operation_name attribute to API G. method
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wperron committed Jul 1, 2020
1 parent de7897c commit d283ae5
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
25 changes: 25 additions & 0 deletions aws/resource_aws_api_gateway_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func resourceAwsApiGatewayMethod() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},

"operation_name": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)),
Expand Down
107 changes: 107 additions & 0 deletions aws/resource_aws_api_gateway_method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions website/docs/r/api_gateway_method.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d283ae5

Please sign in to comment.