2015-12-17 02:59:35 +01:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-08-17 10:11:54 +02:00
|
|
|
"strconv"
|
2015-12-17 02:59:35 +01:00
|
|
|
"strings"
|
|
|
|
|
2016-08-17 10:11:54 +02:00
|
|
|
mysqlc "github.com/ziutek/mymysql/mysql"
|
|
|
|
mysqlts "github.com/ziutek/mymysql/thrsafe"
|
2015-12-17 02:59:35 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2016-08-17 10:11:54 +02:00
|
|
|
type providerConfiguration struct {
|
|
|
|
Conn mysqlc.Conn
|
|
|
|
VersionMajor uint
|
|
|
|
VersionMinor uint
|
|
|
|
VersionPatch uint
|
|
|
|
}
|
|
|
|
|
2015-12-17 02:59:35 +01:00
|
|
|
func Provider() terraform.ResourceProvider {
|
|
|
|
return &schema.Provider{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"endpoint": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("MYSQL_ENDPOINT", nil),
|
2016-06-17 12:32:30 +02:00
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
|
|
|
if value == "" {
|
|
|
|
errors = append(errors, fmt.Errorf("Endpoint must not be an empty string"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
},
|
2015-12-17 02:59:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"username": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("MYSQL_USERNAME", nil),
|
2016-06-17 12:32:30 +02:00
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
|
|
|
if value == "" {
|
|
|
|
errors = append(errors, fmt.Errorf("Username must not be an empty string"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
},
|
2015-12-17 02:59:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"password": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("MYSQL_PASSWORD", nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
ResourcesMap: map[string]*schema.Resource{
|
|
|
|
"mysql_database": resourceDatabase(),
|
2016-07-26 20:19:39 +02:00
|
|
|
"mysql_user": resourceUser(),
|
|
|
|
"mysql_grant": resourceGrant(),
|
2015-12-17 02:59:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
ConfigureFunc: providerConfigure,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
|
|
|
|
|
|
|
var username = d.Get("username").(string)
|
|
|
|
var password = d.Get("password").(string)
|
|
|
|
var endpoint = d.Get("endpoint").(string)
|
|
|
|
|
|
|
|
proto := "tcp"
|
|
|
|
if endpoint[0] == '/' {
|
|
|
|
proto = "unix"
|
|
|
|
}
|
|
|
|
|
2016-08-17 10:11:54 +02:00
|
|
|
// mysqlts is the thread-safe implementation of mymysql, so we can
|
2015-12-17 02:59:35 +01:00
|
|
|
// safely re-use the same connection between multiple parallel
|
|
|
|
// operations.
|
2016-08-17 10:11:54 +02:00
|
|
|
conn := mysqlts.New(proto, "", endpoint, username, password)
|
2015-12-17 02:59:35 +01:00
|
|
|
|
|
|
|
err := conn.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-17 10:11:54 +02:00
|
|
|
major, minor, patch, err := mysqlVersion(conn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &providerConfiguration{
|
|
|
|
Conn: conn,
|
|
|
|
VersionMajor: major,
|
|
|
|
VersionMinor: minor,
|
|
|
|
VersionPatch: patch,
|
|
|
|
}, nil
|
2015-12-17 02:59:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var identQuoteReplacer = strings.NewReplacer("`", "``")
|
|
|
|
|
|
|
|
func quoteIdentifier(in string) string {
|
|
|
|
return fmt.Sprintf("`%s`", identQuoteReplacer.Replace(in))
|
|
|
|
}
|
2016-08-17 10:11:54 +02:00
|
|
|
|
|
|
|
func mysqlVersion(conn mysqlc.Conn) (uint, uint, uint, error) {
|
|
|
|
rows, _, err := conn.Query("SELECT VERSION()")
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, 0, err
|
|
|
|
}
|
|
|
|
if len(rows) == 0 {
|
|
|
|
return 0, 0, 0, fmt.Errorf("SELECT VERSION() returned an empty set")
|
|
|
|
}
|
|
|
|
|
|
|
|
versionString := rows[0].Str(0)
|
|
|
|
version := strings.Split(versionString, ".")
|
|
|
|
invalidVersionErr := fmt.Errorf("Invalid major.minor.patch in %q", versionString)
|
|
|
|
if len(version) != 3 {
|
|
|
|
return 0, 0, 0, invalidVersionErr
|
|
|
|
}
|
|
|
|
|
|
|
|
major, err := strconv.ParseUint(version[0], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, 0, invalidVersionErr
|
|
|
|
}
|
|
|
|
|
|
|
|
minor, err := strconv.ParseUint(version[1], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, 0, invalidVersionErr
|
|
|
|
}
|
|
|
|
|
|
|
|
patch, err := strconv.ParseUint(version[2], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, 0, invalidVersionErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint(major), uint(minor), uint(patch), nil
|
|
|
|
}
|