Skip to content

Commit

Permalink
Configure base path when constructing client (#21)
Browse files Browse the repository at this point in the history
* replace deprecated ioutils.Discard with io.Discard

* feat: only use base path if not specified (fixes #20)

* address pr comments

* Update pdnshttp/client.go

Co-authored-by: Martin Helmich <[email protected]>

* Update pdnshttp/client.go

Co-authored-by: Martin Helmich <[email protected]>

---------

Co-authored-by: Martin Helmich <[email protected]>
  • Loading branch information
oliemansm and martin-helmich authored Jul 3, 2024
1 parent f0c9721 commit 0b5137f
Show file tree
Hide file tree
Showing 31 changed files with 69 additions and 43 deletions.
2 changes: 1 addition & 1 deletion apis/cache/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func (c *client) Flush(ctx context.Context, serverID string, name string) (*FlushResult, error) {
cfr := FlushResult{}
path := fmt.Sprintf("/api/v1/servers/%s/cache/flush", url.PathEscape(serverID))
path := fmt.Sprintf("/servers/%s/cache/flush", url.PathEscape(serverID))

err := c.httpClient.Put(ctx, path, &cfr, pdnshttp.WithQueryValue("domain", name))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion apis/cryptokeys/cryptokey_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func (c *client) CreateCryptokey(ctx context.Context, serverID, zoneID string, opts Cryptokey) (*Cryptokey, error) {
cryptokey := Cryptokey{}
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s/cryptokeys",
path := fmt.Sprintf("/servers/%s/zones/%s/cryptokeys",
url.PathEscape(serverID), url.PathEscape(zoneID))

err := c.httpClient.Post(ctx, path, &cryptokey, pdnshttp.WithJSONRequestBody(opts))
Expand Down
4 changes: 2 additions & 2 deletions apis/cryptokeys/cryptokey_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/mittwald/go-powerdns/pdnshttp"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
"io/ioutil"
"io"
"net/http"
"testing"
)
Expand Down Expand Up @@ -34,7 +34,7 @@ func TestClient_CreateCryptokey(t *testing.T) {
}`)

hc := &http.Client{Transport: gock.DefaultTransport}
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, ioutil.Discard)
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, io.Discard)
cc := New(c)

key, err := cc.CreateCryptokey(context.Background(), "localhost", "pdns-test.de", Cryptokey{})
Expand Down
2 changes: 1 addition & 1 deletion apis/cryptokeys/cryptokey_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (c *client) DeleteCryptokey(ctx context.Context, serverID, zoneID string, cryptokeyID int) error {
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s/cryptokeys/%s",
path := fmt.Sprintf("/servers/%s/zones/%s/cryptokeys/%s",
url.PathEscape(serverID), url.PathEscape(zoneID), url.PathEscape(strconv.Itoa(cryptokeyID)))

return c.httpClient.Delete(ctx, path, nil)
Expand Down
4 changes: 2 additions & 2 deletions apis/cryptokeys/cryptokey_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/mittwald/go-powerdns/pdnshttp"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
"io/ioutil"
"io"
"net/http"
"testing"
)
Expand All @@ -16,7 +16,7 @@ func TestClient_DeleteCryptokey(t *testing.T) {
Reply(http.StatusNoContent)

hc := &http.Client{Transport: gock.DefaultTransport}
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, ioutil.Discard)
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, io.Discard)
cc := New(c)

err := cc.DeleteCryptokey(context.Background(), "localhost", "pdns-test.de", 102)
Expand Down
2 changes: 1 addition & 1 deletion apis/cryptokeys/cryptokey_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func (c *client) GetCryptokey(ctx context.Context, serverID, zoneID string, cryptokeyID int) (*Cryptokey, error) {
cryptokey := Cryptokey{}
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s/cryptokeys/%s",
path := fmt.Sprintf("/servers/%s/zones/%s/cryptokeys/%s",
url.PathEscape(serverID), url.PathEscape(zoneID), url.PathEscape(strconv.Itoa(cryptokeyID)))

err := c.httpClient.Get(ctx, path, &cryptokey)
Expand Down
4 changes: 2 additions & 2 deletions apis/cryptokeys/cryptokey_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
"io/ioutil"
"io"
"net/http"
"testing"
)
Expand Down Expand Up @@ -35,7 +35,7 @@ func TestClient_GetCryptokey(t *testing.T) {
}`)

hc := &http.Client{Transport: gock.DefaultTransport}
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, ioutil.Discard)
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, io.Discard)
cc := New(c)

