Skip to content

Commit

Permalink
pdns: reconstruct zone URLs to enable non-root folder API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Tammo Siebert committed Mar 15, 2024
1 parent 61553c4 commit b54178a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
4 changes: 2 additions & 2 deletions providers/dns/pdns/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (c *Client) GetHostedZone(ctx context.Context, authZone string) (*HostedZon
}

func (c *Client) UpdateRecords(ctx context.Context, zone *HostedZone, sets RRSets) error {
endpoint := c.joinPath("/", zone.URL)
endpoint := c.joinPath("/", "servers", c.serverName, "zones", zone.ID)

req, err := newJSONRequest(ctx, http.MethodPatch, endpoint, sets)
if err != nil {
Expand All @@ -137,7 +137,7 @@ func (c *Client) Notify(ctx context.Context, zone *HostedZone) error {
return nil
}

endpoint := c.joinPath("/", zone.URL, "/notify")
endpoint := c.joinPath("/", "servers", c.serverName, "zones", zone.ID, "notify")

req, err := newJSONRequest(ctx, http.MethodPut, endpoint, nil)
if err != nil {
Expand Down
63 changes: 55 additions & 8 deletions providers/dns/pdns/internal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,43 @@ func TestClient_GetHostedZone_v0(t *testing.T) {
}

func TestClient_UpdateRecords(t *testing.T) {
client := setupTest(t, http.MethodPatch, "/api/v1/servers/localhost/zones/example.org.", http.StatusOK, "zone.json")
client := setupTest(t, http.MethodPatch, "/api/v1/servers/server/zones/example.org.", http.StatusOK, "zone.json")
client.apiVersion = 1

zone := &HostedZone{
ID: "example.org.",
Name: "example.org.",
URL: "api/v1/servers/localhost/zones/example.org.",
URL: "api/v1/servers/server/zones/example.org.",
Kind: "Master",
}

rrSets := RRSets{
RRSets: []RRSet{{
Name: "example.org.",
Type: "NS",
ChangeType: "REPLACE",
Records: []Record{{
Content: "192.0.2.5",
Name: "ns1.example.org.",
TTL: 86400,
Type: "A",
}},
}},
}

err := client.UpdateRecords(context.Background(), zone, rrSets)
require.NoError(t, err)
}

func TestClient_UpdateRecords_NonRootApi(t *testing.T) {
client := setupTest(t, http.MethodPatch, "/some/path/api/v1/servers/server/zones/example.org.", http.StatusOK, "zone.json")
client.Host = client.Host.JoinPath("some", "path")
client.apiVersion = 1

zone := &HostedZone{
ID: "example.org.",
Name: "example.org.",
URL: "some/path/api/v1/servers/server/zones/example.org.",
Kind: "Master",
}

Expand All @@ -284,13 +314,13 @@ func TestClient_UpdateRecords(t *testing.T) {
}

func TestClient_UpdateRecords_v0(t *testing.T) {
client := setupTest(t, http.MethodPatch, "/servers/localhost/zones/example.org.", http.StatusOK, "zone.json")
client := setupTest(t, http.MethodPatch, "/servers/server/zones/example.org.", http.StatusOK, "zone.json")
client.apiVersion = 0

zone := &HostedZone{
ID: "example.org.",
Name: "example.org.",
URL: "servers/localhost/zones/example.org.",
URL: "servers/server/zones/example.org.",
Kind: "Master",
}

Expand All @@ -313,13 +343,29 @@ func TestClient_UpdateRecords_v0(t *testing.T) {
}

func TestClient_Notify(t *testing.T) {
client := setupTest(t, http.MethodPut, "/api/v1/servers/localhost/zones/example.org./notify", http.StatusOK, "")
client := setupTest(t, http.MethodPut, "/api/v1/servers/server/zones/example.org./notify", http.StatusOK, "")
client.apiVersion = 1

zone := &HostedZone{
ID: "example.org.",
Name: "example.org.",
URL: "api/v1/servers/localhost/zones/example.org.",
URL: "api/v1/servers/server/zones/example.org.",
Kind: "Master",
}

err := client.Notify(context.Background(), zone)
require.NoError(t, err)
}

func TestClient_Notify_NonRootApi(t *testing.T) {
client := setupTest(t, http.MethodPut, "/some/path/api/v1/servers/server/zones/example.org./notify", http.StatusOK, "")
client.Host = client.Host.JoinPath("some", "path")
client.apiVersion = 1

zone := &HostedZone{
ID: "example.org.",
Name: "example.org.",
URL: "/some/path/api/v1/servers/server/zones/example.org.",
Kind: "Master",
}

Expand All @@ -328,12 +374,13 @@ func TestClient_Notify(t *testing.T) {
}

func TestClient_Notify_v0(t *testing.T) {
client := setupTest(t, http.MethodPut, "/api/v1/servers/localhost/zones/example.org./notify", http.StatusOK, "")
client := setupTest(t, http.MethodPut, "/not/queried/for/api/version/0", http.StatusOK, "")
client.apiVersion = 0

zone := &HostedZone{
ID: "example.org.",
Name: "example.org.",
URL: "servers/localhost/zones/example.org.",
URL: "servers/server/zones/example.org.",
Kind: "Master",
}

Expand Down

0 comments on commit b54178a

Please sign in to comment.