Skip to content

Commit

Permalink
Merge pull request petoju#1 from petoju/master
Browse files Browse the repository at this point in the history
Merge upstream master
  • Loading branch information
boltandrke authored Aug 9, 2022
2 parents 4e47831 + 18291df commit 4d45c99
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 122 deletions.
14 changes: 13 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bin/terraform:
testacc: fmtcheck bin/terraform
PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout=60s

acceptance: testversion5.6 testversion5.7 testversion8.0 testpercona5.7 testpercona8.0 testtidb6.1.0
acceptance: testversion5.6 testversion5.7 testversion8.0 testpercona5.7 testpercona8.0 testmariadb10.3 testmariadb10.8 testtidb6.1.0

testversion%:
$(MAKE) MYSQL_VERSION=$* MYSQL_PORT=33$(shell echo "$*" | tr -d '.') testversion
Expand Down Expand Up @@ -59,6 +59,18 @@ testtidb:
MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc
docker rm -f test-tidb$(MYSQL_VERSION)

testmariadb%:
$(MAKE) MYSQL_VERSION=$* MYSQL_PORT=36$(shell echo "$*" | tr -d '.') testmariadb

testmariadb:
-docker run --rm --name test-mariadb$(MYSQL_VERSION) -e MYSQL_ROOT_PASSWORD="$(TEST_PASSWORD)" -d -p $(MYSQL_PORT):3306 mariadb:$(MYSQL_VERSION)
@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)



vet:
@echo "go vet ."
@go vet $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \
Expand Down
12 changes: 8 additions & 4 deletions mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
cleartextPasswords = "cleartext"
nativePasswords = "native"
unknownVarErrCode = 1193
unknownUserErrCode = 1396
)