key, err := cc.GetCryptokey(context.Background(), "localhost", "pdns-test.de", 102)
Expand Down
2 changes: 1 addition & 1 deletion apis/cryptokeys/cryptokey_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func (c *client) ListCryptokeys(ctx context.Context, serverID, zoneID string) ([]Cryptokey, error) {
cryptokeys := []Cryptokey{}
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s/cryptokeys", url.PathEscape(serverID), url.PathEscape(zoneID))
path := fmt.Sprintf("/servers/%s/zones/%s/cryptokeys", url.PathEscape(serverID), url.PathEscape(zoneID))

err := c.httpClient.Get(ctx, path, &cryptokeys)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions apis/cryptokeys/cryptokey_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
"io/ioutil"
"io"
"net/http"
"testing"
)
Expand Down Expand Up @@ -36,7 +36,7 @@ func TestClient_ListCryptokeys(t *testing.T) {
]`)

hc := &http.Client{Transport: gock.DefaultTransport}
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, ioutil.Discard)
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, io.Discard)
cc := New(c)

list, err := cc.ListCryptokeys(context.Background(), "localhost", "pdns-test.de")
Expand Down
2 changes: 1 addition & 1 deletion apis/cryptokeys/cryptokey_toggle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (c *client) ToggleCryptokey(ctx context.Context, serverID, zoneID string, cryptokeyID int) error {
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s/cryptokeys/%s",
path := fmt.Sprintf("/servers/%s/zones/%s/cryptokeys/%s",
url.PathEscape(serverID), url.PathEscape(zoneID), url.PathEscape(strconv.Itoa(cryptokeyID)))

return c.httpClient.Put(ctx, path, nil)
Expand Down
4 changes: 2 additions & 2 deletions apis/cryptokeys/cryptokey_toggle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/mittwald/go-powerdns/pdnshttp"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
"io/ioutil"
"io"
"net/http"
"testing"
)
Expand All @@ -16,7 +16,7 @@ func TestClient_ToggleCryptokey(t *testing.T) {
Reply(http.StatusNoContent)

hc := &http.Client{Transport: gock.DefaultTransport}
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, ioutil.Discard)
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, io.Discard)
cc := New(c)

err := cc.ToggleCryptokey(context.Background(), "localhost", "pdns-test.de", 102)
Expand Down
2 changes: 1 addition & 1 deletion apis/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (c *client) Search(ctx context.Context, serverID, query string, max int, objectType ObjectType) (ResultList, error) {
path := fmt.Sprintf("/api/v1/servers/%s/search-data", url.PathEscape(serverID))
path := fmt.Sprintf("/servers/%s/search-data", url.PathEscape(serverID))
results := make(ResultList, 0)

err := c.httpClient.Get(
Expand Down
4 changes: 2 additions & 2 deletions apis/search/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/mittwald/go-powerdns/pdnshttp"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
"io/ioutil"
"io"
"net/http"
"testing"
)
Expand Down Expand Up @@ -37,7 +37,7 @@ func TestSearchExecutesCorrectRequest(t *testing.T) {
BodyString(exampleSearchResult)

hc := &http.Client{Transport: gock.DefaultTransport}
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, ioutil.Discard)
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, io.Discard)
sc := New(c)

results, err := sc.Search(
Expand Down
2 changes: 1 addition & 1 deletion apis/servers/servers_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func (c *client) GetServer(ctx context.Context, serverID string) (*Server, error) {
server := Server{}
err := c.httpClient.Get(ctx, fmt.Sprintf("/api/v1/servers/%s", url.PathEscape(serverID)), &server)
err := c.httpClient.Get(ctx, fmt.Sprintf("/servers/%s", url.PathEscape(serverID)), &server)

if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion apis/servers/servers_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "context"
func (c *client) ListServers(ctx context.Context) ([]Server, error) {
servers := make([]Server, 0)

err := c.httpClient.Get(ctx, "/api/v1/servers", &servers)
err := c.httpClient.Get(ctx, "/servers", &servers)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion apis/zones/zones_addrecordset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

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))
path := fmt.Sprintf("/servers/%s/zones/%s", url.PathEscape(serverID), url.PathEscape(zoneID))

set.ChangeType = ChangeTypeReplace
patch := Zone{
Expand Down
2 changes: 1 addition & 1 deletion apis/zones/zones_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

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))
path := fmt.Sprintf("/servers/%s/zones", url.PathEscape(serverID))

zone.ID = ""
zone.Type = ZoneTypeZone
Expand Down
4 changes: 2 additions & 2 deletions apis/zones/zones_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
"io/ioutil"
"io"
"net/http"
"testing"
)
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestCreateZoneCreatesZone(t *testing.T) {
}`)

hc := &http.Client{Transport: gock.DefaultTransport}
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, ioutil.Discard)
c := pdnshttp.NewClient("http://dns.example", hc, &pdnshttp.APIKeyAuthenticator{APIKey: "secret"}, io.Discard)
sc := New(c)

zone, err := sc.CreateZone(
Expand Down
2 changes: 1 addition & 1 deletion apis/zones/zones_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

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))
path := fmt.Sprintf("/servers/%s/zones/%s", url.PathEscape(serverID), url.PathEscape(zoneID))

