Skip to content

Commit 7b2b7e6

Browse files
committed
review
1 parent 133b820 commit 7b2b7e6

File tree

5 files changed

+27
-35
lines changed

5 files changed

+27
-35
lines changed

providers/dns/pdns/internal/client.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ type Client struct {
3030
}
3131

3232
// NewClient creates a new Client.
33-
func NewClient(host *url.URL, serverName string, apiKey string) *Client {
33+
func NewClient(host *url.URL, serverName string, apiVersion int, apiKey string) *Client {
3434
return &Client{
3535
serverName: serverName,
3636
apiKey: apiKey,
37+
apiVersion: apiVersion,
3738
Host: host,
3839
HTTPClient: &http.Client{Timeout: 5 * time.Second},
3940
}
@@ -43,10 +44,6 @@ func (c *Client) APIVersion() int {
4344
return c.apiVersion
4445
}
4546

46-
func (c *Client) SetCustomAPIVersion(version int) {
47-
c.apiVersion = version
48-
}
49-
5047
func (c *Client) SetAPIVersion(ctx context.Context) error {
5148
var err error
5249

providers/dns/pdns/internal/client_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func setupTest(t *testing.T, method, pattern string, status int, file string) *C
5757

5858
serverURL, _ := url.Parse(server.URL)
5959

60-
client := NewClient(serverURL, "server", "secret")
60+
client := NewClient(serverURL, "server", 0, "secret")
6161
client.HTTPClient = server.Client()
6262

6363
return client
@@ -151,8 +151,7 @@ func TestClient_joinPath(t *testing.T) {
151151
host, err := url.Parse(test.baseURL)
152152
require.NoError(t, err)
153153

154-
client := NewClient(host, "test", "secret")
155-
client.apiVersion = test.apiVersion
154+
client := NewClient(host, "test", test.apiVersion, "secret")
156155

157156
endpoint := client.joinPath(test.uri)
158157

providers/dns/pdns/pdns.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
EnvAPIURL = envNamespace + "API_URL"
2424

2525
EnvTTL = envNamespace + "TTL"
26-
EnvCustomAPIVersion = envNamespace + "CUSTOM_API_VERSION"
26+
EnvAPIVersion = envNamespace + "API_VERSION"
2727
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
2828
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
2929
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
@@ -35,21 +35,21 @@ type Config struct {
3535
APIKey string
3636
Host *url.URL
3737
ServerName string
38+
APIVersion int
3839
PropagationTimeout time.Duration
3940
PollingInterval time.Duration
4041
TTL int
41-
CustomAPIVersion int
4242
HTTPClient *http.Client
4343
}
4444

4545
// NewDefaultConfig returns a default configuration for the DNSProvider.
4646
func NewDefaultConfig() *Config {
4747
return &Config{
48+
ServerName: env.GetOrDefaultString(EnvServerName, "localhost"),
49+
APIVersion: env.GetOrDefaultInt(EnvAPIVersion, 0),
4850
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
49-
CustomAPIVersion: env.GetOrDefaultInt(EnvCustomAPIVersion, 0),
5051
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second),
5152
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
52-
ServerName: env.GetOrDefaultString(EnvServerName, "localhost"),
5353
HTTPClient: &http.Client{
5454
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
5555
},
@@ -97,12 +97,9 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
9797
return nil, errors.New("pdns: API URL missing")
9898
}
9999

100-
client := internal.NewClient(config.Host, config.ServerName, config.APIKey)
100+
client := internal.NewClient(config.Host, config.ServerName, config.APIVersion, config.APIKey)
101101

102-
if config.CustomAPIVersion > 0 {
103-
client.SetCustomAPIVersion(config.CustomAPIVersion)
104-
log.Infof("pdns: using custom API version %d", config.CustomAPIVersion)
105-
} else {
102+
if config.APIVersion <= 0 {
106103
err := client.SetAPIVersion(context.Background())
107104
if err != nil {
108105
log.Warnf("pdns: failed to get API version %v", err)

providers/dns/pdns/pdns.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ Tested and confirmed to work with PowerDNS authoritative server 3.4.8 and 4.0.1.
1818
PowerDNS Notes:
1919
- PowerDNS API does not currently support SSL, therefore you should take care to ensure that traffic between lego and the PowerDNS API is over a trusted network, VPN etc.
2020
- In order to have the SOA serial automatically increment each time the `_acme-challenge` record is added/modified via the API, set `SOA-EDIT-API` to `INCEPTION-INCREMENT` for the zone in the `domainmetadata` table
21-
- Some PowerDNS servers doesn't have root API endpoints enabled and API version autodetection will not work. In that case version number can be defined using `PDNS_CUSTOM_API_VERSION`.
21+
- Some PowerDNS servers doesn't have root API endpoints enabled and API version autodetection will not work. In that case version number can be defined using `PDNS_API_VERSION`.
2222
'''
2323

2424
[Configuration]
2525
[Configuration.Credentials]
2626
PDNS_API_KEY = "API key"
2727
PDNS_API_URL = "API URL"
2828
[Configuration.Additional]
29+
PDNS_SERVER_NAME = "Name of the server in the URL, 'localhost' by default"
30+
PDNS_API_VERSION = "Skip API version autodetection and use the provided version number."
2931
PDNS_POLLING_INTERVAL = "Time between DNS propagation check"
3032
PDNS_PROPAGATION_TIMEOUT = "Maximum waiting time for DNS propagation"
3133
PDNS_TTL = "The TTL of the TXT record used for the DNS challenge"
3234
PDNS_HTTP_TIMEOUT = "API request timeout"
33-
PDNS_SERVER_NAME = "Name of the server in the URL, 'localhost' by default"
34-
PDNS_CUSTOM_API_VERSION = "Skip API version autodetection and use custom version number."
3535

3636
[Links]
3737
API = "https://doc.powerdns.com/md/httpapi/README/"

providers/dns/pdns/pdns_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,22 @@ func TestNewDNSProviderConfig(t *testing.T) {
8585
{
8686
desc: "success",
8787
apiKey: "123",
88-
host: func() *url.URL {
89-
u, _ := url.Parse("http://example.com")
90-
return u
91-
}(),
88+
host: mustParse("http://example.com"),
9289
},
9390
{
9491
desc: "success custom API version",
9592
apiKey: "123",
9693
customAPIVersion: 1,
97-
host: func() *url.URL {
98-
u, _ := url.Parse("http://example.com")
99-
return u
100-
}(),
94+
host: mustParse("http://example.com"),
10195
},
10296
{
10397
desc: "missing credentials",
10498
expected: "pdns: API key missing",
10599
},
106100
{
107-
desc: "missing API key",
108-
apiKey: "",
109-
host: func() *url.URL {
110-
u, _ := url.Parse("http://example.com")
111-
return u
112-
}(),
101+
desc: "missing API key",
102+
apiKey: "",
103+
host: mustParse("http://example.com"),
113104
expected: "pdns: API key missing",
114105
},
115106
{
@@ -124,7 +115,7 @@ func TestNewDNSProviderConfig(t *testing.T) {
124115
config := NewDefaultConfig()
125116
config.APIKey = test.apiKey
126117
config.Host = test.host
127-
config.CustomAPIVersion = test.customAPIVersion
118+
config.APIVersion = test.customAPIVersion
128119

129120
p, err := NewDNSProviderConfig(config)
130121

@@ -154,3 +145,11 @@ func TestLivePresentAndCleanup(t *testing.T) {
154145
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
155146
require.NoError(t, err)
156147
}
148+
149+
func mustParse(rawURL string) *url.URL {
150+
u, err := url.Parse(rawURL)
151+
if err != nil {
152+
panic(err)
153+
}
154+
return u
155+
}

0 commit comments

Comments
 (0)