Skip to content

Commit

Permalink
tailscale: use V2 client for dns split nameservers resource
Browse files Browse the repository at this point in the history
Updates tailscale/corp#21867

Signed-off-by: Percy Wegmann <[email protected]>
  • Loading branch information
oxtoacart committed Aug 21, 2024
1 parent 3a9fb56 commit 27aa0bf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
A client implementation for the [Tailscale](https://tailscale.com) HTTP API.
For more details, please see [API documentation](https://github.com/tailscale/tailscale/blob/main/api.md).

A [V2](v2) implementation of the client is under active development, use at your own risk.
A [V2](v2) implementation of the client is under active development, use at your own risk and expect breaking changes.

# Example

Expand Down
2 changes: 1 addition & 1 deletion v2/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package tsclient contains a basic implementation of a client for the Tailscale HTTP api. Documentation is here:
// https://tailscale.com/api
//
// WARNING - this v2 implementation is under active development, use at your own risk.
// WARNING - this v2 implementation is under active development, use at your own risk and expect breaking changes.
package tsclient

import (
Expand Down
34 changes: 20 additions & 14 deletions v2/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ type DNSResource struct {
}

type (
// SplitDnsRequest is a map from domain names to a list of nameservers.
SplitDnsRequest map[string][]string
// SplitDNSRequest is a map from domain names to a list of nameservers.
SplitDNSRequest map[string][]string

// SplitDnsResponse is a map from domain names to a list of nameservers.
SplitDnsResponse SplitDnsRequest
// SplitDNSResponse is a map from domain names to a list of nameservers.
SplitDNSResponse SplitDNSRequest

DNSPreferences struct {
MagicDNS bool `json:"magicDNS"`
Expand Down Expand Up @@ -77,29 +77,32 @@ func (dr *DNSResource) Nameservers(ctx context.Context) ([]string, error) {
}

// UpdateSplitDNS updates the split DNS settings for a tailnet using the
// provided SplitDnsRequest object. This is a PATCH operation that performs
// provided [SplitDNSRequest] object. This is a PATCH operation that performs
// partial updates of the underlying data structure.
//
// Mapping a domain to a nil slice in the request will unset the nameservers
// associated with that domain. Values provided for domains will overwrite the
// current value associated with the domain. Domains not included in the request
// will remain unchanged.
func (dr *DNSResource) UpdateSplitDNS(ctx context.Context, request SplitDnsRequest) (*SplitDnsResponse, error) {
func (dr *DNSResource) UpdateSplitDNS(ctx context.Context, request SplitDNSRequest) (SplitDNSResponse, error) {
req, err := dr.buildRequest(ctx, http.MethodPatch, dr.buildTailnetURL("dns", "split-dns"), requestBody(request))
if err != nil {
return nil, err
}

var resp SplitDnsResponse
return &resp, dr.do(req, &resp)
var resp SplitDNSResponse
if err := dr.do(req, &resp); err != nil {
return nil, err
}
return resp, nil
}

// SetSplitDNS sets the split DNS settings for a tailnet using the provided
// SplitDnsRequest object. This is a PUT operation that fully replaces the underlying
// [SplitDNSRequest] object. This is a PUT operation that fully replaces the underlying
// data structure.
//
// Passing in an empty SplitDnsRequest will unset all split DNS mappings for the tailnet.
func (dr *DNSResource) SetSplitDNS(ctx context.Context, request SplitDnsRequest) error {
// Passing in an empty [SplitDNSRequest] will unset all split DNS mappings for the tailnet.
func (dr *DNSResource) SetSplitDNS(ctx context.Context, request SplitDNSRequest) error {
req, err := dr.buildRequest(ctx, http.MethodPut, dr.buildTailnetURL("dns", "split-dns"), requestBody(request))
if err != nil {
return err
Expand All @@ -109,14 +112,17 @@ func (dr *DNSResource) SetSplitDNS(ctx context.Context, request SplitDnsRequest)
}

// SplitDNS retrieves the split DNS configuration for a tailnet.
func (dr *DNSResource) SplitDNS(ctx context.Context) (*SplitDnsResponse, error) {
func (dr *DNSResource) SplitDNS(ctx context.Context) (SplitDNSResponse, error) {
req, err := dr.buildRequest(ctx, http.MethodGet, dr.buildTailnetURL("dns", "split-dns"))
if err != nil {
return nil, err
}

var resp SplitDnsResponse
return &resp, dr.do(req, &resp)
var resp SplitDNSResponse
if err := dr.do(req, &resp); err != nil {
return nil, err
}
return resp, nil
}

// Preferences retrieves the DNS preferences that are currently set for the given tailnet. Supply the tailnet of
Expand Down
12 changes: 6 additions & 6 deletions v2/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestClient_SplitDNS(t *testing.T) {
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK

expectedNameservers := &tsclient.SplitDnsResponse{
expectedNameservers := tsclient.SplitDNSResponse{
"example.com": {"1.1.1.1", "1.2.3.4"},
}

Expand Down Expand Up @@ -141,11 +141,11 @@ func TestClient_UpdateSplitDNS(t *testing.T) {
server.ResponseCode = http.StatusOK

nameservers := []string{"1.1.2.1", "3.3.3.4"}
request := tsclient.SplitDnsRequest{
request := tsclient.SplitDNSRequest{
"example.com": nameservers,
}

expectedNameservers := &tsclient.SplitDnsResponse{
expectedNameservers := tsclient.SplitDNSResponse{
"example.com": nameservers,
}
server.ResponseBody = expectedNameservers
Expand All @@ -155,7 +155,7 @@ func TestClient_UpdateSplitDNS(t *testing.T) {
assert.Equal(t, http.MethodPatch, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/dns/split-dns", server.Path)

body := make(tsclient.SplitDnsResponse)
body := make(tsclient.SplitDNSResponse)
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &body))
assert.EqualValues(t, nameservers, body["example.com"])
assert.EqualValues(t, expectedNameservers, resp)
Expand All @@ -168,15 +168,15 @@ func TestClient_SetSplitDNS(t *testing.T) {
server.ResponseCode = http.StatusOK

nameservers := []string{"1.1.2.1", "3.3.3.4"}
request := tsclient.SplitDnsRequest{
request := tsclient.SplitDNSRequest{
"example.com": nameservers,
}

assert.NoError(t, client.DNS().SetSplitDNS(context.Background(), request))
assert.Equal(t, http.MethodPut, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/dns/split-dns", server.Path)

body := make(tsclient.SplitDnsResponse)
body := make(tsclient.SplitDNSResponse)
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &body))
assert.EqualValues(t, nameservers, body["example.com"])
}

0 comments on commit 27aa0bf

Please sign in to comment.