type MySQLConfiguration struct {
Expand All @@ -32,6 +33,7 @@ type MySQLConfiguration struct {
MaxConnLifetime time.Duration
MaxOpenConns int
ConnectRetryTimeoutSec time.Duration
Version *version.Version
}

var (
Expand Down Expand Up @@ -197,24 +199,26 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}

mysqlConf.Db = db
if err := afterConnect(d, db); err != nil {
if err := afterConnect(mysqlConf, db); err != nil {
return nil, fmt.Errorf("Failed running after connect command: %v", err)
}

return mysqlConf, nil
}

func afterConnect(d *schema.ResourceData, db *sql.DB) error {
func afterConnect(mysqlConf *MySQLConfiguration, db *sql.DB) error {
// Set up env so that we won't create users randomly.
currentVersion, err := serverVersion(db)
if err != nil {
return fmt.Errorf("Failed getting server version: %v", err)
}

mysqlConf.Version = currentVersion

versionMinInclusive, _ := version.NewVersion("5.7.5")
versionMaxExclusive, _ := version.NewVersion("8.0.0")
if currentVersion.GreaterThanOrEqual(versionMinInclusive) &&
currentVersion.LessThan(versionMaxExclusive) {
if mysqlConf.Version.GreaterThanOrEqual(versionMinInclusive) &&
mysqlConf.Version.LessThan(versionMaxExclusive) {
// CONCAT and setting works even if there is no value.
_, err := db.Exec(`SET SESSION sql_mode=CONCAT(@@sql_mode, ',NO_AUTO_CREATE_USER')`)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions mysql/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,20 @@ func testAccPreCheckSkipTiDB(t *testing.T) {
t.Skip("Skip on TiDB")
}
}

func testAccPreCheckSkipMariaDB(t *testing.T) {
testAccPreCheck(t)
db, err := connectToMySQL(testAccProvider.Meta().(*MySQLConfiguration))
if err != nil {
return
}

currentVersionString, err := serverVersionString(db)
if err != nil {
return
}

if strings.Contains(currentVersionString, "MariaDB") {
t.Skip("Skip on MariaDB")
}
}
9 changes: 3 additions & 6 deletions mysql/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func UpdateDatabase(d *schema.ResourceData, meta interface{}) error {
}

func ReadDatabase(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
mysqlConf := meta.(*MySQLConfiguration)
db := mysqlConf.Db

// This is kinda flimsy-feeling, since it depends on the formatting
// of the SHOW CREATE DATABASE output... but this data doesn't seem
Expand Down Expand Up @@ -111,10 +112,6 @@ func ReadDatabase(d *schema.ResourceData, meta interface{}) error {
var empty interface{}

requiredVersion, _ := version.NewVersion("8.0.0")
currentVersion, err := serverVersion(db)
if err != nil {
return err
}

serverVersionString, err := serverVersionString(db)
if err != nil {
Expand All @@ -123,7 +120,7 @@ func ReadDatabase(d *schema.ResourceData, meta interface{}) error {

// MySQL 8 returns more data in a row.
var res error
if !strings.Contains(serverVersionString, "MariaDB") && currentVersion.GreaterThan(requiredVersion) {
if !strings.Contains(serverVersionString, "MariaDB") && mysqlConf.Version.GreaterThan(requiredVersion) {
res = db.QueryRow(stmtSQL, defaultCharset).Scan(&defaultCollation, &empty, &empty, &empty, &empty, &empty, &empty)
} else {
res = db.QueryRow(stmtSQL, defaultCharset).Scan(&defaultCollation, &empty, &empty, &empty, &empty, &empty)
Expand Down
2 changes: 1 addition & 1 deletion mysql/resource_global_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestAccGlobalVar_basic(t *testing.T) {
resourceName := "mysql_global_variable.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipMariaDB(t) },
Providers: testAccProviders,
CheckDestroy: testAccGlobalVarCheckDestroy(varName),
Steps: []resource.TestStep{
Expand Down
24 changes: 11 additions & 13 deletions mysql/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ func resourceGrant() *schema.Resource {
},

"tls_option": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "NONE",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Deprecated: "Please use tls_option in mysql_user.",
Default: "NONE",
},
},
}
Expand Down Expand Up @@ -145,11 +146,8 @@ func userOrRole(user string, host string, role string, hasRoles bool) (string, b
}
}

func supportsRoles(db *sql.DB) (bool, error) {
currentVersion, err := serverVersion(db)
if err != nil {
return false, err
}
func supportsRoles(meta interface{}) (bool, error) {
currentVersion := meta.(*MySQLConfiguration).Version

requiredVersion, _ := version.NewVersion("8.0.0")
hasRoles := currentVersion.GreaterThan(requiredVersion)
Expand All @@ -159,7 +157,7 @@ func supportsRoles(db *sql.DB) (bool, error) {
func CreateGrant(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db

hasRoles, err := supportsRoles(db)
hasRoles, err := supportsRoles(meta)
if err != nil {
return err
}
Expand Down Expand Up @@ -262,7 +260,7 @@ func CreateGrant(d *schema.ResourceData, meta interface{}) error {
func ReadGrant(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db

hasRoles, err := supportsRoles(db)
hasRoles, err := supportsRoles(meta)
if err != nil {
return err
}
Expand Down Expand Up @@ -322,7 +320,7 @@ func ReadGrant(d *schema.ResourceData, meta interface{}) error {
func UpdateGrant(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db

hasRoles, err := supportsRoles(db)
hasRoles, err := supportsRoles(meta)

if err != nil {
return err
Expand Down Expand Up @@ -401,7 +399,7 @@ func DeleteGrant(d *schema.ResourceData, meta interface{}) error {

table := formatTableName(d.Get("table").(string))

hasRoles, err := supportsRoles(db)
hasRoles, err := supportsRoles(meta)
if err != nil {
return err
}
Expand Down
31 changes: 1 addition & 30 deletions mysql/resource_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@ func TestAccGrant(t *testing.T) {
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
resource.TestCheckResourceAttr("mysql_grant.test", "database", dbName),
resource.TestCheckResourceAttr("mysql_grant.test", "table", "*"),
resource.TestCheckResourceAttr("mysql_grant.test", "tls_option", "NONE"),
),
},
{
Config: testAccGrantConfig_ssl(dbName),
Config: testAccGrantConfig_basic(dbName),
Check: resource.ComposeTestCheckFunc(
testAccPrivilege("mysql_grant.test", "SELECT", true),
resource.TestCheckResourceAttr("mysql_grant.test", "user", fmt.Sprintf("jdoe-%s", dbName)),
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
resource.TestCheckResourceAttr("mysql_grant.test", "database", dbName),
resource.TestCheckResourceAttr("mysql_grant.test", "tls_option", "SSL"),
),
},
},
Expand Down Expand Up @@ -162,7 +160,6 @@ func TestAccGrantComplex(t *testing.T) {
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
resource.TestCheckResourceAttr("mysql_grant.test", "database", dbName),
resource.TestCheckResourceAttr("mysql_grant.test", "table", "tbl"),
resource.TestCheckResourceAttr("mysql_grant.test", "tls_option", "NONE"),
),
},
{
Expand All @@ -176,7 +173,6 @@ func TestAccGrantComplex(t *testing.T) {
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
resource.TestCheckResourceAttr("mysql_grant.test", "database", dbName),
resource.TestCheckResourceAttr("mysql_grant.test", "table", "tbl"),
resource.TestCheckResourceAttr("mysql_grant.test", "tls_option", "NONE"),
),
},
{
Expand All @@ -187,7 +183,6 @@ func TestAccGrantComplex(t *testing.T) {
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
resource.TestCheckResourceAttr("mysql_grant.test", "database", dbName),
resource.TestCheckResourceAttr("mysql_grant.test", "table", "tbl"),
resource.TestCheckResourceAttr("mysql_grant.test", "tls_option", "NONE"),
),
},
{
Expand All @@ -198,7 +193,6 @@ func TestAccGrantComplex(t *testing.T) {
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
resource.TestCheckResourceAttr("mysql_grant.test", "database", dbName),
resource.TestCheckResourceAttr("mysql_grant.test", "table", "tbl"),
resource.TestCheckResourceAttr("mysql_grant.test", "tls_option", "NONE"),
),
},
{
Expand All @@ -209,7 +203,6 @@ func TestAccGrantComplex(t *testing.T) {
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
resource.TestCheckResourceAttr("mysql_grant.test", "database", dbName),
resource.TestCheckResourceAttr("mysql_grant.test", "table", "tbl"),
resource.TestCheckResourceAttr("mysql_grant.test", "tls_option", "NONE"),
),
},
{
Expand All @@ -224,7 +217,6 @@ func TestAccGrantComplex(t *testing.T) {
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
resource.TestCheckResourceAttr("mysql_grant.test", "database", dbName),
resource.TestCheckResourceAttr("mysql_grant.test", "table", "tbl"),
resource.TestCheckResourceAttr("mysql_grant.test", "tls_option", "NONE"),
),
},
},
Expand Down Expand Up @@ -598,27 +590,6 @@ resource "mysql_grant" "test2" {
}
`, dbName, dbName)
}
func testAccGrantConfig_ssl(dbName string) string {
return fmt.Sprintf(`
resource "mysql_database" "test" {
name = "%s"
}
resource "mysql_user" "test" {
user = "jdoe-%s"
host = "example.com"
}
resource "mysql_grant" "test" {
user = "${mysql_user.test.user}"
host = "${mysql_user.test.host}"
database = "${mysql_database.test.name}"
privileges = ["UPDATE", "SELECT"]
tls_option = "SSL"
}
`, dbName, dbName)
}

func testAccGrantConfig_role(dbName string, roleName string) string {
return fmt.Sprintf(`
resource "mysql_database" "test" {
Expand Down
Loading

0 comments on commit 4d45c99

Please sign in to comment.