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

Commit 24cb978

Browse files
author
Thierno BARRY
committed
add proxy arguments for provider
1 parent 1892127 commit 24cb978

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

mysql/provider.go

Lines changed: 33 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

@@ -53,6 +55,12 @@ func Provider() terraform.ResourceProvider {
5355
DefaultFunc: schema.EnvDefaultFunc("MYSQL_PASSWORD", nil),
5456
},
5557

58+
"proxy": {
59+
Type: schema.TypeString,
60+
Optional: true,
61+
ValidateFunc: validation.StringMatch(regexp.MustCompile("^socks5h?://.*:\\d+$"), "The proxy URL is not a valid socks url."),
62+
},
63+
5664
"tls": {
5765
Type: schema.TypeString,
5866
Optional: true,
@@ -105,7 +113,11 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
105113
AllowNativePasswords: true,
106114
}
107115

108-
dialer := proxy.FromEnvironment()
116+
dialer, err := getDialerFromArgOrFromEnv(d)
117+
if err != nil {
118+
return nil, err
119+
}
120+
109121
mysql.RegisterDial("tcp", func(network string) (net.Conn, error) {
110122
return dialer.Dial("tcp", network)
111123
})
@@ -119,6 +131,26 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
119131

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

134+
func getDialerFromArgOrFromEnv(d *schema.ResourceData) (proxy.Dialer, error) {
135+
proxyFromEnv := proxy.FromEnvironment()
136+
proxyArg := d.Get("proxy").(string)
137+
138+
if len(proxyArg) > 0 {
139+
proxyURL, err := url.Parse(proxyArg)
140+
if err != nil {
141+
return nil, err
142+
}
143+
proxy, err := proxy.FromURL(proxyURL, proxyFromEnv)
144+
if err != nil {
145+
return nil, err
146+
}
147+
148+
return proxy, nil
149+
}
150+
151+
return proxyFromEnv, nil
152+
}
153+
122154
func quoteIdentifier(in string) string {
123155
return fmt.Sprintf("`%s`", identQuoteReplacer.Replace(in))
124156
}

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. This superseed `ALL_PROXY` and/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)