Skip to content

Commit

Permalink
Support specifying a table in mysql_grant (petoju#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
dozen authored and Joe Stump committed Oct 20, 2018
1 parent 538e0f8 commit 13f7423
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions mysql/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func resourceGrant() *schema.Resource {
ForceNew: true,
},

"table": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "*",
},

"privileges": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Expand Down Expand Up @@ -74,9 +81,10 @@ func CreateGrant(d *schema.ResourceData, meta interface{}) error {
}
privileges = strings.Join(privilegesList, ",")

stmtSQL := fmt.Sprintf("GRANT %s on %s.* TO '%s'@'%s'",
stmtSQL := fmt.Sprintf("GRANT %s on %s.%s TO '%s'@'%s'",
privileges,
d.Get("database").(string),
d.Get("table").(string),
d.Get("user").(string),
d.Get("host").(string))

Expand Down Expand Up @@ -119,8 +127,9 @@ func ReadGrant(d *schema.ResourceData, meta interface{}) error {
func DeleteGrant(d *schema.ResourceData, meta interface{}) error {
db := meta.(*providerConfiguration).DB

stmtSQL := fmt.Sprintf("REVOKE GRANT OPTION ON %s.* FROM '%s'@'%s'",
stmtSQL := fmt.Sprintf("REVOKE GRANT OPTION ON %s.%s FROM '%s'@'%s'",
d.Get("database").(string),
d.Get("table").(string),
d.Get("user").(string),
d.Get("host").(string))

Expand All @@ -130,8 +139,9 @@ func DeleteGrant(d *schema.ResourceData, meta interface{}) error {
return err
}

stmtSQL = fmt.Sprintf("REVOKE ALL ON %s.* FROM '%s'@'%s'",
stmtSQL = fmt.Sprintf("REVOKE ALL ON %s.%s FROM '%s'@'%s'",
d.Get("database").(string),
d.Get("table").(string),
d.Get("user").(string),
d.Get("host").(string))

Expand Down
1 change: 1 addition & 0 deletions mysql/resource_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestAccGrant(t *testing.T) {
resource.TestCheckResourceAttr("mysql_grant.test", "user", "jdoe"),
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
resource.TestCheckResourceAttr("mysql_grant.test", "database", "foo"),
resource.TestCheckResourceAttr("mysql_grant.test", "table", "*"),
resource.TestCheckResourceAttr("mysql_grant.test", "tls_option", "NONE"),
),
},
Expand Down

0 comments on commit 13f7423

Please sign in to comment.