Fix import/read/concurrency for WLAN

This commit is contained in:
Paul Tyng
2019-12-28 19:49:30 -05:00
parent f050b19c87
commit ab915f7722
2 changed files with 65 additions and 13 deletions

View File

@@ -2,6 +2,7 @@ package provider
import (
"fmt"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
@@ -96,6 +97,28 @@ func resourceWLANCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(resp.ID)
return resourceWLANSetResourceData(resp, d)
}
func resourceWLANSetResourceData(resp *unifi.WLAN, d *schema.ResourceData) error {
var err error
vlan := 0
if resp.VLANEnabled {
vlan, err = strconv.Atoi(resp.VLAN)
if err != nil {
return err
}
}
d.Set("name", resp.Name)
d.Set("vlan_id", vlan)
d.Set("passphrase", resp.XPassphrase)
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)
return nil
}
@@ -104,7 +127,7 @@ func resourceWLANRead(d *schema.ResourceData, meta interface{}) error {
id := d.Id()
_, err := c.c.GetWLAN(c.site, id)
resp, err := c.c.GetWLAN(c.site, id)
if _, ok := err.(*unifi.NotFoundError); ok {
d.SetId("")
return nil
@@ -113,7 +136,7 @@ func resourceWLANRead(d *schema.ResourceData, meta interface{}) error {
return err
}
return nil
return resourceWLANSetResourceData(resp, d)
}
func resourceWLANUpdate(d *schema.ResourceData, meta interface{}) error {

View File

@@ -1,23 +1,43 @@
package provider
import (
"os"
"strconv"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
func wlanImportStep() resource.TestStep {
return importStep("unifi_wlan.test",
"name", "passphrase", "vlan_id", "wlan_group_id",
"user_group_id", "security",
)
var wlanConcurrency chan struct{}
func init() {
wcs := os.Getenv("UNIFI_ACC_WLAN_CONCURRENCY")
if wcs == "" {
// default concurrent SSIDs
wcs = "1"
}
wc, err := strconv.Atoi(wcs)
if err != nil {
panic(err)
}
wlanConcurrency = make(chan struct{}, wc)
}
func TestAccWLAN_wpapsk(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
Providers: providers,
// TODO: CheckDestroy: ,
PreCheck: func() {
preCheck(t)
wlanConcurrency <- struct{}{}
},
CheckDestroy: func(*terraform.State) error {
// TODO: actual CheckDestroy
<-wlanConcurrency
return nil
},
Steps: []resource.TestStep{
{
Config: testAccWLANConfig_wpapsk,
@@ -25,16 +45,25 @@ func TestAccWLAN_wpapsk(t *testing.T) {
// testCheckNetworkExists(t, "name"),
),
},
wlanImportStep(),
importStep("unifi_wlan.test"),
},
})
}
func TestAccWLAN_open(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
Providers: providers,
// TODO: CheckDestroy: ,
PreCheck: func() {
preCheck(t)
wlanConcurrency <- struct{}{}
},
CheckDestroy: func(*terraform.State) error {
// TODO: actual CheckDestroy
<-wlanConcurrency
return nil
},
Steps: []resource.TestStep{
{
Config: testAccWLANConfig_open,
@@ -42,7 +71,7 @@ func TestAccWLAN_open(t *testing.T) {
// testCheckNetworkExists(t, "name"),
),
},
wlanImportStep(),
importStep("unifi_wlan.test"),
},
})
}