Skip to content

Commit

Permalink
fix: ignore host when comparing role grants (#190)
Browse files Browse the repository at this point in the history
* fix: ignore host when comparing role grants

* test: add test case for role-to-role grant
  • Loading branch information
jessebye authored Jan 13, 2025
1 parent 14ab080 commit 51183e9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mysql/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ func (t *RoleGrant) ConflictsWithGrant(other MySQLGrant) bool {
if !ok {
return false
}
return otherTyped.GetUserOrRole() == t.GetUserOrRole()
// Only compare Name, since the Host is irrelevant and may not match what is returned from MySQL
return otherTyped.GetUserOrRole().Name == t.GetUserOrRole().Name
}

func resourceGrant() *schema.Resource {
Expand Down
43 changes: 43 additions & 0 deletions mysql/resource_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,27 @@ func TestAccGrant_complexRoleGrants(t *testing.T) {
})
}

func TestAccGrant_roleToRoleGrant(t *testing.T) {
dbName := fmt.Sprintf("tf-test-%d", rand.Intn(100))
roleName1 := fmt.Sprintf("TFRole1-%d", rand.Intn(100))
roleName2 := fmt.Sprintf("TFRole2-%d", rand.Intn(100))
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckSkipRds(t)
testAccPreCheckSkipNotMySQLVersionMin(t, "8.0.0")
testAccPreCheckSkipTiDB(t)
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccGrantCheckDestroy,
Steps: []resource.TestStep{
{
Config: testAccGrantConfigRoleToRole(dbName, roleName1, roleName2),
},
},
})
}

func prepareTable(dbname string, tableName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ctx := context.Background()
Expand Down Expand Up @@ -847,6 +868,28 @@ func testAccGrantConfigComplexRoleGrants(user string) string {
}`, user)
}

func testAccGrantConfigRoleToRole(dbName string, roleName1 string, roleName2 string) string {
return fmt.Sprintf(`
resource "mysql_database" "test" {
name = "%s"
}
resource "mysql_role" "test1" {
name = "%s"
}
resource "mysql_role" "test2" {
name = "%s"
}
resource "mysql_grant" "test" {
role = "${mysql_role.test1.name}"
database = "${mysql_database.test.name}"
roles = ["${mysql_role.test2.name}"]
}
`, dbName, roleName1, roleName2)
}

func prepareProcedure(dbname string, procedureName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ctx := context.Background()
Expand Down

0 comments on commit 51183e9

Please sign in to comment.