github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/mysql/provider.go (about) 1 package mysql 2 3 import ( 4 "fmt" 5 "strings" 6 7 mysqlc "github.com/ziutek/mymysql/thrsafe" 8 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func Provider() terraform.ResourceProvider { 14 return &schema.Provider{ 15 Schema: map[string]*schema.Schema{ 16 "endpoint": &schema.Schema{ 17 Type: schema.TypeString, 18 Required: true, 19 DefaultFunc: schema.EnvDefaultFunc("MYSQL_ENDPOINT", nil), 20 }, 21 22 "username": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 DefaultFunc: schema.EnvDefaultFunc("MYSQL_USERNAME", nil), 26 }, 27 28 "password": &schema.Schema{ 29 Type: schema.TypeString, 30 Optional: true, 31 DefaultFunc: schema.EnvDefaultFunc("MYSQL_PASSWORD", nil), 32 }, 33 }, 34 35 ResourcesMap: map[string]*schema.Resource{ 36 "mysql_database": resourceDatabase(), 37 }, 38 39 ConfigureFunc: providerConfigure, 40 } 41 } 42 43 func providerConfigure(d *schema.ResourceData) (interface{}, error) { 44 45 var username = d.Get("username").(string) 46 var password = d.Get("password").(string) 47 var endpoint = d.Get("endpoint").(string) 48 49 proto := "tcp" 50 if endpoint[0] == '/' { 51 proto = "unix" 52 } 53 54 // mysqlc is the thread-safe implementation of mymysql, so we can 55 // safely re-use the same connection between multiple parallel 56 // operations. 57 conn := mysqlc.New(proto, "", endpoint, username, password) 58 59 err := conn.Connect() 60 if err != nil { 61 return nil, err 62 } 63 64 return conn, nil 65 } 66 67 var identQuoteReplacer = strings.NewReplacer("`", "``") 68 69 func quoteIdentifier(in string) string { 70 return fmt.Sprintf("`%s`", identQuoteReplacer.Replace(in)) 71 }