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

add timeouts support for sql database instance #1288

Merged
merged 2 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"regexp"
"strings"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -41,6 +42,12 @@ func resourceSqlDatabaseInstance() *schema.Resource {
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},

Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -670,7 +677,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})

d.SetId(instance.Name)

err = sqladminOperationWait(config, op, project, "Create Instance")
err = sqladminOperationWaitTime(config, op, project, "Create Instance", int(d.Timeout(schema.TimeoutCreate).Minutes()))
if err != nil {
d.SetId("")
return err
Expand All @@ -697,7 +704,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
err = retry(func() error {
op, err = config.clientSqlAdmin.Users.Delete(project, instance.Name, u.Host, u.Name).Do()
if err == nil {
err = sqladminOperationWait(config, op, project, "Delete default root User")
err = sqladminOperationWaitTime(config, op, project, "Delete default root User", int(d.Timeout(schema.TimeoutCreate).Minutes()))
}
return err
})
Expand Down Expand Up @@ -1027,7 +1034,7 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error, failed to update instance %s: %s", instance.Name, err)
}

err = sqladminOperationWait(config, op, project, "Create Instance")
err = sqladminOperationWaitTime(config, op, project, "Update Instance", int(d.Timeout(schema.TimeoutUpdate).Minutes()))
if err != nil {
return err
}
Expand All @@ -1049,7 +1056,7 @@ func resourceSqlDatabaseInstanceDelete(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error, failed to delete instance %s: %s", d.Get("name").(string), err)
}

err = sqladminOperationWait(config, op, project, "Delete Instance")
err = sqladminOperationWaitTime(config, op, project, "Delete Instance", int(d.Timeout(schema.TimeoutDelete).Minutes()))
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion google/sqladmin_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ func (e SqlAdminOperationError) Error() string {
}

func sqladminOperationWait(config *Config, op *sqladmin.Operation, project, activity string) error {
return sqladminOperationWaitTime(config, op, project, activity, 10)
}

func sqladminOperationWaitTime(config *Config, op *sqladmin.Operation, project, activity string, timeoutMinutes int) error {
w := &SqlAdminOperationWaiter{
Service: config.clientSqlAdmin,
Op: op,
Project: project,
}

state := w.Conf()
state.Timeout = 10 * time.Minute
state.Timeout = time.Duration(timeoutMinutes) * time.Minute
state.MinTimeout = 2 * time.Second
state.Delay = 5 * time.Second
opRaw, err := state.WaitForState()
Expand Down
10 changes: 10 additions & 0 deletions website/docs/r/sql_database_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ when the resource is configured with a `count`.
* `settings.version` - Used to make sure changes to the `settings` block are
atomic.

<a id="timeouts"></a>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this. The h2 tag generated from ##timeouts will have its id set to timeouts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

## Timeouts

`google_sql_database_instance` provides the following
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

- `create` - Default is 10 minutes.
- `update` - Default is 10 minutes.
- `delete` - Default is 10 minutes.

## Import

Database instances can be imported using the `name`, e.g.
Expand Down