Add unifi_user_group resource
This commit is contained in:
17
README.md
17
README.md
@@ -34,6 +34,19 @@ resource "unifi_network" "test" {
|
||||
}
|
||||
```
|
||||
|
||||
## unifi_user_group
|
||||
|
||||
Example:
|
||||
|
||||
```terraform
|
||||
resource "unifi_user_group" "test" {
|
||||
name = "foo"
|
||||
|
||||
qos_rate_max_down = 2000 # 2mbps
|
||||
qos_rate_max_up = 10 # 10kbps
|
||||
}
|
||||
```
|
||||
|
||||
## unifi_wlan
|
||||
|
||||
Example:
|
||||
@@ -56,3 +69,7 @@ resource "unifi_wlan" "test" {
|
||||
```
|
||||
|
||||
## TODO
|
||||
|
||||
* [ ] automatically fixup subnet cidrs from .0 to .1?
|
||||
* [ ] update support for wlan
|
||||
* [ ] update support for network
|
||||
|
||||
@@ -16,6 +16,15 @@ func dataUserGroup() *schema.Resource {
|
||||
Optional: true,
|
||||
Default: "Default",
|
||||
},
|
||||
|
||||
"qos_rate_max_down": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
},
|
||||
"qos_rate_max_up": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -32,6 +41,10 @@ func dataUserGroupRead(d *schema.ResourceData, meta interface{}) error {
|
||||
for _, g := range groups {
|
||||
if g.Name == name {
|
||||
d.SetId(g.ID)
|
||||
|
||||
d.Set("qos_rate_max_down", g.QOSRateMaxDown)
|
||||
d.Set("qos_rate_max_up", g.QOSRateMaxUp)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,3 +58,19 @@ func (c *lazyClient) GetWLAN(site, id string) (*unifi.WLAN, error) {
|
||||
c.init()
|
||||
return c.inner.GetWLAN(site, id)
|
||||
}
|
||||
func (c *lazyClient) DeleteUserGroup(site, id string) error {
|
||||
c.init()
|
||||
return c.inner.DeleteUserGroup(site, id)
|
||||
}
|
||||
func (c *lazyClient) CreateUserGroup(site string, d *unifi.UserGroup) (*unifi.UserGroup, error) {
|
||||
c.init()
|
||||
return c.inner.CreateUserGroup(site, d)
|
||||
}
|
||||
func (c *lazyClient) GetUserGroup(site, id string) (*unifi.UserGroup, error) {
|
||||
c.init()
|
||||
return c.inner.GetUserGroup(site, id)
|
||||
}
|
||||
func (c *lazyClient) UpdateUserGroup(site string, d *unifi.UserGroup) (*unifi.UserGroup, error) {
|
||||
c.init()
|
||||
return c.inner.UpdateUserGroup(site, d)
|
||||
}
|
||||
|
||||
@@ -43,8 +43,9 @@ func Provider() terraform.ResourceProvider {
|
||||
"unifi_wlan_group": dataWLANGroup(),
|
||||
},
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
"unifi_network": resourceNetwork(),
|
||||
"unifi_wlan": resourceWLAN(),
|
||||
"unifi_network": resourceNetwork(),
|
||||
"unifi_user_group": resourceUserGroup(),
|
||||
"unifi_wlan": resourceWLAN(),
|
||||
},
|
||||
}
|
||||
p.ConfigureFunc = configure(p)
|
||||
@@ -74,6 +75,10 @@ func configure(p *schema.Provider) schema.ConfigureFunc {
|
||||
|
||||
type unifiClient interface {
|
||||
ListUserGroup(site string) ([]unifi.UserGroup, error)
|
||||
DeleteUserGroup(site, id string) error
|
||||
CreateUserGroup(site string, d *unifi.UserGroup) (*unifi.UserGroup, error)
|
||||
GetUserGroup(site, id string) (*unifi.UserGroup, error)
|
||||
UpdateUserGroup(site string, d *unifi.UserGroup) (*unifi.UserGroup, error)
|
||||
|
||||
ListWLANGroup(site string) ([]unifi.WLANGroup, error)
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ variable "subnet" {
|
||||
}
|
||||
|
||||
resource "unifi_network" "test" {
|
||||
name = "foo"
|
||||
name = "tfacc"
|
||||
purpose = "corporate"
|
||||
|
||||
subnet = var.subnet
|
||||
|
||||
119
provider/resource_user_group.go
Normal file
119
provider/resource_user_group.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
||||
"github.com/paultyng/terraform-provider-unifi/unifi"
|
||||
)
|
||||
|
||||
func resourceUserGroup() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceUserGroupCreate,
|
||||
Read: resourceUserGroupRead,
|
||||
Update: resourceUserGroupUpdate,
|
||||
Delete: resourceUserGroupDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"name": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
"qos_rate_max_down": {
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
Default: -1,
|
||||
// TODO: validate does not equal 0,1
|
||||
},
|
||||
"qos_rate_max_up": {
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
Default: -1,
|
||||
// TODO: validate does not equal 0,1
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resourceUserGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
c := meta.(*client)
|
||||
|
||||
req, err := resourceUserGroupGetResourceData(d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := c.c.CreateUserGroup(c.site, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.SetId(resp.ID)
|
||||
|
||||
return resourceUserGroupSetResourceData(resp, d)
|
||||
}
|
||||
|
||||
func resourceUserGroupGetResourceData(d *schema.ResourceData) (*unifi.UserGroup, error) {
|
||||
return &unifi.UserGroup{
|
||||
Name: d.Get("name").(string),
|
||||
QOSRateMaxDown: d.Get("qos_rate_max_down").(int),
|
||||
QOSRateMaxUp: d.Get("qos_rate_max_up").(int),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func resourceUserGroupSetResourceData(resp *unifi.UserGroup, d *schema.ResourceData) error {
|
||||
d.Set("name", resp.Name)
|
||||
d.Set("qos_rate_max_down", resp.QOSRateMaxDown)
|
||||
d.Set("qos_rate_max_up", resp.QOSRateMaxUp)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceUserGroupRead(d *schema.ResourceData, meta interface{}) error {
|
||||
c := meta.(*client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
resp, err := c.c.GetUserGroup(c.site, id)
|
||||
if _, ok := err.(*unifi.NotFoundError); ok {
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resourceUserGroupSetResourceData(resp, d)
|
||||
}
|
||||
|
||||
func resourceUserGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||
c := meta.(*client)
|
||||
|
||||
req, err := resourceUserGroupGetResourceData(d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.ID = d.Id()
|
||||
req.SiteID = c.site
|
||||
|
||||
resp, err := c.c.UpdateUserGroup(c.site, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resourceUserGroupSetResourceData(resp, d)
|
||||
}
|
||||
|
||||
func resourceUserGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
c := meta.(*client)
|
||||
|
||||
id := d.Id()
|
||||
|
||||
err := c.c.DeleteUserGroup(c.site, id)
|
||||
if _, ok := err.(*unifi.NotFoundError); ok {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
46
provider/resource_user_group_test.go
Normal file
46
provider/resource_user_group_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
|
||||
)
|
||||
|
||||
func TestAccUserGroup_basic(t *testing.T) {
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
Providers: providers,
|
||||
PreCheck: func() { preCheck(t) },
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccUserGroupConfig,
|
||||
// Check: resource.ComposeTestCheckFunc(
|
||||
// // testCheckUserGroupExists(t, "name"),
|
||||
// ),
|
||||
},
|
||||
{
|
||||
Config: testAccUserGroupConfig_qos,
|
||||
},
|
||||
importStep("unifi_user_group.test"),
|
||||
{
|
||||
Config: testAccUserGroupConfig,
|
||||
},
|
||||
importStep("unifi_user_group.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const testAccUserGroupConfig = `
|
||||
resource "unifi_user_group" "test" {
|
||||
name = "tfacc"
|
||||
}
|
||||
`
|
||||
|
||||
const testAccUserGroupConfig_qos = `
|
||||
resource "unifi_user_group" "test" {
|
||||
name = "tfacc"
|
||||
|
||||
qos_rate_max_up = 2000
|
||||
qos_rate_max_down = 50
|
||||
}
|
||||
`
|
||||
@@ -84,7 +84,7 @@ data "unifi_user_group" "default" {
|
||||
}
|
||||
|
||||
resource "unifi_wlan" "test" {
|
||||
name = "foowpapsk"
|
||||
name = "tfacc-wpapsk"
|
||||
vlan_id = 202
|
||||
passphrase = "12345678"
|
||||
wlan_group_id = data.unifi_wlan_group.default.id
|
||||
@@ -101,7 +101,7 @@ data "unifi_user_group" "default" {
|
||||
}
|
||||
|
||||
resource "unifi_wlan" "test" {
|
||||
name = "fooopen"
|
||||
name = "tfacc-open"
|
||||
vlan_id = 202
|
||||
wlan_group_id = data.unifi_wlan_group.default.id
|
||||
user_group_id = data.unifi_user_group.default.id
|
||||
|
||||
@@ -14,8 +14,8 @@ type UserGroup struct {
|
||||
NoDelete bool `json:"attr_no_delete,omitempty"`
|
||||
//NoEdit bool `json:"attr_no_edit,omitempty"`
|
||||
|
||||
QOSRateMaxDown int `json:"qos_rate_max_down"`
|
||||
QOSRateMaxUp int `json:"qos_rate_max_up"`
|
||||
QOSRateMaxDown int `json:"qos_rate_max_down"` // -1 for disabled
|
||||
QOSRateMaxUp int `json:"qos_rate_max_up"` // -1 for disabled
|
||||
}
|
||||
|
||||
func (c *Client) ListUserGroup(site string) ([]UserGroup, error) {
|
||||
@@ -31,3 +31,70 @@ func (c *Client) ListUserGroup(site string) ([]UserGroup, error) {
|
||||
|
||||
return respBody.Data, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetUserGroup(site, id string) (*UserGroup, error) {
|
||||
var respBody struct {
|
||||
Meta meta `json:"meta"`
|
||||
Data []UserGroup `json:"data"`
|
||||
}
|
||||
|
||||
err := c.do("GET", fmt.Sprintf("s/%s/rest/usergroup/%s", site, id), nil, &respBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(respBody.Data) != 1 {
|
||||
return nil, &NotFoundError{}
|
||||
}
|
||||
|
||||
d := respBody.Data[0]
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
func (c *Client) DeleteUserGroup(site, id string) error {
|
||||
err := c.do("DELETE", fmt.Sprintf("s/%s/rest/usergroup/%s", site, id), struct{}{}, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) CreateUserGroup(site string, d *UserGroup) (*UserGroup, error) {
|
||||
var respBody struct {
|
||||
Meta meta `json:"meta"`
|
||||
Data []UserGroup `json:"data"`
|
||||
}
|
||||
|
||||
err := c.do("POST", fmt.Sprintf("s/%s/rest/usergroup", site), d, &respBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(respBody.Data) != 1 {
|
||||
return nil, &NotFoundError{}
|
||||
}
|
||||
|
||||
new := respBody.Data[0]
|
||||
|
||||
return &new, nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateUserGroup(site string, d *UserGroup) (*UserGroup, error) {
|
||||
var respBody struct {
|
||||
Meta meta `json:"meta"`
|
||||
Data []UserGroup `json:"data"`
|
||||
}
|
||||
|
||||
err := c.do("PUT", fmt.Sprintf("s/%s/rest/usergroup/%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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user