2015-12-06 19:20:43 +01:00
|
|
|
package powerdns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-01-25 19:50:58 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-12-06 19:20:43 +01:00
|
|
|
"io"
|
2016-01-25 19:50:58 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-07-28 18:01:06 +02:00
|
|
|
"strconv"
|
2015-12-06 19:20:43 +01:00
|
|
|
"strings"
|
2016-01-25 19:50:58 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2015-12-06 19:20:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2016-07-28 18:01:06 +02:00
|
|
|
ServerUrl string // Location of PowerDNS server to use
|
|
|
|
ApiKey string // REST API Static authentication key
|
|
|
|
ApiVersion int // API version to use
|
|
|
|
Http *http.Client
|
2015-12-06 19:20:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new PowerDNS client
|
|
|
|
func NewClient(serverUrl string, apiKey string) (*Client, error) {
|
|
|
|
client := Client{
|
2016-01-25 19:50:58 +01:00
|
|
|
ServerUrl: serverUrl,
|
|
|
|
ApiKey: apiKey,
|
|
|
|
Http: cleanhttp.DefaultClient(),
|
2015-12-06 19:20:43 +01:00
|
|
|
}
|
2016-07-28 18:01:06 +02:00
|
|
|
var err error
|
|
|
|
client.ApiVersion, err = client.detectApiVersion()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-06 19:20:43 +01:00
|
|
|
return &client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new request with necessary headers
|
|
|
|
func (c *Client) newRequest(method string, endpoint string, body []byte) (*http.Request, error) {
|
|
|
|
|
2016-07-28 18:01:06 +02:00
|
|
|
var urlStr string
|
|
|
|
if c.ApiVersion > 0 {
|
|
|
|
urlStr = c.ServerUrl + "/api/v" + strconv.Itoa(c.ApiVersion) + endpoint
|
|
|
|
} else {
|
|
|
|
urlStr = c.ServerUrl + endpoint
|
|
|
|
}
|
|
|
|
url, err := url.Parse(urlStr)
|
2015-12-06 19:20:43 +01:00
|
|
|
if err != nil {
|
2016-07-28 18:01:06 +02:00
|
|
|
return nil, fmt.Errorf("Error during parsing request URL: %s", err)
|
2015-12-06 19:20:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var bodyReader io.Reader
|
|
|
|
if body != nil {
|
|
|
|
bodyReader = bytes.NewReader(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, url.String(), bodyReader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error during creation of request: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("X-API-Key", c.ApiKey)
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
|
|
|
|
if method != "GET" {
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ZoneInfo struct {
|
2016-07-28 18:01:06 +02:00
|
|
|
Id string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
DnsSec bool `json:"dnsssec"`
|
|
|
|
Serial int64 `json:"serial"`
|
|
|
|
Records []Record `json:"records,omitempty"`
|
|
|
|
ResourceRecordSets []ResourceRecordSet `json:"rrsets,omitempty"`
|
2015-12-06 19:20:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Record struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Content string `json:"content"`
|
2016-07-28 18:01:06 +02:00
|
|
|
TTL int `json:"ttl"` // For API v0
|
2016-01-25 19:50:58 +01:00
|
|
|
Disabled bool `json:"disabled"`
|
2015-12-06 19:20:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceRecordSet struct {
|
2016-01-25 19:50:58 +01:00
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
ChangeType string `json:"changetype"`
|
2016-07-28 18:01:06 +02:00
|
|
|
TTL int `json:"ttl"` // For API v1
|
2015-12-06 19:20:43 +01:00
|
|
|
Records []Record `json:"records,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type zonePatchRequest struct {
|
|
|
|
RecordSets []ResourceRecordSet `json:"rrsets"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type errorResponse struct {
|
|
|
|
ErrorMsg string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2016-02-20 11:08:43 +01:00
|
|
|
const idSeparator string = ":::"
|
|
|
|
|
2015-12-06 19:20:43 +01:00
|
|
|
func (record *Record) Id() string {
|
2016-02-20 11:08:43 +01:00
|
|
|
return record.Name + idSeparator + record.Type
|
2015-12-06 19:20:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rrSet *ResourceRecordSet) Id() string {
|
2016-02-20 11:08:43 +01:00
|
|
|
return rrSet.Name + idSeparator + rrSet.Type
|
2015-12-06 19:20:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns name and type of record or record set based on it's ID
|
|
|
|
func parseId(recId string) (string, string, error) {
|
2016-02-20 11:08:43 +01:00
|
|
|
s := strings.Split(recId, idSeparator)
|
2016-01-25 19:50:58 +01:00
|
|
|
if len(s) == 2 {
|
2015-12-06 19:20:43 +01:00
|
|
|
return s[0], s[1], nil
|
|
|
|
} else {
|
|
|
|
return "", "", fmt.Errorf("Unknown record ID format")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 18:01:06 +02:00
|
|
|
// Detects the API version in use on the server
|
|
|
|
// Uses int to represent the API version: 0 is the legacy AKA version 3.4 API
|
|
|
|
// Any other integer correlates with the same API version
|
|
|
|
func (client *Client) detectApiVersion() (int, error) {
|
|
|
|
req, err := client.newRequest("GET", "/api/v1/servers", nil)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
resp, err := client.Http.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode == 200 {
|
|
|
|
return 1, nil
|
|
|
|
} else {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-06 19:20:43 +01:00
|
|
|
// Returns all Zones of server, without records
|
|
|
|
func (client *Client) ListZones() ([]ZoneInfo, error) {
|
|
|
|
|
|
|
|
req, err := client.newRequest("GET", "/servers/localhost/zones", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Http.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var zoneInfos []ZoneInfo
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&zoneInfos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return zoneInfos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns all records in Zone
|
|
|
|
func (client *Client) ListRecords(zone string) ([]Record, error) {
|
|
|
|
req, err := client.newRequest("GET", fmt.Sprintf("/servers/localhost/zones/%s", zone), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Http.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
zoneInfo := new(ZoneInfo)
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(zoneInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-07-28 18:01:06 +02:00
|
|
|
records := zoneInfo.Records
|
|
|
|
// Convert the API v1 response to v0 record structure
|
|
|
|
for _, rrs := range zoneInfo.ResourceRecordSets {
|
|
|
|
for _, record := range rrs.Records {
|
|
|
|
records = append(records, Record{
|
|
|
|
Name: rrs.Name,
|
|
|
|
Type: rrs.Type,
|
|
|
|
Content: record.Content,
|
|
|
|
TTL: rrs.TTL,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
2015-12-06 19:20:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns only records of specified name and type
|
|
|
|
func (client *Client) ListRecordsInRRSet(zone string, name string, tpe string) ([]Record, error) {
|
|
|
|
allRecords, err := client.ListRecords(zone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
records := make([]Record, 0, 10)
|
|
|
|
for _, r := range allRecords {
|
|
|
|
if r.Name == name && r.Type == tpe {
|
|
|
|
records = append(records, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) ListRecordsByID(zone string, recId string) ([]Record, error) {
|
|
|
|
name, tpe, err := parseId(recId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return client.ListRecordsInRRSet(zone, name, tpe)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if requested record exists in Zone
|
|
|
|
func (client *Client) RecordExists(zone string, name string, tpe string) (bool, error) {
|
|
|
|
allRecords, err := client.ListRecords(zone)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, record := range allRecords {
|
|
|
|
if record.Name == name && record.Type == tpe {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if requested record exists in Zone by it's ID
|
|
|
|
func (client *Client) RecordExistsByID(zone string, recId string) (bool, error) {
|
|
|
|
name, tpe, err := parseId(recId)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
} else {
|
|
|
|
return client.RecordExists(zone, name, tpe)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates new record with single content entry
|
|
|
|
func (client *Client) CreateRecord(zone string, record Record) (string, error) {
|
|
|
|
reqBody, _ := json.Marshal(zonePatchRequest{
|
|
|
|
RecordSets: []ResourceRecordSet{
|
2016-01-25 19:50:58 +01:00
|
|
|
{
|
|
|
|
Name: record.Name,
|
|
|
|
Type: record.Type,
|
2015-12-06 19:20:43 +01:00
|
|
|
ChangeType: "REPLACE",
|
2016-01-25 19:50:58 +01:00
|
|
|
Records: []Record{record},
|
2015-12-06 19:20:43 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
req, err := client.newRequest("PATCH", fmt.Sprintf("/servers/localhost/zones/%s", zone), reqBody)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Http.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-07-28 18:01:06 +02:00
|
|
|
if resp.StatusCode != 200 && resp.StatusCode != 204 {
|
2015-12-06 19:20:43 +01:00
|
|
|
errorResp := new(errorResponse)
|
|
|
|
if err = json.NewDecoder(resp.Body).Decode(errorResp); err != nil {
|
|
|
|
return "", fmt.Errorf("Error creating record: %s", record.Id())
|
|
|
|
} else {
|
|
|
|
return "", fmt.Errorf("Error creating record: %s, reason: %q", record.Id(), errorResp.ErrorMsg)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return record.Id(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates new record set in Zone
|
|
|
|
func (client *Client) ReplaceRecordSet(zone string, rrSet ResourceRecordSet) (string, error) {
|
|
|
|
rrSet.ChangeType = "REPLACE"
|
|
|
|
|
|
|
|
reqBody, _ := json.Marshal(zonePatchRequest{
|
2016-01-25 19:50:58 +01:00
|
|
|
RecordSets: []ResourceRecordSet{rrSet},
|
2015-12-06 19:20:43 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
req, err := client.newRequest("PATCH", fmt.Sprintf("/servers/localhost/zones/%s", zone), reqBody)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Http.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-07-28 18:01:06 +02:00
|
|
|
if resp.StatusCode != 200 && resp.StatusCode != 204 {
|
2015-12-06 19:20:43 +01:00
|
|
|
errorResp := new(errorResponse)
|
|
|
|
if err = json.NewDecoder(resp.Body).Decode(errorResp); err != nil {
|
|
|
|
return "", fmt.Errorf("Error creating record set: %s", rrSet.Id())
|
|
|
|
} else {
|
|
|
|
return "", fmt.Errorf("Error creating record set: %s, reason: %q", rrSet.Id(), errorResp.ErrorMsg)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return rrSet.Id(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes record set from Zone
|
|
|
|
func (client *Client) DeleteRecordSet(zone string, name string, tpe string) error {
|
|
|
|
reqBody, _ := json.Marshal(zonePatchRequest{
|
|
|
|
RecordSets: []ResourceRecordSet{
|
2016-01-25 19:50:58 +01:00
|
|
|
{
|
|
|
|
Name: name,
|
|
|
|
Type: tpe,
|
2015-12-06 19:20:43 +01:00
|
|
|
ChangeType: "DELETE",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
req, err := client.newRequest("PATCH", fmt.Sprintf("/servers/localhost/zones/%s", zone), reqBody)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Http.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-07-28 18:01:06 +02:00
|
|
|
if resp.StatusCode != 200 && resp.StatusCode != 204 {
|
2015-12-06 19:20:43 +01:00
|
|
|
errorResp := new(errorResponse)
|
|
|
|
if err = json.NewDecoder(resp.Body).Decode(errorResp); err != nil {
|
|
|
|
return fmt.Errorf("Error deleting record: %s %s", name, tpe)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Error deleting record: %s %s, reason: %q", name, tpe, errorResp.ErrorMsg)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes record from Zone by it's ID
|
|
|
|
func (client *Client) DeleteRecordSetByID(zone string, recId string) error {
|
|
|
|
name, tpe, err := parseId(recId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return client.DeleteRecordSet(zone, name, tpe)
|
|
|
|
}
|
2016-01-25 19:50:58 +01:00
|
|
|
}
|