Skip to content

Commit 4e55f1f

Browse files
committed
add tests&doc
1 parent f3323b4 commit 4e55f1f

File tree

4 files changed

+149
-20
lines changed

4 files changed

+149
-20
lines changed

GNUmakefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
33
WEBSITE_REPO=github.com/hashicorp/terraform-website
44
PKG_NAME=mysql
55
TERRAFORM_VERSION=0.14.7
6+
TERRAFORM_OS=$(shell uname -s | tr A-Z a-z)
67
TEST_USER=root
78
TEST_PASSWORD=my-secret-pw
89

@@ -17,8 +18,8 @@ test: fmtcheck
1718
xargs -t -n4 go test $(TESTARGS) -timeout=60s -parallel=4
1819

1920
bin/terraform:
20-
mkdir "$(CURDIR)/bin"
21-
curl https://releases.hashicorp.com/terraform/$(TERRAFORM_VERSION)/terraform_$(TERRAFORM_VERSION)_linux_amd64.zip > $(CURDIR)/bin/terraform.zip
21+
mkdir -p "$(CURDIR)/bin"
22+
curl -sfL https://releases.hashicorp.com/terraform/$(TERRAFORM_VERSION)/terraform_$(TERRAFORM_VERSION)_$(TERRAFORM_OS)_amd64.zip > $(CURDIR)/bin/terraform.zip
2223
(cd $(CURDIR)/bin/ ; unzip terraform.zip)
2324

2425
testacc: fmtcheck bin/terraform
@@ -31,7 +32,8 @@ testversion%:
3132

3233
testversion:
3334
-docker run --rm --name test-mysql$(MYSQL_VERSION) -e MYSQL_ROOT_PASSWORD="$(TEST_PASSWORD)" -d -p $(MYSQL_PORT):3306 mysql:$(MYSQL_VERSION)
34-
@while ! mysql -h 127.0.0.1 -P $(MYSQL_PORT) -u "$(TEST_USER)" -p"$(TEST_PASSWORD)" -e 'SELECT 1'; do echo 'Waiting for MySQL...'; sleep 1; done
35+
@echo 'Waiting for MySQL...'
36+
@while ! mysql -h 127.0.0.1 -P $(MYSQL_PORT) -u "$(TEST_USER)" -p"$(TEST_PASSWORD)" -e 'SELECT 1' >/dev/null 2>&1; do printf '.'; sleep 1; done ; echo ; echo "Connected!"
3537
-mysql -h 127.0.0.1 -P $(MYSQL_PORT) -u "$(TEST_USER)" -p"$(TEST_PASSWORD)" -e "INSTALL PLUGIN mysql_no_login SONAME 'mysql_no_login.so';"
3638
MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="$(TEST_PASSWORD)" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc
3739
docker rm -f test-mysql$(MYSQL_VERSION)
@@ -46,7 +48,6 @@ testpercona:
4648
MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="$(TEST_PASSWORD)" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc
4749
docker rm -f test-percona$(MYSQL_VERSION)
4850

49-
5051
vet:
5152
@echo "go vet ."
5253
@go vet $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \
@@ -88,4 +89,3 @@ endif
8889
@$(MAKE) -C $(GOPATH)/src/$(WEBSITE_REPO) website-provider PROVIDER_PATH=$(shell pwd) PROVIDER_NAME=$(PKG_NAME)
8990

9091
.PHONY: build test testacc vet fmt fmtcheck errcheck vendor-status test-compile website website-test
91-

