Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/apigateway_vpc_link: move stateConf to internal/service/apigateway and update delete target status #20441

Merged
merged 2 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/20441.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
aws/resource_aws_apigateway_vpc_link: Ensure deletion does not return an error when resource is not found
```
25 changes: 22 additions & 3 deletions aws/internal/service/apigateway/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,35 @@ import (
)

const (
// Maximum amount of time for VpcLink to become available
ApiGatewayVpcLinkAvailableTimeout = 20 * time.Minute

// Maximum amount of time for VpcLink to delete
ApiGatewayVpcLinkDeleteTimeout = 20 * time.Minute
)

func ApiGatewayVpcLinkAvailable(conn *apigateway.APIGateway, vpcLinkId string) error {
stateConf := &resource.StateChangeConf{
Pending: []string{apigateway.VpcLinkStatusPending},
Target: []string{apigateway.VpcLinkStatusAvailable},
Refresh: apiGatewayVpcLinkStatus(conn, vpcLinkId),
Timeout: ApiGatewayVpcLinkAvailableTimeout,
MinTimeout: 3 * time.Second,
}

_, err := stateConf.WaitForState()

return err
}

func ApiGatewayVpcLinkDeleted(conn *apigateway.APIGateway, vpcLinkId string) error {
stateConf := resource.StateChangeConf{
Pending: []string{apigateway.VpcLinkStatusPending,
Pending: []string{
apigateway.VpcLinkStatusPending,
apigateway.VpcLinkStatusAvailable,
apigateway.VpcLinkStatusDeleting},
Target: []string{""},
apigateway.VpcLinkStatusDeleting,
},
Target: []string{},
Timeout: ApiGatewayVpcLinkDeleteTimeout,
MinTimeout: 1 * time.Second,
Refresh: apiGatewayVpcLinkStatus(conn, vpcLinkId),
Expand Down
46 changes: 4 additions & 42 deletions aws/resource_aws_api_gateway_vpc_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@ package aws
import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/apigateway/waiter"
)

const (
// Maximum amount of time for VpcLink to become available
apigatewayVpcLinkAvailableTimeout = 20 * time.Minute
)

func resourceAwsApiGatewayVpcLink() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayVpcLinkCreate,
Expand Down Expand Up @@ -79,17 +72,8 @@ func resourceAwsApiGatewayVpcLinkCreate(d *schema.ResourceData, meta interface{}

d.SetId(aws.StringValue(resp.Id))

stateConf := &resource.StateChangeConf{
Pending: []string{apigateway.VpcLinkStatusPending},
Target: []string{apigateway.VpcLinkStatusAvailable},
Refresh: apigatewayVpcLinkRefreshStatusFunc(conn, *resp.Id),
Timeout: apigatewayVpcLinkAvailableTimeout,
MinTimeout: 3 * time.Second,
}

_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("error waiting for APIGateway Vpc Link status to be \"%s\": %w", apigateway.VpcLinkStatusAvailable, err)
if err := waiter.ApiGatewayVpcLinkAvailable(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for API Gateway VPC Link (%s) availability after creation: %w", d.Id(), err)
}

return resourceAwsApiGatewayVpcLinkRead(d, meta)
Expand Down Expand Up @@ -182,17 +166,8 @@ func resourceAwsApiGatewayVpcLinkUpdate(d *schema.ResourceData, meta interface{}
return err
}

stateConf := &resource.StateChangeConf{
Pending: []string{apigateway.VpcLinkStatusPending},
Target: []string{apigateway.VpcLinkStatusAvailable},
Refresh: apigatewayVpcLinkRefreshStatusFunc(conn, d.Id()),
Timeout: apigatewayVpcLinkAvailableTimeout,
MinTimeout: 3 * time.Second,
}

_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for APIGateway Vpc Link status to be \"%s\": %s", apigateway.VpcLinkStatusAvailable, err)
if err := waiter.ApiGatewayVpcLinkAvailable(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for API Gateway VPC Link (%s) availability after update: %w", d.Id(), err)
}

return resourceAwsApiGatewayVpcLinkRead(d, meta)
Expand Down Expand Up @@ -221,16 +196,3 @@ func resourceAwsApiGatewayVpcLinkDelete(d *schema.ResourceData, meta interface{}

return nil
}

func apigatewayVpcLinkRefreshStatusFunc(conn *apigateway.APIGateway, vl string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &apigateway.GetVpcLinkInput{
VpcLinkId: aws.String(vl),
}
resp, err := conn.GetVpcLink(input)
if err != nil {
return nil, "failed", err
}
return resp, *resp.Status, nil
}
}