Vendor github.com/mittwald/go-powerdns

This commit is contained in:
Tom Limoncelli 2020-06-02 06:53:41 -04:00
parent 968ef8de7b
commit 1d263c4dd6
21 changed files with 475 additions and 0 deletions

1
go.mod
View file

@ -35,6 +35,7 @@ require (
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014 github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014
github.com/philhug/opensrs-go v0.0.0-20171126225031-9dfa7433020d github.com/philhug/opensrs-go v0.0.0-20171126225031-9dfa7433020d
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/psampaz/go-mod-outdated v0.6.0 // indirect
github.com/renier/xmlrpc v0.0.0-20170708154548-ce4a1a486c03 // indirect github.com/renier/xmlrpc v0.0.0-20170708154548-ce4a1a486c03 // indirect
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff
github.com/sergi/go-diff v1.1.0 // indirect github.com/sergi/go-diff v1.1.0 // indirect

7
go.sum
View file

@ -224,6 +224,9 @@ github.com/malexdev/utfutil v0.0.0-20180510171754-00c8d4a8e7a8 h1:A6SLdFpRzUUF5v
github.com/malexdev/utfutil v0.0.0-20180510171754-00c8d4a8e7a8/go.mod h1:UtpLyb/EupVKXF/N0b4NRe1DNg+QYJsnsHQ038romhM= github.com/malexdev/utfutil v0.0.0-20180510171754-00c8d4a8e7a8/go.mod h1:UtpLyb/EupVKXF/N0b4NRe1DNg+QYJsnsHQ038romhM=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/miekg/dns v1.1.29 h1:xHBEhR+t5RzcFJjBLJlax2daXOrTYtr9z4WdKEfWFzg= github.com/miekg/dns v1.1.29 h1:xHBEhR+t5RzcFJjBLJlax2daXOrTYtr9z4WdKEfWFzg=
github.com/miekg/dns v1.1.29/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/miekg/dns v1.1.29/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
@ -245,6 +248,8 @@ github.com/namedotcom/go v0.0.0-20180403034216-08470befbe04/go.mod h1:5sN+Lt1CaY
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4= github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014 h1:37VE5TYj2m/FLA9SNr4z0+A0JefvTmR60Zwf8XSEV7c= github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014 h1:37VE5TYj2m/FLA9SNr4z0+A0JefvTmR60Zwf8XSEV7c=
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014/go.mod h1:joRatxRJaZBsY3JAOEMcoOp05CnZzsx4scTxi95DHyQ= github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014/go.mod h1:joRatxRJaZBsY3JAOEMcoOp05CnZzsx4scTxi95DHyQ=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
@ -259,6 +264,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/psampaz/go-mod-outdated v0.6.0 h1:DXS6rdsz4rpezbPsckQflqrYSEBvsF5GAmUWP+UvnQo=
github.com/psampaz/go-mod-outdated v0.6.0/go.mod h1:r78NYWd1z+F9Zdsfy70svgXOz363B08BWnTyFSgEESs=
github.com/renier/xmlrpc v0.0.0-20170708154548-ce4a1a486c03 h1:Wdi9nwnhFNAlseAOekn6B5G/+GMtks9UKbvRU/CMM/o= github.com/renier/xmlrpc v0.0.0-20170708154548-ce4a1a486c03 h1:Wdi9nwnhFNAlseAOekn6B5G/+GMtks9UKbvRU/CMM/o=
github.com/renier/xmlrpc v0.0.0-20170708154548-ce4a1a486c03/go.mod h1:gRAiPF5C5Nd0eyyRdqIu9qTiFSoZzpTq727b5B8fkkU= github.com/renier/xmlrpc v0.0.0-20170708154548-ce4a1a486c03/go.mod h1:gRAiPF5C5Nd0eyyRdqIu9qTiFSoZzpTq727b5B8fkkU=
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff h1:+6NUiITWwE5q1KO6SAfUX918c+Tab0+tGAM/mtdlUyA= github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff h1:+6NUiITWwE5q1KO6SAfUX918c+Tab0+tGAM/mtdlUyA=

View file

@ -0,0 +1,13 @@
package zones
import "github.com/mittwald/go-powerdns/pdnshttp"
type client struct {
httpClient *pdnshttp.Client
}
func New(hc *pdnshttp.Client) Client {
return &client{
httpClient: hc,
}
}

View file

@ -0,0 +1,6 @@
// This package contains a specialized client for interacting with PowerDNS' "Zones" API.
//
// More information
//
// Official API documentation: https://doc.powerdns.com/authoritative/http-api/zone.html
package zones

View file

@ -0,0 +1,45 @@
package zones
import "context"
// Client defines the interface for Zone operations.
type Client interface {
// ListZones lists known zones for a given serverID
ListZones(ctx context.Context, serverID string) ([]Zone, error)
// ListZone list known zone for a given serverID and zoneID
ListZone(ctx context.Context, serverID string, zoneID string) ([]Zone, error)
// CreateZone creates a new zone for a given server.
CreateZone(ctx context.Context, serverID string, zone Zone) (*Zone, error)
// GetZone returns an existing zone by ID. If not found, the first returned value
// will be nil, and the error return value will be an instance of "pdnshttp.ErrNotFound".
GetZone(ctx context.Context, serverID string, zoneID string) (*Zone, error)
// DeleteZone deletes a zone. No shit.
DeleteZone(ctx context.Context, serverID string, zoneID string) error
// AddRecordSetToZone will add a new set of records to a zone. Existing record sets for
// the exact name/type combination will be replaced.
AddRecordSetToZone(ctx context.Context, serverID string, zoneID string, set ResourceRecordSet) error
// RemoveRecordSetFromZone removes a record set from a zone. The record set is matched
// by name and type.
RemoveRecordSetFromZone(ctx context.Context, serverID string, zoneID string, name string, recordType string) error
// RetrieveFromMaster retrieves a slave zone from its master
RetrieveFromMaster(ctx context.Context, serverID string, zoneID string) error
// NotifySlaves sends a DNS NOTIFY to all slaves
NotifySlaves(ctx context.Context, serverID string, zoneID string) error
// ExportZone exports the entire zone in AXFR format
ExportZone(ctx context.Context, serverID string, zoneID string) ([]byte, error)
// VerifyZone verifies a zone's configuration
VerifyZone(ctx context.Context, serverID string, zoneID string) error
// RectifyZone rectifies the zone data
RectifyZone(ctx context.Context, serverID string, zoneID string) error
}

View file

@ -0,0 +1,22 @@
package zones
type ResourceRecordSet struct {
Name string `json:"name"`
Type string `json:"type"`
TTL int `json:"ttl"`
ChangeType RecordSetChangeType `json:"changetype,omitempty"`
Records []Record `json:"records"`
Comments []Comment `json:"comments"`
}
type Record struct {
Content string `json:"content"`
Disabled bool `json:"disabled"`
SetPTR bool `json:"set-ptr,omitempty"`
}
type Comment struct {
Content string `json:"content"`
Account string `json:"account"`
ModifiedAt int `json:"modified_at"`
}

View file

@ -0,0 +1,35 @@
package zones
import "fmt"
type RecordSetChangeType int
const (
_ = iota
ChangeTypeDelete RecordSetChangeType = iota
ChangeTypeReplace
)
func (k RecordSetChangeType) MarshalJSON() ([]byte, error) {
switch k {
case ChangeTypeDelete:
return []byte(`"DELETE"`), nil
case ChangeTypeReplace:
return []byte(`"REPLACE"`), nil
default:
return nil, fmt.Errorf("unsupported change type: %d", k)
}
}
func (k *RecordSetChangeType) UnmarshalJSON(input []byte) error {
switch string(input) {
case `"DELETE"`:
*k = ChangeTypeDelete
case `"REPLACE"`:
*k = ChangeTypeReplace
default:
return fmt.Errorf("unsupported change type: %s", string(input))
}
return nil
}

View file

@ -0,0 +1,51 @@
package zones
import "encoding/json"
// ZoneNameservers is a special list type to represent the nameservers of a zone.
// When nil, this type will still serialize to an empty JSON list.
// See https://github.com/mittwald/go-powerdns/issues/4 for more information
type ZoneNameservers []string
// MarshalJSON implements the `json.Marshaler` interface
func (z ZoneNameservers) MarshalJSON() ([]byte, error) {
if z == nil {
return []byte("[]"), nil
}
return json.Marshal([]string(z))
}
type Zone struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Type ZoneType `json:"type"`
URL string `json:"url,omitempty"`
Kind ZoneKind `json:"kind,omitempty"`
ResourceRecordSets []ResourceRecordSet `json:"rrsets,omitempty"`
Serial int `json:"serial,omitempty"`
NotifiedSerial int `json:"notified_serial,omitempty"`
Masters []string `json:"masters,omitempty"`
DNSSec bool `json:"dnssec,omitempty"`
NSec3Param string `json:"nsec3param,omitempty"`
NSec3Narrow bool `json:"nsec3narrow,omitempty"`
Presigned bool `json:"presigned,omitempty"`
SOAEdit string `json:"soa_edit,omitempty"`
SOAEditAPI string `json:"soa_edit_api,omitempty"`
APIRectify bool `json:"api_rectify,omitempty"`
Zone string `json:"zone,omitempty"`
Account string `json:"account,omitempty"`
Nameservers ZoneNameservers `json:"nameservers"`
TSIGMasterKeyIDs []string `json:"tsig_master_key_ids,omitempty"`
TSIGSlaveKeyIDs []string `json:"tsig_slave_key_ids,omitempty"`
}
func (z *Zone) GetRecordSet(name, recordType string) *ResourceRecordSet {
for i := range z.ResourceRecordSets {
if z.ResourceRecordSets[i].Name == name && z.ResourceRecordSets[i].Type == recordType {
return &z.ResourceRecordSets[i]
}
}
return nil
}

