Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add disable_dns_servers option to subnet #87

Merged
merged 9 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions cloudscale/resource_cloudscale_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
Type: schema.TypeString,
Optional: true,
}
} else {
m["disable_dns_servers"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"dns_servers"},
}
}
return m
}
Expand All @@ -95,12 +101,22 @@
opts.GatewayAddress = attr.(string)
}

dnsServers := d.Get("dns_servers").([]any)
s := make([]string, len(dnsServers))
for i := range dnsServers {
s[i] = dnsServers[i].(string)
disableDnsServers := d.Get("disable_dns_servers").(bool)
if disableDnsServers {
opts.DNSServers = &[]string{}

Check failure on line 106 in cloudscale/resource_cloudscale_subnet.go

View workflow job for this annotation

GitHub Actions / Build

cannot use &[]string{} (value of type *[]string) as type []string in assignment
} else {
if dnsServersRaw, ok := d.GetOk("dns_servers"); ok {
dnsServers := dnsServersRaw.([]interface{})
dnsServersStr := make([]string, len(dnsServers))
for i := range dnsServers {
dnsServersStr[i] = dnsServers[i].(string)
}
opts.DNSServers = &dnsServersStr

Check failure on line 114 in cloudscale/resource_cloudscale_subnet.go

View workflow job for this annotation

GitHub Actions / Build

cannot use &dnsServersStr (value of type *[]string) as type []string in assignment
} else {
opts.DNSServers = &cloudscale.UseCloudscaleDefaults

Check failure on line 116 in cloudscale/resource_cloudscale_subnet.go

View workflow job for this annotation

GitHub Actions / Build

undefined: cloudscale.UseCloudscaleDefaults
}
}
opts.DNSServers = s

opts.Tags = CopyTags(d)

log.Printf("[DEBUG] Subnet create configuration: %#v", opts)
Expand Down Expand Up @@ -149,21 +165,30 @@
func gatherSubnetUpdateRequests(d *schema.ResourceData) []*cloudscale.SubnetUpdateRequest {
requests := make([]*cloudscale.SubnetUpdateRequest, 0)

for _, attribute := range []string{"gateway_address", "dns_servers", "tags"} {
for _, attribute := range []string{"gateway_address", "dns_servers", "tags", "disable_dns_servers"} {
if d.HasChange(attribute) {
log.Printf("[INFO] Attribute %s changed", attribute)
opts := &cloudscale.SubnetUpdateRequest{}
requests = append(requests, opts)

if attribute == "gateway_address" {
opts.GatewayAddress = d.Get(attribute).(string)
} else if attribute == "dns_servers" {
dnsServers := d.Get("dns_servers").([]any)
s := make([]string, len(dnsServers))
for i := range dnsServers {
s[i] = dnsServers[i].(string)
} else if attribute == "dns_servers" || attribute == "disable_dns_servers" {
disableDnsServers := d.Get("disable_dns_servers").(bool)
if disableDnsServers {
opts.DNSServers = &[]string{}

Check failure on line 179 in cloudscale/resource_cloudscale_subnet.go

View workflow job for this annotation

GitHub Actions / Build

cannot use &[]string{} (value of type *[]string) as type []string in assignment
} else {
if dnsServersRaw, ok := d.GetOk("dns_servers"); ok {
dnsServers := dnsServersRaw.([]interface{})
dnsServersStr := make([]string, len(dnsServers))
for i := range dnsServers {
dnsServersStr[i] = dnsServers[i].(string)
}
opts.DNSServers = &dnsServersStr

Check failure on line 187 in cloudscale/resource_cloudscale_subnet.go

View workflow job for this annotation

GitHub Actions / Build

cannot use &dnsServersStr (value of type *[]string) as type []string in assignment
} else {
opts.DNSServers = &cloudscale.UseCloudscaleDefaults

Check failure on line 189 in cloudscale/resource_cloudscale_subnet.go

View workflow job for this annotation

GitHub Actions / Build

undefined: cloudscale.UseCloudscaleDefaults
}
}
opts.DNSServers = s
} else if attribute == "tags" {
opts.Tags = CopyTags(d)
}
Expand Down
107 changes: 104 additions & 3 deletions cloudscale/resource_cloudscale_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ func TestAccCloudscaleSubnet_AllAttrs(t *testing.T) {
})
}

func TestAccCloudscaleSubnet_NoDNS_Create(t *testing.T) {
var subnet cloudscale.Subnet
var network cloudscale.Network

rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudscaleSubnetDestroy,
Steps: []resource.TestStep{
{
Config: subnetconfigNoDns(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudscaleNetworkExists("cloudscale_network.basic", &network),
testAccCheckCloudscaleSubnetExists("cloudscale_subnet.basic", &subnet),
testAccCheckCloudscaleSubnetOnNetwork(&subnet, &network),
resource.TestCheckResourceAttr("cloudscale_subnet.basic", "cidr", "10.11.12.0/24"),
resource.TestCheckResourceAttrSet("cloudscale_subnet.basic", "network_href"),
resource.TestCheckResourceAttrSet("cloudscale_subnet.basic", "network_uuid"),
resource.TestCheckResourceAttr("cloudscale_subnet.basic", "network_name", fmt.Sprintf("terraform-%d", rInt)),
resource.TestCheckResourceAttr("cloudscale_subnet.basic", "gateway_address", "10.11.12.10"),
resource.TestCheckResourceAttr("cloudscale_subnet.basic", "dns_servers.#", "0"),
),
},
},
})
}

func TestAccCloudscaleSubnet_Update(t *testing.T) {
var subnet cloudscale.Subnet
var network cloudscale.Network
Expand Down Expand Up @@ -124,6 +153,20 @@ func TestAccCloudscaleSubnet_Update(t *testing.T) {
resource.TestCheckResourceAttr("cloudscale_subnet.basic", "dns_servers.2", "9.10.11.12"),
),
},
{
Config: subnetconfigNoDns(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudscaleNetworkExists("cloudscale_network.basic", &network),
testAccCheckCloudscaleSubnetExists("cloudscale_subnet.basic", &subnet),
testAccCheckCloudscaleSubnetOnNetwork(&subnet, &network),
resource.TestCheckResourceAttr("cloudscale_subnet.basic", "cidr", "10.11.12.0/24"),
resource.TestCheckResourceAttrSet("cloudscale_subnet.basic", "network_href"),
resource.TestCheckResourceAttrSet("cloudscale_subnet.basic", "network_uuid"),
resource.TestCheckResourceAttr("cloudscale_subnet.basic", "network_name", fmt.Sprintf("terraform-%d", rInt)),
resource.TestCheckResourceAttr("cloudscale_subnet.basic", "gateway_address", "10.11.12.10"),
resource.TestCheckResourceAttr("cloudscale_subnet.basic", "dns_servers.#", "0"),
),
},
},
})
}
Expand Down Expand Up @@ -180,6 +223,28 @@ func TestAccCloudscaleSubnet_ServerWithPublicAndPrivate(t *testing.T) {
})
}

