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 support for unifi_network.dhcp_ntp #372

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/resources/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ resource "unifi_network" "wan" {
### Optional

- `dhcp_dns` (List of String) Specifies the IPv4 addresses for the DNS server to be returned from the DHCP server. Leave blank to disable this feature.
- `dhcp_ntp` (List of String) Specified the IPv4 addresses for the NTP servers to be returned from the DHCP server in response to an Option 42 (NTP Servers) request. Leave blank to disable this feature.
- `dhcp_enabled` (Boolean) Specifies whether DHCP is enabled or not on this network.
- `dhcp_lease` (Number) Specifies the lease time for DHCP addresses in seconds. Defaults to `86400`.
- `dhcp_relay_enabled` (Boolean) Specifies whether DHCP relay is enabled or not on this network.
Expand Down
35 changes: 35 additions & 0 deletions internal/provider/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ func resourceNetwork() *schema.Resource {
Optional: true,
Default: 86400,
},
"dhcp_ntp": {
Description: "Specified the address for the NTP server to be returned from the DHCP server.",
Type: schema.TypeList,
Optional: true,
MaxItems: 2,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.All(
validation.IsIPv4Address,
validation.StringLenBetween(1, 50),
),
},
},
"dhcp_dns": {
Description: "Specifies the IPv4 addresses for the DNS server to be returned from the DHCP " +
"server. Leave blank to disable this feature.",
Expand Down Expand Up @@ -405,6 +418,10 @@ func resourceNetworkGetResourceData(d *schema.ResourceData, meta interface{}) (*
if err != nil {
return nil, fmt.Errorf("unable to convert dhcp_dns to string slice: %w", err)
}
dhcpNtp, err := listToStringSlice(d.Get("dhcp_ntp").([]interface{}))
if err != nil {
return nil, fmt.Errorf("unable to convert dhcp_ntp to string slice: %w", err)
}
dhcpV6DNS, err := listToStringSlice(d.Get("dhcp_v6_dns").([]interface{}))
if err != nil {
return nil, fmt.Errorf("unable to convert dhcp_v6_dns to string slice: %w", err)
Expand Down Expand Up @@ -439,6 +456,10 @@ func resourceNetworkGetResourceData(d *schema.ResourceData, meta interface{}) (*
DHCPDDNS3: append(dhcpDNS, "", "", "")[2],
DHCPDDNS4: append(dhcpDNS, "", "", "", "")[3],

DHCPDNtpEnabled: len(dhcpNtp) > 0,
DHCPDNtp1: append(dhcpNtp, "")[0],
DHCPDNtp2: append(dhcpNtp, "", "")[1],

VLANEnabled: vlan != 0 && vlan != 1,

Enabled: true,
Expand Down Expand Up @@ -533,6 +554,19 @@ func resourceNetworkSetResourceData(resp *unifi.Network, d *schema.ResourceData,
dhcpLease = 86400
}

dhcpNTP := []string{}
if resp.DHCPDNtpEnabled {
for _, ntp := range []string{
resp.DHCPDNtp1,
resp.DHCPDNtp2,
} {
if ntp == "" {
continue
}
dhcpNTP = append(dhcpNTP, ntp)
}
}

dhcpDNS := []string{}
if resp.DHCPDDNSEnabled {
for _, dns := range []string{
Expand Down Expand Up @@ -568,6 +602,7 @@ func resourceNetworkSetResourceData(resp *unifi.Network, d *schema.ResourceData,
d.Set("subnet", cidrZeroBased(resp.IPSubnet))
d.Set("network_group", resp.NetworkGroup)

d.Set("dhcp_ntp", dhcpNTP)
d.Set("dhcp_dns", dhcpDNS)
d.Set("dhcp_enabled", resp.DHCPDEnabled)
d.Set("dhcp_lease", dhcpLease)
Expand Down
42 changes: 31 additions & 11 deletions internal/provider/resource_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAccNetwork_basic(t *testing.T) {
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccNetworkConfig(name, subnet1, vlan1, true, nil),
Config: testAccNetworkConfig(name, subnet1, vlan1, true, nil, nil),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_network.test", "domain_name", "foo.local"),
resource.TestCheckResourceAttr("unifi_network.test", "vlan_id", strconv.Itoa(vlan1)),
Expand All @@ -32,7 +32,7 @@ func TestAccNetwork_basic(t *testing.T) {
},
importStep("unifi_network.test"),
{
Config: testAccNetworkConfig(name, subnet2, vlan2, false, nil),
Config: testAccNetworkConfig(name, subnet2, vlan2, false, nil, nil),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_network.test", "vlan_id", strconv.Itoa(vlan2)),
resource.TestCheckResourceAttr("unifi_network.test", "igmp_snooping", "false"),
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestAccNetwork_weird_cidr(t *testing.T) {
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccNetworkConfig(name, subnet, vlan, true, nil),
Config: testAccNetworkConfig(name, subnet, vlan, true, nil, nil),
Check: resource.ComposeTestCheckFunc(
// TODO: ...
),
Expand All @@ -80,28 +80,28 @@ func TestAccNetwork_dhcp_dns(t *testing.T) {
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccNetworkConfig(name, subnet, vlan, true, []string{"192.168.1.101"}),
Config: testAccNetworkConfig(name, subnet, vlan, true, []string{"192.168.1.101"}, nil),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_dns.0", "192.168.1.101"),
),
},
importStep("unifi_network.test"),
{
Config: testAccNetworkConfig(name, subnet, vlan, true, []string{"192.168.1.101", "192.168.1.102"}),
Config: testAccNetworkConfig(name, subnet, vlan, true, []string{"192.168.1.101", "192.168.1.102"}, nil),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_dns.0", "192.168.1.101"),
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_dns.1", "192.168.1.102"),
),
},
importStep("unifi_network.test"),
{
Config: testAccNetworkConfig(name, subnet, vlan, true, nil),
Config: testAccNetworkConfig(name, subnet, vlan, true, nil, nil),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_dns.#", "0"),
),
},
{
Config: testAccNetworkConfig(name, subnet, vlan, true, []string{"192.168.1.101"}),
Config: testAccNetworkConfig(name, subnet, vlan, true, []string{"192.168.1.101"}, nil),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_dns.0", "192.168.1.101"),
),
Expand All @@ -110,6 +110,25 @@ func TestAccNetwork_dhcp_dns(t *testing.T) {
})
}

func TestAccNetwork_dhcp_ntp(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
subnet, vlan := getTestVLAN(t)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccNetworkConfig(name, subnet, vlan, true, nil, []string{"192.168.1.123"}),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_ntp.0", "192.168.1.123"),
),
},
},
})
}