View file

@ -0,0 +1,40 @@
package zones
import "fmt"
type ZoneKind int
const (
_ = iota
ZoneKindNative ZoneKind = iota
ZoneKindMaster
ZoneKindSlave
)
func (k ZoneKind) MarshalJSON() ([]byte, error) {
switch k {
case ZoneKindNative:
return []byte(`"Native"`), nil
case ZoneKindMaster:
return []byte(`"Master"`), nil
case ZoneKindSlave:
return []byte(`"Slave"`), nil
default:
return nil, fmt.Errorf("unsupported zone kind: %d", k)
}
}
func (k *ZoneKind) UnmarshalJSON(input []byte) error {
switch string(input) {
case `"Native"`:
*k = ZoneKindNative
case `"Master"`:
*k = ZoneKindMaster
case `"Slave"`:
*k = ZoneKindSlave
default:
return fmt.Errorf("unsupported zone kind: %s", string(input))
}
return nil
}

View file

@ -0,0 +1,29 @@
package zones
import "fmt"
type ZoneType int
const (
ZoneTypeZone ZoneType = iota
)
func (k ZoneType) MarshalJSON() ([]byte, error) {
switch k {
case ZoneTypeZone:
return []byte(`"Zone"`), nil
default:
return nil, fmt.Errorf("unsupported zone type: %d", k)
}
}
func (k *ZoneType) UnmarshalJSON(input []byte) error {
switch string(input) {
case `"Zone"`:
*k = ZoneTypeZone
default:
return fmt.Errorf("unsupported zone type: %s", string(input))
}
return nil
}

