Skip to content

Commit

Permalink
Merge pull request #6 from paultyng/account
Browse files Browse the repository at this point in the history
Expose the Account API (RADIUS users)
  • Loading branch information
paultyng authored Aug 28, 2020
2 parents ae40573 + 47fa522 commit 1e71303
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 2 deletions.
3 changes: 3 additions & 0 deletions fields/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ type %s struct {

for _, name := range fieldNames {
switch {
case structName == "Account" && name == "ip":
code += "\tIP string `json:\"ip,omitempty\"`\n"
continue
case structName == "User" && name == "blocked":
code += "\tBlocked bool `json:\"blocked,omitempty\"`\n"
continue
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/paultyng/go-unifi

go 1.12

require github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
require (
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
github.com/tj/assert v0.0.3
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 h1:VHgatEHNcBFEB7inlalqfNqw65aNkM1lGX2yt3NmbS8=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion unifi/account.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions unifi/account.go
Original file line number Diff line number Diff line change
@@ -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 changes: 37 additions & 0 deletions unifi/account_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}
8 changes: 8 additions & 0 deletions unifi/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1e71303

Please sign in to comment.