From 7f6d2b1e534331813f96ceb22233383eb4d59ae3 Mon Sep 17 00:00:00 2001 From: Dorst Date: Tue, 10 Jan 2023 12:42:51 -0300 Subject: [PATCH] fix: read binlog retention when null. Unique id --- mysql/resource_binlog.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/mysql/resource_binlog.go b/mysql/resource_binlog.go index 68dad59f..924b5c8d 100644 --- a/mysql/resource_binlog.go +++ b/mysql/resource_binlog.go @@ -2,8 +2,11 @@ package mysql import ( "context" + "database/sql" "fmt" "log" + "strconv" + "time" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -44,7 +47,9 @@ func CreateBinLog(ctx context.Context, d *schema.ResourceData, meta interface{}) return diag.Errorf("failed running SQL to set binlog retention period: %v", err) } - d.SetId(d.Get("retention_period").(string)) + id := strconv.FormatInt(time.Now().UTC().UnixNano(), 10) + + d.SetId(id) return ReadBinLog(ctx, d, meta) } @@ -86,15 +91,19 @@ func ReadBinLog(ctx context.Context, d *schema.ResourceData, meta interface{}) d return diag.Errorf("Error verifying binlog retention period: %s", err) } - results := make(map[string]string) + results := make(map[string]interface{}) for rows.Next() { - var name, value, description string + var name, description string + var value sql.NullString if err := rows.Scan(&name, &value, &description); err != nil { return diag.Errorf("failed reading binlog retention period: %v", err) } results[name] = value } + if results["binlog retention hours"] == "NULL" { + results["binlog retention hours"] = "0" + } d.Set("retention_period", results["binlog retention hours"]) @@ -121,9 +130,14 @@ func DeleteBinLog(ctx context.Context, d *schema.ResourceData, meta interface{}) func binlogConfigSQL(d *schema.ResourceData) string { retention_period := d.Get("retention_period").(string) - - return fmt.Sprintf( - "call mysql.rds_set_configuration('binlog retention hours', %s)", - retention_period, - ) + if retention_period == "0" { + return fmt.Sprintf( + "call mysql.rds_set_configuration('binlog retention hours', %s)", + "NULL") + } else { + return fmt.Sprintf( + "call mysql.rds_set_configuration('binlog retention hours', %s)", + retention_period, + ) + } }