Skip to content

Commit

Permalink
New resource: mysql_global_variable
Browse files Browse the repository at this point in the history
To have state for MySQL global configuration with IaC I'd like to introduce new resource: mysql_global_variable
  • Loading branch information
borissavelev committed Jun 25, 2022
1 parent 62015ce commit f3323b4
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ website/node_modules
*~
.*.swp
.idea
/.vscode
*.iml
*.test
*.iml

# goreleaser
/dist

website/vendor

# Test exclusions
Expand Down
13 changes: 7 additions & 6 deletions mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
116 changes: 116 additions & 0 deletions mysql/resource_global_variable.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f3323b4

Please sign in to comment.