mysql/resource_global_variable.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func resourceGlobalVariable() *schema.Resource {
1515
Update: UpdateGlobalVariable,
1616
Delete: DeleteGlobalVariable,
1717
Importer: &schema.ResourceImporter{
18-
State: ImportGlobalVariable,
18+
StateContext: schema.ImportStatePassthroughContext,
1919
},
2020
Schema: map[string]*schema.Schema{
2121
"name": {
@@ -37,7 +37,7 @@ func CreateGlobalVariable(d *schema.ResourceData, meta interface{}) error {
3737
name := d.Get("name").(string)
3838
value := d.Get("value").(string)
3939

40-
sql := fmt.Sprintf("SET GLOBAL %s = %s", quoteIdentifier(name), quoteIdentifier(value))
40+
sql := fmt.Sprintf("SET GLOBAL %s = %s", quoteIdentifier(name), value)
4141
log.Printf("[DEBUG] SQL: %s", sql)
4242

4343
_, err := db.Exec(sql)
@@ -55,15 +55,15 @@ func ReadGlobalVariable(d *schema.ResourceData, meta interface{}) error {
5555

5656
stmt, err := db.Prepare("SHOW GLOBAL VARIABLES WHERE VARIABLE_NAME = ?")
5757
if err != nil {
58-
log.Fatal(err)
58+
return fmt.Errorf("error during prepare statement for global variable: %s", err)
5959
}
6060

6161
var name, value string
6262
err = stmt.QueryRow(d.Id()).Scan(&name, &value)
6363

6464
if err != nil && err != sql.ErrNoRows {
6565
d.SetId("")
66-
return fmt.Errorf("Error during show global variables: %s", err)
66+
return fmt.Errorf("error during show global variables: %s", err)
6767
}
6868

6969
d.Set("name", name)
@@ -78,7 +78,7 @@ func UpdateGlobalVariable(d *schema.ResourceData, meta interface{}) error {
7878
name := d.Get("name").(string)
7979
value := d.Get("value").(string)
8080

81-
sql := fmt.Sprintf("SET GLOBAL %s = %s", quoteIdentifier(name), quoteIdentifier(value))
81+
sql := fmt.Sprintf("SET GLOBAL %s = %s", quoteIdentifier(name), value)
8282
log.Printf("[DEBUG] SQL: %s", sql)
8383

8484
_, err := db.Exec(sql)
@@ -104,13 +104,3 @@ func DeleteGlobalVariable(d *schema.ResourceData, meta interface{}) error {
104104

105105
return nil
106106
}
107-
108-
func ImportGlobalVariable(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
109-
err := ReadGlobalVariable(d, meta)
110-
111-
if err != nil {
112-
return nil, err
113-
}
114-
115-
return []*schema.ResourceData{d}, nil
116-
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package mysql
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
)
11+
12+
func TestAccGlobalVar_basic(t *testing.T) {
13+
varName := "max_connections"
14+
resourceName := "mysql_global_variable.test"
15+
16+
resource.Test(t, resource.TestCase{
17+
Providers: testAccProviders,
18+
CheckDestroy: testAccGlobalVarCheckDestroy(varName),
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccGlobalVarConfig_basic(varName),
22+
Check: resource.ComposeTestCheckFunc(
23+
testAccGlobalVarExists(varName),
24+
resource.TestCheckResourceAttr(resourceName, "name", varName),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccGlobalVarExists(varName string) resource.TestCheckFunc {
32+
return func(s *terraform.State) error {
33+
db, err := connectToMySQL(testAccProvider.Meta().(*MySQLConfiguration))
34+
if err != nil {
35+
return err
36+
}
37+
38+
count, err := testAccGetGlobalVar(varName, db)
39+
40+
if err != nil {
41+
return err
42+
}
43+
44+
if count == 1 {
45+
return nil
46+
}
47+
48+
return fmt.Errorf("variable '%s' not found", varName)
49+
}
50+
}
51+
52+
func testAccGetGlobalVar(varName string, db *sql.DB) (int, error) {
53+
stmt, err := db.Prepare("SHOW GLOBAL VARIABLES WHERE VARIABLE_NAME = ?")
54+
if err != nil {
55+
return 0, err
56+
}
57+
58+
var name string
59+
var value int
60+
err = stmt.QueryRow(varName).Scan(&name, &value)
61+
62+
if err != nil && err != sql.ErrNoRows {
63+
return 0, err
64+
}
65+
66+
return value, nil
67+
}
68+
69+
func testAccGlobalVarCheckDestroy(varName string) resource.TestCheckFunc {
70+
return func(s *terraform.State) error {
71+
db, err := connectToMySQL(testAccProvider.Meta().(*MySQLConfiguration))
72+
if err != nil {
73+
return err
74+
}
75+
76+
count, err := testAccGetGlobalVar(varName, db)
77+
if count == 1 {
78+
return fmt.Errorf("Global variable '%s' still has non default value", varName)
79+
}
80+
81+
return nil
82+
}
83+
}
84+
85+
func testAccGlobalVarConfig_basic(varName string) string {
86+
return fmt.Sprintf(`
87+
resource "mysql_global_variable" "test" {
88+
name = "%s"
89+
value = 1
90+
}
91+
`, varName)
92+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
layout: "mysql"
3+
page_title: "MySQL: mysql_global_variable"
4+
sidebar_current: "docs-mysql-resource-global-variable"
5+
description: |-
6+
Manages a global variables on a MySQL server.
7+
---
8+
9+
# mysql\_global\_variable
10+
11+
The ``mysql_global_variable`` resource manages a global variables on a MySQL
12+
server.
13+
14+
~> **Note on MySQL:** MySQL global variables are [not persistent](https://dev.mysql.com/doc/refman/5.7/en/set-variable.html)
15+
16+
~> **Note on TiDB:** TiDB global variables are [persistent](https://docs.pingcap.com/tidb/v5.4/sql-statement-set-variable#mysql-compatibility)
17+
18+
~> **Note about `destroy`:** `destroy` will try assign `DEFAULT` value for global variable.
19+
Unfortunately not every variable support this.
20+
21+
## Example Usage
22+
23+
```hcl
24+
resource "mysql_global_variable" "max_connections" {
25+
name = "max_connections"
26+
value = "100"
27+
}
28+
```
29+
30+
## Argument Reference
31+
32+
The following arguments are supported:
33+
34+
* `name` - (Required) The name of the global variable.
35+
* `value` - (Required) The value of the global variable.
36+
37+
## Attributes Reference
38+
39+
No further attributes are exported.
40+
41+
## Import
42+
43+
Global variable can be imported using global variable name.
44+
45+
```shell
46+
$ terraform import mysql_global_variable.max_connections max_connections
47+
```

0 commit comments

Comments
 (0)