terraform/builtin/providers/mysql/provider.go

72 lines
1.6 KiB
Go

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),
},
"username": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("MYSQL_USERNAME", nil),
},
"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))
}