Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Add proxy argument #102

Merged
merged 2 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"database/sql"
"fmt"
"net"
"net/url"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -53,6 +55,16 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("MYSQL_PASSWORD", nil),
},

"proxy": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"ALL_PROXY",
"all_proxy",
}, nil),
ValidateFunc: validation.StringMatch(regexp.MustCompile("^socks5h?://.*:\\d+$"), "The proxy URL is not a valid socks url."),
davidji99 marked this conversation as resolved.
Show resolved Hide resolved
},

"tls": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -105,7 +117,11 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
AllowNativePasswords: true,
}

dialer := proxy.FromEnvironment()
dialer, err := makeDialer(d)
if err != nil {
return nil, err
}

mysql.RegisterDial("tcp", func(network string) (net.Conn, error) {
return dialer.Dial("tcp", network)
})
Expand All @@ -119,6 +135,26 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {

var identQuoteReplacer = strings.NewReplacer("`", "``")

func makeDialer(d *schema.ResourceData) (proxy.Dialer, error) {
proxyFromEnv := proxy.FromEnvironment()
proxyArg := d.Get("proxy").(string)

if len(proxyArg) > 0 {
proxyURL, err := url.Parse(proxyArg)
if err != nil {
return nil, err
}
proxy, err := proxy.FromURL(proxyURL, proxyFromEnv)
if err != nil {
return nil, err
}

return proxy, nil
}

return proxyFromEnv, nil
}

func quoteIdentifier(in string) string {
return fmt.Sprintf("`%s`", identQuoteReplacer.Replace(in))
}
Expand Down
1 change: 1 addition & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ The following arguments are supported:
* `endpoint` - (Required) The address of the MySQL server to use. Most often a "hostname:port" pair, but may also be an absolute path to a Unix socket when the host OS is Unix-compatible. Can also be sourced from the `MYSQL_ENDPOINT` environment variable.
* `username` - (Required) Username to use to authenticate with the server, can also be sourced from the `MYSQL_USERNAME` environment variable.
* `password` - (Optional) Password for the given user, if that user has a password, can also be sourced from the `MYSQL_PASSWORD` environment variable.
* `proxy` - (Optional) Proxy socks url, can also be sourced from `ALL_PROXY` or `all_proxy` environment variables.
* `tls` - (Optional) The TLS configuration. One of `false`, `true`, or `skip-verify`. Defaults to `false`. Can also be sourced from the `MYSQL_TLS_CONFIG` environment variable.
* `max_conn_lifetime_sec` - (Optional) Sets the maximum amount of time a connection may be reused. If d <= 0, connections are reused forever.
* `max_open_conns` - (Optional) Sets the maximum number of open connections to the database. If n <= 0, then there is no limit on the number of open connections.