Skip to content

Commit

Permalink
Refactoring ImportGrant
Browse files Browse the repository at this point in the history
  • Loading branch information
winebarrel committed Apr 9, 2020
1 parent e1bc8fd commit 0015879
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ github.com/hashicorp/terraform-config-inspect v0.0.0-20190821133035-82a99dc22ef4
github.com/hashicorp/terraform-config-inspect v0.0.0-20190821133035-82a99dc22ef4/go.mod h1:JDmizlhaP5P0rYTTZB0reDMefAiJyfWPEtugV4in1oI=
github.com/hashicorp/terraform-plugin-sdk v1.0.0 h1:3AjuuV1LJKs1NlG+heUgqWN6/QCSx2kDhyS6K7F0fTw=
github.com/hashicorp/terraform-plugin-sdk v1.0.0/go.mod h1:NuwtLpEpPsFaKJPJNGtMcn9vlhe6Ofe+Y6NqXhJgV2M=
github.com/hashicorp/terraform-plugin-sdk v1.9.1 h1:AgHnd6yPCg7o57XWrv4L7tIMdF0KQpcZro1pDHF1Xbw=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
Expand Down
89 changes: 58 additions & 31 deletions mysql/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (

const nonexistingGrantErrCode = 1141

type MySQLGrant struct {
Database string
Table string
Privileges []string
}

func resourceGrant() *schema.Resource {
return &schema.Resource{
Create: CreateGrant,
Expand Down Expand Up @@ -316,61 +322,82 @@ func ImportGrant(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceDa

db := meta.(*MySQLConfiguration).Db

sql := fmt.Sprintf("SHOW GRANTS FOR '%s'@'%s'", user, host)
rows, err := db.Query(sql)
grants, err := showGrants(db, fmt.Sprintf("'%s'@'%s'", user, host))

if err != nil {
return nil, err
}

defer rows.Close()
re := regexp.MustCompile(`^GRANT (.+) ON (.+?)\.(.+?) TO`)
results := []*schema.ResourceData{}

for rows.Next() {
var grant string

err := rows.Scan(&grant)

if err != nil {
return nil, err
}

m := re.FindStringSubmatch(grant)

if len(m) != 4 {
return nil, fmt.Errorf("failed to parse grant statement: %s", grant)
}

privs := m[1]
database := strings.Trim(m[2], "`")
table := strings.Trim(m[3], "`")
results = append(results, restoreGrant(user, host, database, table, privs))
for _, grant := range grants {
results = append(results, restoreGrant(user, host, grant))
}

return results, nil
}

func restoreGrant(user string, host string, database string, table string, privsStr string) *schema.ResourceData {
func restoreGrant(user string, host string, grant *MySQLGrant) *schema.ResourceData {
d := resourceGrant().Data(nil)

database := grant.Database
id := fmt.Sprintf("%s@%s:%s", user, host, formatDatabaseName(database))
d.SetId(id)

d.Set("user", user)
d.Set("host", host)
d.Set("database", database)
d.Set("table", table)
d.Set("table", grant.Table)
d.Set("tls_option", "NONE")
d.Set("privileges", grant.Privileges)

priv_list := strings.Split(privsStr, ",")
privileges := make([]string, len(priv_list))
return d
}

func showGrants(db *sql.DB, user string) ([]*MySQLGrant, error) {
grants := []*MySQLGrant{}

for i, priv := range priv_list {
privileges[i] = strings.TrimSpace(priv)
sql := fmt.Sprintf("SHOW GRANTS FOR %s", user)
rows, err := db.Query(sql)

if err != nil {
return nil, err
}

d.Set("privileges", privileges)
defer rows.Close()
re := regexp.MustCompile(`^GRANT (.+) ON (.+?)\.(.+?) TO`)

for rows.Next() {
var rawGrant string

return d
err := rows.Scan(&rawGrant)

if err != nil {
return nil, err
}

m := re.FindStringSubmatch(rawGrant)

if len(m) != 4 {
return nil, fmt.Errorf("failed to parse grant statement: %s", rawGrant)
}

privsStr := m[1]
priv_list := strings.Split(privsStr, ",")
privileges := make([]string, len(priv_list))

for i, priv := range priv_list {
privileges[i] = strings.TrimSpace(priv)
}

grant := &MySQLGrant{
Database: strings.Trim(m[2], "`"),
Table: strings.Trim(m[3], "`"),
Privileges: privileges,
}

grants = append(grants, grant)
}

return grants, nil
}

0 comments on commit 0015879

Please sign in to comment.