From e37e29ae75da3fea644b39b92cf020db3c8e59df Mon Sep 17 00:00:00 2001 From: Gregor Jasny Date: Fri, 22 Mar 2024 22:11:05 +0100 Subject: [PATCH] aws_ec2_host: Add configurable timeout --- .changelog/36538.txt | 3 +++ internal/service/ec2/ec2_host.go | 13 ++++++++++--- internal/service/ec2/wait.go | 18 ++++++------------ website/docs/r/ec2_host.html.markdown | 8 ++++++++ 4 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 .changelog/36538.txt diff --git a/.changelog/36538.txt b/.changelog/36538.txt new file mode 100644 index 000000000000..811bd70e5c81 --- /dev/null +++ b/.changelog/36538.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_ec2_host: Add user configurable timeouts +``` diff --git a/internal/service/ec2/ec2_host.go b/internal/service/ec2/ec2_host.go index f16aebb17dc3..6a46a1581a3f 100644 --- a/internal/service/ec2/ec2_host.go +++ b/internal/service/ec2/ec2_host.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "log" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/arn" @@ -39,6 +40,12 @@ func ResourceHost() *schema.Resource { CustomizeDiff: verify.SetTagsDiff, + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(10 * time.Minute), + Update: schema.DefaultTimeout(10 * time.Minute), + Delete: schema.DefaultTimeout(20 * time.Minute), + }, + Schema: map[string]*schema.Schema{ "arn": { Type: schema.TypeString, @@ -130,7 +137,7 @@ func resourceHostCreate(ctx context.Context, d *schema.ResourceData, meta interf d.SetId(aws.StringValue(output.HostIds[0])) - if _, err := WaitHostCreated(ctx, conn, d.Id()); err != nil { + if _, err := WaitHostCreated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for EC2 Host (%s) create: %s", d.Id(), err) } @@ -210,7 +217,7 @@ func resourceHostUpdate(ctx context.Context, d *schema.ResourceData, meta interf return sdkdiag.AppendErrorf(diags, "modifying EC2 Host (%s): %s", d.Id(), err) } - if _, err := WaitHostUpdated(ctx, conn, d.Id()); err != nil { + if _, err := WaitHostUpdated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for EC2 Host (%s) update: %s", d.Id(), err) } } @@ -239,7 +246,7 @@ func resourceHostDelete(ctx context.Context, d *schema.ResourceData, meta interf return sdkdiag.AppendErrorf(diags, "releasing EC2 Host (%s): %s", d.Id(), err) } - if _, err := WaitHostDeleted(ctx, conn, d.Id()); err != nil { + if _, err := WaitHostDeleted(ctx, conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil { return sdkdiag.AppendErrorf(diags, "waiting for EC2 Host (%s) delete: %s", d.Id(), err) } diff --git a/internal/service/ec2/wait.go b/internal/service/ec2/wait.go index 5dd825c3e43b..c74852bbedb8 100644 --- a/internal/service/ec2/wait.go +++ b/internal/service/ec2/wait.go @@ -2160,17 +2160,11 @@ func WaitVPNGatewayDeleted(ctx context.Context, conn *ec2.EC2, id string) (*ec2. return nil, err } -const ( - HostCreatedTimeout = 10 * time.Minute - HostUpdatedTimeout = 10 * time.Minute - HostDeletedTimeout = 20 * time.Minute -) - -func WaitHostCreated(ctx context.Context, conn *ec2.EC2, id string) (*ec2.Host, error) { +func WaitHostCreated(ctx context.Context, conn *ec2.EC2, id string, timeout time.Duration) (*ec2.Host, error) { stateConf := &retry.StateChangeConf{ Pending: []string{ec2.AllocationStatePending}, Target: []string{ec2.AllocationStateAvailable}, - Timeout: HostCreatedTimeout, + Timeout: timeout, Refresh: StatusHostState(ctx, conn, id), } @@ -2183,11 +2177,11 @@ func WaitHostCreated(ctx context.Context, conn *ec2.EC2, id string) (*ec2.Host, return nil, err } -func WaitHostUpdated(ctx context.Context, conn *ec2.EC2, id string) (*ec2.Host, error) { +func WaitHostUpdated(ctx context.Context, conn *ec2.EC2, id string, timeout time.Duration) (*ec2.Host, error) { stateConf := &retry.StateChangeConf{ Pending: []string{ec2.AllocationStatePending}, Target: []string{ec2.AllocationStateAvailable}, - Timeout: HostUpdatedTimeout, + Timeout: timeout, Refresh: StatusHostState(ctx, conn, id), } @@ -2200,11 +2194,11 @@ func WaitHostUpdated(ctx context.Context, conn *ec2.EC2, id string) (*ec2.Host, return nil, err } -func WaitHostDeleted(ctx context.Context, conn *ec2.EC2, id string) (*ec2.Host, error) { +func WaitHostDeleted(ctx context.Context, conn *ec2.EC2, id string, timeout time.Duration) (*ec2.Host, error) { stateConf := &retry.StateChangeConf{ Pending: []string{ec2.AllocationStateAvailable}, Target: []string{}, - Timeout: HostDeletedTimeout, + Timeout: timeout, Refresh: StatusHostState(ctx, conn, id), } diff --git a/website/docs/r/ec2_host.html.markdown b/website/docs/r/ec2_host.html.markdown index 6a3aa7cf292f..438b77ac7e84 100644 --- a/website/docs/r/ec2_host.html.markdown +++ b/website/docs/r/ec2_host.html.markdown @@ -45,6 +45,14 @@ This resource exports the following attributes in addition to the arguments abov * `owner_id` - The ID of the AWS account that owns the Dedicated Host. * `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block). +## Timeouts + +[Configuration options](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts): + +* `create` - (Default `10m`) +* `update` - (Default `10m`) +* `delete` - (Default `20m`) + ## Import In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import hosts using the host `id`. For example: