From 0dc96e08252e1d3c7c6d6ee599054b60cd16aafa Mon Sep 17 00:00:00 2001 From: Dorst Date: Wed, 1 Feb 2023 20:35:54 -0300 Subject: [PATCH] chore: rename binlog attribute, better error messages. Remove randomness from tests --- GNUmakefile | 2 +- mysql/resource_rds_config.go | 43 +++++++------------------ mysql/resource_rds_config_test.go | 21 ++++++------ vendor/modules.txt | 12 ------- website/docs/r/rds_config.html.markdown | 8 +++-- 5 files changed, 28 insertions(+), 58 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index ad3003fb6..888ef45d5 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -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 testmariadb10.3 testmariadb10.8 testmariadb10.10 testtidb6.1.0 testrdsdb5.7 testrdsdb8.0 +acceptance: testversion5.6 testversion5.7 testversion8.0 testpercona5.7 testpercona8.0 testmariadb10.3 testmariadb10.8 testmariadb10.10 testtidb6.1.0 testversion%: $(MAKE) MYSQL_VERSION=$* MYSQL_PORT=33$(shell echo "$*" | tr -d '.') testversion diff --git a/mysql/resource_rds_config.go b/mysql/resource_rds_config.go index 906fc43e6..7c78c5591 100644 --- a/mysql/resource_rds_config.go +++ b/mysql/resource_rds_config.go @@ -9,8 +9,6 @@ import ( "time" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - - "github.com/go-sql-driver/mysql" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -21,10 +19,10 @@ func resourceRDSConfig() *schema.Resource { ReadContext: ReadRDSConfig, DeleteContext: DeleteRDSConfig, Importer: &schema.ResourceImporter{ - StateContext: ImportRDSConfig, + StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ - "binlog_retention_period": { + "binlog_retention_hours": { Type: schema.TypeInt, Optional: true, Default: 0, @@ -91,13 +89,8 @@ func ReadRDSConfig(ctx context.Context, d *schema.ResourceData, meta interface{} log.Println("Executing query:", stmtSQL) rows, err := db.QueryContext(ctx, stmtSQL) if err != nil { - if mysqlErr, ok := err.(*mysql.MySQLError); ok { - if mysqlErr.Number == unknownDatabaseErrCode { - d.SetId("") - return nil - } - } - return diag.Errorf("Error verifying RDS config: %s", err) + d.SetId("") + return diag.Errorf("Error reading RDS config from DB: %v", err) } results := make(map[string]string) @@ -106,7 +99,7 @@ func ReadRDSConfig(ctx context.Context, d *schema.ResourceData, meta interface{} var value sql.NullString if err := rows.Scan(&name, &value, &description); err != nil { - return diag.Errorf("failed reading RDS config: %v", err) + return diag.Errorf("failed validating RDS config: %v", err) } if value.Valid { @@ -118,15 +111,15 @@ func ReadRDSConfig(ctx context.Context, d *schema.ResourceData, meta interface{} binlog_retention_period, err := strconv.Atoi(results["binlog retention hours"]) if err != nil { - return diag.Errorf("failed reading RDS config: %v", err) + return diag.Errorf("failed reading binlog retention hours in RDS config: %v", err) } replication_target_delay, err := strconv.Atoi(results["target delay"]) if err != nil { - return diag.Errorf("failed reading RDS config: %v", err) + return diag.Errorf("failed reading target delay in RDS config: %v", err) } d.Set("replication_target_delay", replication_target_delay) - d.Set("binlog_retention_period", binlog_retention_period) + d.Set("binlog_retention_hours", binlog_retention_period) return nil } @@ -153,8 +146,8 @@ func DeleteRDSConfig(ctx context.Context, d *schema.ResourceData, meta interface func RDSConfigSQL(d *schema.ResourceData) []string { result := []string{} - if d.Get("binlog_retention_period") != nil { - retention_period := strconv.Itoa(d.Get("binlog_retention_period").(int)) + if d.Get("binlog_retention_hours") != nil { + retention_period := strconv.Itoa(d.Get("binlog_retention_hours").(int)) if retention_period == "0" { retention_period = "NULL" } @@ -162,21 +155,9 @@ func RDSConfigSQL(d *schema.ResourceData) []string { } if d.Get("replication_target_delay") != nil { - target_delay := strconv.Itoa(d.Get("replication_target_delay").(int)) - result = append(result, (fmt.Sprintf("call mysql.rds_set_configuration('target delay', %s)", target_delay))) + target_delay := d.Get("replication_target_delay") + result = append(result, (fmt.Sprintf("call mysql.rds_set_configuration('target delay', %v)", target_delay))) } return result } - -func ImportRDSConfig(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - id := strconv.FormatInt(time.Now().UTC().UnixNano(), 10) - d.SetId(id) - - err := ReadRDSConfig(ctx, d, meta) - if err != nil { - return nil, fmt.Errorf("error while importing: %v", err) - } - - return []*schema.ResourceData{d}, nil -} diff --git a/mysql/resource_rds_config_test.go b/mysql/resource_rds_config_test.go index 886d26d49..067459458 100644 --- a/mysql/resource_rds_config_test.go +++ b/mysql/resource_rds_config_test.go @@ -8,15 +8,14 @@ import ( "strconv" "testing" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func TestAccResourceRDS(t *testing.T) { - rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) - binlog := acctest.RandIntRange(0, 78) - targetDelay := acctest.RandIntRange(0, 7200) + rName := "test" + binlog := 24 + targetDelay := 3200 resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -25,7 +24,7 @@ func TestAccResourceRDS(t *testing.T) { Config: testAccRDSConfig_basic(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_period", fmt.Sprintf("%d", binlog)), + resource.TestCheckResourceAttr(fmt.Sprintf("mysql_rds_config.%s", rName), "binlog_retention_hours", fmt.Sprintf("%d", binlog)), resource.TestCheckResourceAttr(fmt.Sprintf("mysql_rds_config.%s", rName), "replication_target_delay", fmt.Sprintf("%d", targetDelay)), ), }, @@ -36,7 +35,7 @@ func TestAccResourceRDS(t *testing.T) { func testAccRDSConfig_basic(rName string, binlog int, replication int) string { return fmt.Sprintf(` resource "mysql_rds_config" "%s" { - binlog_retention_period = %d + binlog_retention_hours = %d replication_target_delay = %d }`, rName, binlog, replication) } @@ -96,12 +95,12 @@ func testAccRDSCheckDestroy() resource.TestCheckFunc { } func TestAccResourceRDSConfigChange(t *testing.T) { - rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) + rName := "test_update" fullResourceName := fmt.Sprintf("mysql_rds_config.%s", rName) - binlog := acctest.RandIntRange(0, 72) - binlogUpdated := acctest.RandIntRange(73, 96) - targetDelay := acctest.RandIntRange(0, 7200) - targetDelayUpdated := acctest.RandIntRange(7201, 8000) + binlog := 24 + binlogUpdated := 48 + targetDelay := 3200 + targetDelayUpdated := 7400 ctx := context.Background() diff --git a/vendor/modules.txt b/vendor/modules.txt index 1d3093724..d746c90cd 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -16,7 +16,6 @@ cloud.google.com/go/compute/metadata github.com/agext/levenshtein # github.com/apparentlymart/go-cidr v1.1.0 ## explicit -github.com/apparentlymart/go-cidr/cidr # github.com/apparentlymart/go-textseg/v13 v13.0.0 ## explicit; go 1.16 github.com/apparentlymart/go-textseg/v13/textseg @@ -159,7 +158,6 @@ github.com/hashicorp/terraform-plugin-log/tfsdklog # github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1 ## explicit; go 1.18 github.com/hashicorp/terraform-plugin-sdk/v2/diag -github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema @@ -261,22 +259,13 @@ go.opencensus.io/trace/propagation go.opencensus.io/trace/tracestate # golang.org/x/crypto v0.5.0 ## explicit; go 1.17 -golang.org/x/crypto/blowfish golang.org/x/crypto/cast5 -golang.org/x/crypto/chacha20 -golang.org/x/crypto/curve25519 -golang.org/x/crypto/curve25519/internal/field -golang.org/x/crypto/ed25519 -golang.org/x/crypto/internal/alias -golang.org/x/crypto/internal/poly1305 golang.org/x/crypto/openpgp golang.org/x/crypto/openpgp/armor golang.org/x/crypto/openpgp/elgamal golang.org/x/crypto/openpgp/errors golang.org/x/crypto/openpgp/packet golang.org/x/crypto/openpgp/s2k -golang.org/x/crypto/ssh -golang.org/x/crypto/ssh/internal/bcrypt_pbkdf # golang.org/x/net v0.5.0 ## explicit; go 1.17 golang.org/x/net/context @@ -300,7 +289,6 @@ golang.org/x/oauth2/jws golang.org/x/oauth2/jwt # golang.org/x/sys v0.4.0 ## explicit; go 1.17 -golang.org/x/sys/cpu golang.org/x/sys/unix # golang.org/x/text v0.6.0 ## explicit; go 1.17 diff --git a/website/docs/r/rds_config.html.markdown b/website/docs/r/rds_config.html.markdown index 6144c392e..dffbdb306 100644 --- a/website/docs/r/rds_config.html.markdown +++ b/website/docs/r/rds_config.html.markdown @@ -17,8 +17,8 @@ server. ```hcl resource "mysql_rds_config" "this" { - binlog_retention_period = 48 - replication_target_delay = 30 + binlog_retention_hours = 48 + replication_target_delay = 3200 } ``` @@ -26,9 +26,11 @@ resource "mysql_rds_config" "this" { The following arguments are supported: -* `binlog_retention_period` - (Optional) binlog retention period in hours +* `binlog_retention_hours` - (Optional) binlog retention period in hours * `replication_target_delay` - (Optional) replicaation target delay in seconds +[Amazon RDS MySQL](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/mysql_rds_set_configuration.html) + ## Attributes Reference No further attributes are exported.