From 7ecef9c48a8e36194ab102055d5a5ea1bb417189 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 3 Jun 2019 09:40:32 -0400 Subject: [PATCH 1/2] Add 'aws_api_gateway2_route_response' resource. --- aws/provider.go | 1 + ...esource_aws_api_gateway2_route_response.go | 160 +++++++++++++++++ ...ce_aws_api_gateway2_route_response_test.go | 163 ++++++++++++++++++ website/aws.erb | 3 + ...pi_gateway_v2_route_response.html.markdown | 48 ++++++ 5 files changed, 375 insertions(+) create mode 100644 aws/resource_aws_api_gateway2_route_response.go create mode 100644 aws/resource_aws_api_gateway2_route_response_test.go create mode 100644 website/docs/r/api_gateway_v2_route_response.html.markdown diff --git a/aws/provider.go b/aws/provider.go index 11d9d824c9c..bf905a628f1 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -372,6 +372,7 @@ func Provider() terraform.ResourceProvider { "aws_apigatewayv2_integration_response": resourceAwsApiGatewayV2IntegrationResponse(), "aws_apigatewayv2_model": resourceAwsApiGatewayV2Model(), "aws_apigatewayv2_route": resourceAwsApiGatewayV2Route(), + "aws_api_gateway_v2_route_response": resourceAwsApiGateway2RouteResponse(), "aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(), "aws_appautoscaling_target": resourceAwsAppautoscalingTarget(), "aws_appautoscaling_policy": resourceAwsAppautoscalingPolicy(), diff --git a/aws/resource_aws_api_gateway2_route_response.go b/aws/resource_aws_api_gateway2_route_response.go new file mode 100644 index 00000000000..105b99e45dc --- /dev/null +++ b/aws/resource_aws_api_gateway2_route_response.go @@ -0,0 +1,160 @@ +package aws + +import ( + "fmt" + "log" + "strings" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/apigatewayv2" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func resourceAwsApiGateway2RouteResponse() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsApiGateway2RouteResponseCreate, + Read: resourceAwsApiGateway2RouteResponseRead, + Update: resourceAwsApiGateway2RouteResponseUpdate, + Delete: resourceAwsApiGateway2RouteResponseDelete, + Importer: &schema.ResourceImporter{ + State: resourceAwsApiGateway2RouteResponseImport, + }, + + Schema: map[string]*schema.Schema{ + "api_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "model_selection_expression": { + Type: schema.TypeString, + Optional: true, + }, + "response_models": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "route_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "route_response_key": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func resourceAwsApiGateway2RouteResponseCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigatewayv2conn + + req := &apigatewayv2.CreateRouteResponseInput{ + ApiId: aws.String(d.Get("api_id").(string)), + RouteId: aws.String(d.Get("route_id").(string)), + RouteResponseKey: aws.String(d.Get("route_response_key").(string)), + } + if v, ok := d.GetOk("model_selection_expression"); ok { + req.ModelSelectionExpression = aws.String(v.(string)) + } + if v, ok := d.GetOk("response_models"); ok { + req.ResponseModels = stringMapToPointers(v.(map[string]interface{})) + } + + log.Printf("[DEBUG] Creating API Gateway v2 route response: %s", req) + resp, err := conn.CreateRouteResponse(req) + if err != nil { + return fmt.Errorf("error creating API Gateway v2 route response: %s", err) + } + + d.SetId(aws.StringValue(resp.RouteResponseId)) + + return resourceAwsApiGateway2RouteResponseRead(d, meta) +} + +func resourceAwsApiGateway2RouteResponseRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigatewayv2conn + + resp, err := conn.GetRouteResponse(&apigatewayv2.GetRouteResponseInput{ + ApiId: aws.String(d.Get("api_id").(string)), + RouteId: aws.String(d.Get("route_id").(string)), + RouteResponseId: aws.String(d.Id()), + }) + if isAWSErr(err, apigatewayv2.ErrCodeNotFoundException, "") { + log.Printf("[WARN] API Gateway v2 route response (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + if err != nil { + return fmt.Errorf("error reading API Gateway v2 route response: %s", err) + } + + d.Set("model_selection_expression", resp.ModelSelectionExpression) + if err := d.Set("response_models", pointersMapToStringList(resp.ResponseModels)); err != nil { + return fmt.Errorf("error setting response_models: %s", err) + } + d.Set("route_response_key", resp.RouteResponseKey) + + return nil +} + +func resourceAwsApiGateway2RouteResponseUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigatewayv2conn + + req := &apigatewayv2.UpdateRouteResponseInput{ + ApiId: aws.String(d.Get("api_id").(string)), + RouteId: aws.String(d.Get("route_id").(string)), + RouteResponseId: aws.String(d.Id()), + } + if d.HasChange("model_selection_expression") { + req.ModelSelectionExpression = aws.String(d.Get("model_selection_expression").(string)) + } + if d.HasChange("response_models") { + req.ResponseModels = stringMapToPointers(d.Get("response_models").(map[string]interface{})) + } + if d.HasChange("route_response_key") { + req.RouteResponseKey = aws.String(d.Get("route_response_key").(string)) + } + + log.Printf("[DEBUG] Updating API Gateway v2 route response: %s", req) + _, err := conn.UpdateRouteResponse(req) + if err != nil { + return fmt.Errorf("error updating API Gateway v2 route response: %s", err) + } + + return resourceAwsApiGateway2RouteResponseRead(d, meta) +} + +func resourceAwsApiGateway2RouteResponseDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigatewayv2conn + + log.Printf("[DEBUG] Deleting API Gateway v2 route response (%s)", d.Id()) + _, err := conn.DeleteRouteResponse(&apigatewayv2.DeleteRouteResponseInput{ + ApiId: aws.String(d.Get("api_id").(string)), + RouteId: aws.String(d.Get("route_id").(string)), + RouteResponseId: aws.String(d.Id()), + }) + if isAWSErr(err, apigatewayv2.ErrCodeNotFoundException, "") { + return nil + } + if err != nil { + return fmt.Errorf("error deleting API Gateway v2 route response: %s", err) + } + + return nil +} + +func resourceAwsApiGateway2RouteResponseImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + parts := strings.Split(d.Id(), "/") + if len(parts) != 3 { + return []*schema.ResourceData{}, fmt.Errorf("Wrong format of resource: %s. Please follow 'api-id/route-id/route-response-id'", d.Id()) + } + + d.SetId(parts[2]) + d.Set("api_id", parts[0]) + d.Set("route_id", parts[1]) + + return []*schema.ResourceData{d}, nil +} diff --git a/aws/resource_aws_api_gateway2_route_response_test.go b/aws/resource_aws_api_gateway2_route_response_test.go new file mode 100644 index 00000000000..ef5fb8a6e32 --- /dev/null +++ b/aws/resource_aws_api_gateway2_route_response_test.go @@ -0,0 +1,163 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/apigatewayv2" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccAWSAPIGateway2RouteResponse_basic(t *testing.T) { + resourceName := "aws_api_gateway_v2_route_response.test" + routeResourceName := "aws_api_gateway_v2_route.test" + rName := fmt.Sprintf("tf-testacc-apigwv2-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGateway2RouteResponseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGateway2RouteResponseConfig_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGateway2RouteResponseExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "model_selection_expression", ""), + resource.TestCheckResourceAttr(resourceName, "response_models.%", "0"), + resource.TestCheckResourceAttrPair(resourceName, "route_id", routeResourceName, "id"), + resource.TestCheckResourceAttr(resourceName, "route_response_key", "$default"), + ), + }, + { + ResourceName: resourceName, + ImportStateIdFunc: testAccAWSAPIGateway2RouteResponseImportStateIdFunc(resourceName), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAWSAPIGateway2RouteResponse_Model(t *testing.T) { + resourceName := "aws_api_gateway_v2_route_response.test" + modelResourceName := "aws_api_gateway_v2_model.test" + routeResourceName := "aws_api_gateway_v2_route.test" + rName := fmt.Sprintf("tftestaccapigwv2%s", acctest.RandStringFromCharSet(16, acctest.CharSetAlphaNum)) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGateway2RouteResponseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGateway2RouteResponseConfig_model(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGateway2RouteResponseExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "model_selection_expression", "action"), + resource.TestCheckResourceAttr(resourceName, "response_models.%", "1"), + resource.TestCheckResourceAttrPair(resourceName, "response_models.test", modelResourceName, "name"), + resource.TestCheckResourceAttrPair(resourceName, "route_id", routeResourceName, "id"), + resource.TestCheckResourceAttr(resourceName, "route_response_key", "$default"), + ), + }, + { + ResourceName: resourceName, + ImportStateIdFunc: testAccAWSAPIGateway2RouteResponseImportStateIdFunc(resourceName), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccCheckAWSAPIGateway2RouteResponseDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).apigatewayv2conn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_api_gateway_v2_route_response" { + continue + } + + _, err := conn.GetRouteResponse(&apigatewayv2.GetRouteResponseInput{ + ApiId: aws.String(rs.Primary.Attributes["api_id"]), + RouteId: aws.String(rs.Primary.Attributes["route_id"]), + RouteResponseId: aws.String(rs.Primary.ID), + }) + if isAWSErr(err, apigatewayv2.ErrCodeNotFoundException, "") { + continue + } + if err != nil { + return err + } + + return fmt.Errorf("API Gateway v2 route response %s still exists", rs.Primary.ID) + } + + return nil +} + +func testAccCheckAWSAPIGateway2RouteResponseExists(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No API Gateway v2 route response ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).apigatewayv2conn + + _, err := conn.GetRouteResponse(&apigatewayv2.GetRouteResponseInput{ + ApiId: aws.String(rs.Primary.Attributes["api_id"]), + RouteId: aws.String(rs.Primary.Attributes["route_id"]), + RouteResponseId: aws.String(rs.Primary.ID), + }) + if err != nil { + return err + } + + return nil + } +} + +func testAccAWSAPIGateway2RouteResponseImportStateIdFunc(resourceName string) resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return "", fmt.Errorf("Not Found: %s", resourceName) + } + + return fmt.Sprintf("%s/%s/%s", rs.Primary.Attributes["api_id"], rs.Primary.Attributes["route_id"], rs.Primary.ID), nil + } +} + +func testAccAWSAPIGateway2RouteResponseConfig_basic(rName string) string { + return testAccAWSAPIGatewayV2RouteConfig_basic(rName) + fmt.Sprintf(` +resource "aws_api_gateway_v2_route_response" "test" { + api_id = "${aws_api_gateway_v2_api.test.id}" + route_id = "${aws_api_gateway_v2_route.test.id}" + route_response_key = "$default" +} +`) +} + +func testAccAWSAPIGateway2RouteResponseConfig_model(rName string) string { + return testAccAWSAPIGatewayV2RouteConfig_model(rName) + fmt.Sprintf(` +resource "aws_api_gateway_v2_route_response" "test" { + api_id = "${aws_api_gateway_v2_api.test.id}" + route_id = "${aws_api_gateway_v2_route.test.id}" + route_response_key = "$default" + + model_selection_expression = "action" + + response_models = { + "test" = "${aws_api_gateway_v2_model.test.name}" + } +} +`) +} diff --git a/website/aws.erb b/website/aws.erb index 2ad1eef73ad..97d2d5d5f60 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -236,6 +236,9 @@
  • aws_apigatewayv2_route
  • +
  • + aws_api_gateway_v2_route_response +
  • diff --git a/website/docs/r/api_gateway_v2_route_response.html.markdown b/website/docs/r/api_gateway_v2_route_response.html.markdown new file mode 100644 index 00000000000..abdc8464a32 --- /dev/null +++ b/website/docs/r/api_gateway_v2_route_response.html.markdown @@ -0,0 +1,48 @@ +--- +layout: "aws" +page_title: "AWS: aws_api_gateway_v2_route_response" +sidebar_current: "docs-aws-resource-api-gateway-v2-route-response" +description: |- + Manages an Amazon API Gateway Version 2 route response. +--- + +# Resource: aws_api_gateway_v2_route_response + +Manages an Amazon API Gateway Version 2 route response. +More information can be found in the [Amazon API Gateway Developer Guide](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html). + +## Example Usage + +### Basic + +```hcl +resource "aws_api_gateway_v2_route_response" "example" { + api_id = "${aws_api_gateway_v2_api.example.id}" + route_id = "${aws_api_gateway_v2_route.example.id}" + route_response_key = "$default" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `api_id` - (Required) The API identifier. +* `route_id` - (Required) The identifier of the [`aws_api_gateway_v2_route`](/docs/providers/aws/r/api_gateway_v2_route.html). +* `route_response_key` - (Required) The route response key. +* `model_selection_expression` - (Optional) The [model selection expression](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-selection-expressions.html#apigateway-websocket-api-model-selection-expressions) for the route response. +* `response_models` - (Optional) The response models for the route response. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The route response identifier. + +## Import + +`aws_api_gateway_v2_route_response` can be imported by using the API identifier, route identifier and route response identifier, e.g. + +``` +$ terraform import aws_api_gateway_v2_route.example aabbccddee/1122334/998877 +``` From 1d45f8026e2d76528ea4376a6b71cf749dc671f4 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 15 Apr 2020 11:08:17 -0400 Subject: [PATCH 2/2] Rename resource to 'aws_apigatewayv2_route_response'. --- aws/provider.go | 2 +- ...ce_aws_api_gateway2_route_response_test.go | 163 -------------- ...source_aws_apigatewayv2_route_response.go} | 26 +-- ...ce_aws_apigatewayv2_route_response_test.go | 212 ++++++++++++++++++ website/aws.erb | 2 +- ...apigatewayv2_route_response.html.markdown} | 18 +- 6 files changed, 236 insertions(+), 187 deletions(-) delete mode 100644 aws/resource_aws_api_gateway2_route_response_test.go rename aws/{resource_aws_api_gateway2_route_response.go => resource_aws_apigatewayv2_route_response.go} (81%) create mode 100644 aws/resource_aws_apigatewayv2_route_response_test.go rename website/docs/r/{api_gateway_v2_route_response.html.markdown => apigatewayv2_route_response.html.markdown} (60%) diff --git a/aws/provider.go b/aws/provider.go index bf905a628f1..8cdc8336818 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -372,7 +372,7 @@ func Provider() terraform.ResourceProvider { "aws_apigatewayv2_integration_response": resourceAwsApiGatewayV2IntegrationResponse(), "aws_apigatewayv2_model": resourceAwsApiGatewayV2Model(), "aws_apigatewayv2_route": resourceAwsApiGatewayV2Route(), - "aws_api_gateway_v2_route_response": resourceAwsApiGateway2RouteResponse(), + "aws_apigatewayv2_route_response": resourceAwsApiGatewayV2RouteResponse(), "aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(), "aws_appautoscaling_target": resourceAwsAppautoscalingTarget(), "aws_appautoscaling_policy": resourceAwsAppautoscalingPolicy(), diff --git a/aws/resource_aws_api_gateway2_route_response_test.go b/aws/resource_aws_api_gateway2_route_response_test.go deleted file mode 100644 index ef5fb8a6e32..00000000000 --- a/aws/resource_aws_api_gateway2_route_response_test.go +++ /dev/null @@ -1,163 +0,0 @@ -package aws - -import ( - "fmt" - "testing" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/apigatewayv2" - "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" -) - -func TestAccAWSAPIGateway2RouteResponse_basic(t *testing.T) { - resourceName := "aws_api_gateway_v2_route_response.test" - routeResourceName := "aws_api_gateway_v2_route.test" - rName := fmt.Sprintf("tf-testacc-apigwv2-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSAPIGateway2RouteResponseDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSAPIGateway2RouteResponseConfig_basic(rName), - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGateway2RouteResponseExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "model_selection_expression", ""), - resource.TestCheckResourceAttr(resourceName, "response_models.%", "0"), - resource.TestCheckResourceAttrPair(resourceName, "route_id", routeResourceName, "id"), - resource.TestCheckResourceAttr(resourceName, "route_response_key", "$default"), - ), - }, - { - ResourceName: resourceName, - ImportStateIdFunc: testAccAWSAPIGateway2RouteResponseImportStateIdFunc(resourceName), - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func TestAccAWSAPIGateway2RouteResponse_Model(t *testing.T) { - resourceName := "aws_api_gateway_v2_route_response.test" - modelResourceName := "aws_api_gateway_v2_model.test" - routeResourceName := "aws_api_gateway_v2_route.test" - rName := fmt.Sprintf("tftestaccapigwv2%s", acctest.RandStringFromCharSet(16, acctest.CharSetAlphaNum)) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSAPIGateway2RouteResponseDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSAPIGateway2RouteResponseConfig_model(rName), - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAPIGateway2RouteResponseExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "model_selection_expression", "action"), - resource.TestCheckResourceAttr(resourceName, "response_models.%", "1"), - resource.TestCheckResourceAttrPair(resourceName, "response_models.test", modelResourceName, "name"), - resource.TestCheckResourceAttrPair(resourceName, "route_id", routeResourceName, "id"), - resource.TestCheckResourceAttr(resourceName, "route_response_key", "$default"), - ), - }, - { - ResourceName: resourceName, - ImportStateIdFunc: testAccAWSAPIGateway2RouteResponseImportStateIdFunc(resourceName), - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func testAccCheckAWSAPIGateway2RouteResponseDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*AWSClient).apigatewayv2conn - - for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_api_gateway_v2_route_response" { - continue - } - - _, err := conn.GetRouteResponse(&apigatewayv2.GetRouteResponseInput{ - ApiId: aws.String(rs.Primary.Attributes["api_id"]), - RouteId: aws.String(rs.Primary.Attributes["route_id"]), - RouteResponseId: aws.String(rs.Primary.ID), - }) - if isAWSErr(err, apigatewayv2.ErrCodeNotFoundException, "") { - continue - } - if err != nil { - return err - } - - return fmt.Errorf("API Gateway v2 route response %s still exists", rs.Primary.ID) - } - - return nil -} - -func testAccCheckAWSAPIGateway2RouteResponseExists(n string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No API Gateway v2 route response ID is set") - } - - conn := testAccProvider.Meta().(*AWSClient).apigatewayv2conn - - _, err := conn.GetRouteResponse(&apigatewayv2.GetRouteResponseInput{ - ApiId: aws.String(rs.Primary.Attributes["api_id"]), - RouteId: aws.String(rs.Primary.Attributes["route_id"]), - RouteResponseId: aws.String(rs.Primary.ID), - }) - if err != nil { - return err - } - - return nil - } -} - -func testAccAWSAPIGateway2RouteResponseImportStateIdFunc(resourceName string) resource.ImportStateIdFunc { - return func(s *terraform.State) (string, error) { - rs, ok := s.RootModule().Resources[resourceName] - if !ok { - return "", fmt.Errorf("Not Found: %s", resourceName) - } - - return fmt.Sprintf("%s/%s/%s", rs.Primary.Attributes["api_id"], rs.Primary.Attributes["route_id"], rs.Primary.ID), nil - } -} - -func testAccAWSAPIGateway2RouteResponseConfig_basic(rName string) string { - return testAccAWSAPIGatewayV2RouteConfig_basic(rName) + fmt.Sprintf(` -resource "aws_api_gateway_v2_route_response" "test" { - api_id = "${aws_api_gateway_v2_api.test.id}" - route_id = "${aws_api_gateway_v2_route.test.id}" - route_response_key = "$default" -} -`) -} - -func testAccAWSAPIGateway2RouteResponseConfig_model(rName string) string { - return testAccAWSAPIGatewayV2RouteConfig_model(rName) + fmt.Sprintf(` -resource "aws_api_gateway_v2_route_response" "test" { - api_id = "${aws_api_gateway_v2_api.test.id}" - route_id = "${aws_api_gateway_v2_route.test.id}" - route_response_key = "$default" - - model_selection_expression = "action" - - response_models = { - "test" = "${aws_api_gateway_v2_model.test.name}" - } -} -`) -} diff --git a/aws/resource_aws_api_gateway2_route_response.go b/aws/resource_aws_apigatewayv2_route_response.go similarity index 81% rename from aws/resource_aws_api_gateway2_route_response.go rename to aws/resource_aws_apigatewayv2_route_response.go index 105b99e45dc..ed8f895aad3 100644 --- a/aws/resource_aws_api_gateway2_route_response.go +++ b/aws/resource_aws_apigatewayv2_route_response.go @@ -10,14 +10,14 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) -func resourceAwsApiGateway2RouteResponse() *schema.Resource { +func resourceAwsApiGatewayV2RouteResponse() *schema.Resource { return &schema.Resource{ - Create: resourceAwsApiGateway2RouteResponseCreate, - Read: resourceAwsApiGateway2RouteResponseRead, - Update: resourceAwsApiGateway2RouteResponseUpdate, - Delete: resourceAwsApiGateway2RouteResponseDelete, + Create: resourceAwsApiGatewayV2RouteResponseCreate, + Read: resourceAwsApiGatewayV2RouteResponseRead, + Update: resourceAwsApiGatewayV2RouteResponseUpdate, + Delete: resourceAwsApiGatewayV2RouteResponseDelete, Importer: &schema.ResourceImporter{ - State: resourceAwsApiGateway2RouteResponseImport, + State: resourceAwsApiGatewayV2RouteResponseImport, }, Schema: map[string]*schema.Schema{ @@ -48,7 +48,7 @@ func resourceAwsApiGateway2RouteResponse() *schema.Resource { } } -func resourceAwsApiGateway2RouteResponseCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsApiGatewayV2RouteResponseCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).apigatewayv2conn req := &apigatewayv2.CreateRouteResponseInput{ @@ -71,10 +71,10 @@ func resourceAwsApiGateway2RouteResponseCreate(d *schema.ResourceData, meta inte d.SetId(aws.StringValue(resp.RouteResponseId)) - return resourceAwsApiGateway2RouteResponseRead(d, meta) + return resourceAwsApiGatewayV2RouteResponseRead(d, meta) } -func resourceAwsApiGateway2RouteResponseRead(d *schema.ResourceData, meta interface{}) error { +func resourceAwsApiGatewayV2RouteResponseRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).apigatewayv2conn resp, err := conn.GetRouteResponse(&apigatewayv2.GetRouteResponseInput{ @@ -100,7 +100,7 @@ func resourceAwsApiGateway2RouteResponseRead(d *schema.ResourceData, meta interf return nil } -func resourceAwsApiGateway2RouteResponseUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsApiGatewayV2RouteResponseUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).apigatewayv2conn req := &apigatewayv2.UpdateRouteResponseInput{ @@ -124,10 +124,10 @@ func resourceAwsApiGateway2RouteResponseUpdate(d *schema.ResourceData, meta inte return fmt.Errorf("error updating API Gateway v2 route response: %s", err) } - return resourceAwsApiGateway2RouteResponseRead(d, meta) + return resourceAwsApiGatewayV2RouteResponseRead(d, meta) } -func resourceAwsApiGateway2RouteResponseDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAwsApiGatewayV2RouteResponseDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).apigatewayv2conn log.Printf("[DEBUG] Deleting API Gateway v2 route response (%s)", d.Id()) @@ -146,7 +146,7 @@ func resourceAwsApiGateway2RouteResponseDelete(d *schema.ResourceData, meta inte return nil } -func resourceAwsApiGateway2RouteResponseImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { +func resourceAwsApiGatewayV2RouteResponseImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { parts := strings.Split(d.Id(), "/") if len(parts) != 3 { return []*schema.ResourceData{}, fmt.Errorf("Wrong format of resource: %s. Please follow 'api-id/route-id/route-response-id'", d.Id()) diff --git a/aws/resource_aws_apigatewayv2_route_response_test.go b/aws/resource_aws_apigatewayv2_route_response_test.go new file mode 100644 index 00000000000..d38970eaede --- /dev/null +++ b/aws/resource_aws_apigatewayv2_route_response_test.go @@ -0,0 +1,212 @@ +package aws + +import ( + "fmt" + "strings" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/apigatewayv2" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccAWSAPIGatewayV2RouteResponse_basic(t *testing.T) { + var apiId, routeId string + var v apigatewayv2.GetRouteResponseOutput + resourceName := "aws_apigatewayv2_route_response.test" + routeResourceName := "aws_apigatewayv2_route.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayV2RouteResponseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGatewayV2RouteResponseConfig_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayV2RouteResponseExists(resourceName, &apiId, &routeId, &v), + resource.TestCheckResourceAttr(resourceName, "model_selection_expression", ""), + resource.TestCheckResourceAttr(resourceName, "response_models.%", "0"), + resource.TestCheckResourceAttrPair(resourceName, "route_id", routeResourceName, "id"), + resource.TestCheckResourceAttr(resourceName, "route_response_key", "$default"), + ), + }, + { + ResourceName: resourceName, + ImportStateIdFunc: testAccAWSAPIGatewayV2RouteResponseImportStateIdFunc(resourceName), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAWSAPIGatewayV2RouteResponse_disappears(t *testing.T) { + var apiId, routeId string + var v apigatewayv2.GetRouteResponseOutput + resourceName := "aws_apigatewayv2_route_response.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayV2RouteResponseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGatewayV2RouteResponseConfig_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayV2RouteResponseExists(resourceName, &apiId, &routeId, &v), + testAccCheckAWSAPIGatewayV2RouteResponseDisappears(&apiId, &routeId, &v), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func TestAccAWSAPIGatewayV2RouteResponse_Model(t *testing.T) { + var apiId, routeId string + var v apigatewayv2.GetRouteResponseOutput + resourceName := "aws_apigatewayv2_route_response.test" + modelResourceName := "aws_apigatewayv2_model.test" + routeResourceName := "aws_apigatewayv2_route.test" + // Model name must be alphanumeric. + rName := strings.ReplaceAll(acctest.RandomWithPrefix("tf-acc-test"), "-", "") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayV2RouteResponseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGatewayV2RouteResponseConfig_model(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayV2RouteResponseExists(resourceName, &apiId, &routeId, &v), + resource.TestCheckResourceAttr(resourceName, "model_selection_expression", "action"), + resource.TestCheckResourceAttr(resourceName, "response_models.%", "1"), + resource.TestCheckResourceAttrPair(resourceName, "response_models.test", modelResourceName, "name"), + resource.TestCheckResourceAttrPair(resourceName, "route_id", routeResourceName, "id"), + resource.TestCheckResourceAttr(resourceName, "route_response_key", "$default"), + ), + }, + { + ResourceName: resourceName, + ImportStateIdFunc: testAccAWSAPIGatewayV2RouteResponseImportStateIdFunc(resourceName), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccCheckAWSAPIGatewayV2RouteResponseDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).apigatewayv2conn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_apigatewayv2_route_response" { + continue + } + + _, err := conn.GetRouteResponse(&apigatewayv2.GetRouteResponseInput{ + ApiId: aws.String(rs.Primary.Attributes["api_id"]), + RouteId: aws.String(rs.Primary.Attributes["route_id"]), + RouteResponseId: aws.String(rs.Primary.ID), + }) + if isAWSErr(err, apigatewayv2.ErrCodeNotFoundException, "") { + continue + } + if err != nil { + return err + } + + return fmt.Errorf("API Gateway v2 route response %s still exists", rs.Primary.ID) + } + + return nil +} + +func testAccCheckAWSAPIGatewayV2RouteResponseDisappears(apiId, routeId *string, v *apigatewayv2.GetRouteResponseOutput) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).apigatewayv2conn + + _, err := conn.DeleteRouteResponse(&apigatewayv2.DeleteRouteResponseInput{ + ApiId: apiId, + RouteId: routeId, + RouteResponseId: v.RouteResponseId, + }) + + return err + } +} + +func testAccCheckAWSAPIGatewayV2RouteResponseExists(n string, vApiId, vRouteId *string, v *apigatewayv2.GetRouteResponseOutput) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No API Gateway v2 route response ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).apigatewayv2conn + + apiId := aws.String(rs.Primary.Attributes["api_id"]) + routeId := aws.String(rs.Primary.Attributes["route_id"]) + resp, err := conn.GetRouteResponse(&apigatewayv2.GetRouteResponseInput{ + ApiId: apiId, + RouteId: routeId, + RouteResponseId: aws.String(rs.Primary.ID), + }) + if err != nil { + return err + } + + *vApiId = *apiId + *vRouteId = *routeId + *v = *resp + + return nil + } +} + +func testAccAWSAPIGatewayV2RouteResponseImportStateIdFunc(resourceName string) resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return "", fmt.Errorf("Not Found: %s", resourceName) + } + + return fmt.Sprintf("%s/%s/%s", rs.Primary.Attributes["api_id"], rs.Primary.Attributes["route_id"], rs.Primary.ID), nil + } +} + +func testAccAWSAPIGatewayV2RouteResponseConfig_basic(rName string) string { + return testAccAWSAPIGatewayV2RouteConfig_basic(rName) + fmt.Sprintf(` +resource "aws_apigatewayv2_route_response" "test" { + api_id = "${aws_apigatewayv2_api.test.id}" + route_id = "${aws_apigatewayv2_route.test.id}" + route_response_key = "$default" +} +`) +} + +func testAccAWSAPIGatewayV2RouteResponseConfig_model(rName string) string { + return testAccAWSAPIGatewayV2RouteConfig_model(rName) + fmt.Sprintf(` +resource "aws_apigatewayv2_route_response" "test" { + api_id = "${aws_apigatewayv2_api.test.id}" + route_id = "${aws_apigatewayv2_route.test.id}" + route_response_key = "$default" + + model_selection_expression = "action" + + response_models = { + "test" = "${aws_apigatewayv2_model.test.name}" + } +} +`) +} diff --git a/website/aws.erb b/website/aws.erb index 97d2d5d5f60..42d2ba89cfc 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -237,7 +237,7 @@ aws_apigatewayv2_route
  • - aws_api_gateway_v2_route_response + aws_apigatewayv2_route_response
  • diff --git a/website/docs/r/api_gateway_v2_route_response.html.markdown b/website/docs/r/apigatewayv2_route_response.html.markdown similarity index 60% rename from website/docs/r/api_gateway_v2_route_response.html.markdown rename to website/docs/r/apigatewayv2_route_response.html.markdown index abdc8464a32..09d9c05f3b8 100644 --- a/website/docs/r/api_gateway_v2_route_response.html.markdown +++ b/website/docs/r/apigatewayv2_route_response.html.markdown @@ -1,12 +1,12 @@ --- +subcategory: "API Gateway v2 (WebSocket and HTTP APIs)" layout: "aws" -page_title: "AWS: aws_api_gateway_v2_route_response" -sidebar_current: "docs-aws-resource-api-gateway-v2-route-response" +page_title: "AWS: aws_apigatewayv2_route_response" description: |- Manages an Amazon API Gateway Version 2 route response. --- -# Resource: aws_api_gateway_v2_route_response +# Resource: aws_apigatewayv2_route_response Manages an Amazon API Gateway Version 2 route response. More information can be found in the [Amazon API Gateway Developer Guide](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html). @@ -16,9 +16,9 @@ More information can be found in the [Amazon API Gateway Developer Guide](https: ### Basic ```hcl -resource "aws_api_gateway_v2_route_response" "example" { - api_id = "${aws_api_gateway_v2_api.example.id}" - route_id = "${aws_api_gateway_v2_route.example.id}" +resource "aws_apigatewayv2_route_response" "example" { + api_id = "${aws_apigatewayv2_api.example.id}" + route_id = "${aws_apigatewayv2_route.example.id}" route_response_key = "$default" } ``` @@ -28,7 +28,7 @@ resource "aws_api_gateway_v2_route_response" "example" { The following arguments are supported: * `api_id` - (Required) The API identifier. -* `route_id` - (Required) The identifier of the [`aws_api_gateway_v2_route`](/docs/providers/aws/r/api_gateway_v2_route.html). +* `route_id` - (Required) The identifier of the [`aws_apigatewayv2_route`](/docs/providers/aws/r/apigatewayv2_route.html). * `route_response_key` - (Required) The route response key. * `model_selection_expression` - (Optional) The [model selection expression](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-selection-expressions.html#apigateway-websocket-api-model-selection-expressions) for the route response. * `response_models` - (Optional) The response models for the route response. @@ -41,8 +41,8 @@ In addition to all arguments above, the following attributes are exported: ## Import -`aws_api_gateway_v2_route_response` can be imported by using the API identifier, route identifier and route response identifier, e.g. +`aws_apigatewayv2_route_response` can be imported by using the API identifier, route identifier and route response identifier, e.g. ``` -$ terraform import aws_api_gateway_v2_route.example aabbccddee/1122334/998877 +$ terraform import aws_apigatewayv2_route_response.example aabbccddee/1122334/998877 ```