Skip to content

Commit 0ac4ea4

Browse files
author
andrey.menzhinskiy
committed
Fixed remarks from @mbag
1 parent 4194aab commit 0ac4ea4

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

powerdns/client.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9+
"log"
910
"net/http"
1011
"net/url"
1112
"strconv"
@@ -31,10 +32,11 @@ type Client struct {
3132
HTTP *http.Client
3233
CacheEnable bool // Enable/Disable chache for REST API requests
3334
Cache *freecache.Cache
35+
CacheTTL int
3436
}
3537

3638
// NewClient returns a new PowerDNS client
37-
func NewClient(serverURL string, apiKey string, configTLS *tls.Config, cacheEnable bool, cacheSizeMB string) (*Client, error) {
39+
func NewClient(serverURL string, apiKey string, configTLS *tls.Config, cacheEnable bool, cacheSizeMB string, cacheTTL int) (*Client, error) {
3840

3941
cleanURL, err := sanitizeURL(serverURL)
4042

@@ -60,6 +62,7 @@ func NewClient(serverURL string, apiKey string, configTLS *tls.Config, cacheEnab
6062
APIVersion: -1,
6163
CacheEnable: cacheEnable,
6264
Cache: freecache.NewCache(DefaultCacheSize),
65+
CacheTTL: cacheTTL,
6366
}
6467

6568
if err := client.setServerVersion(); err != nil {
@@ -440,20 +443,27 @@ func (client *Client) DeleteZone(name string) error {
440443
func (client *Client) GetZoneInfoFromCache(zone string) (*ZoneInfo, error) {
441444
if client.CacheEnable {
442445
cacheZoneInfo, err := client.Cache.Get([]byte(zone))
443-
444446
if err != nil {
445447
return nil, err
446448
}
449+
447450
zoneInfo := new(ZoneInfo)
448-
json.Unmarshal(cacheZoneInfo, &zoneInfo)
451+
err = json.Unmarshal(cacheZoneInfo, &zoneInfo)
452+
if err != nil {
453+
return nil, err
454+
}
455+
449456
return zoneInfo, err
450457
}
451458
return nil, nil
452459
}
453460

454461
// ListRecords returns all records in Zone
455462
func (client *Client) ListRecords(zone string) ([]Record, error) {
456-
zoneInfo, _ := client.GetZoneInfoFromCache(zone)
463+
zoneInfo, err := client.GetZoneInfoFromCache(zone)
464+
if err != nil {
465+
log.Printf("[WARN] module.freecache: %s: %s", zone, err)
466+
}
457467

458468
if zoneInfo == nil {
459469
req, err := client.newRequest("GET", fmt.Sprintf("/servers/localhost/zones/%s", zone), nil)
@@ -478,8 +488,8 @@ func (client *Client) ListRecords(zone string) ([]Record, error) {
478488
if err != nil {
479489
return nil, err
480490
}
481-
// Default cache ttl is 30 sec, I think that is enough
482-
err = client.Cache.Set([]byte(zone), cacheValue, 30)
491+
492+
err = client.Cache.Set([]byte(zone), cacheValue, client.CacheTTL)
483493
if err != nil {
484494
return nil, fmt.Errorf("The cache for REST API requests is enabled but the size isn't enough: cacheSize: %db \n %s",
485495
DefaultCacheSize, err)

powerdns/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Config struct {
1717
CACertificate string
1818
CacheEnable bool
1919
CacheMemorySize string
20+
CacheTTL int
2021
}
2122

2223
// Client returns a new client for accessing PowerDNS
@@ -38,7 +39,7 @@ func (c *Config) Client() (*Client, error) {
3839

3940
tlsConfig.InsecureSkipVerify = c.InsecureHTTPS
4041

41-
client, err := NewClient(c.ServerURL, c.APIKey, tlsConfig, c.CacheEnable, c.CacheMemorySize)
42+
client, err := NewClient(c.ServerURL, c.APIKey, tlsConfig, c.CacheEnable, c.CacheMemorySize, c.CacheTTL)
4243

4344
if err != nil {
4445
return nil, fmt.Errorf("Error setting up PowerDNS client: %s", err)

powerdns/provider.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ func Provider() terraform.ResourceProvider {
4343
Type: schema.TypeString,
4444
Optional: true,
4545
DefaultFunc: schema.EnvDefaultFunc("PDNS_CACHE_MEM_SIZE", "100"),
46-
Description: "Set cache memory size in Mb",
46+
Description: "Set cache memory size in MB",
47+
},
48+
"cache_ttl": {
49+
Type: schema.TypeInt,
50+
Optional: true,
51+
DefaultFunc: schema.EnvDefaultFunc("PDNS_CACHE_TTL", 30),
52+
Description: "Set cache TTL in seconds",
4753
},
4854
},
4955

@@ -64,6 +70,7 @@ func providerConfigure(data *schema.ResourceData) (interface{}, error) {
6470
CACertificate: data.Get("ca_certificate").(string),
6571
CacheEnable: data.Get("cache_requests").(bool),
6672
CacheMemorySize: data.Get("cache_mem_size").(string),
73+
CacheTTL: data.Get("cache_ttl").(int),
6774
}
6875

6976
return config.Client()

website/docs/index.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ The following arguments are supported:
4040
* `server_url` - (Required) The address of PowerDNS server. This can also be specified with `PDNS_SERVER_URL` environment variable. When no schema is provided, the default is `https`.
4141
* `ca_certificate` - (Optional) A valid path of a Root CA Certificate in PEM format _or_ the content of a Root CA certificate in PEM format. This can also be specified with `PDNS_CACERT` environment variable.
4242
* `insecure_https` - (Optional) Set this to `true` to disable verification of the PowerDNS server's TLS certificate. This can also be specified with the `PDNS_INSECURE_HTTPS` environment variable.
43-
* `cache_requests` - (Optional) Set this to `true` to enable cache of the PowerDNS REST API requests. This can also be specified with the `PDNS_CACHE_REQUESTS` environment variable.
43+
* `cache_requests` - (Optional) Set this to `true` to enable cache of the PowerDNS REST API requests. This can also be specified with the `PDNS_CACHE_REQUESTS` environment variable. `WARNING! Enabling this option can lead to the use of stale records when you use other automation to populate the DNS zone records at the same time.`
4444
* `cache_mem_size` - (Optional) Memory size in MB for a cache of the PowerDNS REST API requests. This can also be specified with the `PDNS_CACHE_MEM_SIZE` environment variable.

0 commit comments

Comments
 (0)