-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from paultyng/account
Expose the Account API (RADIUS users)
- Loading branch information
Showing
7 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters