Skip to content

Commit 6f3ba94

Browse files
committed
resource_dns_configuration: add magic_dns_name attr
It's not presently possible to determine the tailnet/MagicDNS name without a `tailscale_device(s)` data source and some string manipulation. Ideally, there'd be an API for this directy, but in lieu of that this commit moves the device list/error (or 0 device) handling/string manipulation into the provider; so that the terraform config can more cleanly and naturally read from: tailscale_dns_configuration.x.magic_dns_name to get the name like 'hyperactive-sloth.ts.net' directly. Signed-off-by: Oliver Ford <[email protected]>
1 parent 73e7c60 commit 6f3ba94

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

docs/resources/dns_configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ resource "tailscale_dns_configuration" "sample_configuration" {
5959
### Read-Only
6060

6161
- `id` (String) The ID of this resource.
62+
- `magic_dns_name` (String) If enabled, the tailnet/MagicDNS domain name.
6263

6364
<a id="nestedblock--nameservers"></a>
6465
### Nested Schema for `nameservers`

tailscale/resource_dns_configuration.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ func resourceDNSConfiguration() *schema.Resource {
9999
Optional: true,
100100
Default: true,
101101
},
102+
"magic_dns_name": {
103+
Description: "The tailnet/MagicDNS domain name. Null if disabled or undeterminable.",
104+
Type: schema.TypeString,
105+
Computed: true,
106+
},
102107
},
103108
}
104109
}
@@ -135,14 +140,41 @@ func resourceDNSConfigurationRead(ctx context.Context, d *schema.ResourceData, m
135140
})
136141
}
137142

138-
if diag := setProperties(d, map[string]any{
143+
var magicDNSName string
144+
var diags diag.Diagnostics
145+
if configuration.Preferences.MagicDNS {
146+
devices, err := client.Devices().List(ctx)
147+
if err != nil {
148+
diags = append(diags, diagnosticsError(err, "There is a MagicDNS name, but we failed to get devices")...)
149+
} else if len(devices) == 0 {
150+
diags = append(diags, diag.Diagnostic{
151+
Severity: diag.Warning,
152+
Summary: "There is a MagicDNS name, but we can't determine it with 0 devices",
153+
})
154+
} else {
155+
parts := strings.Split(devices[0].Name, ".")
156+
if len(parts) != 4 {
157+
diags = append(diags, diag.Diagnostic{
158+
Severity: diag.Error,
159+
Summary: "There is a MagicDNS name, but unexpected device name format",
160+
)
161+
} else {
162+
magicDNSName = strings.Join(parts[1:], ".")
163+
}
164+
}
165+
}
166+
167+
diags = append(diags, setProperties(d, map[string]any{
139168
"nameservers": nameservers,
140169
"split_dns": splitDNS,
141170
"search_paths": configuration.SearchPaths,
142171
"override_local_dns": configuration.Preferences.OverrideLocalDNS,
143172
"magic_dns": configuration.Preferences.MagicDNS,
144-
}); diag != nil {
145-
return diag
173+
"magic_dns_name": magicDNSName,
174+
})...)
175+
176+
if diags != nil {
177+
return diags
146178
}
147179

148180
return []diag.Diagnostic{

0 commit comments

Comments
 (0)