Skip to content

Commit bbdc4cf

Browse files
oxtoacartrajsinghtech
authored andcommitted
resource_dns_configuration: create resource for managing entire DNS configuration at once
This resource uses the new `/tailnet/-/dns/configuration` endpoint to manage the complete DNS configuration for a tailnet. Updates tailscale/corp#32130 Signed-off-by: Percy Wegmann <percy@tailscale.com>
1 parent 3f59dfe commit bbdc4cf

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

dns.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,42 @@ func (dr *DNSResource) SetPreferences(ctx context.Context, preferences DNSPrefer
147147

148148
return dr.do(req, nil)
149149
}
150+
151+
type DNSConfiguration struct {
152+
Nameservers []DNSConfigurationResolver `json:"nameservers,omitempty"`
153+
SplitDNS map[string][]DNSConfigurationResolver `json:"splitDNS,omitempty"`
154+
SearchPaths []string `json:"searchPaths,omitempty"`
155+
Preferences DNSConfigurationPreferences `json:"preferences,omitempty"`
156+
}
157+
158+
type DNSConfigurationResolver struct {
159+
Address string `json:"address,omitempty"`
160+
UseWithExitNode bool `json:"useWithExitNode,omitempty"`
161+
}
162+
163+
type DNSConfigurationPreferences struct {
164+
OverrideLocalDNS bool `json:"overrideLocalDNS,omitempty"`
165+
MagicDNS bool `json:"magicDNS,omitempty"`
166+
}
167+
168+
// Configuration retrieves the tailnet's complete DNS configuration.
169+
// WARNING - this is currently in alpha and subject to change.
170+
func (dr *DNSResource) Configuration(ctx context.Context) (*DNSConfiguration, error) {
171+
req, err := dr.buildRequest(ctx, http.MethodGet, dr.buildTailnetURL("dns", "configuration"))
172+
if err != nil {
173+
return nil, err
174+
}
175+
176+
return body[DNSConfiguration](dr, req)
177+
}
178+
179+
// SetConfiguration sets the tailnet's complete DNS configuration.
180+
// WARNING - this is currently in alpha and subject to change.
181+
func (dr *DNSResource) SetConfiguration(ctx context.Context, configuration DNSConfiguration) error {
182+
req, err := dr.buildRequest(ctx, http.MethodPost, dr.buildTailnetURL("dns", "configuration"), requestBody(configuration))
183+
if err != nil {
184+
return nil
185+
}
186+
187+
return dr.do(req, nil)
188+
}

dns_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,72 @@ func TestClient_SetSplitDNS(t *testing.T) {
182182
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &body))
183183
assert.EqualValues(t, nameservers, body["example.com"])
184184
}
185+
186+
func TestClient_DNSConfiguration(t *testing.T) {
187+
t.Parallel()
188+
189+
client, server := NewTestHarness(t)
190+
server.ResponseCode = http.StatusOK
191+
server.ResponseBody = &DNSConfiguration{
192+
Nameservers: []DNSConfigurationResolver{
193+
{
194+
Address: "8.8.8.8",
195+
UseWithExitNode: true,
196+
},
197+
},
198+
SplitDNS: map[string][]DNSConfigurationResolver{
199+
"example.com": []DNSConfigurationResolver{
200+
{
201+
Address: "4.4.4.4",
202+
UseWithExitNode: true,
203+
},
204+
},
205+
},
206+
Preferences: DNSConfigurationPreferences{
207+
OverrideLocalDNS: true,
208+
MagicDNS: true,
209+
},
210+
}
211+
212+
configuration, err := client.DNS().Configuration(context.Background())
213+
assert.NoError(t, err)
214+
assert.Equal(t, http.MethodGet, server.Method)
215+
assert.Equal(t, "/api/v2/tailnet/example.com/dns/configuration", server.Path)
216+
assert.Equal(t, server.ResponseBody, configuration)
217+
}
218+
219+
func TestClient_SetDNSConfiguration(t *testing.T) {
220+
t.Parallel()
221+
222+
client, server := NewTestHarness(t)
223+
server.ResponseCode = http.StatusOK
224+
225+
configuration := DNSConfiguration{
226+
Nameservers: []DNSConfigurationResolver{
227+
{
228+
Address: "8.8.8.8",
229+
UseWithExitNode: true,
230+
},
231+
},
232+
SplitDNS: map[string][]DNSConfigurationResolver{
233+
"example.com": {
234+
{
235+
Address: "4.4.4.4",
236+
UseWithExitNode: true,
237+
},
238+
},
239+
},
240+
Preferences: DNSConfigurationPreferences{
241+
OverrideLocalDNS: true,
242+
MagicDNS: true,
243+
},
244+
}
245+
246+
assert.NoError(t, client.DNS().SetConfiguration(context.Background(), configuration))
247+
assert.Equal(t, http.MethodPost, server.Method)
248+
assert.Equal(t, "/api/v2/tailnet/example.com/dns/configuration", server.Path)
249+
250+
var body DNSConfiguration
251+
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &body))
252+
assert.EqualValues(t, configuration, body)
253+
}

0 commit comments

Comments
 (0)