mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-15 10:03:44 -06:00
There are currently no checks on username and endpoint in the provider schema from being an empty value. This PR adds support to make sure that endpoint and username are not empty strings as that can cause a panic Results of the PR: ``` % terraform apply There are warnings and/or errors related to your configuration. Please fix these before continuing. Errors: * provider.mysql: Endpoint must not be an empty string ```
88 lines
2.0 KiB
Go
88 lines
2.0 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),
|
|
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))
|
|
}
|