Add unifi_user ip and hostname
Bump go-unifi to 1.1.0
This commit is contained in:
47
internal/provider/cidr.go
Normal file
47
internal/provider/cidr.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
||||
)
|
||||
|
||||
func cidrDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
|
||||
_, oldNet, err := net.ParseCIDR(old)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
_, newNet, err := net.ParseCIDR(new)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return oldNet.String() == newNet.String()
|
||||
}
|
||||
|
||||
func cidrZeroBased(cidr string) string {
|
||||
_, cidrNet, err := net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if len(cidrNet.Mask) == net.IPv6len {
|
||||
return ""
|
||||
}
|
||||
|
||||
return cidrNet.String()
|
||||
}
|
||||
|
||||
func cidrOneBased(cidr string) string {
|
||||
_, cidrNet, err := net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if len(cidrNet.Mask) == net.IPv6len {
|
||||
return ""
|
||||
}
|
||||
|
||||
cidrNet.IP[3]++
|
||||
|
||||
return cidrNet.String()
|
||||
}
|
||||
@@ -32,8 +32,9 @@ func resourceNetwork() *schema.Resource {
|
||||
Optional: true,
|
||||
},
|
||||
"subnet": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
DiffSuppressFunc: cidrDiffSuppress,
|
||||
},
|
||||
"network_group": {
|
||||
Type: schema.TypeString,
|
||||
@@ -94,7 +95,7 @@ func resourceNetworkGetResourceData(d *schema.ResourceData) (*unifi.Network, err
|
||||
Name: d.Get("name").(string),
|
||||
Purpose: d.Get("purpose").(string),
|
||||
VLAN: vlan,
|
||||
IPSubnet: d.Get("subnet").(string),
|
||||
IPSubnet: cidrOneBased(d.Get("subnet").(string)),
|
||||
NetworkGroup: d.Get("network_group").(string),
|
||||
DHCPDStart: d.Get("dhcp_start").(string),
|
||||
DHCPDStop: d.Get("dhcp_stop").(string),
|
||||
@@ -127,7 +128,7 @@ func resourceNetworkSetResourceData(resp *unifi.Network, d *schema.ResourceData)
|
||||
d.Set("name", resp.Name)
|
||||
d.Set("purpose", resp.Purpose)
|
||||
d.Set("vlan_id", vlan)
|
||||
d.Set("subnet", resp.IPSubnet)
|
||||
d.Set("subnet", cidrZeroBased(resp.IPSubnet))
|
||||
d.Set("network_group", resp.NetworkGroup)
|
||||
d.Set("dhcp_start", resp.DHCPDStart)
|
||||
d.Set("dhcp_stop", resp.DHCPDStop)
|
||||
|
||||
@@ -14,19 +14,19 @@ func TestAccNetwork_basic(t *testing.T) {
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccNetworkConfig("10.0.202.1/24", 202, true),
|
||||
Config: testAccNetworkConfig("10.0.202.0/24", 202, true),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "domain_name", "foo.local"),
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "subnet", "10.0.202.1/24"),
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "subnet", "10.0.202.0/24"),
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "vlan_id", "202"),
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "igmp_snooping", "true"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
{
|
||||
Config: testAccNetworkConfig("10.0.203.1/24", 203, false),
|
||||
Config: testAccNetworkConfig("10.0.203.0/24", 203, false),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "subnet", "10.0.203.1/24"),
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "subnet", "10.0.203.0/24"),
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "vlan_id", "203"),
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "igmp_snooping", "false"),
|
||||
),
|
||||
@@ -36,6 +36,23 @@ func TestAccNetwork_basic(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccNetwork_weird_cidr(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
Providers: providers,
|
||||
PreCheck: func() { preCheck(t) },
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccNetworkConfig("10.0.202.3/24", 202, true),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "subnet", "10.0.202.0/24"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccNetworkConfig(subnet string, vlan int, igmpSnoop bool) string {
|
||||
return fmt.Sprintf(`
|
||||
variable "subnet" {
|
||||
|
||||
@@ -41,6 +41,7 @@ func resourceUser() *schema.Resource {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
// TODO: combine this with output IP for a single attribute ip_address?
|
||||
"fixed_ip": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
@@ -66,6 +67,16 @@ func resourceUser() *schema.Resource {
|
||||
Optional: true,
|
||||
Default: false,
|
||||
},
|
||||
|
||||
// computed only attributes
|
||||
"hostname": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"ip": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -145,6 +156,9 @@ func resourceUserSetResourceData(resp *unifi.User, d *schema.ResourceData) error
|
||||
d.Set("network_id", resp.NetworkID)
|
||||
d.Set("blocked", resp.Blocked)
|
||||
|
||||
d.Set("hostname", resp.Hostname)
|
||||
d.Set("ip", resp.IP)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -162,6 +176,18 @@ func resourceUserRead(d *schema.ResourceData, meta interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// for some reason the IP address is only on this endpoint, so issue another request
|
||||
macResp, err := c.c.GetUserByMAC(c.site, resp.MAC)
|
||||
if _, ok := err.(*unifi.NotFoundError); ok {
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp.IP = macResp.IP
|
||||
|
||||
return resourceUserSetResourceData(resp, d)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user