-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tailscale: add
dns_split_nameservers
resource
Add `resource_dns_split_nameservers` to allow for controlling split DNS settings for a given tailnet. Updates tailscale/corp#19483 Signed-off-by: Mario Minardi <mario@tailscale.com>
- Loading branch information
Showing
7 changed files
with
192 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "tailscale_dns_split_nameservers Resource - terraform-provider-tailscale" | ||
subcategory: "" | ||
description: |- | ||
The dnssplitnameservers resource allows you to configure split DNS nameservers for your Tailscale network. See https://tailscale.com/kb/1054/dns for more information. | ||
--- | ||
|
||
# tailscale_dns_split_nameservers (Resource) | ||
|
||
The dns_split_nameservers resource allows you to configure split DNS nameservers for your Tailscale network. See https://tailscale.com/kb/1054/dns for more information. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "tailscale_dns_split_nameservers" "sample_split_nameservers" { | ||
domain = "foo.example.com" | ||
nameservers = ["1.1.1.1"] | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `domain` (String) Domain to configure split DNS for. Requests for this domain will be resolved using the provided nameservers. | ||
- `nameservers` (Set of String) Devices on your network will use these nameservers to resolve DNS names. IPv4 or IPv6 addresses are accepted. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. |
5 changes: 5 additions & 0 deletions
5
examples/resources/tailscale_dns_split_nameservers/resource.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
resource "tailscale_dns_split_nameservers" "sample_split_nameservers" { | ||
domain = "foo.example.com" | ||
|
||
nameservers = ["1.1.1.1"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package tailscale | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/tailscale/tailscale-client-go/tailscale" | ||
) | ||
|
||
func resourceDNSSplitNameservers() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "The dns_split_nameservers resource allows you to configure split DNS nameservers for your Tailscale network. See https://tailscale.com/kb/1054/dns for more information.", | ||
ReadContext: resourceSplitDNSNameserversRead, | ||
CreateContext: resourceSplitDNSNameserversCreate, | ||
UpdateContext: resourceSplitDNSNameserversUpdate, | ||
DeleteContext: resourceSplitDNSNameserversDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"domain": { | ||
Type: schema.TypeString, | ||
Description: "Domain to configure split DNS for. Requests for this domain will be resolved using the provided nameservers.", | ||
Required: true, | ||
}, | ||
"nameservers": { | ||
Type: schema.TypeSet, | ||
Description: "Devices on your network will use these nameservers to resolve DNS names. IPv4 or IPv6 addresses are accepted.", | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceSplitDNSNameserversRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*tailscale.Client) | ||
splitDNS, err := client.DNSSplitDNS(ctx) | ||
if err != nil { | ||
return diagnosticsError(err, "Failed to fetch split DNS configs") | ||
} | ||
|
||
nameservers := splitDNS[d.Get("domain").(string)] | ||
|
||
if err = d.Set("nameservers", nameservers); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceSplitDNSNameserversCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*tailscale.Client) | ||
nameserversSet := d.Get("nameservers").(*schema.Set) | ||
domain := d.Get("domain").(string) | ||
|
||
nameserversList := nameserversSet.List() | ||
|
||
req := make(tailscale.SplitDnsRequest) | ||
var nameservers []string | ||
for _, nameserver := range nameserversList { | ||
nameservers = append(nameservers, nameserver.(string)) | ||
} | ||
req[domain] = &nameservers | ||
|
||
if err := client.UpdateDNSSplitDNS(ctx, req); err != nil { | ||
return diagnosticsError(err, "Failed to set dns split nameservers") | ||
} | ||
|
||
d.SetId(domain) | ||
return nil | ||
} | ||
|
||
func resourceSplitDNSNameserversUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
if !d.HasChange("nameservers") { | ||
return resourceSplitDNSNameserversRead(ctx, d, m) | ||
} | ||
|
||
client := m.(*tailscale.Client) | ||
domain := d.Get("domain").(string) | ||
nameserversSet := d.Get("nameservers").(*schema.Set) | ||
|
||
nameserversList := nameserversSet.List() | ||
|
||
req := make(tailscale.SplitDnsRequest) | ||
var nameservers []string | ||
for _, nameserver := range nameserversList { | ||
nameservers = append(nameservers, nameserver.(string)) | ||
} | ||
req[domain] = &nameservers | ||
|
||
if err := client.UpdateDNSSplitDNS(ctx, req); err != nil { | ||
return diagnosticsError(err, "Failed to set dns split nameservers") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceSplitDNSNameserversDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*tailscale.Client) | ||
domain := d.Get("domain").(string) | ||
|
||
req := make(tailscale.SplitDnsRequest) | ||
req[domain] = &[]string{} | ||
|
||
if err := client.UpdateDNSSplitDNS(ctx, req); err != nil { | ||
return diagnosticsError(err, "Failed to set dns split nameservers") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package tailscale_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
const testSplitNameservers = ` | ||
resource "tailscale_dns_split_nameservers" "test_nameservers" { | ||
domain = "example.com" | ||
nameservers = ["1.2.3.4", "4.5.6.7"] | ||
}` | ||
|
||
func TestProvider_TailscaleSplitDNSNameservers(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
IsUnitTest: true, | ||
PreCheck: func() { | ||
testServer.ResponseCode = http.StatusOK | ||
testServer.ResponseBody = nil | ||
}, | ||
ProviderFactories: testProviderFactories(t), | ||
Steps: []resource.TestStep{ | ||
testResourceCreated("tailscale_dns_split_nameservers.test_nameservers", testSplitNameservers), | ||
testResourceDestroyed("tailscale_dns_split_nameservers.test_nameservers", testSplitNameservers), | ||
}, | ||
}) | ||
} |