From 911e02e34c53048e96e17f9e8cf9bfb159496c99 Mon Sep 17 00:00:00 2001 From: Dorst Date: Thu, 15 Dec 2022 13:19:19 -0300 Subject: [PATCH] feat: add binlog retention resource --- mysql/provider.go | 17 +++--- mysql/resource_binlog.go | 129 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 mysql/resource_binlog.go diff --git a/mysql/provider.go b/mysql/provider.go index 86e5f2e3..7aabf8fc 100644 --- a/mysql/provider.go +++ b/mysql/provider.go @@ -141,14 +141,15 @@ func Provider() *schema.Provider { }, ResourcesMap: map[string]*schema.Resource{ - "mysql_database": resourceDatabase(), - "mysql_global_variable": resourceGlobalVariable(), - "mysql_grant": resourceGrant(), - "mysql_role": resourceRole(), - "mysql_sql": resourceSql(), - "mysql_user_password": resourceUserPassword(), - "mysql_user": resourceUser(), - "mysql_ti_config": resourceTiConfigVariable(), + "mysql_database": resourceDatabase(), + "mysql_global_variable": resourceGlobalVariable(), + "mysql_grant": resourceGrant(), + "mysql_role": resourceRole(), + "mysql_sql": resourceSql(), + "mysql_user_password": resourceUserPassword(), + "mysql_user": resourceUser(), + "mysql_ti_config": resourceTiConfigVariable(), + "mysql_binlog_retention": resourceBinLog(), }, ConfigureContextFunc: providerConfigure, diff --git a/mysql/resource_binlog.go b/mysql/resource_binlog.go new file mode 100644 index 00000000..68dad59f --- /dev/null +++ b/mysql/resource_binlog.go @@ -0,0 +1,129 @@ +package mysql + +import ( + "context" + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + + "github.com/go-sql-driver/mysql" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func resourceBinLog() *schema.Resource { + return &schema.Resource{ + CreateContext: CreateBinLog, + UpdateContext: UpdateBinLog, + ReadContext: ReadBinLog, + DeleteContext: DeleteBinLog, + Importer: &schema.ResourceImporter{ + StateContext: ImportDatabase, + }, + Schema: map[string]*schema.Schema{ + "retention_period": { + Type: schema.TypeString, + Optional: true, + Default: "NULL", + }, + }, + } +} + +func CreateBinLog(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + db, err := getDatabaseFromMeta(ctx, meta) + if err != nil { + return diag.FromErr(err) + } + + stmtSQL := binlogConfigSQL(d) + log.Println("Executing statement:", stmtSQL) + + _, err = db.ExecContext(ctx, stmtSQL) + if err != nil { + return diag.Errorf("failed running SQL to set binlog retention period: %v", err) + } + + d.SetId(d.Get("retention_period").(string)) + + return ReadBinLog(ctx, d, meta) +} + +func UpdateBinLog(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + db, err := getDatabaseFromMeta(ctx, meta) + if err != nil { + return diag.FromErr(err) + } + + stmtSQL := binlogConfigSQL(d) + log.Println("Executing statement:", stmtSQL) + + _, err = db.ExecContext(ctx, stmtSQL) + if err != nil { + return diag.Errorf("failed updating binlog retention period: %v", err) + } + + return ReadBinLog(ctx, d, meta) +} + +func ReadBinLog(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + db, err := getDatabaseFromMeta(ctx, meta) + if err != nil { + return diag.FromErr(err) + } + + stmtSQL := "call mysql.rds_show_configuration" + + 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 binlog retention period: %s", err) + } + + results := make(map[string]string) + for rows.Next() { + var name, value, description string + + if err := rows.Scan(&name, &value, &description); err != nil { + return diag.Errorf("failed reading binlog retention period: %v", err) + } + results[name] = value + } + + d.Set("retention_period", results["binlog retention hours"]) + + return nil +} + +func DeleteBinLog(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + db, err := getDatabaseFromMeta(ctx, meta) + if err != nil { + return diag.FromErr(err) + } + + stmtSQL := "call mysql.rds_set_configuration('binlog retention hours', NULL)" + log.Println("Executing statement:", stmtSQL) + + _, err = db.ExecContext(ctx, stmtSQL) + if err != nil { + return diag.Errorf("failed unsetting binlog retention period: %v", err) + } + + d.SetId("") + return nil +} + +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, + ) +}