Skip to content

Commit

Permalink
Merge pull request petoju#18 from winebarrel/read_privs_when_plan
Browse files Browse the repository at this point in the history
Read existing privileges when terraform plan
  • Loading branch information
winebarrel authored Apr 9, 2020
2 parents 7808f3d + 22cc10f commit da4dcec
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions mysql/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,27 @@ func ReadGrant(d *schema.ResourceData, meta interface{}) error {
return err
}

sql := fmt.Sprintf("SHOW GRANTS FOR %s", userOrRole)
grants, err := showGrants(db, userOrRole)

log.Println("[DEBUG] SQL:", sql)

_, err = db.Exec(sql)
if err != nil {
log.Printf("[WARN] GRANT not found for %s - removing from state", userOrRole)
d.SetId("")
return nil
}

database := d.Get("database").(string)
table := d.Get("table").(string)

var privileges []string

for _, grant := range grants {
if grant.Database == database && grant.Table == table {
privileges = grant.Privileges
}
}

d.Set("privileges", privileges)

return nil
}

Expand Down

0 comments on commit da4dcec

Please sign in to comment.