func TestAccCloudscaleSubnet_ValidationError(t *testing.T) {
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudscaleSubnetDestroy,
Steps: []resource.TestStep{
{
Config: subnetconfigInvalidDnsCombination(rInt),
ExpectError: regexp.MustCompile(`Error: Conflicting configuration arguments.*`),
Check: resource.ComposeTestCheckFunc(),
},
{
Config: subnetconfigInvalidEmptyDnsCombination(rInt),
ExpectError: regexp.MustCompile(`Error: Conflicting configuration arguments.*`),
Check: resource.ComposeTestCheckFunc(),
},
},
})
}

func TestAccCloudscaleSubnet_ServerAndMultipleSubnets(t *testing.T) {
count := 2
networks := make([]cloudscale.Network, count, count)
Expand Down Expand Up @@ -317,9 +382,10 @@ func TestAccCloudscaleSubnet_import_basic(t *testing.T) {
),
},
{
ResourceName: "cloudscale_subnet.basic",
ImportState: true,
ImportStateVerify: true,
ResourceName: "cloudscale_subnet.basic",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"disable_dns_servers"},
},
{
ResourceName: "cloudscale_subnet.basic",
Expand Down Expand Up @@ -488,6 +554,41 @@ resource "cloudscale_subnet" "basic" {
`)
}

func subnetconfigNoDns(rInt int) string {
return networkconfigMinimal(rInt, false) + "\n" + fmt.Sprintf(`
resource "cloudscale_subnet" "basic" {
cidr = "10.11.12.0/24"
network_uuid = cloudscale_network.basic.id
gateway_address = "10.11.12.10"
disable_dns_servers = true
}
`)
}

func subnetconfigInvalidDnsCombination(rInt int) string {
return networkconfigMinimal(rInt, false) + "\n" + fmt.Sprintf(`
resource "cloudscale_subnet" "basic" {
cidr = "10.11.12.0/24"
network_uuid = cloudscale_network.basic.id
gateway_address = "10.11.12.10"
dns_servers = ["1.2.3.4", "5.6.7.8", "9.10.11.12"]
disable_dns_servers = true
}
`)
}

func subnetconfigInvalidEmptyDnsCombination(rInt int) string {
return networkconfigMinimal(rInt, false) + "\n" + fmt.Sprintf(`
resource "cloudscale_subnet" "basic" {
cidr = "10.11.12.0/24"
network_uuid = cloudscale_network.basic.id
gateway_address = "10.11.12.10"
dns_servers = []
disable_dns_servers = true
}
`)
}

func serverConfigWithPublicAndLayerThree(rInt int, fixedAddress string) string {
template := `
resource "cloudscale_server" "basic" {
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/subnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ The following arguments are supported when creating/changing subnets:
* `cidr` - (Required) The address range in CIDR notation. Must be at least /24.
* `network_uuid` - (Required) The network of the subnet.
* `gateway_address` - (Optional) The gateway address of the subnet.
* `dns_servers` - (Optional) A list of DNS resolver IP addresses, that act as DNS servers.
* `dns_servers` - (Optional) A list of DNS resolver IP addresses, that act as DNS servers. If not defined, default DNS servers are used. Do not explicitly set to an empty list (`dns_servers = []`), use `disable_dns_servers` instead.
* `disable_dns_servers` - (Optional) If set to true, no DNS servers are set. Can not be used together with `dns_servers`.
* `tags` - (Optional) Tags allow you to assign custom metadata to resources:
```
tags = {
Expand Down
3 changes: 3 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.terraform*
terraform.tfstate
terraform.tfstate.backup
Loading