Skip to content

Commit 45a47c6

Browse files
committed
Convert changed Account function to 1.3
1 parent 187f736 commit 45a47c6

File tree

6 files changed

+238
-11
lines changed

6 files changed

+238
-11
lines changed

upcloud/account.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@ import "encoding/json"
44

55
// Account represents an account
66
type Account struct {
7-
Credits float64 `json:"credits"`
8-
UserName string `json:"username"`
7+
Credits float64 `json:"credits"`
8+
UserName string `json:"username"`
9+
ResourceLimits ResourceLimits `json:"resource_limits"`
10+
}
11+
12+
// ResourceLimits represents an account's resource limits
13+
type ResourceLimits struct {
14+
Cores int `json:"cores"`
15+
DetachedFloatingIps int `json:"detached_floating_ips"`
16+
Memory int `json:"memory"`
17+
Networks int `json:"networks"`
18+
PublicIPv4 int `json:"public_ipv4"`
19+
PublicIPv6 int `json:"public_ipv6"`
20+
StorageHDD int `json:"storage_hdd"`
21+
StorageSSD int `json:"storage_ssd"`
922
}
1023

1124
// UnmarshalJSON is a custom unmarshaller that deals with

upcloud/account_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,35 @@ import (
1010
// TestUnmarshalAccount tests that Account objects unmarshal correctly
1111
func TestUnmarshalAccount(t *testing.T) {
1212
originalJSON := `
13-
{
14-
"account": {
15-
"credits": 9972.2324,
16-
"username": "username"
17-
}
18-
}
13+
{
14+
"account": {
15+
"credits": 9972.2324,
16+
"username": "username",
17+
"resource_limits": {
18+
"cores": 200,
19+
"detached_floating_ips": 10,
20+
"memory": 1048576,
21+
"networks": 100,
22+
"public_ipv4": 100,
23+
"public_ipv6": 100,
24+
"storage_hdd": 10240,
25+
"storage_ssd": 10240
26+
}
27+
}
28+
}
1929
`
2030

2131
account := Account{}
2232
err := json.Unmarshal([]byte(originalJSON), &account)
2333
assert.NoError(t, err)
2434
assert.Equal(t, 9972.2324, account.Credits)
2535
assert.Equal(t, "username", account.UserName)
36+
assert.Equal(t, 200, account.ResourceLimits.Cores)
37+
assert.Equal(t, 10, account.ResourceLimits.DetachedFloatingIps)
38+
assert.Equal(t, 1048576, account.ResourceLimits.Memory)
39+
assert.Equal(t, 100, account.ResourceLimits.Networks)
40+
assert.Equal(t, 100, account.ResourceLimits.PublicIPv4)
41+
assert.Equal(t, 100, account.ResourceLimits.PublicIPv6)
42+
assert.Equal(t, 10240, account.ResourceLimits.StorageHDD)
43+
assert.Equal(t, 10240, account.ResourceLimits.StorageSSD)
2644
}

upcloud/service/service.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ func New(client *client.Client) *Service {
2626
// GetAccount returns the current user's account
2727
func (s *Service) GetAccount() (*upcloud.Account, error) {
2828
account := upcloud.Account{}
29-
response, err := s.basicJSONGetRequest("/account")
29+
response, err := s.basicFutureGetRequest("/account")
3030

3131
if err != nil {
3232
return nil, err
3333
}
3434

35-
json.Unmarshal(response, &account)
35+
err = json.Unmarshal(response, &account)
36+
if err != nil {
37+
return nil, err
38+
}
3639

3740
return &account, nil
3841
}

upcloud/service/service_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,22 @@ func TestGetAccount(t *testing.T) {
135135
}
136136

137137
svc := getService()
138+
138139
account, err := svc.GetAccount()
139-
username, _ := getCredentials()
140140
require.NoError(t, err)
141141

142+
username, _ := getCredentials()
143+
142144
if account.UserName != username {
143145
t.Errorf("TestGetAccount expected %s, got %s", username, account.UserName)
144146
}
147+
148+
assert.NotZero(t, account.ResourceLimits.Cores)
149+
assert.NotZero(t, account.ResourceLimits.Memory)
150+
assert.NotZero(t, account.ResourceLimits.Networks)
151+
assert.NotZero(t, account.ResourceLimits.PublicIPv6)
152+
assert.NotZero(t, account.ResourceLimits.StorageHDD)
153+
assert.NotZero(t, account.ResourceLimits.StorageSSD)
145154
}
146155

147156
// TestGetZones tests that the GetZones() function returns proper data

upcloud/utility.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package upcloud
2+
3+
type Boolean bool
4+
5+
func (b *Boolean) UnmarshalJSON(buf []byte) error {
6+
str := string(buf)
7+
if str == `true` ||
8+
str == `"true"` ||
9+
str == `"yes"` ||
10+
str == `1` ||
11+
str == `"1"` {
12+
(*b) = true
13+
return nil
14+
}
15+
16+
(*b) = false
17+
return nil
18+
}

upcloud/utility_test.go

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package upcloud
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type testStruct struct {
11+
Value Boolean `json:"value"`
12+
}
13+
14+
func TestBoolean_TrueAsBool(t *testing.T) {
15+
trueJSON := `
16+
{
17+
"value": true
18+
}
19+
`
20+
21+
s := testStruct{}
22+
23+
err := json.Unmarshal([]byte(trueJSON), &s)
24+
assert.NoError(t, err)
25+
assert.True(t, bool(s.Value))
26+
}
27+
28+
func TestBoolean_TrueAsString(t *testing.T) {
29+
trueJSON := `
30+
{
31+
"value": "true"
32+
}
33+
`
34+
35+
s := testStruct{}
36+
37+
err := json.Unmarshal([]byte(trueJSON), &s)
38+
assert.NoError(t, err)
39+
assert.True(t, bool(s.Value))
40+
}
41+
42+
func TestBoolean_TrueAsOne(t *testing.T) {
43+
trueJSON := `
44+
{
45+
"value": 1
46+
}
47+
`
48+
49+
s := testStruct{}
50+
51+
err := json.Unmarshal([]byte(trueJSON), &s)
52+
assert.NoError(t, err)
53+
assert.True(t, bool(s.Value))
54+
}
55+
56+
func TestBoolean_TrueAsOneString(t *testing.T) {
57+
trueJSON := `
58+
{
59+
"value": "1"
60+
}
61+
`
62+
63+
s := testStruct{}
64+
65+
err := json.Unmarshal([]byte(trueJSON), &s)
66+
assert.NoError(t, err)
67+
assert.True(t, bool(s.Value))
68+
}
69+
70+
func TestBoolean_TrueAsYesString(t *testing.T) {
71+
trueJSON := `
72+
{
73+
"value": "yes"
74+
}
75+
`
76+
77+
s := testStruct{}
78+
79+
err := json.Unmarshal([]byte(trueJSON), &s)
80+
assert.NoError(t, err)
81+
assert.True(t, bool(s.Value))
82+
}
83+
84+
func TestBoolean_FalseAsBool(t *testing.T) {
85+
trueJSON := `
86+
{
87+
"value": false
88+
}
89+
`
90+
91+
s := testStruct{}
92+
93+
err := json.Unmarshal([]byte(trueJSON), &s)
94+
assert.NoError(t, err)
95+
assert.False(t, bool(s.Value))
96+
}
97+
98+
func TestBoolean_FalseAsString(t *testing.T) {
99+
trueJSON := `
100+
{
101+
"value": "false"
102+
}
103+
`
104+
105+
s := testStruct{}
106+
107+
err := json.Unmarshal([]byte(trueJSON), &s)
108+
assert.NoError(t, err)
109+
assert.False(t, bool(s.Value))
110+
}
111+
112+
func TestBoolean_FalseAsZero(t *testing.T) {
113+
trueJSON := `
114+
{
115+
"value": 0
116+
}
117+
`
118+
119+
s := testStruct{}
120+
121+
err := json.Unmarshal([]byte(trueJSON), &s)
122+
assert.NoError(t, err)
123+
assert.False(t, bool(s.Value))
124+
}
125+
126+
func TestBoolean_FalseAsZeroString(t *testing.T) {
127+
trueJSON := `
128+
{
129+
"value": "0"
130+
}
131+
`
132+
133+
s := testStruct{}
134+
135+
err := json.Unmarshal([]byte(trueJSON), &s)
136+
assert.NoError(t, err)
137+
assert.False(t, bool(s.Value))
138+
}
139+
140+
func TestBoolean_FalseAsNo(t *testing.T) {
141+
trueJSON := `
142+
{
143+
"value": "no"
144+
}
145+
`
146+
147+
s := testStruct{}
148+
149+
err := json.Unmarshal([]byte(trueJSON), &s)
150+
assert.NoError(t, err)
151+
assert.False(t, bool(s.Value))
152+
}
153+
154+
func TestBoolean_FalseAnything(t *testing.T) {
155+
trueJSON := `
156+
{
157+
"value": "fudge"
158+
}
159+
`
160+
161+
s := testStruct{}
162+
163+
err := json.Unmarshal([]byte(trueJSON), &s)
164+
assert.NoError(t, err)
165+
assert.False(t, bool(s.Value))
166+
}

0 commit comments

Comments
 (0)