Skip to content

Commit

Permalink
chore: rename binlog attribute, better error messages. Remove randomn…
Browse files Browse the repository at this point in the history
…ess from tests
  • Loading branch information
cemdorst committed Feb 1, 2023
1 parent e1c2a94 commit 0dc96e0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 58 deletions.
2 changes: 1 addition & 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 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
Expand Down
43 changes: 12 additions & 31 deletions mysql/resource_rds_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -153,30 +146,18 @@ 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"
}
result = append(result, (fmt.Sprintf("call mysql.rds_set_configuration('binlog retention hours', %s)", retention_period)))
}

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
}
21 changes: 10 additions & 11 deletions mysql/resource_rds_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)),
),
},
Expand All @@ -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)
}
Expand Down Expand Up @@ -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()

Expand Down
12 changes: 0 additions & 12 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions website/docs/r/rds_config.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ server.

```hcl
resource "mysql_rds_config" "this" {
binlog_retention_period = 48
replication_target_delay = 30
binlog_retention_hours = 48
replication_target_delay = 3200
}
```

## Argument Reference

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.
Expand Down

0 comments on commit 0dc96e0

Please sign in to comment.