Skip to content

Commit

Permalink
Add support for wpaeap and radius profiles
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
paultyng committed May 17, 2020
1 parent 382887a commit e618554
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 2 deletions.
45 changes: 45 additions & 0 deletions internal/provider/data_radius_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataRADIUSProfile() *schema.Resource {
return &schema.Resource{
Description: `
unifi_radius_profile data source can be used to retrieve the ID for a RADIUS profile by name.
`,

Read: dataRADIUSProfileRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Default: "Default",
},
},
}
}

func dataRADIUSProfileRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*client)

name := d.Get("name").(string)

profiles, err := c.c.ListRADIUSProfile(context.TODO(), c.site)
if err != nil {
return err
}
for _, g := range profiles {
if g.Name == name {
d.SetId(g.ID)
return nil
}
}

return fmt.Errorf("RADIUS profile not found with name %s", name)
}
20 changes: 20 additions & 0 deletions internal/provider/lazy_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,23 @@ func (c *lazyClient) UpdatePortForward(ctx context.Context, site string, d *unif
c.init(ctx)
return c.inner.UpdatePortForward(ctx, site, d)
}
func (c *lazyClient) ListRADIUSProfile(ctx context.Context, site string) ([]unifi.RADIUSProfile, error) {
c.init(ctx)
return c.inner.ListRADIUSProfile(ctx, site)
}
func (c *lazyClient) GetRADIUSProfile(ctx context.Context, site, id string) (*unifi.RADIUSProfile, error) {
c.init(ctx)
return c.inner.GetRADIUSProfile(ctx, site, id)
}
func (c *lazyClient) DeleteRADIUSProfile(ctx context.Context, site, id string) error {
c.init(ctx)
return c.inner.DeleteRADIUSProfile(ctx, site, id)
}
func (c *lazyClient) CreateRADIUSProfile(ctx context.Context, site string, d *unifi.RADIUSProfile) (*unifi.RADIUSProfile, error) {
c.init(ctx)
return c.inner.CreateRADIUSProfile(ctx, site, d)
}
func (c *lazyClient) UpdateRADIUSProfile(ctx context.Context, site string, d *unifi.RADIUSProfile) (*unifi.RADIUSProfile, error) {
c.init(ctx)
return c.inner.UpdateRADIUSProfile(ctx, site, d)
}
11 changes: 9 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ func Provider() *schema.Provider {
},
},
DataSourcesMap: map[string]*schema.Resource{
"unifi_user_group": dataUserGroup(),
"unifi_wlan_group": dataWLANGroup(),
"unifi_radius_profile": dataRADIUSProfile(),
"unifi_user_group": dataUserGroup(),
"unifi_wlan_group": dataWLANGroup(),
},
ResourcesMap: map[string]*schema.Resource{
"unifi_firewall_group": resourceFirewallGroup(),
Expand Down Expand Up @@ -123,6 +124,12 @@ type unifiClient interface {
DeletePortForward(ctx context.Context, site, id string) error
CreatePortForward(ctx context.Context, site string, d *unifi.PortForward) (*unifi.PortForward, error)
UpdatePortForward(ctx context.Context, site string, d *unifi.PortForward) (*unifi.PortForward, error)

ListRADIUSProfile(ctx context.Context, site string) ([]unifi.RADIUSProfile, error)
GetRADIUSProfile(ctx context.Context, site, id string) (*unifi.RADIUSProfile, error)
DeleteRADIUSProfile(ctx context.Context, site, id string) error
CreateRADIUSProfile(ctx context.Context, site string, d *unifi.RADIUSProfile) (*unifi.RADIUSProfile, error)
UpdateRADIUSProfile(ctx context.Context, site string, d *unifi.RADIUSProfile) (*unifi.RADIUSProfile, error)
}

type client struct {
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/resource_wlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ unifi_wlan manages a WiFi network / SSID.
Default: "deny",
ValidateFunc: validation.StringInSlice([]string{"allow", "deny"}, false),
},
"radius_profile_id": {
Type: schema.TypeString,
Optional: true,
},
"schedule": {
Description: "Start and stop schedules for the WLAN",
Type: schema.TypeList,
Expand Down Expand Up @@ -168,6 +172,7 @@ func resourceWLANGetResourceData(d *schema.ResourceData) (*unifi.WLAN, error) {
MACFilterEnabled: macFilterEnabled,
MACFilterList: macFilterList,
MACFilterPolicy: d.Get("mac_filter_policy").(string),
RADIUSProfileID: d.Get("radius_profile_id").(string),
Schedule: schedule,
ScheduleEnabled: len(schedule) > 0,

Expand Down Expand Up @@ -244,6 +249,7 @@ func resourceWLANSetResourceData(resp *unifi.WLAN, d *schema.ResourceData) error
d.Set("mac_filter_enabled", macFilterEnabled)
d.Set("mac_filter_list", macFilterList)
d.Set("mac_filter_policy", macFilterPolicy)
d.Set("radius_profile_id", resp.RADIUSProfileID)
d.Set("schedule", schedule)

return nil
Expand Down
43 changes: 43 additions & 0 deletions internal/provider/resource_wlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,27 @@ func TestAccWLAN_schedule(t *testing.T) {
})
}

func TestAccWLAN_wpaeap(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: wlanPreCheck(t),
CheckDestroy: func(*terraform.State) error {
// TODO: actual CheckDestroy

<-wlanConcurrency
return nil
},
Steps: []resource.TestStep{
{
Config: testAccWLANConfig_wpaeap,
Check: resource.ComposeTestCheckFunc(
// testCheckNetworkExists(t, "name"),
),
},
importStep("unifi_wlan.test"),
},
})
}

const testAccWLANConfig_wpapsk = `
data "unifi_wlan_group" "default" {
}
Expand All @@ -168,6 +189,28 @@ resource "unifi_wlan" "test" {
}
`

const testAccWLANConfig_wpaeap = `
data "unifi_wlan_group" "default" {
}
data "unifi_user_group" "default" {
}
data "unifi_radius_profile" "default" {
}
resource "unifi_wlan" "test" {
name = "tfacc-wpapsk"
vlan_id = 202
passphrase = "12345678"
wlan_group_id = data.unifi_wlan_group.default.id
user_group_id = data.unifi_user_group.default.id
security = "wpaeap"
radius_profile_id = data.unifi_radius_profile.default.id
}
`

const testAccWLANConfig_open = `
data "unifi_wlan_group" "default" {
}
Expand Down

0 comments on commit e618554

Please sign in to comment.