View file

@ -0,0 +1,21 @@
package zones
import (
"context"
"fmt"
"github.com/mittwald/go-powerdns/pdnshttp"
"net/url"
)
func (c *client) AddRecordSetToZone(ctx context.Context, serverID string, zoneID string, set ResourceRecordSet) error {
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s", url.PathEscape(serverID), url.PathEscape(zoneID))
set.ChangeType = ChangeTypeReplace
patch := Zone{
ResourceRecordSets: []ResourceRecordSet{
set,
},
}
return c.httpClient.Patch(ctx, path, nil, pdnshttp.WithJSONRequestBody(&patch))
}

View file

@ -0,0 +1,28 @@
package zones
import (
"context"
"fmt"
"net/url"
"github.com/mittwald/go-powerdns/pdnshttp"
)
func (c *client) CreateZone(ctx context.Context, serverID string, zone Zone) (*Zone, error) {
created := Zone{}
path := fmt.Sprintf("/api/v1/servers/%s/zones", url.PathEscape(serverID))
zone.ID = ""
zone.Type = ZoneTypeZone
if zone.Kind == 0 {
zone.Kind = ZoneKindNative
}
err := c.httpClient.Post(ctx, path, &created, pdnshttp.WithJSONRequestBody(&zone))
if err != nil {
return nil, err
}
return &created, nil
}

View file

@ -0,0 +1,13 @@
package zones
import (
"context"
"fmt"
"net/url"
)
func (c *client) DeleteZone(ctx context.Context, serverID string, zoneID string) error {
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s", url.PathEscape(serverID), url.PathEscape(zoneID))
return c.httpClient.Delete(ctx, path, nil)
}

View file

