Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore host when comparing role grants #190

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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