Skip to content

Commit

Permalink
Merge pull request #37040 from hashicorp/b-aws_api_gateway_rest_api.r…
Browse files Browse the repository at this point in the history
…oot_resource_id

r/aws_api_gateway_rest_api: Correct set `root_resource_id` on resource Read
  • Loading branch information
ewbankkit committed Apr 23, 2024
2 parents 0a344a8 + a936385 commit 36de4bc
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .changelog/37040.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_api_gateway_rest_api: Correct set `root_resource_id` on resource Read
```
50 changes: 41 additions & 9 deletions internal/service/apigateway/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/service/apigateway"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand Down Expand Up @@ -111,6 +112,37 @@ func TestAccAPIGatewayResource_disappears(t *testing.T) {
})
}

// https://github.com/hashicorp/terraform-provider-aws/issues/37007.
func TestAccAPIGatewayResource_withSleep(t *testing.T) {
ctx := acctest.Context(t)
var conf apigateway.GetResourceOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_api_gateway_resource.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckAPIGatewayTypeEDGE(t) },
ErrorCheck: acctest.ErrorCheck(t, names.APIGatewayServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckResourceDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccResourceConfig_base(rName),
Check: resource.ComposeTestCheckFunc(
acctest.CheckSleep(t, 10*time.Second),
),
},
{
Config: testAccResourceConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists(ctx, resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "path", "/test"),
resource.TestCheckResourceAttr(resourceName, "path_part", "test"),
),
},
},
})
}

func testAccCheckResourceExists(ctx context.Context, n string, v *apigateway.GetResourceOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -169,30 +201,30 @@ func testAccResourceImportStateIdFunc(resourceName string) resource.ImportStateI
}
}

func testAccResourceConfig_basic(rName string) string {
func testAccResourceConfig_base(rName string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_rest_api" "test" {
name = "%s"
name = %[1]q
}
`, rName)
}

func testAccResourceConfig_basic(rName string) string {
return acctest.ConfigCompose(testAccResourceConfig_base(rName), `
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"
}
`, rName)
`)
}

func testAccResourceConfig_updatePathPart(rName string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_rest_api" "test" {
name = "%s"
}
return acctest.ConfigCompose(testAccResourceConfig_base(rName), `
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_changed"
}
`, rName)
`)
}
18 changes: 17 additions & 1 deletion internal/service/apigateway/rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,23 @@ func resourceRestAPIRead(ctx context.Context, d *schema.ResourceData, meta inter
d.Set("minimum_compression_size", strconv.FormatInt(int64(aws.ToInt32(api.MinimumCompressionSize)), 10))
}
d.Set("name", api.Name)
d.Set("root_resource_id", api.RootResourceId)

input := &apigateway.GetResourcesInput{
RestApiId: aws.String(d.Id()),
}

rootResource, err := findResource(ctx, conn, input, func(v *types.Resource) bool {
return aws.ToString(v.Path) == "/"
})

switch {
case err == nil:
d.Set("root_resource_id", rootResource.Id)
case tfresource.NotFound(err):
d.Set("root_resource_id", nil)
default:
return sdkdiag.AppendErrorf(diags, "reading API Gateway REST API (%s) root resource: %s", d.Id(), err)
}

policy, err := flattenAPIPolicy(api.Policy)
if err != nil {
Expand Down
22 changes: 19 additions & 3 deletions internal/service/apigateway/rest_api_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ func dataSourceRestAPIRead(ctx context.Context, d *schema.ResourceData, meta int
conn := meta.(*conns.AWSClient).APIGatewayClient(ctx)

name := d.Get("name")
input := &apigateway.GetRestApisInput{}
inputGRAs := &apigateway.GetRestApisInput{}

match, err := findRestAPI(ctx, conn, input, func(v *types.RestApi) bool {
match, err := findRestAPI(ctx, conn, inputGRAs, func(v *types.RestApi) bool {
return aws.ToString(v.Name) == name
})

Expand All @@ -118,7 +118,23 @@ func dataSourceRestAPIRead(ctx context.Context, d *schema.ResourceData, meta int
d.Set("minimum_compression_size", strconv.FormatInt(int64(aws.ToInt32(match.MinimumCompressionSize)), 10))
}
d.Set("policy", match.Policy)
d.Set("root_resource_id", match.RootResourceId)

inputGRs := &apigateway.GetResourcesInput{
RestApiId: aws.String(d.Id()),
}

rootResource, err := findResource(ctx, conn, inputGRs, func(v *types.Resource) bool {
return aws.ToString(v.Path) == "/"
})

switch {
case err == nil:
d.Set("root_resource_id", rootResource.Id)
case tfresource.NotFound(err):
d.Set("root_resource_id", nil)
default:
return sdkdiag.AppendErrorf(diags, "reading API Gateway REST API (%s) root resource: %s", d.Id(), err)
}

setTagsOut(ctx, match.Tags)

Expand Down

0 comments on commit 36de4bc

Please sign in to comment.