Skip to content

Commit

Permalink
Replaced users.list api with users.get api to increase efficiency. (G…
Browse files Browse the repository at this point in the history
  • Loading branch information
ravisiddhu authored and Shourya Singh committed May 25, 2023
1 parent 31ff2bb commit 55149c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1069,27 +1069,25 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
// Users in a replica instance are inherited from the master instance and should be left alone.
// This deletion is done immediately after the instance is created, in order to minimize the
// risk of it being left on the instance, which would present a security concern.
if sqlDatabaseIsMaster(d) {
var users *sqladmin.UsersListResponse
if sqlDatabaseIsMaster(d) && strings.Contains(strings.ToUpper(databaseVersion), "MYSQL") {
var user *sqladmin.User
err = transport_tpg.RetryTimeDuration(func() error {
users, err = config.NewSqlAdminClient(userAgent).Users.List(project, instance.Name).Do()
user, err = config.NewSqlAdminClient(userAgent).Users.Get(project, instance.Name, "root").Host("%").Do()
return err
}, d.Timeout(schema.TimeoutRead), transport_tpg.IsSqlOperationInProgressError)
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 fetch root user associated with instance %s: %s", instance.Name, err)
}
for _, u := range users.Items {
if u.Name == "root" && u.Host == "%" {
err = transport_tpg.Retry(func() error {
op, err = config.NewSqlAdminClient(userAgent).Users.Delete(project, instance.Name).Host(u.Host).Name(u.Name).Do()
if err == nil {
err = SqlAdminOperationWaitTime(config, op, project, "Delete default root User", userAgent, d.Timeout(schema.TimeoutCreate))
}
return err
})
if err != nil {
return fmt.Errorf("Error, failed to delete default 'root'@'*' user, but the database was created successfully: %s", err)
if user != nil {
err = transport_tpg.Retry(func() error {
op, err = config.NewSqlAdminClient(userAgent).Users.Delete(project, instance.Name).Host(user.Host).Name(user.Name).Do()
if err == nil {
err = SqlAdminOperationWaitTime(config, op, project, "Delete default root User", userAgent, d.Timeout(schema.TimeoutCreate))
}
return err
})
if err != nil {
return fmt.Errorf("Error, failed to delete default 'root'@'*' user, but the database was created successfully: %s", err)
}
}
}
Expand Down
27 changes: 3 additions & 24 deletions mmv1/third_party/terraform/resources/resource_sql_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func ResourceSqlUser() *schema.Resource {
"password_policy": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -206,7 +207,6 @@ func expandPasswordPolicy(cfg interface{}) *sqladmin.UserPasswordValidationPolic
raw := cfg.([]interface{})[0].(map[string]interface{})

upvp := &sqladmin.UserPasswordValidationPolicy{}

if v, ok := raw["allowed_failed_attempts"]; ok {
upvp.AllowedFailedAttempts = int64(v.(int))
}
Expand Down Expand Up @@ -316,38 +316,17 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
host := d.Get("host").(string)

var users *sqladmin.UsersListResponse
var user *sqladmin.User
err = nil
err = transport_tpg.RetryTime(func() error {
users, err = config.NewSqlAdminClient(userAgent).Users.List(project, instance).Do()
user, err = config.NewSqlAdminClient(userAgent).Users.Get(project, instance, name).Host(host).Do()
return err
}, 5)
if err != nil {
// move away from transport_tpg.HandleNotFoundError() as we need to handle both 404 and 403
return handleUserNotFoundError(err, d, fmt.Sprintf("SQL User %q in instance %q", name, instance))
}

var user *sqladmin.User
databaseInstance, err := config.NewSqlAdminClient(userAgent).Instances.Get(project, instance).Do()
if err != nil {
return err
}

for _, currentUser := range users.Items {
if !strings.Contains(databaseInstance.DatabaseVersion, "POSTGRES") {
name = strings.Split(name, "@")[0]
}

if currentUser.Name == name {
// Host can only be empty for postgres instances,
// so don't compare the host if the API host is empty.
if host == "" || currentUser.Host == host {
user = currentUser
break
}
}
}

if user == nil {
log.Printf("[WARN] Removing SQL User %q because it's gone", d.Get("name").(string))
d.SetId("")
Expand Down

0 comments on commit 55149c7

Please sign in to comment.