return c.httpClient.Delete(ctx, path, nil)
}
2 changes: 1 addition & 1 deletion apis/zones/zones_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

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))
path := fmt.Sprintf("/servers/%s/zones/%s/export", url.PathEscape(serverID), url.PathEscape(zoneID))

err := c.httpClient.Get(ctx, path, &output)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion apis/zones/zones_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func WithResourceRecordSetFilter(name, recordType string) GetZoneOption {

func (c *client) GetZone(ctx context.Context, serverID, zoneID string, opts ...GetZoneOption) (*Zone, error) {
zone := Zone{}
path := fmt.Sprintf("/api/v1/servers/%s/zones/%s", url.PathEscape(serverID), url.PathEscape(zoneID))
path := fmt.Sprintf("/servers/%s/zones/%s", url.PathEscape(serverID), url.PathEscape(zoneID))

req, err := c.httpClient.NewRequest(http.MethodGet, path, nil)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions apis/zones/zones_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

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))
path := fmt.Sprintf("/servers/%s/zones", url.PathEscape(serverID))

err := c.httpClient.Get(ctx, path, &zones)
if err != nil {
Expand All @@ -22,7 +22,7 @@ func (c *client) ListZones(ctx context.Context, serverID string) ([]Zone, error)

func (c *client) ListZone(ctx context.Context, serverID string, zoneName string) ([]Zone, error) {
zones := make([]Zone, 0)
path := fmt.Sprintf("/api/v1/servers/%s/zones", url.PathEscape(serverID))
path := fmt.Sprintf("/servers/%s/zones", url.PathEscape(serverID))

err := c.httpClient.Get(ctx, path, &zones, pdnshttp.WithQueryValue("zone", zoneName))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion apis/zones/zones_notifyslaves.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

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))
path := fmt.Sprintf("/servers/%s/zones/%s/notify", url.PathEscape(serverID), url.PathEscape(zoneID))

return c.httpClient.Put(ctx, path, nil)
}
2 changes: 1 addition & 1 deletion apis/zones/zones_rectify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

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))
path := fmt.Sprintf("/servers/%s/zones/%s/rectify", url.PathEscape(serverID), url.PathEscape(zoneID))

return c.httpClient.Put(ctx, path, nil)
}
2 changes: 1 addition & 1 deletion apis/zones/zones_removerecordset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

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))
path := fmt.Sprintf("/servers/%s/zones/%s", url.PathEscape(serverID), url.PathEscape(zoneID))

set := ResourceRecordSet{
Name: name,
Expand Down
2 changes: 1 addition & 1 deletion apis/zones/zones_retrievemaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

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))
path := fmt.Sprintf("/servers/%s/zones/%s/axfr-retrieve", url.PathEscape(serverID), url.PathEscape(zoneID))

return c.httpClient.Put(ctx, path, nil)
}
2 changes: 1 addition & 1 deletion apis/zones/zones_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

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))
path := fmt.Sprintf("/servers/%s/zones/%s/check", url.PathEscape(serverID), url.PathEscape(zoneID))

return c.httpClient.Get(ctx, path, nil)
}
3 changes: 1 addition & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"github.com/mittwald/go-powerdns/apis/cryptokeys"
"io"
"io/ioutil"
"net/http"
"time"

Expand Down Expand Up @@ -37,7 +36,7 @@ func New(opt ...ClientOption) (Client, error) {
c := client{
baseURL: "http://localhost:8081",
httpClient: http.DefaultClient,
debugOutput: ioutil.Discard,
debugOutput: io.Discard,
authenticator: &pdnshttp.NoopAuthenticator{},
}

Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/mittwald/go-powerdns/pdnshttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"io"
"os"
"os/exec"
"testing"
Expand Down Expand Up @@ -417,7 +417,7 @@ func TestExportZone(t *testing.T) {
}

func buildClient(t *testing.T) Client {
debug := ioutil.Discard
debug := io.Discard

if testing.Verbose() {
debug = os.Stderr
Expand Down
12 changes: 11 additions & 1 deletion pdnshttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)

Expand All @@ -19,8 +20,17 @@ type Client struct {

// NewClient returns a new PowerDNS HTTP client
func NewClient(baseURL string, hc *http.Client, auth ClientAuthenticator, debugOutput io.Writer) *Client {
u, err := url.ParseRequestURI(baseURL)
if err != nil {
panic(err)
}
if strings.TrimSuffix(u.Path, "/") == "" {
u.Path = "/api/v1"
} else {
u.Path = strings.TrimSuffix(u.Path, "/")
}
c := Client{
baseURL: baseURL,
baseURL: u.String(),
httpClient: hc,
authenticator: auth,
debugOutput: debugOutput,
Expand Down
Loading

0 comments on commit 0b5137f

Please sign in to comment.