Add support for wlan update

This commit is contained in:
Paul Tyng
2020-01-10 11:21:18 -05:00
parent 43ae5113e8
commit 9a8560d268
6 changed files with 110 additions and 34 deletions

View File

@@ -65,6 +65,10 @@ func (c *lazyClient) GetWLAN(site, id string) (*unifi.WLAN, error) {
c.init()
return c.inner.GetWLAN(site, id)
}
func (c *lazyClient) UpdateWLAN(site string, d *unifi.WLAN) (*unifi.WLAN, error) {
c.init()
return c.inner.UpdateWLAN(site, d)
}
func (c *lazyClient) DeleteUserGroup(site, id string) error {
c.init()
return c.inner.DeleteUserGroup(site, id)

View File

@@ -89,6 +89,7 @@ type unifiClient interface {
DeleteWLAN(site, id string) error
CreateWLAN(site string, d *unifi.WLAN) (*unifi.WLAN, error)
GetWLAN(site, id string) (*unifi.WLAN, error)
UpdateWLAN(site string, d *unifi.WLAN) (*unifi.WLAN, error)
GetUser(site, id string) (*unifi.User, error)
GetUserByMAC(site, mac string) (*unifi.User, error)

View File

@@ -5,9 +5,6 @@ import (
"testing"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/paultyng/terraform-provider-unifi/unifi"
)
func TestAccNetwork_basic(t *testing.T) {
@@ -19,7 +16,6 @@ func TestAccNetwork_basic(t *testing.T) {
{
Config: testAccNetworkConfig("10.0.202.1/24", 202),
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
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", "vlan_id", "202"),
@@ -29,7 +25,6 @@ func TestAccNetwork_basic(t *testing.T) {
{
Config: testAccNetworkConfig("10.0.203.1/24", 203),
Check: resource.ComposeTestCheckFunc(
testCheckNetworkExists(t, "tfacc", nil),
resource.TestCheckResourceAttr("unifi_network.test", "subnet", "10.0.203.1/24"),
resource.TestCheckResourceAttr("unifi_network.test", "vlan_id", "203"),
),
@@ -39,26 +34,6 @@ func TestAccNetwork_basic(t *testing.T) {
})
}
func testCheckNetworkExists(t *testing.T, name string, network *unifi.Network) resource.TestCheckFunc {
return func(s *terraform.State) error {
networks, err := testClient.ListNetwork("default")
if err != nil {
return err
}
for _, net := range networks {
if net.Name == name {
if network != nil {
*network = net
}
return nil
}
}
return fmt.Errorf("unable to find network %q", name)
}
}
func testAccNetworkConfig(subnet string, vlan int) string {
return fmt.Sprintf(`
variable "subnet" {

View File

@@ -58,20 +58,25 @@ func resourceWLAN() *schema.Resource {
}
}
func resourceWLANCreate(d *schema.ResourceData, meta interface{}) error {
c := meta.(*client)
func resourceWLANGetResourceData(d *schema.ResourceData) (*unifi.WLAN, error) {
vlan := d.Get("vlan_id").(int)
security := d.Get("security").(string)
passphrase := d.Get("passphrase").(string)
req := &unifi.WLAN{
switch security {
case "open":
passphrase = ""
}
return &unifi.WLAN{
Name: d.Get("name").(string),
VLAN: vlan,
XPassphrase: d.Get("passphrase").(string),
XPassphrase: passphrase,
HideSSID: d.Get("hide_ssid").(bool),
IsGuest: d.Get("is_guest").(bool),
WLANGroupID: d.Get("wlan_group_id").(string),
UserGroupID: d.Get("user_group_id").(string),
Security: d.Get("security").(string),
Security: security,
VLANEnabled: vlan != 0 && vlan != 1,
@@ -85,6 +90,15 @@ func resourceWLANCreate(d *schema.ResourceData, meta interface{}) error {
DTIMMode: "default",
No2GhzOui: true,
MinrateNgCckRatesEnabled: true,
}, nil
}
func resourceWLANCreate(d *schema.ResourceData, meta interface{}) error {
c := meta.(*client)
req, err := resourceWLANGetResourceData(d)
if err != nil {
return err
}
resp, err := c.c.CreateWLAN(c.site, req)
@@ -103,14 +117,22 @@ func resourceWLANSetResourceData(resp *unifi.WLAN, d *schema.ResourceData) error
vlan = resp.VLAN
}
security := resp.Security
passphrase := resp.XPassphrase
switch security {
case "open":
passphrase = ""
}
d.Set("name", resp.Name)
d.Set("vlan_id", vlan)
d.Set("passphrase", resp.XPassphrase)
d.Set("passphrase", passphrase)
d.Set("hide_ssid", resp.HideSSID)
d.Set("is_guest", resp.IsGuest)
d.Set("wlan_group_id", resp.WLANGroupID)
d.Set("user_group_id", resp.UserGroupID)
d.Set("security", resp.Security)
d.Set("security", security)
return nil
}
@@ -133,7 +155,22 @@ func resourceWLANRead(d *schema.ResourceData, meta interface{}) error {
}
func resourceWLANUpdate(d *schema.ResourceData, meta interface{}) error {
panic("not implemented")
c := meta.(*client)
req, err := resourceWLANGetResourceData(d)
if err != nil {
return err
}
req.ID = d.Id()
req.SiteID = c.site
resp, err := c.c.UpdateWLAN(c.site, req)
if err != nil {
return err
}
return resourceWLANSetResourceData(resp, d)
}
func resourceWLANDelete(d *schema.ResourceData, meta interface{}) error {

View File

@@ -76,6 +76,45 @@ func TestAccWLAN_open(t *testing.T) {
})
}
func TestAccWLAN_change_security(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
Providers: providers,
PreCheck: func() {
preCheck(t)
wlanConcurrency <- struct{}{}
},
CheckDestroy: func(*terraform.State) error {
// TODO: actual CheckDestroy
<-wlanConcurrency
return nil
},
Steps: []resource.TestStep{
{
Config: testAccWLANConfig_wpapsk,
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
),
},
importStep("unifi_wlan.test"),
{
Config: testAccWLANConfig_open,
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
),
},
importStep("unifi_wlan.test"),
{
Config: testAccWLANConfig_wpapsk,
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
),
},
},
})
}
const testAccWLANConfig_wpapsk = `
data "unifi_wlan_group" "default" {
}

View File

@@ -92,3 +92,23 @@ func (c *Client) CreateWLAN(site string, d *WLAN) (*WLAN, error) {
return &new, nil
}
func (c *Client) UpdateWLAN(site string, d *WLAN) (*WLAN, error) {
var respBody struct {
Meta meta `json:"meta"`
Data []WLAN `json:"data"`
}
err := c.do("PUT", fmt.Sprintf("s/%s/rest/wlanconf/%s", site, d.ID), d, &respBody)
if err != nil {
return nil, err
}
if len(respBody.Data) != 1 {
return nil, &NotFoundError{}
}
new := respBody.Data[0]
return &new, nil
}