Expose the Account API (RADIUS users)
* Adds Account CRUD API * Enahnces `emptyStringInt` with a predicate for dynamically setting which value should be interpreted as a blank JSON payload
This commit is contained in:
committed by
Paul Tyng
parent
ae40573bb7
commit
47fa522aba
@@ -23,7 +23,7 @@ type Account struct {
|
||||
NoDelete bool `json:"attr_no_delete,omitempty"`
|
||||
NoEdit bool `json:"attr_no_edit,omitempty"`
|
||||
|
||||
IP string `json:"ip"` // ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^$
|
||||
IP string `json:"ip,omitempty"`
|
||||
Name string `json:"name,omitempty"` // ^[^"' ]+$
|
||||
TunnelConfigType string `json:"tunnel_config_type,omitempty"` // vpn|802.1x|custom
|
||||
TunnelMediumType int `json:"tunnel_medium_type,omitempty"` // [1-9]|1[0-5]|^$
|
||||
|
||||
71
unifi/account.go
Normal file
71
unifi/account.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package unifi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (dst *Account) UnmarshalJSON(b []byte) error {
|
||||
type Alias Account
|
||||
aux := &struct {
|
||||
TunnelType emptyStringInt `json:"tunnel_type"`
|
||||
TunnelMediumType emptyStringInt `json:"tunnel_medium_type"`
|
||||
VLAN emptyStringInt `json:"vlan"`
|
||||
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(b, &aux)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal alias: %w", err)
|
||||
}
|
||||
|
||||
dst.TunnelType = int(aux.TunnelType)
|
||||
dst.TunnelMediumType = int(aux.TunnelMediumType)
|
||||
dst.VLAN = int(aux.VLAN)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst *Account) MarshalJSON() ([]byte, error) {
|
||||
type Alias Account
|
||||
aux := &struct {
|
||||
*Alias
|
||||
|
||||
TunnelType emptyStringInt `json:"tunnel_type"`
|
||||
TunnelMediumType emptyStringInt `json:"tunnel_medium_type"`
|
||||
VLAN emptyStringInt `json:"vlan"`
|
||||
}{
|
||||
Alias: (*Alias)(dst),
|
||||
}
|
||||
|
||||
aux.TunnelType = emptyStringInt(dst.TunnelType)
|
||||
aux.TunnelMediumType = emptyStringInt(dst.TunnelMediumType)
|
||||
aux.VLAN = emptyStringInt(dst.VLAN)
|
||||
|
||||
b, err := json.Marshal(aux)
|
||||
return b, err
|
||||
}
|
||||
|
||||
func (c *Client) ListAccount(ctx context.Context, site string) ([]Account, error) {
|
||||
return c.listAccount(ctx, site)
|
||||
}
|
||||
|
||||
func (c *Client) GetAccount(ctx context.Context, site, id string) (*Account, error) {
|
||||
return c.getAccount(ctx, site, id)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteAccount(ctx context.Context, site, id string) error {
|
||||
return c.deleteAccount(ctx, site, id)
|
||||
}
|
||||
|
||||
func (c *Client) CreateAccount(ctx context.Context, site string, d *Account) (*Account, error) {
|
||||
return c.createAccount(ctx, site, d)
|
||||
}
|
||||
|
||||
func (c *Client) UpdateAccount(ctx context.Context, site string, d *Account) (*Account, error) {
|
||||
return c.updateAccount(ctx, site, d)
|
||||
}
|
||||
37
unifi/account_test.go
Normal file
37
unifi/account_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package unifi_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/paultyng/go-unifi/unifi"
|
||||
"github.com/tj/assert"
|
||||
)
|
||||
|
||||
func TestAccountMarshalJSON(t *testing.T) {
|
||||
for n, c := range map[string]struct {
|
||||
expectedJSON string
|
||||
acc unifi.Account
|
||||
}{
|
||||
"empty strings": {
|
||||
`{"vlan":"","tunnel_type":"","tunnel_medium_type":""}`,
|
||||
unifi.Account{},
|
||||
},
|
||||
"response": {
|
||||
`{"vlan":10,"tunnel_type":1,"tunnel_medium_type":1}`,
|
||||
unifi.Account{
|
||||
VLAN: 10,
|
||||
TunnelType: 1,
|
||||
TunnelMediumType: 1,
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(n, func(t *testing.T) {
|
||||
actual, err := json.Marshal(&c.acc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.JSONEq(t, c.expectedJSON, string(actual))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -31,3 +31,11 @@ func (e *emptyStringInt) UnmarshalJSON(b []byte) error {
|
||||
*e = emptyStringInt(i)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *emptyStringInt) MarshalJSON() ([]byte, error) {
|
||||
if e == nil || *e == 0 {
|
||||
return []byte(`""`), nil
|
||||
}
|
||||
|
||||
return []byte(strconv.Itoa(int(*e))), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user