diff --git a/.gitignore b/.gitignore index 1b116c58..544851cf 100644 --- a/.gitignore +++ b/.gitignore @@ -21,10 +21,14 @@ website/node_modules *~ .*.swp .idea +/.vscode *.iml *.test *.iml +# goreleaser +/dist + website/vendor # Test exclusions diff --git a/mysql/provider.go b/mysql/provider.go index 159ccbef..05e69cff 100644 --- a/mysql/provider.go +++ b/mysql/provider.go @@ -131,12 +131,13 @@ func Provider() *schema.Provider { }, ResourcesMap: map[string]*schema.Resource{ - "mysql_database": resourceDatabase(), - "mysql_grant": resourceGrant(), - "mysql_role": resourceRole(), - "mysql_user": resourceUser(), - "mysql_user_password": resourceUserPassword(), - "mysql_sql": resourceSql(), + "mysql_database": resourceDatabase(), + "mysql_global_variable": resourceGlobalVariable(), + "mysql_grant": resourceGrant(), + "mysql_role": resourceRole(), + "mysql_sql": resourceSql(), + "mysql_user_password": resourceUserPassword(), + "mysql_user": resourceUser(), }, ConfigureFunc: providerConfigure, diff --git a/mysql/resource_global_variable.go b/mysql/resource_global_variable.go new file mode 100644 index 00000000..6d7d901e --- /dev/null +++ b/mysql/resource_global_variable.go @@ -0,0 +1,116 @@ +package mysql + +import ( + "database/sql" + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func resourceGlobalVariable() *schema.Resource { + return &schema.Resource{ + Create: CreateGlobalVariable, + Read: ReadGlobalVariable, + Update: UpdateGlobalVariable, + Delete: DeleteGlobalVariable, + Importer: &schema.ResourceImporter{ + State: ImportGlobalVariable, + }, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "value": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func CreateGlobalVariable(d *schema.ResourceData, meta interface{}) error { + db := meta.(*MySQLConfiguration).Db + + name := d.Get("name").(string) + value := d.Get("value").(string) + + sql := fmt.Sprintf("SET GLOBAL %s = %s", quoteIdentifier(name), quoteIdentifier(value)) + log.Printf("[DEBUG] SQL: %s", sql) + + _, err := db.Exec(sql) + if err != nil { + return fmt.Errorf("error setting value: %s", err) + } + + d.SetId(name) + + return nil +} + +func ReadGlobalVariable(d *schema.ResourceData, meta interface{}) error { + db := meta.(*MySQLConfiguration).Db + + stmt, err := db.Prepare("SHOW GLOBAL VARIABLES WHERE VARIABLE_NAME = ?") + if err != nil { + log.Fatal(err) + } + + var name, value string + err = stmt.QueryRow(d.Id()).Scan(&name, &value) + + if err != nil && err != sql.ErrNoRows { + d.SetId("") + return fmt.Errorf("Error during show global variables: %s", err) + } + + d.Set("name", name) + d.Set("value", value) + + return nil +} + +func UpdateGlobalVariable(d *schema.ResourceData, meta interface{}) error { + db := meta.(*MySQLConfiguration).Db + + name := d.Get("name").(string) + value := d.Get("value").(string) + + sql := fmt.Sprintf("SET GLOBAL %s = %s", quoteIdentifier(name), quoteIdentifier(value)) + log.Printf("[DEBUG] SQL: %s", sql) + + _, err := db.Exec(sql) + if err != nil { + return fmt.Errorf("error update value: %s", err) + } + return ReadGlobalVariable(d, meta) +} + +func DeleteGlobalVariable(d *schema.ResourceData, meta interface{}) error { + db := meta.(*MySQLConfiguration).Db + name := d.Get("name").(string) + + sql := fmt.Sprintf("SET GLOBAL %s = DEFAULT", quoteIdentifier(name)) + log.Printf("[DEBUG] SQL: %s", sql) + + _, err := db.Exec(sql) + if err != nil { + log.Printf("[WARN] Variable_name (%s) not found; removing from state", d.Id()) + d.SetId("") + return nil + } + + return nil +} + +func ImportGlobalVariable(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + err := ReadGlobalVariable(d, meta) + + if err != nil { + return nil, err + } + + return []*schema.ResourceData{d}, nil +}