mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-10-02 00:31:04 +00:00
Investigate and fix flaky sql user test. (#1212)
This commit is contained in:
parent
1621932c0e
commit
4b15990dc3
@ -685,10 +685,10 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
|
|||||||
// replica, then any users are inherited from the master instance and should be left alone.
|
// replica, then any users are inherited from the master instance and should be left alone.
|
||||||
if !sqlResourceIsReplica(d) {
|
if !sqlResourceIsReplica(d) {
|
||||||
var users *sqladmin.UsersListResponse
|
var users *sqladmin.UsersListResponse
|
||||||
err = retry(func() error {
|
err = retryTime(func() error {
|
||||||
users, err = config.clientSqlAdmin.Users.List(project, instance.Name).Do()
|
users, err = config.clientSqlAdmin.Users.List(project, instance.Name).Do()
|
||||||
return err
|
return err
|
||||||
})
|
}, 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error, attempting to list users associated with instance %s: %s", instance.Name, err)
|
return fmt.Errorf("Error, attempting to list users associated with instance %s: %s", instance.Name, err)
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,9 @@ func resourceSqlUserCreate(d *schema.ResourceData, meta interface{}) error {
|
|||||||
"user %s into instance %s: %s", name, instance, err)
|
"user %s into instance %s: %s", name, instance, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
d.SetId(fmt.Sprintf("%s/%s", instance, name))
|
// This will include a double-slash (//) for 2nd generation instances,
|
||||||
|
// for which user.Host is an empty string. That's okay.
|
||||||
|
d.SetId(fmt.Sprintf("%s/%s/%s", user.Name, user.Host, user.Instance))
|
||||||
|
|
||||||
err = sqladminOperationWait(config, op, project, "Insert User")
|
err = sqladminOperationWait(config, op, project, "Insert User")
|
||||||
|
|
||||||
@ -111,14 +113,20 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error {
|
|||||||
name := d.Get("name").(string)
|
name := d.Get("name").(string)
|
||||||
host := d.Get("host").(string)
|
host := d.Get("host").(string)
|
||||||
|
|
||||||
users, err := config.clientSqlAdmin.Users.List(project, instance).Do()
|
var users *sqladmin.UsersListResponse
|
||||||
|
err = nil
|
||||||
|
err = retry(func() error {
|
||||||
|
users, err = config.clientSqlAdmin.Users.List(project, instance).Do()
|
||||||
|
return err
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return handleNotFoundError(err, d, fmt.Sprintf("SQL User %q in instance %q", name, instance))
|
return handleNotFoundError(err, d, fmt.Sprintf("SQL User %q in instance %q", name, instance))
|
||||||
}
|
}
|
||||||
|
|
||||||
var user *sqladmin.User
|
var user *sqladmin.User
|
||||||
for _, currentUser := range users.Items {
|
for _, currentUser := range users.Items {
|
||||||
|
// The second part of this conditional is irrelevant for 2nd generation instances because
|
||||||
|
// host and currentUser.Host will always both be empty.
|
||||||
if currentUser.Name == name && currentUser.Host == host {
|
if currentUser.Name == name && currentUser.Host == host {
|
||||||
user = currentUser
|
user = currentUser
|
||||||
break
|
break
|
||||||
@ -136,6 +144,7 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error {
|
|||||||
d.Set("instance", user.Instance)
|
d.Set("instance", user.Instance)
|
||||||
d.Set("name", user.Name)
|
d.Set("name", user.Name)
|
||||||
d.Set("project", project)
|
d.Set("project", project)
|
||||||
|
d.SetId(fmt.Sprintf("%s/%s/%s", user.Name, user.Host, user.Instance))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,7 +234,6 @@ func resourceSqlUserImporter(d *schema.ResourceData, meta interface{}) ([]*schem
|
|||||||
d.Set("instance", parts[0])
|
d.Set("instance", parts[0])
|
||||||
d.Set("host", parts[1])
|
d.Set("host", parts[1])
|
||||||
d.Set("name", parts[2])
|
d.Set("name", parts[2])
|
||||||
d.SetId(fmt.Sprintf("%s/%s", parts[0], parts[2]))
|
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("Invalid specifier. Expecting {instance}/{name} for 2nd generation instance and {instance}/{host}/{name} for 1st generation instance")
|
return nil, fmt.Errorf("Invalid specifier. Expecting {instance}/{name} for 2nd generation instance and {instance}/{host}/{name} for 1st generation instance")
|
||||||
}
|
}
|
||||||
|
@ -68,6 +68,7 @@ func TestAccSqlUser_secondGen(t *testing.T) {
|
|||||||
},
|
},
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
ResourceName: "google_sql_user.user",
|
ResourceName: "google_sql_user.user",
|
||||||
|
ImportStateId: instance + "/admin",
|
||||||
ImportState: true,
|
ImportState: true,
|
||||||
ImportStateVerify: true,
|
ImportStateVerify: true,
|
||||||
ImportStateVerifyIgnore: []string{"password"},
|
ImportStateVerifyIgnore: []string{"password"},
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
|
"google.golang.org/api/googleapi"
|
||||||
"google.golang.org/api/sqladmin/v1beta4"
|
"google.golang.org/api/sqladmin/v1beta4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,13 +19,12 @@ type SqlAdminOperationWaiter struct {
|
|||||||
|
|
||||||
func (w *SqlAdminOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
|
func (w *SqlAdminOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
|
||||||
return func() (interface{}, string, error) {
|
return func() (interface{}, string, error) {
|
||||||
var op *sqladmin.Operation
|
|
||||||
var err error
|
|
||||||
|
|
||||||
log.Printf("[DEBUG] self_link: %s", w.Op.SelfLink)
|
log.Printf("[DEBUG] self_link: %s", w.Op.SelfLink)
|
||||||
op, err = w.Service.Operations.Get(w.Project, w.Op.Name).Do()
|
op, err := w.Service.Operations.Get(w.Project, w.Op.Name).Do()
|
||||||
|
|
||||||
if err != nil {
|
if e, ok := err.(*googleapi.Error); ok && (e.Code == 429 || e.Code == 503) {
|
||||||
|
return w.Op, "PENDING", nil
|
||||||
|
} else if err != nil {
|
||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user