forked from petoju/terraform-provider-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request petoju#14 from borissavelev/mysql-global-variable
New resource: mysql_global_variable
- Loading branch information
Showing
10 changed files
with
296 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
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{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
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), 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 { | ||
return fmt.Errorf("error during prepare statement for global variable: %s", 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), 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package mysql | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccGlobalVar_basic(t *testing.T) { | ||
varName := "max_connections" | ||
resourceName := "mysql_global_variable.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccGlobalVarCheckDestroy(varName), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccGlobalVarConfig_basic(varName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccGlobalVarExists(varName), | ||
resource.TestCheckResourceAttr(resourceName, "name", varName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccGlobalVarExists(varName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
db, err := connectToMySQL(testAccProvider.Meta().(*MySQLConfiguration)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
count, err := testAccGetGlobalVar(varName, db) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if count == 1 { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("variable '%s' not found", varName) | ||
} | ||
} | ||
|
||
func testAccGetGlobalVar(varName string, db *sql.DB) (int, error) { | ||
stmt, err := db.Prepare("SHOW GLOBAL VARIABLES WHERE VARIABLE_NAME = ?") | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var name string | ||
var value int | ||
err = stmt.QueryRow(varName).Scan(&name, &value) | ||
|
||
if err != nil && err != sql.ErrNoRows { | ||
return 0, err | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
func testAccGlobalVarCheckDestroy(varName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
db, err := connectToMySQL(testAccProvider.Meta().(*MySQLConfiguration)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
count, err := testAccGetGlobalVar(varName, db) | ||
if count == 1 { | ||
return fmt.Errorf("Global variable '%s' still has non default value", varName) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccGlobalVarConfig_basic(varName string) string { | ||
return fmt.Sprintf(` | ||
resource "mysql_global_variable" "test" { | ||
name = "%s" | ||
value = 1 | ||
} | ||
`, varName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.