Skip to content

Commit

Permalink
f/aws_mq_broker: Allow custom timeouts for common operations
Browse files Browse the repository at this point in the history
For peripheral AWS regions it is common that AmazonMQ RabbitMQ, for
instance, takes more than 30min for a create or even a broker update
when a cluster requires a full reboot.

Since there is already support in the code, the API is just not exposed,
this is mostly about allowing terraform code to configure the currently
static timeouts.

Nothing else changes, the defaults continue to be 30m, so it is
backwards compatible.
  • Loading branch information
marceloboeira committed Sep 30, 2022
1 parent 9c21d5d commit a8c1d97
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
12 changes: 9 additions & 3 deletions internal/service/mq/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"regexp"
"strconv"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/mq"
Expand All @@ -36,6 +37,11 @@ func ResourceBroker() *schema.Resource {
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"apply_immediately": {
Expand Down Expand Up @@ -407,7 +413,7 @@ func resourceBrokerCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(aws.StringValue(out.BrokerId))
d.Set("arn", out.BrokerArn)

if _, err := WaitBrokerCreated(conn, d.Id()); err != nil {
if _, err := WaitBrokerCreated(conn, d.Id(), schema.TimeoutCreate); err != nil {
return fmt.Errorf("error waiting for MQ Broker (%s) creation: %w", d.Id(), err)
}

Expand Down Expand Up @@ -593,7 +599,7 @@ func resourceBrokerUpdate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error rebooting MQ Broker (%s): %w", d.Id(), err)
}

if _, err := WaitBrokerRebooted(conn, d.Id()); err != nil {
if _, err := WaitBrokerRebooted(conn, d.Id(), schema.TimeoutDelete); err != nil {
return fmt.Errorf("error waiting for MQ Broker (%s) reboot: %w", d.Id(), err)
}
}
Expand All @@ -620,7 +626,7 @@ func resourceBrokerDelete(d *schema.ResourceData, meta interface{}) error {
return err
}

if _, err := WaitBrokerDeleted(conn, d.Id()); err != nil {
if _, err := WaitBrokerDeleted(conn, d.Id(), schema.TimeoutDelete); err != nil {
return fmt.Errorf("error waiting for MQ Broker (%s) deletion: %w", d.Id(), err)
}

Expand Down
18 changes: 6 additions & 12 deletions internal/service/mq/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
BrokerCreateTimeout = 30 * time.Minute
BrokerDeleteTimeout = 30 * time.Minute
BrokerRebootTimeout = 30 * time.Minute
)

func WaitBrokerCreated(conn *mq.MQ, id string) (*mq.DescribeBrokerResponse, error) {
func WaitBrokerCreated(conn *mq.MQ, id string, timeout time.Duration) (*mq.DescribeBrokerResponse, error) {
stateConf := resource.StateChangeConf{
Pending: []string{
mq.BrokerStateCreationInProgress,
mq.BrokerStateRebootInProgress,
},
Target: []string{mq.BrokerStateRunning},
Timeout: BrokerCreateTimeout,
Timeout: timeout,
Refresh: StatusBroker(conn, id),
}
outputRaw, err := stateConf.WaitForState()
Expand All @@ -32,7 +26,7 @@ func WaitBrokerCreated(conn *mq.MQ, id string) (*mq.DescribeBrokerResponse, erro
return nil, err
}

func WaitBrokerDeleted(conn *mq.MQ, id string) (*mq.DescribeBrokerResponse, error) {
func WaitBrokerDeleted(conn *mq.MQ, id string, timeout time.Duration) (*mq.DescribeBrokerResponse, error) {
stateConf := resource.StateChangeConf{
Pending: []string{
mq.BrokerStateCreationFailed,
Expand All @@ -41,7 +35,7 @@ func WaitBrokerDeleted(conn *mq.MQ, id string) (*mq.DescribeBrokerResponse, erro
mq.BrokerStateRunning,
},
Target: []string{},
Timeout: BrokerDeleteTimeout,
Timeout: timeout,
Refresh: StatusBroker(conn, id),
}
outputRaw, err := stateConf.WaitForState()
Expand All @@ -53,13 +47,13 @@ func WaitBrokerDeleted(conn *mq.MQ, id string) (*mq.DescribeBrokerResponse, erro
return nil, err
}

func WaitBrokerRebooted(conn *mq.MQ, id string) (*mq.DescribeBrokerResponse, error) {
func WaitBrokerRebooted(conn *mq.MQ, id string, timeout time.Duration) (*mq.DescribeBrokerResponse, error) {
stateConf := resource.StateChangeConf{
Pending: []string{
mq.BrokerStateRebootInProgress,
},
Target: []string{mq.BrokerStateRunning},
Timeout: BrokerRebootTimeout,
Timeout: timeout,
Refresh: StatusBroker(conn, id),
}
outputRaw, err := stateConf.WaitForState()
Expand Down
9 changes: 9 additions & 0 deletions website/docs/r/mq_broker.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ In addition to all arguments above, the following attributes are exported:
* `amqps://broker-id.mq.us-west-2.amazonaws.com:5671`
* `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://www.terraform.io/docs/configuration/blocks/resources/syntax.html#operation-timeouts):

- `create` - (Default `30m`)
- `update` - (Default `30m`)
- `delete` - (Default `30m`)
any cleanup task during the destroying process.

## Import

MQ Brokers can be imported using their broker id, e.g.,
Expand Down

0 comments on commit a8c1d97

Please sign in to comment.