terraform/builtin/providers/mysql/provider.go

88 lines
2.0 KiB
Go
Raw Normal View History

package mysql
import (
"fmt"
"strings"
mysqlc "github.com/ziutek/mymysql/thrsafe"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
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),
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
},
},
"username": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("MYSQL_USERNAME", nil),
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
},
},
"password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("MYSQL_PASSWORD", nil),
},
},
ResourcesMap: map[string]*schema.Resource{
"mysql_database": resourceDatabase(),
},
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"
}
// mysqlc is the thread-safe implementation of mymysql, so we can
// safely re-use the same connection between multiple parallel
// operations.
conn := mysqlc.New(proto, "", endpoint, username, password)
err := conn.Connect()
if err != nil {
return nil, err
}
return conn, nil
}
var identQuoteReplacer = strings.NewReplacer("`", "``")
func quoteIdentifier(in string) string {
return fmt.Sprintf("`%s`", identQuoteReplacer.Replace(in))
}