@ -0,0 +1,28 @@
package zones
import (
"bytes"
"context"
"fmt"
"github.com/mittwald/go-powerdns/pdnshttp"
"net/http"
"net/url"
)
func (c *client) ExportZone(ctx context.Context, serverID, zoneID string) ([]byte, error) {
output := bytes.Buffer{}
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s/export", url.PathEscape(serverID), url.PathEscape(zoneID))
err := c.httpClient.Get(ctx, path, &output)
if err != nil {
if e, ok := err.(pdnshttp.ErrUnexpectedStatus); ok {
if e.StatusCode == http.StatusUnprocessableEntity {
return nil, pdnshttp.ErrNotFound{}
}
}
return nil, err
}
return output.Bytes(), nil
}

View file

@ -0,0 +1,27 @@
package zones
import (
"context"
"fmt"
"github.com/mittwald/go-powerdns/pdnshttp"
"net/http"
"net/url"
)
func (c *client) GetZone(ctx context.Context, serverID, zoneID string) (*Zone, error) {
zone := Zone{}
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s", url.PathEscape(serverID), url.PathEscape(zoneID))
err := c.httpClient.Get(ctx, path, &zone)
if err != nil {
if e, ok := err.(pdnshttp.ErrUnexpectedStatus); ok {
if e.StatusCode == http.StatusUnprocessableEntity {
return nil, pdnshttp.ErrNotFound{}
}
}
return nil, err
}
return &zone, nil
}

View file

@ -0,0 +1,33 @@
package zones
import (
"context"
"fmt"
"net/url"
"github.com/mittwald/go-powerdns/pdnshttp"
)
func (c *client) ListZones(ctx context.Context, serverID string) ([]Zone, error) {
zones := make([]Zone, 0)
path := fmt.Sprintf("/api/v1/servers/%s/zones", url.PathEscape(serverID))
err := c.httpClient.Get(ctx, path, &zones)
if err != nil {
return nil, err
}
return zones, nil
}
func (c *client) ListZone(ctx context.Context, serverID string, zoneID string) ([]Zone, error) {
zones := make([]Zone, 0)
path := fmt.Sprintf("/api/v1/servers/%s/zones", url.PathEscape(serverID))
err := c.httpClient.Get(ctx, path, &zones, pdnshttp.WithQueryValue("zone", zoneID))
if err != nil {
return nil, err
}
return zones, nil
}

View file

@ -0,0 +1,13 @@
package zones
import (
"context"
"fmt"
"net/url"
)
func (c *client) NotifySlaves(ctx context.Context, serverID string, zoneID string) error {
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s/notify", url.PathEscape(serverID), url.PathEscape(zoneID))
return c.httpClient.Put(ctx, path, nil)
}

View file

@ -0,0 +1,13 @@
package zones
import (
"context"
"fmt"
"net/url"
)
func (c *client) RectifyZone(ctx context.Context, serverID string, zoneID string) error {
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s/rectify", url.PathEscape(serverID), url.PathEscape(zoneID))
return c.httpClient.Put(ctx, path, nil)
}

View file

@ -0,0 +1,24 @@
package zones
import (
"context"
"fmt"
"github.com/mittwald/go-powerdns/pdnshttp"
"net/url"
)
func (c *client) RemoveRecordSetFromZone(ctx context.Context, serverID string, zoneID string, name string, recordType string) error {
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s", url.PathEscape(serverID), url.PathEscape(zoneID))
set := ResourceRecordSet{
Name: name,
Type: recordType,
ChangeType: ChangeTypeDelete,
}
patch := Zone{
ResourceRecordSets: []ResourceRecordSet{set},
}
return c.httpClient.Patch(ctx, path, nil, pdnshttp.WithJSONRequestBody(&patch))
}

View file

@ -0,0 +1,13 @@
package zones
import (
"context"
"fmt"
"net/url"
)
func (c *client) RetrieveFromMaster(ctx context.Context, serverID string, zoneID string) error {
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s/axfr-retrieve", url.PathEscape(serverID), url.PathEscape(zoneID))
return c.httpClient.Put(ctx, path, nil)
}

View file

@ -0,0 +1,13 @@
package zones
import (
"context"
"fmt"
"net/url"
)
func (c *client) VerifyZone(ctx context.Context, serverID string, zoneID string) error {
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s/check", url.PathEscape(serverID), url.PathEscape(zoneID))
return c.httpClient.Get(ctx, path, nil)
}