Skip to content

Commit 27aa0bf

Browse files
committed
tailscale: use V2 client for dns split nameservers resource
Updates tailscale/corp#21867 Signed-off-by: Percy Wegmann <[email protected]>
1 parent 3a9fb56 commit 27aa0bf

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
A client implementation for the [Tailscale](https://tailscale.com) HTTP API.
99
For more details, please see [API documentation](https://github.com/tailscale/tailscale/blob/main/api.md).
1010

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

1313
# Example
1414

v2/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Package tsclient contains a basic implementation of a client for the Tailscale HTTP api. Documentation is here:
22
// https://tailscale.com/api
33
//
4-
// WARNING - this v2 implementation is under active development, use at your own risk.
4+
// WARNING - this v2 implementation is under active development, use at your own risk and expect breaking changes.
55
package tsclient
66

77
import (

v2/dns.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ type DNSResource struct {
1010
}
1111

1212
type (
13-
// SplitDnsRequest is a map from domain names to a list of nameservers.
14-
SplitDnsRequest map[string][]string
13+
// SplitDNSRequest is a map from domain names to a list of nameservers.
14+
SplitDNSRequest map[string][]string
1515

16-
// SplitDnsResponse is a map from domain names to a list of nameservers.
17-
SplitDnsResponse SplitDnsRequest
16+
// SplitDNSResponse is a map from domain names to a list of nameservers.
17+
SplitDNSResponse SplitDNSRequest
1818

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

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

93-
var resp SplitDnsResponse
94-
return &resp, dr.do(req, &resp)
93+
var resp SplitDNSResponse
94+
if err := dr.do(req, &resp); err != nil {
95+
return nil, err
96+
}
97+
return resp, nil
9598
}
9699

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

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

118-
var resp SplitDnsResponse
119-
return &resp, dr.do(req, &resp)
121+
var resp SplitDNSResponse
122+
if err := dr.do(req, &resp); err != nil {
123+
return nil, err
124+
}
125+
return resp, nil
120126
}
121127

122128
// Preferences retrieves the DNS preferences that are currently set for the given tailnet. Supply the tailnet of

v2/dns_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestClient_SplitDNS(t *testing.T) {
6969
client, server := NewTestHarness(t)
7070
server.ResponseCode = http.StatusOK
7171

72-
expectedNameservers := &tsclient.SplitDnsResponse{
72+
expectedNameservers := tsclient.SplitDNSResponse{
7373
"example.com": {"1.1.1.1", "1.2.3.4"},
7474
}
7575

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

143143
nameservers := []string{"1.1.2.1", "3.3.3.4"}
144-
request := tsclient.SplitDnsRequest{
144+
request := tsclient.SplitDNSRequest{
145145
"example.com": nameservers,
146146
}
147147

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

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

170170
nameservers := []string{"1.1.2.1", "3.3.3.4"}
171-
request := tsclient.SplitDnsRequest{
171+
request := tsclient.SplitDNSRequest{
172172
"example.com": nameservers,
173173
}
174174

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

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

0 commit comments

Comments
 (0)