Add IPv6 support to static routes (#349)

* Improve tests

* Add Ipv6 support

* We can't create IPv6 static routes without a gateway

* Revert "We can't create IPv6 static routes without a gateway"

This reverts commit dfff85c5b7d22c28d50485536c567e3a8b657136.

* Adopt device

* Fix

* Revert "Adopt device"

This reverts commit a1fb209c7b2cd0d9ae44640fece91f4bafeed2ae.
This commit is contained in:
Joshua Spence
2023-03-10 20:15:54 +11:00
committed by GitHub
parent 65e1701bc5
commit ca71d9e4f9
3 changed files with 159 additions and 36 deletions

View File

@@ -40,9 +40,6 @@ func cidrZeroBased(cidr string) string {
if err != nil {
return ""
}
if len(cidrNet.Mask) == net.IPv6len {
return ""
}
return cidrNet.String()
}
@@ -52,9 +49,6 @@ func cidrOneBased(cidr string) string {
if err != nil {
return ""
}
if len(cidrNet.Mask) == net.IPv6len {
return ""
}
cidrNet.IP[3]++

View File

@@ -64,7 +64,7 @@ func resourceStaticRoute() *schema.Resource {
Description: "The next hop of the static route (only valid for `nexthop-route` type).",
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsIPv4Address,
ValidateFunc: validation.IsIPAddress,
},
"interface": {
Description: "The interface of the static route (only valid for `interface-route` type). This can be `WAN1`, `WAN2`, or a network ID.",

View File

@@ -1,22 +1,67 @@
package provider
import (
"fmt"
"net"
"strconv"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)
func TestAccStaticRoute_nextHop(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
network := &net.IPNet{
IP: net.IPv4(172, 17, 0, 0).To4(),
Mask: net.IPv4Mask(255, 255, 0, 0),
}
distance := 1
nextHop := net.IPv4(172, 16, 0, 1).To4()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccStaticRouteConfig_nextHop,
// Check: resource.ComposeTestCheckFunc(
// // testCheckFirewallGroupExists(t, "name"),
// ),
Config: testAccStaticRouteConfig_nextHop(name, network, distance, &nextHop),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_static_route.test", "type", "nexthop-route"),
resource.TestCheckResourceAttr("unifi_static_route.test", "network", network.String()),
resource.TestCheckResourceAttr("unifi_static_route.test", "name", name),
resource.TestCheckResourceAttr("unifi_static_route.test", "distance", strconv.Itoa(distance)),
resource.TestCheckResourceAttr("unifi_static_route.test", "next_hop", nextHop.String()),
),
},
importStep("unifi_static_route.test"),
},
})
}
func TestAccStaticRoute_nextHop_ipv6(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
network := &net.IPNet{
IP: net.IP{0xfd, 0x6a, 0x37, 0xbe, 0xe3, 0x62, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
Mask: net.IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
}
distance := 1
nextHop := net.IP{0xfd, 0x6a, 0x37, 0xbe, 0xe3, 0x62, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccStaticRouteConfig_nextHop(name, network, distance, &nextHop),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_static_route.test", "type", "nexthop-route"),
resource.TestCheckResourceAttr("unifi_static_route.test", "network", network.String()),
resource.TestCheckResourceAttr("unifi_static_route.test", "name", name),
resource.TestCheckResourceAttr("unifi_static_route.test", "distance", strconv.Itoa(distance)),
resource.TestCheckResourceAttr("unifi_static_route.test", "next_hop", nextHop.String()),
),
},
importStep("unifi_static_route.test"),
},
@@ -24,16 +69,53 @@ func TestAccStaticRoute_nextHop(t *testing.T) {
}
func TestAccStaticRoute_blackhole(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
network := &net.IPNet{
IP: net.IPv4(172, 18, 0, 0).To4(),
Mask: net.IPv4Mask(255, 255, 0, 0),
}
distance := 1
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccStaticRouteConfig_blackhole,
// Check: resource.ComposeTestCheckFunc(
// // testCheckFirewallGroupExists(t, "name"),
// ),
Config: testAccStaticRouteConfig_blackhole(name, network, distance),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_static_route.test", "type", "blackhole"),
resource.TestCheckResourceAttr("unifi_static_route.test", "network", network.String()),
resource.TestCheckResourceAttr("unifi_static_route.test", "name", name),
resource.TestCheckResourceAttr("unifi_static_route.test", "distance", strconv.Itoa(distance)),
),
},
importStep("unifi_static_route.test"),
},
})
}
func TestAccStaticRoute_blackhole_ipv6(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
network := &net.IPNet{
IP: net.IP{0xfd, 0x6a, 0x37, 0xbe, 0xe3, 0x62, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
Mask: net.IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
}
distance := 1
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccStaticRouteConfig_blackhole(name, network, distance),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_static_route.test", "type", "blackhole"),
resource.TestCheckResourceAttr("unifi_static_route.test", "network", network.String()),
resource.TestCheckResourceAttr("unifi_static_route.test", "name", name),
resource.TestCheckResourceAttr("unifi_static_route.test", "distance", strconv.Itoa(distance)),
),
},
importStep("unifi_static_route.test"),
},
@@ -41,47 +123,94 @@ func TestAccStaticRoute_blackhole(t *testing.T) {
}
func TestAccStaticRoute_interface(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
network := &net.IPNet{
IP: net.IPv4(172, 19, 0, 0).To4(),
Mask: net.IPv4Mask(255, 255, 0, 0),
}
distance := 1
networkInterface := "WAN2"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccStaticRouteConfig_interface,
// Check: resource.ComposeTestCheckFunc(
// // testCheckFirewallGroupExists(t, "name"),
// ),
Config: testAccStaticRouteConfig_interface(name, network, distance, networkInterface),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_static_route.test", "type", "interface-route"),
resource.TestCheckResourceAttr("unifi_static_route.test", "network", network.String()),
resource.TestCheckResourceAttr("unifi_static_route.test", "name", name),
resource.TestCheckResourceAttr("unifi_static_route.test", "distance", strconv.Itoa(distance)),
resource.TestCheckResourceAttr("unifi_static_route.test", "interface", networkInterface),
),
},
importStep("unifi_static_route.test"),
},
})
}
const testAccStaticRouteConfig_nextHop = `
func TestAccStaticRoute_interface_ipv6(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
network := &net.IPNet{
IP: net.IP{0xfd, 0x6a, 0x37, 0xbe, 0xe3, 0x62, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
Mask: net.IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
}
distance := 1
networkInterface := "WAN2"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccStaticRouteConfig_interface(name, network, distance, networkInterface),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_static_route.test", "type", "interface-route"),
resource.TestCheckResourceAttr("unifi_static_route.test", "network", network.String()),
resource.TestCheckResourceAttr("unifi_static_route.test", "name", name),
resource.TestCheckResourceAttr("unifi_static_route.test", "distance", strconv.Itoa(distance)),
resource.TestCheckResourceAttr("unifi_static_route.test", "interface", networkInterface),
),
},
importStep("unifi_static_route.test"),
},
})
}
func testAccStaticRouteConfig_nextHop(name string, network *net.IPNet, distance int, nextHop *net.IP) string {
return fmt.Sprintf(`
resource "unifi_static_route" "test" {
type = "nexthop-route"
network = "172.17.0.0/16"
name = "tf-acc basic nexthop"
distance = 1
next_hop = "172.16.0.1"
network = "%[2]s"
name = "%[1]s"
distance = %[3]d
next_hop = "%[4]s"
}
`, name, network, distance, nextHop)
}
`
const testAccStaticRouteConfig_blackhole = `
func testAccStaticRouteConfig_blackhole(name string, network *net.IPNet, distance int) string {
return fmt.Sprintf(`
resource "unifi_static_route" "test" {
type = "blackhole"
network = "172.17.0.0/16"
name = "tf-acc basic blackhole"
distance = 1
network = "%[2]s"
name = "%[1]s"
distance = %[3]d
}
`, name, network, distance)
}
`
const testAccStaticRouteConfig_interface = `
func testAccStaticRouteConfig_interface(name string, network *net.IPNet, distance int, networkInterface string) string {
return fmt.Sprintf(`
resource "unifi_static_route" "test" {
type = "interface-route"
network = "172.17.0.0/16"
name = "tf-acc basic interface"
distance = 1
interface = "WAN2"
network = "%[2]s"
name = "%[1]s"
distance = %[3]d
interface = "%[4]s"
}
`, name, network, distance, networkInterface)
}
`