resource_dns_configuration: add magic_dns_name attr#607
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new magic_dns_name computed attribute to the tailscale_dns_configuration resource, making it easier to obtain the tailnet's MagicDNS domain name (e.g., 'hyperactive-sloth.ts.net') without requiring a separate tailscale_device(s) data source and manual string manipulation.
Key changes:
- Added
magic_dns_nameas a computed string attribute to the DNS configuration schema - Implemented logic to extract the MagicDNS domain name from the first device's name when MagicDNS is enabled
- Updated documentation to reflect the new read-only attribute
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| tailscale/resource_dns_configuration.go | Adds the magic_dns_name schema attribute and implements the logic to retrieve and populate it by fetching the device list and extracting the tailnet domain from the first device's name |
| docs/resources/dns_configuration.md | Documents the new magic_dns_name read-only attribute in the schema reference section |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| var magicDNSName string | ||
| var diags diag.Diagnostics | ||
| if configuration.Preferences.MagicDNS { | ||
| devices, err := client.Devices().List(ctx) | ||
| if err != nil { | ||
| diags = append(diags, diagnosticsError(err, "There is a MagicDNS name, but we failed to get devices")...) | ||
| } else if len(devices) == 0 { | ||
| diags = append(diags, diag.Diagnostic{ | ||
| Severity: diag.Warning, | ||
| Summary: "There is a MagicDNS name, but we failed to get devices", | ||
| }) | ||
| } else { | ||
| parts := strings.Split(devices[0].Name, ".") | ||
| if len(parts) != 4 { | ||
| diags = append(diags, diagnosticsError(err, "There is a MagicDNSName, but unexpected device name format")...) | ||
| } else { | ||
| magicDNSName = strings.Join(parts[1:], ".") | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The new magic_dns_name computed attribute lacks test coverage. The tests should verify that the attribute is properly populated when MagicDNS is enabled and devices are present, and that it handles edge cases like when there are no devices or when MagicDNS is disabled.
| parts := strings.Split(devices[0].Name, ".") | ||
| if len(parts) != 4 { | ||
| diags = append(diags, diagnosticsError(err, "There is a MagicDNSName, but unexpected device name format")...) |
There was a problem hiding this comment.
The code assumes all device names have exactly 4 parts when split by "." (hostname.user.tailnet.domain), but this assumption may not always hold. If the device name format varies (e.g., shorter names in certain configurations), the code will silently fail to set magic_dns_name without providing useful debugging information about what the actual format was. Consider logging or including the actual device name in the error diagnostic to aid troubleshooting.
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 <dev@ojford.com>
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.