Minor fixes for mac address filtering
This commit is contained in:
16
internal/provider/mac.go
Normal file
16
internal/provider/mac.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
||||
)
|
||||
|
||||
var macAddressRegexp = regexp.MustCompile("^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$")
|
||||
|
||||
func macDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
|
||||
old = strings.TrimSpace(strings.ReplaceAll(strings.ToLower(old), "-", ":"))
|
||||
new = strings.TrimSpace(strings.ReplaceAll(strings.ToLower(new), "-", ":"))
|
||||
return old == new
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
||||
"github.com/paultyng/go-unifi/unifi"
|
||||
)
|
||||
@@ -19,14 +17,10 @@ func resourceUser() *schema.Resource {
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"mac": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
|
||||
old = strings.TrimSpace(strings.ReplaceAll(strings.ToLower(old), "-", ":"))
|
||||
new = strings.TrimSpace(strings.ReplaceAll(strings.ToLower(new), "-", ":"))
|
||||
return old == new
|
||||
},
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
DiffSuppressFunc: macDiffSuppressFunc,
|
||||
// Validation:
|
||||
},
|
||||
"name": {
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
|
||||
"github.com/paultyng/go-unifi/unifi"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func resourceWLAN() *schema.Resource {
|
||||
@@ -61,14 +60,14 @@ func resourceWLAN() *schema.Resource {
|
||||
"mac_filter_enabled": {
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
Default: false,
|
||||
},
|
||||
"mac_filter_list": &schema.Schema{
|
||||
Type: schema.TypeList,
|
||||
Type: schema.TypeSet,
|
||||
Optional: true,
|
||||
Elem: &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
ValidateFunc: validation.StringMatch(regexp.MustCompile("^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$"), "Mac address is invalid"),
|
||||
Type: schema.TypeString,
|
||||
ValidateFunc: validation.StringMatch(macAddressRegexp, "Mac address is invalid"),
|
||||
DiffSuppressFunc: macDiffSuppressFunc,
|
||||
},
|
||||
},
|
||||
"mac_filter_policy": {
|
||||
@@ -83,14 +82,23 @@ func resourceWLAN() *schema.Resource {
|
||||
|
||||
func resourceWLANGetResourceData(d *schema.ResourceData) (*unifi.WLAN, error) {
|
||||
vlan := d.Get("vlan_id").(int)
|
||||
|
||||
security := d.Get("security").(string)
|
||||
passphrase := d.Get("passphrase").(string)
|
||||
|
||||
switch security {
|
||||
case "open":
|
||||
passphrase = ""
|
||||
}
|
||||
|
||||
macFilterEnabled := d.Get("mac_filter_enabled").(bool)
|
||||
macFilterList, err := setToStringSlice(d.Get("mac_filter_list").(*schema.Set))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !macFilterEnabled {
|
||||
macFilterList = nil
|
||||
}
|
||||
|
||||
return &unifi.WLAN{
|
||||
Name: d.Get("name").(string),
|
||||
VLAN: vlan,
|
||||
@@ -101,8 +109,8 @@ func resourceWLANGetResourceData(d *schema.ResourceData) (*unifi.WLAN, error) {
|
||||
UserGroupID: d.Get("user_group_id").(string),
|
||||
Security: security,
|
||||
MulticastEnhanceEnabled: d.Get("multicast_enhance").(bool),
|
||||
MACFilterEnabled: d.Get("mac_filter_enabled").(bool),
|
||||
MACFilterList: d.Get("mac_filter_list").([]string),
|
||||
MACFilterEnabled: macFilterEnabled,
|
||||
MACFilterList: macFilterList,
|
||||
MACFilterPolicy: d.Get("mac_filter_policy").(string),
|
||||
|
||||
VLANEnabled: vlan != 0 && vlan != 1,
|
||||
@@ -146,12 +154,19 @@ func resourceWLANSetResourceData(resp *unifi.WLAN, d *schema.ResourceData) error
|
||||
|
||||
security := resp.Security
|
||||
passphrase := resp.XPassphrase
|
||||
|
||||
switch security {
|
||||
case "open":
|
||||
passphrase = ""
|
||||
}
|
||||
|
||||
macFilterEnabled := resp.MACFilterEnabled
|
||||
var macFilterList *schema.Set
|
||||
macFilterPolicy := "deny"
|
||||
if macFilterEnabled {
|
||||
macFilterList = stringSliceToSet(resp.MACFilterList)
|
||||
macFilterPolicy = resp.MACFilterPolicy
|
||||
}
|
||||
|
||||
d.Set("name", resp.Name)
|
||||
d.Set("vlan_id", vlan)
|
||||
d.Set("passphrase", passphrase)
|
||||
@@ -161,9 +176,9 @@ func resourceWLANSetResourceData(resp *unifi.WLAN, d *schema.ResourceData) error
|
||||
d.Set("user_group_id", resp.UserGroupID)
|
||||
d.Set("security", security)
|
||||
d.Set("multicast_enhance", resp.MulticastEnhanceEnabled)
|
||||
d.Set("mac_filter_enabled", resp.MACFilterEnabled)
|
||||
d.Set("mac_filter_list", resp.MACFilterList)
|
||||
d.Set("mac_filter_policy", resp.MACFilterPolicy)
|
||||
d.Set("mac_filter_enabled", macFilterEnabled)
|
||||
d.Set("mac_filter_list", macFilterList)
|
||||
d.Set("mac_filter_policy", macFilterPolicy)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -72,6 +72,20 @@ func TestAccWLAN_open(t *testing.T) {
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_open_mac_filter,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
{
|
||||
Config: testAccWLANConfig_open,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
// testCheckNetworkExists(t, "name"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_wlan.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -149,3 +163,23 @@ resource "unifi_wlan" "test" {
|
||||
security = "open"
|
||||
}
|
||||
`
|
||||
|
||||
const testAccWLANConfig_open_mac_filter = `
|
||||
data "unifi_wlan_group" "default" {
|
||||
}
|
||||
|
||||
data "unifi_user_group" "default" {
|
||||
}
|
||||
|
||||
resource "unifi_wlan" "test" {
|
||||
name = "tfacc-open"
|
||||
vlan_id = 202
|
||||
wlan_group_id = data.unifi_wlan_group.default.id
|
||||
user_group_id = data.unifi_user_group.default.id
|
||||
security = "open"
|
||||
|
||||
mac_filter_enabled = true
|
||||
mac_filter_list = ["ab:cd:ef:12:34:56"]
|
||||
mac_filter_policy = "allow"
|
||||
}
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user