func TestAccNetwork_dhcp_boot(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")
subnet, vlan := getTestVLAN(t)
Expand Down Expand Up @@ -312,10 +331,10 @@ func TestAccNetwork_importByName(t *testing.T) {
Steps: []resource.TestStep{
// Apply and import network by name.
{
Config: testAccNetworkConfig(name, subnet1, vlan1, true, nil),
Config: testAccNetworkConfig(name, subnet1, vlan1, true, nil, nil),
},
{
Config: testAccNetworkConfig(name, subnet1, vlan1, true, nil),
Config: testAccNetworkConfig(name, subnet1, vlan1, true, nil, nil),
ResourceName: "unifi_network.test",
ImportState: true,
ImportStateVerify: true,
Expand Down Expand Up @@ -470,7 +489,7 @@ resource "unifi_network" "test" {
`, name, subnet, vlan)
}

func testAccNetworkConfig(name string, subnet *net.IPNet, vlan int, igmpSnoop bool, dhcpDNS []string) string {
func testAccNetworkConfig(name string, subnet *net.IPNet, vlan int, igmpSnoop bool, dhcpDNS []string, dhcpNTP []string) string {
return fmt.Sprintf(`
locals {
subnet = "%[2]s"
Expand All @@ -490,8 +509,9 @@ resource "unifi_network" "test" {
igmp_snooping = %[4]t

dhcp_dns = [%[5]s]
dhcp_ntp = [%[6]s]
}
`, name, subnet, vlan, igmpSnoop, strings.Join(quoteStrings(dhcpDNS), ","))
`, name, subnet, vlan, igmpSnoop, strings.Join(quoteStrings(dhcpDNS), ","), strings.Join(quoteStrings(dhcpNTP), ","))
}

func testAccNetworkConfigV6(name string, subnet *net.IPNet, vlan int, ipv6Type string, ipv6Subnet string) string {
Expand Down