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

Commit b754b59

Browse files
authored
Merge pull request #102 from Polyconseil/tba-add-proxy-argument
Add proxy argument
2 parents 54055f9 + 5076acf commit b754b59

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

mysql/provider.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"database/sql"
55
"fmt"
66
"net"
7+
"net/url"
8+
"regexp"
79
"strings"
810
"time"
911

@@ -58,6 +60,16 @@ func Provider() terraform.ResourceProvider {
5860
DefaultFunc: schema.EnvDefaultFunc("MYSQL_PASSWORD", nil),
5961
},
6062

63+
"proxy": {
64+
Type: schema.TypeString,
65+
Optional: true,
66+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
67+
"ALL_PROXY",
68+
"all_proxy",
69+
}, nil),
70+
ValidateFunc: validation.StringMatch(regexp.MustCompile("^socks5h?://.*:\\d+$"), "The proxy URL is not a valid socks url."),
71+
},
72+
6173
"tls": {
6274
Type: schema.TypeString,
6375
Optional: true,
@@ -118,7 +130,11 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
118130
AllowCleartextPasswords: d.Get("authentication_plugin").(string) == cleartextPasswords,
119131
}
120132

121-
dialer := proxy.FromEnvironment()
133+
dialer, err := makeDialer(d)
134+
if err != nil {
135+
return nil, err
136+
}
137+
122138
mysql.RegisterDial("tcp", func(network string) (net.Conn, error) {
123139
return dialer.Dial("tcp", network)
124140
})
@@ -132,6 +148,26 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
132148

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

151+
func makeDialer(d *schema.ResourceData) (proxy.Dialer, error) {
152+
proxyFromEnv := proxy.FromEnvironment()
153+
proxyArg := d.Get("proxy").(string)
154+
155+
if len(proxyArg) > 0 {
156+
proxyURL, err := url.Parse(proxyArg)
157+
if err != nil {
158+
return nil, err
159+
}
160+
proxy, err := proxy.FromURL(proxyURL, proxyFromEnv)
161+
if err != nil {
162+
return nil, err
163+
}
164+
165+
return proxy, nil
166+
}
167+
168+
return proxyFromEnv, nil
169+
}
170+
135171
func quoteIdentifier(in string) string {
136172
return fmt.Sprintf("`%s`", identQuoteReplacer.Replace(in))
137173
}

website/docs/index.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ The following arguments are supported:
7979
* `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.
8080
* `username` - (Required) Username to use to authenticate with the server, can also be sourced from the `MYSQL_USERNAME` environment variable.
8181
* `password` - (Optional) Password for the given user, if that user has a password, can also be sourced from the `MYSQL_PASSWORD` environment variable.
82+
* `proxy` - (Optional) Proxy socks url, can also be sourced from `ALL_PROXY` or `all_proxy` environment variables.
8283
* `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.
8384
* `max_conn_lifetime_sec` - (Optional) Sets the maximum amount of time a connection may be reused. If d <= 0, connections are reused forever.
8485
* `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 commit comments

Comments
 (0)