Skip to content

Commit

Permalink
Read grant option
Browse files Browse the repository at this point in the history
  • Loading branch information
winebarrel committed Apr 9, 2020
1 parent da4dcec commit 5ce7110
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions mysql/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type MySQLGrant struct {
Database string
Table string
Privileges []string
Grant bool
}

func resourceGrant() *schema.Resource {
Expand Down Expand Up @@ -253,14 +254,20 @@ func ReadGrant(d *schema.ResourceData, meta interface{}) error {
table := d.Get("table").(string)

var privileges []string
var grantOption bool

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

if grant.Grant {
grantOption = true
}
}

d.Set("privileges", privileges)
d.Set("grant", grantOption)

return nil
}
Expand Down Expand Up @@ -359,6 +366,7 @@ func restoreGrant(user string, host string, grant *MySQLGrant) *schema.ResourceD
d.Set("host", host)
d.Set("database", database)
d.Set("table", grant.Table)
d.Set("grant", grant.Grant)
d.Set("tls_option", "NONE")
d.Set("privileges", grant.Privileges)

Expand All @@ -377,6 +385,7 @@ func showGrants(db *sql.DB, user string) ([]*MySQLGrant, error) {

defer rows.Close()
re := regexp.MustCompile(`^GRANT (.+) ON (.+?)\.(.+?) TO`)
reGrant := regexp.MustCompile(`\bGRANT OPTION\b`)

for rows.Next() {
var rawGrant string
Expand Down Expand Up @@ -405,6 +414,7 @@ func showGrants(db *sql.DB, user string) ([]*MySQLGrant, error) {
Database: strings.Trim(m[2], "`"),
Table: strings.Trim(m[3], "`"),
Privileges: privileges,
Grant: reGrant.MatchString(rawGrant),
}

grants = append(grants, grant)
Expand Down

0 comments on commit 5ce7110

Please sign in to comment.