Skip to content

Commit

Permalink
Fix grant import (petoju#126)
Browse files Browse the repository at this point in the history
Recent refactoring caused some issues after setting database/table was
removed.

Adding it there fixes the issues
  • Loading branch information
petoju authored Mar 16, 2024
1 parent d35fcf2 commit 2cdc94d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
6 changes: 3 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ testversion:
@while ! mysql -h 127.0.0.1 -P $(MYSQL_PORT) -u "$(TEST_USER)" -p"$(TEST_PASSWORD)" -e 'SELECT 1' >/dev/null 2>&1; do printf '.'; sleep 1; done ; echo ; echo "Connected!"
-mysql -h 127.0.0.1 -P $(MYSQL_PORT) -u "$(TEST_USER)" -p"$(TEST_PASSWORD)" -e "INSTALL PLUGIN mysql_no_login SONAME 'mysql_no_login.so';"
MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="$(TEST_PASSWORD)" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc
docker rm -f test-mysql$(MYSQL_VERSION)
-docker rm -f test-mysql$(MYSQL_VERSION)

testpercona%:
$(MAKE) MYSQL_VERSION=$* MYSQL_PORT=34$(shell echo "$*" | tr -d '.') testpercona
Expand All @@ -44,7 +44,7 @@ testpercona:
@while ! mysql -h 127.0.0.1 -P $(MYSQL_PORT) -u "$(TEST_USER)" -p"$(TEST_PASSWORD)" -e 'SELECT 1' >/dev/null 2>&1; do printf '.'; sleep 1; done ; echo ; echo "Connected!"
-mysql -h 127.0.0.1 -P $(MYSQL_PORT) -u "$(TEST_USER)" -p"$(TEST_PASSWORD)" -e "INSTALL PLUGIN mysql_no_login SONAME 'mysql_no_login.so';"
MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="$(TEST_PASSWORD)" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc
docker rm -f test-percona$(MYSQL_VERSION)
-docker rm -f test-percona$(MYSQL_VERSION)

testrdsdb%:
$(MAKE) MYSQL_VERSION=$* MYSQL_USERNAME=${MYSQL_USERNAME} MYSQL_HOST=$(shell echo ${MYSQL_ENDPOINT} | cut -d: -f1) MYSQL_PASSWORD=${MYSQL_PASSWORD} MYSQL_PORT=$(shell echo ${MYSQL_ENDPOINT} | cut -d: -f2) testrdsdb
Expand Down Expand Up @@ -72,7 +72,7 @@ testmariadb:
@echo 'Waiting for MySQL...'
@while ! mysql -h 127.0.0.1 -P $(MYSQL_PORT) -u "$(TEST_USER)" -p"$(TEST_PASSWORD)" -e 'SELECT 1' >/dev/null 2>&1; do printf '.'; sleep 1; done ; echo ; echo "Connected!"
MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="$(TEST_PASSWORD)" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc
docker rm -f test-mariadb$(MYSQL_VERSION)
-docker rm -f test-mariadb$(MYSQL_VERSION)

vet:
@echo "go vet ."
Expand Down
7 changes: 7 additions & 0 deletions mysql/resource_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ func TestAccDatabase(t *testing.T) {
"mysql_database.test", dbName,
),
},
{
Config: testAccDatabaseConfig_basic(dbName),
ResourceName: "mysql_database.test",
ImportState: true,
ImportStateVerify: true,
ImportStateId: dbName,
},
},
})
}
Expand Down
13 changes: 10 additions & 3 deletions mysql/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,7 @@ func ImportGrant(ctx context.Context, d *schema.ResourceData, meta interface{})
}
}

// No match found
results := []*schema.ResourceData{}
return results, nil
return nil, fmt.Errorf("Failed to find the grant to import: %v -- found %v", userHostDatabaseTable, grants)
}

// setDataFromGrant copies the values from MySQLGrant to the schema.ResourceData
Expand Down Expand Up @@ -749,6 +747,12 @@ func setDataFromGrant(grant MySQLGrant, d *schema.ResourceData) *schema.Resource
}
}

// We need to use the raw pointer to access Table / Database without wrapping them with backticks.
if tablePrivGrant, isTablePriv := grant.(*TablePrivilegeGrant); isTablePriv {
d.Set("table", tablePrivGrant.Table)
d.Set("database", tablePrivGrant.Database)
}

// This is a bit of a hack, since we don't have a way to distingush between users and roles
// from the grant itself. We can only infer it from the schema.
userOrRole := grant.GetUserOrRole()
Expand All @@ -759,6 +763,9 @@ func setDataFromGrant(grant MySQLGrant, d *schema.ResourceData) *schema.Resource
d.Set("host", userOrRole.Host)
}

// This needs to happen for import to work.
d.SetId(grant.GetId())

return d
}

Expand Down
23 changes: 21 additions & 2 deletions mysql/resource_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

func TestAccGrant(t *testing.T) {
dbName := fmt.Sprintf("tf-test-%d", rand.Intn(100))
userName := fmt.Sprintf("jdoe-%s", dbName)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipRds(t) },
Providers: testAccProviders,
Expand All @@ -25,7 +26,7 @@ func TestAccGrant(t *testing.T) {
Config: testAccGrantConfigBasic(dbName),
Check: resource.ComposeTestCheckFunc(
testAccPrivilege("mysql_grant.test", "SELECT", true, false),
resource.TestCheckResourceAttr("mysql_grant.test", "user", fmt.Sprintf("jdoe-%s", dbName)),
resource.TestCheckResourceAttr("mysql_grant.test", "user", userName),
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
resource.TestCheckResourceAttr("mysql_grant.test", "database", dbName),
resource.TestCheckResourceAttr("mysql_grant.test", "table", "*"),
Expand All @@ -35,11 +36,18 @@ func TestAccGrant(t *testing.T) {
Config: testAccGrantConfigBasic(dbName),
Check: resource.ComposeTestCheckFunc(
testAccPrivilege("mysql_grant.test", "SELECT", true, false),
resource.TestCheckResourceAttr("mysql_grant.test", "user", fmt.Sprintf("jdoe-%s", dbName)),
resource.TestCheckResourceAttr("mysql_grant.test", "user", userName),
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
resource.TestCheckResourceAttr("mysql_grant.test", "database", dbName),
),
},
{
Config: testAccGrantConfigBasic(dbName),
ResourceName: "mysql_grant.test",
ImportState: true,
ImportStateVerify: true,
ImportStateId: fmt.Sprintf("%v@%v@%v@%v", userName, "example.com", dbName, "*"),
},
},
})
}
Expand Down Expand Up @@ -266,6 +274,17 @@ func TestAccGrantComplex(t *testing.T) {
resource.TestCheckResourceAttr("mysql_grant.test", "table", "tbl"),
),
},
// Test import with grant option
{
Config: testAccGrantConfigBasic(dbName),
ResourceName: "mysql_grant.test",
ImportState: true,
ImportStateVerify: true,
// TF (incorrectly) compares items directly without any kind of suppress function.
// So ALL should be "ALL PRIVILEGES". To avoid the issues, we'll ignore that here.
ImportStateVerifyIgnore: []string{"privileges.0"},
ImportStateId: fmt.Sprintf("%v@%v@%v@%v@", fmt.Sprintf("jdoe-%s", dbName), "example.com", dbName, "tbl"),
},
// Finally, revoke all privileges
{
Config: testAccGrantConfigNoGrant(dbName),
Expand Down

0 comments on commit 2cdc94d

Please sign in to comment.