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

Commit

Permalink
add proxy arguments for provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Thierno BARRY committed Nov 4, 2019
1 parent 1892127 commit 24cb978
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
34 changes: 33 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,12 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("MYSQL_PASSWORD", nil),
},

"proxy": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile("^socks5h?://.*:\\d+$"), "The proxy URL is not a valid socks url."),
},

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

dialer := proxy.FromEnvironment()
dialer, err := getDialerFromArgOrFromEnv(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 +131,26 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {

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

func getDialerFromArgOrFromEnv(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. This superseed `ALL_PROXY` and/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.

0 comments on commit 24cb978

Please sign in to comment.