Skip to content

Added functionality to use Powerdns Zone Control #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions powerdns/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ type Client struct {
CacheEnable bool // Enable/Disable chache for REST API requests
Cache *freecache.Cache
CacheTTL int
ZC string
}

// NewClient returns a new PowerDNS client
func NewClient(serverURL string, apiKey string, configTLS *tls.Config, cacheEnable bool, cacheSizeMB string, cacheTTL int) (*Client, error) {
func NewClient(serverURL string, apiKey string, configTLS *tls.Config, cacheEnable bool, cacheSizeMB string, cacheTTL int, zC string) (*Client, error) {

cleanURL, err := sanitizeURL(serverURL)

Expand All @@ -63,6 +64,7 @@ func NewClient(serverURL string, apiKey string, configTLS *tls.Config, cacheEnab
CacheEnable: cacheEnable,
Cache: freecache.NewCache(DefaultCacheSize),
CacheTTL: cacheTTL,
ZC: zC,
}

if err := client.setServerVersion(); err != nil {
Expand Down Expand Up @@ -285,7 +287,7 @@ func (client *Client) detectAPIVersion() (int, error) {

// ListZones returns all Zones of server, without records
func (client *Client) ListZones() ([]ZoneInfo, error) {
req, err := client.newRequest("GET", "/servers/localhost/zones", nil)
req, err := client.newRequest("GET", fmt.Sprintf("/servers/%s/zones", client.ZC), nil)
if err != nil {
return nil, err
}
Expand All @@ -308,7 +310,7 @@ func (client *Client) ListZones() ([]ZoneInfo, error) {

// GetZone gets a zone
func (client *Client) GetZone(name string) (ZoneInfo, error) {
req, err := client.newRequest("GET", fmt.Sprintf("/servers/localhost/zones/%s", name), nil)
req, err := client.newRequest("GET", fmt.Sprintf("/servers/%s/zones/%s", client.ZC, name), nil)
if err != nil {
return ZoneInfo{}, err
}
Expand Down Expand Up @@ -338,7 +340,7 @@ func (client *Client) GetZone(name string) (ZoneInfo, error) {

// ZoneExists checks if requested zone exists
func (client *Client) ZoneExists(name string) (bool, error) {
req, err := client.newRequest("GET", fmt.Sprintf("/servers/localhost/zones/%s", name), nil)
req, err := client.newRequest("GET", fmt.Sprintf("/servers/%s/zones/%s", client.ZC, name), nil)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -367,7 +369,7 @@ func (client *Client) CreateZone(zoneInfo ZoneInfo) (ZoneInfo, error) {
return ZoneInfo{}, err
}

req, err := client.newRequest("POST", "/servers/localhost/zones", body)
req, err := client.newRequest("POST", fmt.Sprintf("/servers/%s/zones", client.ZC), body)
if err != nil {
return ZoneInfo{}, err
}
Expand Down Expand Up @@ -402,7 +404,7 @@ func (client *Client) UpdateZone(name string, zoneInfo ZoneInfoUpd) error {
return err
}

req, err := client.newRequest("PUT", fmt.Sprintf("/servers/localhost/zones/%s", name), body)
req, err := client.newRequest("PUT", fmt.Sprintf("/servers/%s/zones/%s", client.ZC, name), body)
if err != nil {
return err
}
Expand All @@ -426,7 +428,7 @@ func (client *Client) UpdateZone(name string, zoneInfo ZoneInfoUpd) error {

// DeleteZone deletes a zone
func (client *Client) DeleteZone(name string) error {
req, err := client.newRequest("DELETE", fmt.Sprintf("/servers/localhost/zones/%s", name), nil)
req, err := client.newRequest("DELETE", fmt.Sprintf("/servers/%s/zones/%s", client.ZC, name), nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -474,7 +476,7 @@ func (client *Client) ListRecords(zone string) ([]Record, error) {
}

if zoneInfo == nil {
req, err := client.newRequest("GET", fmt.Sprintf("/servers/localhost/zones/%s", zone), nil)
req, err := client.newRequest("GET", fmt.Sprintf("/servers/%s/zones/%s", client.ZC, zone), nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -579,7 +581,7 @@ func (client *Client) ReplaceRecordSet(zone string, rrSet ResourceRecordSet) (st
RecordSets: []ResourceRecordSet{rrSet},
})

req, err := client.newRequest("PATCH", fmt.Sprintf("/servers/localhost/zones/%s", zone), reqBody)
req, err := client.newRequest("PATCH", fmt.Sprintf("/servers/%s/zones/%s", client.ZC, zone), reqBody)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -612,7 +614,7 @@ func (client *Client) DeleteRecordSet(zone string, name string, tpe string) erro
},
})

req, err := client.newRequest("PATCH", fmt.Sprintf("/servers/localhost/zones/%s", zone), reqBody)
req, err := client.newRequest("PATCH", fmt.Sprintf("/servers/%s/zones/%s", client.ZC, zone), reqBody)
if err != nil {
return err
}
Expand Down Expand Up @@ -643,7 +645,7 @@ func (client *Client) DeleteRecordSetByID(zone string, recID string) error {
}

func (client *Client) setServerVersion() error {
req, err := client.newRequest("GET", "/servers/localhost", nil)
req, err := client.newRequest("GET", fmt.Sprintf("/servers/%s", client.ZC), nil)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion powerdns/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
CacheEnable bool
CacheMemorySize string
CacheTTL int
ZC string
}

// Client returns a new client for accessing PowerDNS
Expand All @@ -39,7 +40,7 @@ func (c *Config) Client() (*Client, error) {

tlsConfig.InsecureSkipVerify = c.InsecureHTTPS

client, err := NewClient(c.ServerURL, c.APIKey, tlsConfig, c.CacheEnable, c.CacheMemorySize, c.CacheTTL)
client, err := NewClient(c.ServerURL, c.APIKey, tlsConfig, c.CacheEnable, c.CacheMemorySize, c.CacheTTL, c.ZC)

if err != nil {
return nil, fmt.Errorf("Error setting up PowerDNS client: %s", err)
Expand Down
7 changes: 7 additions & 0 deletions powerdns/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("PDNS_SERVER_URL", nil),
Description: "Location of PowerDNS server",
},
"zone_control": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PDNS_ZONE_CONTROL", "localhost"),
Description: "Location of PowerDNS Zone Control server",
},
"insecure_https": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -66,6 +72,7 @@ func providerConfigure(data *schema.ResourceData) (interface{}, error) {
config := Config{
APIKey: data.Get("api_key").(string),
ServerURL: data.Get("server_url").(string),
ZC: data.Get("zone_control").(string),
InsecureHTTPS: data.Get("insecure_https").(bool),
CACertificate: data.Get("ca_certificate").(string),
CacheEnable: data.Get("cache_requests").(bool),
Expand Down