From 841718b4361d9fb57c8af414514d6b97753d5e2f Mon Sep 17 00:00:00 2001 From: petoju Date: Mon, 6 May 2024 19:09:37 +0200 Subject: [PATCH] Refactoring: remove deprecated function calls, use our functions (#146) * Refactoring: remove deprecated function calls, use our functions This should change no functionality at all * Add forgotten file --- mysql/data_source_tables.go | 4 +- mysql/data_source_tables_test.go | 22 ++++---- mysql/provider.go | 60 +++++++--------------- mysql/provider_test.go | 10 ++-- mysql/resource_database.go | 9 ++-- mysql/resource_database_test.go | 46 ++++++++--------- mysql/resource_default_roles_test.go | 4 +- mysql/resource_global_variable.go | 16 +++--- mysql/resource_global_variable_test.go | 34 ++++++------- mysql/resource_grant.go | 16 ++---- mysql/resource_grant_test.go | 62 +++++++++++------------ mysql/resource_rds_config.go | 12 ++--- mysql/resource_rds_config_test.go | 20 ++++---- mysql/resource_role_test.go | 12 ++--- mysql/resource_ti_config_variable.go | 5 +- mysql/resource_ti_config_variable_test.go | 35 ++++++------- mysql/resource_user.go | 3 +- mysql/resource_user_password_test.go | 6 +-- mysql/resource_user_test.go | 26 +++++----- mysql/utils.go | 30 +++++++++++ 20 files changed, 214 insertions(+), 218 deletions(-) diff --git a/mysql/data_source_tables.go b/mysql/data_source_tables.go index 7966cc89c..9fe65bc80 100644 --- a/mysql/data_source_tables.go +++ b/mysql/data_source_tables.go @@ -6,7 +6,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "log" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -69,7 +69,7 @@ func ShowTables(ctx context.Context, d *schema.ResourceData, meta interface{}) d return diag.Errorf("failed setting tables field: %v", err) } - d.SetId(resource.UniqueId()) + d.SetId(id.UniqueId()) return nil } diff --git a/mysql/data_source_tables_test.go b/mysql/data_source_tables_test.go index 55dd7eea0..57f99c885 100644 --- a/mysql/data_source_tables_test.go +++ b/mysql/data_source_tables_test.go @@ -11,16 +11,16 @@ import ( func TestAccDataSourceTables(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, Steps: []resource.TestStep{ { - Config: testAccTablesConfig_basic("mysql", "%"), + Config: testAccTablesConfigBasic("mysql", "%"), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("data.mysql_tables.test", "database", "mysql"), resource.TestCheckResourceAttr("data.mysql_tables.test", "pattern", "%"), - testAccTablesCount("data.mysql_tables.test", "tables.#", func(rn string, table_count int) error { - if table_count < 1 { + testAccTablesCount("data.mysql_tables.test", "tables.#", func(rn string, tableCount int) error { + if tableCount < 1 { return fmt.Errorf("%s: tables not found", rn) } @@ -29,12 +29,12 @@ func TestAccDataSourceTables(t *testing.T) { ), }, { - Config: testAccTablesConfig_basic("mysql", "__table_does_not_exist__"), + Config: testAccTablesConfigBasic("mysql", "__table_does_not_exist__"), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("data.mysql_tables.test", "database", "mysql"), resource.TestCheckResourceAttr("data.mysql_tables.test", "pattern", "__table_does_not_exist__"), - testAccTablesCount("data.mysql_tables.test", "tables.#", func(rn string, table_count int) error { - if table_count > 0 { + testAccTablesCount("data.mysql_tables.test", "tables.#", func(rn string, tableCount int) error { + if tableCount > 0 { return fmt.Errorf("%s: unexpected table found", rn) } @@ -60,17 +60,17 @@ func testAccTablesCount(rn string, key string, check func(string, int) error) re return fmt.Errorf("%s: attribute '%s' not found", rn, key) } - table_count, err := strconv.Atoi(value) + tableCount, err := strconv.Atoi(value) if err != nil { return err } - return check(rn, table_count) + return check(rn, tableCount) } } -func testAccTablesConfig_basic(database string, pattern string) string { +func testAccTablesConfigBasic(database string, pattern string) string { return fmt.Sprintf(` data "mysql_tables" "test" { database = "%s" diff --git a/mysql/provider.go b/mysql/provider.go index 47930f58a..9507910f0 100644 --- a/mysql/provider.go +++ b/mysql/provider.go @@ -6,8 +6,8 @@ import ( "crypto/x509" "database/sql" "encoding/json" - "errors" "fmt" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "log" "net" "net/url" @@ -19,10 +19,7 @@ import ( "github.com/go-sql-driver/mysql" "github.com/hashicorp/go-version" - "google.golang.org/api/googleapi" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -83,7 +80,7 @@ func Provider() *schema.Provider { ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { value := v.(string) if value == "" { - errors = append(errors, fmt.Errorf("Endpoint must not be an empty string")) + errors = append(errors, fmt.Errorf("endpoint must not be an empty string")) } return @@ -219,8 +216,8 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{} var allowClearTextPasswords = authPlugin == cleartextPasswords var allowNativePasswords = authPlugin == nativePasswords var password = d.Get("password").(string) - var iam_auth = d.Get("iam_database_authentication").(bool) - var private_ip = d.Get("private_ip").(bool) + var iamAuth = d.Get("iam_database_authentication").(bool) + var privateIp = d.Get("private_ip").(bool) var tlsConfig = d.Get("tls").(string) var tlsConfigStruct *tls.Config @@ -230,12 +227,12 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{} customMap := customTLSMap[0].(map[string]interface{}) customTLSJson, err := json.Marshal(customMap) if err != nil { - return nil, diag.Errorf("failed to marshal tls config: %v", customTLSMap) + return nil, diag.Errorf("failed to marshal tls config %v with error %v", customTLSMap, err) } err = json.Unmarshal(customTLSJson, &customTLS) if err != nil { - return nil, diag.Errorf("failed to unmarshal tls config: %v", customTLSJson) + return nil, diag.Errorf("failed to unmarshal tls config %v with error %v", customTLSJson, err) } var pem []byte @@ -269,7 +266,10 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{} RootCAs: rootCertPool, Certificates: clientCert, } - mysql.RegisterTLSConfig(customTLS.ConfigKey, tlsConfigStruct) + err = mysql.RegisterTLSConfig(customTLS.ConfigKey, tlsConfigStruct) + if err != nil { + return nil, diag.Errorf("failed registering TLS config: %v", err) + } tlsConfig = customTLS.ConfigKey } @@ -280,7 +280,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{} proto = "cloudsql" endpoint = strings.ReplaceAll(endpoint, "cloudsql://", "") var err error - if iam_auth { // Access token will be in the password field + if iamAuth { // Access token will be in the password field var opts []cloudsqlconn.Option @@ -292,7 +292,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{} _, err = cloudsql.RegisterDriver("cloudsql", opts...) } else { var endpointParams []cloudsqlconn.DialOption - if private_ip { + if privateIp { endpointParams = append(endpointParams, cloudsqlconn.WithPrivateIP()) } @@ -508,22 +508,22 @@ func createNewConnection(ctx context.Context, conf *MySQLConfiguration) (*OneCon // when Terraform thinks it's available and when it is actually available. // This is particularly acute when provisioning a server and then immediately // trying to provision a database on it. - retryError := resource.RetryContext(ctx, conf.ConnectRetryTimeoutSec, func() *resource.RetryError { + retryError := retry.RetryContext(ctx, conf.ConnectRetryTimeoutSec, func() *retry.RetryError { db, err = sql.Open(driverName, conf.Config.FormatDSN()) if err != nil { if mysqlErrorNumber(err) != 0 || cloudsqlErrorNumber(err) != 0 || ctx.Err() != nil { - return resource.NonRetryableError(err) + return retry.NonRetryableError(err) } - return resource.RetryableError(err) + return retry.RetryableError(err) } err = db.PingContext(ctx) if err != nil { if mysqlErrorNumber(err) != 0 || cloudsqlErrorNumber(err) != 0 || ctx.Err() != nil { - return resource.NonRetryableError(err) + return retry.NonRetryableError(err) } - return resource.RetryableError(err) + return retry.RetryableError(err) } return nil @@ -549,29 +549,3 @@ func createNewConnection(ctx context.Context, conf *MySQLConfiguration) (*OneCon Version: currentVersion, }, nil } - -// 0 == not mysql error or not error at all. -func mysqlErrorNumber(err error) uint16 { - if err == nil { - return 0 - } - me, ok := err.(*mysql.MySQLError) - if !ok { - return 0 - } - return me.Number -} - -func cloudsqlErrorNumber(err error) int { - if err == nil { - return 0 - } - - var gapiError *googleapi.Error - if errors.As(err, &gapiError) { - if gapiError.Code >= 400 && gapiError.Code < 500 { - return gapiError.Code - } - } - return 0 -} diff --git a/mysql/provider_test.go b/mysql/provider_test.go index e0e8fbdd1..9b1899289 100644 --- a/mysql/provider_test.go +++ b/mysql/provider_test.go @@ -30,13 +30,15 @@ import ( // You can run the tests like this: // make testacc TEST=./builtin/providers/mysql -var testAccProviders map[string]*schema.Provider +var testAccProviderFactories map[string]func() (*schema.Provider, error) + +// var testAccProviders map[string]*schema.Provider var testAccProvider *schema.Provider func init() { testAccProvider = Provider() - testAccProviders = map[string]*schema.Provider{ - "mysql": testAccProvider, + testAccProviderFactories = map[string]func() (*schema.Provider, error){ + "mysql": func() (*schema.Provider, error) { return testAccProvider, nil }, } } @@ -47,7 +49,7 @@ func TestProvider(t *testing.T) { } func TestProvider_impl(t *testing.T) { - var _ *schema.Provider = Provider() + var _ = Provider() } func testAccPreCheck(t *testing.T) { diff --git a/mysql/resource_database.go b/mysql/resource_database.go index 00c7c4f04..12a286e33 100644 --- a/mysql/resource_database.go +++ b/mysql/resource_database.go @@ -8,7 +8,6 @@ import ( "log" "strings" - "github.com/go-sql-driver/mysql" "github.com/hashicorp/go-version" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -102,11 +101,9 @@ func ReadDatabase(ctx context.Context, d *schema.ResourceData, meta interface{}) var createSQL, _database string err = db.QueryRowContext(ctx, stmtSQL).Scan(&_database, &createSQL) if err != nil { - if mysqlErr, ok := err.(*mysql.MySQLError); ok { - if mysqlErr.Number == unknownDatabaseErrCode { - d.SetId("") - return nil - } + if mysqlErrorNumber(err) == unknownDatabaseErrCode { + d.SetId("") + return nil } return diag.Errorf("Error during show create database: %s", err) } diff --git a/mysql/resource_database_test.go b/mysql/resource_database_test.go index e237cd53f..9fc7c54e4 100644 --- a/mysql/resource_database_test.go +++ b/mysql/resource_database_test.go @@ -6,8 +6,6 @@ import ( "strings" "testing" - "github.com/go-sql-driver/mysql" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) @@ -15,18 +13,18 @@ import ( func TestAccDatabase(t *testing.T) { dbName := "terraform_acceptance_test" resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheckSkipTiDB(t) }, - Providers: testAccProviders, - CheckDestroy: testAccDatabaseCheckDestroy(dbName), + PreCheck: func() { testAccPreCheckSkipTiDB(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccDatabaseCheckDestroy(dbName), Steps: []resource.TestStep{ { - Config: testAccDatabaseConfig_basic(dbName), - Check: testAccDatabaseCheck_basic( + Config: testAccDatabaseConfigBasic(dbName), + Check: testAccDatabaseCheckBasic( "mysql_database.test", dbName, ), }, { - Config: testAccDatabaseConfig_basic(dbName), + Config: testAccDatabaseConfigBasic(dbName), ResourceName: "mysql_database.test", ImportState: true, ImportStateVerify: true, @@ -48,14 +46,14 @@ func TestAccDatabase_collationChange(t *testing.T) { ctx := context.Background() resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheckSkipTiDB(t) }, - Providers: testAccProviders, - CheckDestroy: testAccDatabaseCheckDestroy(dbName), + PreCheck: func() { testAccPreCheckSkipTiDB(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccDatabaseCheckDestroy(dbName), Steps: []resource.TestStep{ { - Config: testAccDatabaseConfig_full(dbName, charset1, collation1), + Config: testAccDatabaseConfigFull(dbName, charset1, collation1), Check: resource.ComposeTestCheckFunc( - testAccDatabaseCheck_full("mysql_database.test", dbName, charset1, collation1), + testAccDatabaseCheckFull("mysql_database.test", dbName, charset1, collation1), ), }, { @@ -72,20 +70,20 @@ func TestAccDatabase_collationChange(t *testing.T) { db.Exec(fmt.Sprintf("ALTER DATABASE %s CHARACTER SET %s COLLATE %s", dbName, charset2, collation2)) }, - Config: testAccDatabaseConfig_full(dbName, charset1, collation1), + Config: testAccDatabaseConfigFull(dbName, charset1, collation1), Check: resource.ComposeTestCheckFunc( - testAccDatabaseCheck_full(resourceName, dbName, charset1, collation1), + testAccDatabaseCheckFull(resourceName, dbName, charset1, collation1), ), }, }, }) } -func testAccDatabaseCheck_basic(rn string, name string) resource.TestCheckFunc { - return testAccDatabaseCheck_full(rn, name, "utf8mb4", "utf8mb4_bin") +func testAccDatabaseCheckBasic(rn string, name string) resource.TestCheckFunc { + return testAccDatabaseCheckFull(rn, name, "utf8mb4", "utf8mb4_bin") } -func testAccDatabaseCheck_full(rn string, name string, charset string, collation string) resource.TestCheckFunc { +func testAccDatabaseCheckFull(rn string, name string, charset string, collation string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[rn] if !ok { @@ -133,21 +131,19 @@ func testAccDatabaseCheckDestroy(name string) resource.TestCheckFunc { return fmt.Errorf("database still exists after destroy") } - if mysqlErr, ok := err.(*mysql.MySQLError); ok { - if mysqlErr.Number == unknownDatabaseErrCode { - return nil - } + if mysqlErrorNumber(err) == unknownDatabaseErrCode { + return nil } return fmt.Errorf("got unexpected error: %s", err) } } -func testAccDatabaseConfig_basic(name string) string { - return testAccDatabaseConfig_full(name, "utf8mb4", "utf8mb4_bin") +func testAccDatabaseConfigBasic(name string) string { + return testAccDatabaseConfigFull(name, "utf8mb4", "utf8mb4_bin") } -func testAccDatabaseConfig_full(name string, charset string, collation string) string { +func testAccDatabaseConfigFull(name string, charset string, collation string) string { return fmt.Sprintf(` resource "mysql_database" "test" { name = "%s" diff --git a/mysql/resource_default_roles_test.go b/mysql/resource_default_roles_test.go index 88efd4eaa..1f4ec7cc4 100644 --- a/mysql/resource_default_roles_test.go +++ b/mysql/resource_default_roles_test.go @@ -17,8 +17,8 @@ func TestAccDefaultRoles_basic(t *testing.T) { testAccPreCheckSkipNotMySQL8(t) testAccPreCheckSkipMariaDB(t) }, - Providers: testAccProviders, - CheckDestroy: testAccDefaultRolesCheckDestroy, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccDefaultRolesCheckDestroy, Steps: []resource.TestStep{ { Config: testAccDefaultRoles_basic, diff --git a/mysql/resource_global_variable.go b/mysql/resource_global_variable.go index 650aee568..428fa78f5 100644 --- a/mysql/resource_global_variable.go +++ b/mysql/resource_global_variable.go @@ -44,7 +44,7 @@ func resourceGlobalVariable() *schema.Resource { } func CreateOrUpdateGlobalVariable(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var sql string + var sqlCommand string db, err := getDatabaseFromMeta(ctx, meta) if err != nil { @@ -57,14 +57,14 @@ func CreateOrUpdateGlobalVariable(ctx context.Context, d *schema.ResourceData, m // Detect number or string if _, err := strconv.ParseFloat(value, 64); err == nil { - sql = fmt.Sprintf("%s%s", sqlBaseQuery, value) + sqlCommand = fmt.Sprintf("%s%s", sqlBaseQuery, value) } else { - sql = fmt.Sprintf("%s'%s'", sqlBaseQuery, value) + sqlCommand = fmt.Sprintf("%s'%s'", sqlBaseQuery, value) } - log.Printf("[DEBUG] SQL: %s", sql) + log.Printf("[DEBUG] SQL: %s", sqlCommand) - _, err = db.ExecContext(ctx, sql) + _, err = db.ExecContext(ctx, sqlCommand) if err != nil { return diag.Errorf("error setting value: %s", err) } @@ -106,10 +106,10 @@ func DeleteGlobalVariable(ctx context.Context, d *schema.ResourceData, meta inte } name := d.Get("name").(string) - sql := fmt.Sprintf("SET GLOBAL %s = DEFAULT", quoteIdentifier(name)) - log.Printf("[DEBUG] SQL: %s", sql) + sqlCommand := fmt.Sprintf("SET GLOBAL %s = DEFAULT", quoteIdentifier(name)) + log.Printf("[DEBUG] SQL: %s", sqlCommand) - _, err = db.ExecContext(ctx, sql) + _, err = db.ExecContext(ctx, sqlCommand) if err != nil { log.Printf("[WARN] Variable_name (%s) not found; removing from state", d.Id()) d.SetId("") diff --git a/mysql/resource_global_variable_test.go b/mysql/resource_global_variable_test.go index 76450c7ce..ea129f3e9 100644 --- a/mysql/resource_global_variable_test.go +++ b/mysql/resource_global_variable_test.go @@ -17,12 +17,12 @@ func TestAccGlobalVar_basic(t *testing.T) { varValue := "1" resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipMariaDB(t); testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGlobalVarCheckDestroy(varName, varValue), + PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipMariaDB(t); testAccPreCheckSkipRds(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGlobalVarCheckDestroy(varName, varValue), Steps: []resource.TestStep{ { - Config: testAccGlobalVarConfig_basic(varName, varValue), + Config: testAccGlobalVarConfigBasic(varName, varValue), Check: resource.ComposeTestCheckFunc( testAccGlobalVarExists(varName, varValue), resource.TestCheckResourceAttr(resourceName, "name", varName), @@ -44,22 +44,22 @@ func TestAccGlobalVar_parseString(t *testing.T) { testAccPreCheckSkipNotTiDB(t) testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGlobalVarCheckDestroy(varName, varValue), + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGlobalVarCheckDestroy(varName, varValue), Steps: []resource.TestStep{ { - Config: testAccGlobalVarConfig_basic(varName, "varValue'varValue"), + Config: testAccGlobalVarConfigBasic(varName, "varValue'varValue"), ExpectError: regexp.MustCompile(".*is badly formatted.*"), }, { - Config: testAccGlobalVarConfig_basic("tidb_auto_analyze_ratio", "0.4"), + Config: testAccGlobalVarConfigBasic("tidb_auto_analyze_ratio", "0.4"), Check: resource.ComposeTestCheckFunc( testAccGlobalVarExists("tidb_auto_analyze_ratio", "0.4"), resource.TestCheckResourceAttr(resourceName, "name", "tidb_auto_analyze_ratio"), ), }, { - Config: testAccGlobalVarConfig_basic(varName, varValue), + Config: testAccGlobalVarConfigBasic(varName, varValue), Check: resource.ComposeTestCheckFunc( testAccGlobalVarExists(varName, varValue), resource.TestCheckResourceAttr(resourceName, "name", varName), @@ -81,11 +81,11 @@ func TestAccGlobalVar_parseFloat(t *testing.T) { testAccPreCheckSkipNotTiDB(t) testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGlobalVarCheckDestroy(varName, varValue), + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGlobalVarCheckDestroy(varName, varValue), Steps: []resource.TestStep{ { - Config: testAccGlobalVarConfig_basic(varName, varValue), + Config: testAccGlobalVarConfigBasic(varName, varValue), Check: resource.ComposeTestCheckFunc( testAccGlobalVarExists(varName, varValue), resource.TestCheckResourceAttr(resourceName, "name", varName), @@ -107,11 +107,11 @@ func TestAccGlobalVar_parseBoolean(t *testing.T) { testAccPreCheckSkipNotTiDB(t) testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGlobalVarCheckDestroy(varName, varValue), + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGlobalVarCheckDestroy(varName, varValue), Steps: []resource.TestStep{ { - Config: testAccGlobalVarConfig_basic(varName, varValue), + Config: testAccGlobalVarConfigBasic(varName, varValue), Check: resource.ComposeTestCheckFunc( testAccGlobalVarExists(varName, varValue), resource.TestCheckResourceAttr(resourceName, "name", varName), @@ -170,14 +170,14 @@ func testAccGlobalVarCheckDestroy(varName, varExpected string) resource.TestChec res, _ := testAccGetGlobalVar(varName, db) if res == varExpected { - return fmt.Errorf("Global variable '%s' still has non default value", varName) + return fmt.Errorf("global variable '%s' still has non default value", varName) } return nil } } -func testAccGlobalVarConfig_basic(varName, varValue string) string { +func testAccGlobalVarConfigBasic(varName, varValue string) string { return fmt.Sprintf(` resource "mysql_global_variable" "test" { name = "%s" diff --git a/mysql/resource_grant.go b/mysql/resource_grant.go index 69635a791..6e4f42544 100644 --- a/mysql/resource_grant.go +++ b/mysql/resource_grant.go @@ -14,7 +14,6 @@ import ( "github.com/hashicorp/go-version" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/go-sql-driver/mysql" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -658,16 +657,11 @@ func DeleteGrant(ctx context.Context, d *schema.ResourceData, meta interface{}) } func isNonExistingGrant(err error) bool { - if driverErr, ok := err.(*mysql.MySQLError); ok { - // 1141 = ER_NONEXISTING_GRANT - // 1147 = ER_NONEXISTING_TABLE_GRANT - // 1403 = ER_NONEXISTING_PROC_GRANT - - if driverErr.Number == 1141 || driverErr.Number == 1147 || driverErr.Number == 1403 { - return true - } - } - return false + errorNumber := mysqlErrorNumber(err) + // 1141 = ER_NONEXISTING_GRANT + // 1147 = ER_NONEXISTING_TABLE_GRANT + // 1403 = ER_NONEXISTING_PROC_GRANT + return errorNumber == 1141 || errorNumber == 1147 || errorNumber == 1403 } func ImportGrant(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { diff --git a/mysql/resource_grant_test.go b/mysql/resource_grant_test.go index 2b91920c6..015e8efea 100644 --- a/mysql/resource_grant_test.go +++ b/mysql/resource_grant_test.go @@ -18,9 +18,9 @@ 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, - CheckDestroy: testAccGrantCheckDestroy, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipRds(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { Config: testAccGrantConfigBasic(dbName), @@ -56,9 +56,9 @@ func TestAccRevokePrivRefresh(t *testing.T) { dbName := fmt.Sprintf("tf-test-%d", rand.Intn(100)) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGrantCheckDestroy, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipRds(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { Config: testAccGrantConfigBasic(dbName), @@ -101,9 +101,9 @@ func TestAccRevokePrivRefresh(t *testing.T) { func TestAccBroken(t *testing.T) { dbName := fmt.Sprintf("tf-test-%d", rand.Intn(100)) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGrantCheckDestroy, + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { Config: testAccGrantConfigBasic(dbName), @@ -137,8 +137,8 @@ func TestAccDifferentHosts(t *testing.T) { testAccPreCheck(t) testAccPreCheckSkipTiDB(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGrantCheckDestroy, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { Config: testAccGrantConfigExtraHost(dbName, false), @@ -168,9 +168,9 @@ func TestAccDifferentHosts(t *testing.T) { func TestAccGrantComplex(t *testing.T) { dbName := fmt.Sprintf("tf-test-%d", rand.Intn(100)) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheckSkipTiDB(t); testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGrantCheckDestroy, + PreCheck: func() { testAccPreCheckSkipTiDB(t); testAccPreCheckSkipRds(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { // Create table first @@ -302,8 +302,8 @@ func TestAccGrantComplexMySQL8(t *testing.T) { testAccPreCheckSkipMariaDB(t) testAccPreCheckSkipNotMySQLVersionMin(t, "8.0.0") }, - Providers: testAccProviders, - CheckDestroy: testAccGrantCheckDestroy, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { // Create table first @@ -333,8 +333,8 @@ func TestAccGrant_role(t *testing.T) { testAccPreCheckSkipRds(t) testAccPreCheckSkipNotMySQLVersionMin(t, "8.0.0") }, - Providers: testAccProviders, - CheckDestroy: testAccGrantCheckDestroy, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { Config: testAccGrantConfigRole(dbName, roleName), @@ -368,8 +368,8 @@ func TestAccGrant_roleToUser(t *testing.T) { testAccPreCheckSkipRds(t) testAccPreCheckSkipNotMySQLVersionMin(t, "8.0.0") }, - Providers: testAccProviders, - CheckDestroy: testAccGrantCheckDestroy, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { Config: testAccGrantConfigRoleToUser(dbName, roleName), @@ -391,8 +391,8 @@ func TestAccGrant_complexRoleGrants(t *testing.T) { testAccPreCheckSkipMariaDB(t) testAccPreCheckSkipNotMySQLVersionMin(t, "8.0.0") }, - Providers: testAccProviders, - CheckDestroy: testAccGrantCheckDestroy, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { Config: testAccGrantConfigComplexRoleGrants(dbName), @@ -897,9 +897,9 @@ func TestAccGrantOnProcedure(t *testing.T) { hostName := "%" resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheckSkipTiDB(t); testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGrantCheckDestroy, + PreCheck: func() { testAccPreCheckSkipTiDB(t); testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { // Create table first @@ -1097,9 +1097,9 @@ func TestAllowDuplicateUsersDifferentTables(t *testing.T) { `, dbName, dbName) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGrantCheckDestroy, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipRds(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { // Create table first @@ -1162,9 +1162,9 @@ func TestDisallowDuplicateUsersSameTable(t *testing.T) { `, dbName, dbName) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGrantCheckDestroy, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipRds(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccGrantCheckDestroy, Steps: []resource.TestStep{ { Config: testAccGrantConfigNoGrant(dbName), diff --git a/mysql/resource_rds_config.go b/mysql/resource_rds_config.go index 15889a1dd..63548d6da 100644 --- a/mysql/resource_rds_config.go +++ b/mysql/resource_rds_config.go @@ -153,16 +153,16 @@ func DeleteRDSConfig(ctx context.Context, d *schema.ResourceData, meta interface func RDSConfigSQL(d *schema.ResourceData) []string { result := []string{} if d.Get("binlog_retention_hours") != nil { - retention_period := strconv.Itoa(d.Get("binlog_retention_hours").(int)) - if retention_period == "0" { - retention_period = "NULL" + retentionPeriod := strconv.Itoa(d.Get("binlog_retention_hours").(int)) + if retentionPeriod == "0" { + retentionPeriod = "NULL" } - result = append(result, (fmt.Sprintf("call mysql.rds_set_configuration('binlog retention hours', %s)", retention_period))) + result = append(result, fmt.Sprintf("call mysql.rds_set_configuration('binlog retention hours', %s)", retentionPeriod)) } if d.Get("replication_target_delay") != nil { - target_delay := d.Get("replication_target_delay") - result = append(result, (fmt.Sprintf("call mysql.rds_set_configuration('target delay', %v)", target_delay))) + targetDelay := d.Get("replication_target_delay") + result = append(result, fmt.Sprintf("call mysql.rds_set_configuration('target delay', %v)", targetDelay)) } return result diff --git a/mysql/resource_rds_config_test.go b/mysql/resource_rds_config_test.go index 4dc2d4134..00a8cd431 100644 --- a/mysql/resource_rds_config_test.go +++ b/mysql/resource_rds_config_test.go @@ -17,11 +17,11 @@ func TestAccResourceRDS(t *testing.T) { binlog := 24 targetDelay := 3200 resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheckSkipNotRds(t) }, - Providers: testAccProviders, + PreCheck: func() { testAccPreCheckSkipNotRds(t) }, + ProviderFactories: testAccProviderFactories, Steps: []resource.TestStep{ { - Config: testAccRDSConfig_basic(rName, binlog, targetDelay), + Config: testAccRDSConfigBasic(rName, binlog, targetDelay), Check: resource.ComposeTestCheckFunc( testAccRDSConfigExists(fmt.Sprintf("mysql_rds_config.%s", rName)), resource.TestCheckResourceAttr(fmt.Sprintf("mysql_rds_config.%s", rName), "binlog_retention_hours", fmt.Sprintf("%d", binlog)), @@ -32,7 +32,7 @@ func TestAccResourceRDS(t *testing.T) { }) } -func testAccRDSConfig_basic(rName string, binlog int, replication int) string { +func testAccRDSConfigBasic(rName string, binlog int, replication int) string { return fmt.Sprintf(` resource "mysql_rds_config" "%s" { binlog_retention_hours = %d @@ -105,12 +105,12 @@ func TestAccResourceRDSConfigChange(t *testing.T) { ctx := context.Background() resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheckSkipNotRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccRDSCheckDestroy(), + PreCheck: func() { testAccPreCheckSkipNotRds(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccRDSCheckDestroy(), Steps: []resource.TestStep{ { - Config: testAccRDSConfig_basic(rName, binlog, targetDelay), + Config: testAccRDSConfigBasic(rName, binlog, targetDelay), Check: resource.ComposeTestCheckFunc( testAccRDSConfigExists(fmt.Sprintf("mysql_rds_config.%s", rName)), ), @@ -136,14 +136,14 @@ func TestAccResourceRDSConfigChange(t *testing.T) { }, Check: resource.ComposeTestCheckFunc( testAccRDSConfigExists(fmt.Sprintf("mysql_rds_config.%s", rName)), - testAccRDSCheck_full(fullResourceName, binlogUpdated, targetDelayUpdated), + testAccRDSCheckFull(fullResourceName, binlogUpdated, targetDelayUpdated), ), }, }, }) } -func testAccRDSCheck_full(rn string, binlogUpdated, targetDelayUpdated int) resource.TestCheckFunc { +func testAccRDSCheckFull(rn string, binlogUpdated, targetDelayUpdated int) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[rn] if !ok { diff --git a/mysql/resource_role_test.go b/mysql/resource_role_test.go index d4b3cf68d..631d16908 100644 --- a/mysql/resource_role_test.go +++ b/mysql/resource_role_test.go @@ -35,11 +35,11 @@ func TestAccRole_basic(t *testing.T) { t.Skip("Roles require MySQL 8+") } }, - Providers: testAccProviders, - CheckDestroy: testAccRoleCheckDestroy(roleName), + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccRoleCheckDestroy(roleName), Steps: []resource.TestStep{ { - Config: testAccRoleConfig_basic(roleName), + Config: testAccRoleConfigBasic(roleName), Check: resource.ComposeTestCheckFunc( testAccRoleExists(roleName), resource.TestCheckResourceAttr(resourceName, "name", roleName), @@ -67,7 +67,7 @@ func testAccRoleExists(roleName string) resource.TestCheckFunc { return nil } - return fmt.Errorf("No grants found for role %s", roleName) + return fmt.Errorf("no grants found for role %s", roleName) } } @@ -97,14 +97,14 @@ func testAccRoleCheckDestroy(roleName string) resource.TestCheckFunc { count, err := testAccGetRoleGrantCount(roleName, db) if count > 0 { - return fmt.Errorf("Role %s still has grants/exists", roleName) + return fmt.Errorf("role %s still has grants/exists", roleName) } return nil } } -func testAccRoleConfig_basic(roleName string) string { +func testAccRoleConfigBasic(roleName string) string { return fmt.Sprintf(` resource "mysql_role" "test" { name = "%s" diff --git a/mysql/resource_ti_config_variable.go b/mysql/resource_ti_config_variable.go index ef0dca305..ec86f98ec 100644 --- a/mysql/resource_ti_config_variable.go +++ b/mysql/resource_ti_config_variable.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "encoding/json" + "errors" "fmt" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "log" @@ -121,13 +122,13 @@ func ReadConfigVariable(ctx context.Context, d *schema.ResourceData, meta interf configQuery := fmt.Sprintf("SHOW CONFIG WHERE type = '%s' AND name = '%s'", splitedResType, splitedResName) if len(indexParts) > 2 { - configQuery = configQuery + fmt.Sprintf(" AND instance = '%s'", (indexParts[2])) + configQuery = configQuery + fmt.Sprintf(" AND instance = '%s'", indexParts[2]) } log.Printf("[DEBUG] SQL: %s\n", configQuery) err = db.QueryRow(configQuery).Scan(&resType, &resInstance, &resName, &resValue) - if err != nil && err != sql.ErrNoRows { + if err != nil && !errors.Is(err, sql.ErrNoRows) { d.SetId("") return diag.Errorf("error during show config variables: %s", err) } diff --git a/mysql/resource_ti_config_variable_test.go b/mysql/resource_ti_config_variable_test.go index 4cd76f84c..204a3805b 100644 --- a/mysql/resource_ti_config_variable_test.go +++ b/mysql/resource_ti_config_variable_test.go @@ -3,6 +3,7 @@ package mysql import ( "context" "database/sql" + "errors" "fmt" "os" "regexp" @@ -25,32 +26,32 @@ func TestPdConfigVar_basic(t *testing.T) { testAccPreCheckSkipNotTiDB(t) testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccConfigVarCheckDestroy(varName, varType), + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccConfigVarCheckDestroy(varName, varType), Steps: []resource.TestStep{ { - Config: testAccConfigVarConfig_basic(varName, varValue, varType), + Config: testAccConfigVarConfigBasic(varName, varValue, varType), Check: resource.ComposeTestCheckFunc( testAccConfigVarExists(varName, varValue, varType), resource.TestCheckResourceAttr(resourceName, "name", varName), ), }, { - Config: testAccConfigVarConfig_withInstanceAndType(varName, varValue, varType, ""), + Config: testAccConfigVarConfigWithInstanceAndType(varName, varValue, varType, ""), Check: resource.ComposeTestCheckFunc( testAccConfigVarExists(varName, varValue, varType), resource.TestCheckResourceAttr(resourceName, "name", varName), ), }, { - Config: testAccConfigVarConfig_withInstanceAndType(varName, varValue, varType, varInstance), + Config: testAccConfigVarConfigWithInstanceAndType(varName, varValue, varType, varInstance), Check: resource.ComposeTestCheckFunc( testAccConfigVarExists(varName, varValue, varType), resource.TestCheckResourceAttr(resourceName, "name", varName), ), }, { - Config: testAccConfigVarConfig_withInstanceAndType(varName, varValue, varType, varInstance), + Config: testAccConfigVarConfigWithInstanceAndType(varName, varValue, varType, varInstance), ExpectError: regexp.MustCompile("variable 'log.level' not found"), Check: resource.ComposeTestCheckFunc( testAccConfigVarExists(varName, varValue, "badType"), @@ -58,7 +59,7 @@ func TestPdConfigVar_basic(t *testing.T) { ), }, { - Config: testAccConfigVarConfig_withInstanceAndType("varName", varValue, varType, varInstance), + Config: testAccConfigVarConfigWithInstanceAndType("varName", varValue, varType, varInstance), ExpectError: regexp.MustCompile(".*Error: bad request to:*"), Check: resource.ComposeTestCheckFunc( testAccConfigVarExists(varName, varValue, varType), @@ -66,7 +67,7 @@ func TestPdConfigVar_basic(t *testing.T) { ), }, { - Config: testAccConfigVarConfig_withInstanceAndType(varName, varValue, "varType", varInstance), + Config: testAccConfigVarConfigWithInstanceAndType(varName, varValue, "varType", varInstance), ExpectError: regexp.MustCompile(".*Error: expected type to be one of.*"), Check: resource.ComposeTestCheckFunc( testAccConfigVarExists(varName, varValue, varType), @@ -74,7 +75,7 @@ func TestPdConfigVar_basic(t *testing.T) { ), }, { - Config: testAccConfigVarConfig_withInstanceAndType(varName, "varValue'varValue", varType, varInstance), + Config: testAccConfigVarConfigWithInstanceAndType(varName, "varValue'varValue", varType, varInstance), ExpectError: regexp.MustCompile(".*Error: \"value\" is badly formatted.*"), Check: resource.ComposeTestCheckFunc( testAccConfigVarExists(varName, varValue, varType), @@ -98,25 +99,25 @@ func TestTiKvConfigVar_basic(t *testing.T) { testAccPreCheckSkipNotTiDB(t) testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccConfigVarCheckDestroy(varName, varType), + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccConfigVarCheckDestroy(varName, varType), Steps: []resource.TestStep{ { - Config: testAccConfigVarConfig_basic(varName, varValue, varType), + Config: testAccConfigVarConfigBasic(varName, varValue, varType), Check: resource.ComposeTestCheckFunc( testAccConfigVarExists(varName, varValue, varType), resource.TestCheckResourceAttr(resourceName, "name", varName), ), }, { - Config: testAccConfigVarConfig_withInstanceAndType(varName, varValue, varType, varInstance), + Config: testAccConfigVarConfigWithInstanceAndType(varName, varValue, varType, varInstance), Check: resource.ComposeTestCheckFunc( testAccConfigVarExists(varName, varValue, varType), resource.TestCheckResourceAttr(resourceName, "name", varName), ), }, { - Config: testAccConfigVarConfig_withInstanceAndType(varName, "varValue", varType, varInstance), + Config: testAccConfigVarConfigWithInstanceAndType(varName, "varValue", varType, varInstance), ExpectError: regexp.MustCompile(".*Error: error setting value*"), Check: resource.ComposeTestCheckFunc( testAccConfigVarExists(varName, "varValue", varType), @@ -172,7 +173,7 @@ func getGetInstance(varType string, t *testing.T) string { err = stmt.QueryRow(varType).Scan(&resInstanceType, &resInstance, &resName, &resValue) - if err != nil && err != sql.ErrNoRows { + if err != nil && !errors.Is(err, sql.ErrNoRows) { return err.Error() } @@ -205,7 +206,7 @@ func testAccConfigVarCheckDestroy(varName string, varType string) resource.TestC } } -func testAccConfigVarConfig_basic(varName string, varValue string, varType string) string { +func testAccConfigVarConfigBasic(varName string, varValue string, varType string) string { return fmt.Sprintf(` resource "mysql_ti_config" "test" { name = "%s" @@ -215,7 +216,7 @@ resource "mysql_ti_config" "test" { `, varName, varValue, varType) } -func testAccConfigVarConfig_withInstanceAndType(varName string, varValue string, varType string, varInstance string) string { +func testAccConfigVarConfigWithInstanceAndType(varName string, varValue string, varType string, varInstance string) string { return fmt.Sprintf(` resource "mysql_ti_config" "test" { name = "%s" diff --git a/mysql/resource_user.go b/mysql/resource_user.go index 545a004fc..583a98e49 100644 --- a/mysql/resource_user.go +++ b/mysql/resource_user.go @@ -351,7 +351,8 @@ func ReadUser(ctx context.Context, d *schema.ResourceData, meta interface{}) dia var createUserStmt string err := db.QueryRowContext(ctx, stmt, d.Get("user").(string), d.Get("host").(string)).Scan(&createUserStmt) if err != nil { - if mysqlErrorNumber(err) == unknownUserErrCode || mysqlErrorNumber(err) == userNotFoundErrCode { + errorNumber := mysqlErrorNumber(err) + if errorNumber == unknownUserErrCode || errorNumber == userNotFoundErrCode { d.SetId("") return nil } diff --git a/mysql/resource_user_password_test.go b/mysql/resource_user_password_test.go index ccdade7e1..12eec9fbe 100644 --- a/mysql/resource_user_password_test.go +++ b/mysql/resource_user_password_test.go @@ -7,9 +7,9 @@ import ( func TestAccUserPassword_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccUserCheckDestroy, + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccUserCheckDestroy, Steps: []resource.TestStep{ { Config: testAccUserPasswordConfig_basic, diff --git a/mysql/resource_user_test.go b/mysql/resource_user_test.go index a0a072de3..d6aef0d74 100644 --- a/mysql/resource_user_test.go +++ b/mysql/resource_user_test.go @@ -14,9 +14,9 @@ import ( func TestAccUser_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipMariaDB(t) }, - Providers: testAccProviders, - CheckDestroy: testAccUserCheckDestroy, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckSkipMariaDB(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccUserCheckDestroy, Steps: []resource.TestStep{ { Config: testAccUserConfig_basic, @@ -54,9 +54,9 @@ func TestAccUser_basic(t *testing.T) { func TestAccUser_auth(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheckSkipTiDB(t); testAccPreCheckSkipMariaDB(t); testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccUserCheckDestroy, + PreCheck: func() { testAccPreCheckSkipTiDB(t); testAccPreCheckSkipMariaDB(t); testAccPreCheckSkipRds(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccUserCheckDestroy, Steps: []resource.TestStep{ { Config: testAccUserConfig_auth_iam_plugin, @@ -96,8 +96,8 @@ func TestAccUser_authConnect(t *testing.T) { testAccPreCheckSkipMariaDB(t) testAccPreCheckSkipRds(t) }, - Providers: testAccProviders, - CheckDestroy: testAccUserCheckDestroy, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccUserCheckDestroy, Steps: []resource.TestStep{ { Config: testAccUserConfig_basic, @@ -137,8 +137,8 @@ func TestAccUser_authConnectRetainOldPassword(t *testing.T) { testAccPreCheckSkipRds(t) testAccPreCheckSkipNotMySQLVersionMin(t, "8.0.14") }, - Providers: testAccProviders, - CheckDestroy: testAccUserCheckDestroy, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccUserCheckDestroy, Steps: []resource.TestStep{ { Config: testAccUserConfig_basic_retain_old_password, @@ -166,9 +166,9 @@ func TestAccUser_authConnectRetainOldPassword(t *testing.T) { func TestAccUser_deprecated(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccUserCheckDestroy, + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccUserCheckDestroy, Steps: []resource.TestStep{ { Config: testAccUserConfig_deprecated, diff --git a/mysql/utils.go b/mysql/utils.go index f8eef0f31..b5fcd69da 100644 --- a/mysql/utils.go +++ b/mysql/utils.go @@ -4,7 +4,10 @@ import ( "context" "crypto/sha256" "database/sql" + "errors" "fmt" + "github.com/go-sql-driver/mysql" + "google.golang.org/api/googleapi" "log" "sync" @@ -69,3 +72,30 @@ func getVersionFromMeta(ctx context.Context, meta interface{}) *version.Version return oneConnection.Version } + +// 0 == not mysql error or not error at all. +func mysqlErrorNumber(err error) uint16 { + if err == nil { + return 0 + } + var mysqlError *mysql.MySQLError + ok := errors.As(err, &mysqlError) + if !ok { + return 0 + } + return mysqlError.Number +} + +func cloudsqlErrorNumber(err error) int { + if err == nil { + return 0 + } + + var gapiError *googleapi.Error + if errors.As(err, &gapiError) { + if gapiError.Code >= 400 && gapiError.Code < 500 { + return gapiError.Code + } + } + return 0 +}