Skip to content

Commit 3efa1bf

Browse files
authored
feat(load-balancer): label support (#202)
1 parent 3649f9a commit 3efa1bf

9 files changed

Lines changed: 481 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
99
- storage: possibility to control what to do with backups related to the storage that is about to be deleted
1010
- router: label support
1111
- network peering: label support
12+
- load-balancer: label support
1213

1314
## Deprecated
1415
- request: `ServerFilter` and `ServerGroupFilter` types will be replaced with `QueryFilter` type in the future

upcloud/load_balancer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ type LoadBalancer struct {
230230
NetworkUUID string `json:"network_uuid,omitempty"` // deprecated
231231
Networks []LoadBalancerNetwork `json:"networks,omitempty"`
232232
DNSName string `json:"dns_name,omitempty"` // deprecated
233+
Labels []Label `json:"labels,omitempty"`
233234
ConfiguredStatus LoadBalancerConfiguredStatus `json:"configured_status,omitempty"`
234235
OperationalState LoadBalancerOperationalState `json:"operational_state,omitempty"`
235236
Frontends []LoadBalancerFrontend `json:"frontends,omitempty"`

upcloud/load_balancer_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ func TestMarshalLoadBalancer(t *testing.T) {
2121
"updated_at": "2022-02-11T17:33:59.898714Z",
2222
"uuid": "0aff6dac-143c-4300-9b33-ee2756f6592d",
2323
"zone": "fi-hel1",
24+
"labels": [
25+
{
26+
"key": "managedby",
27+
"value": "upcloud-go-sdk-unit-test"
28+
}
29+
],
2430
"resolvers": [
2531
{
2632
"cache_invalid": 30,
@@ -134,6 +140,12 @@ func TestMarshalLoadBalancer(t *testing.T) {
134140
OperationalState: LoadBalancerOperationalStateRunning,
135141
CreatedAt: timeParse("2021-12-07T13:58:30.817272Z"),
136142
UpdatedAt: timeParse("2022-02-11T17:33:59.898714Z"),
143+
Labels: []Label{
144+
{
145+
Key: "managedby",
146+
Value: "upcloud-go-sdk-unit-test",
147+
},
148+
},
137149
Frontends: []LoadBalancerFrontend{
138150
{
139151
Name: "example-frontend",

upcloud/network_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ func TestUnmarshalRouter(t *testing.T) {
502502
Name: "Example router",
503503
Type: "normal",
504504
UUID: "04c0df35-2658-4b0c-8ac7-962090f4e92a",
505-
Labels: LabelSlice{
505+
Labels: []Label{
506506
{
507507
Key: "managedBy",
508508
Value: "upcloud-go-sdk-unit-test",

upcloud/request/load_balancer.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,23 @@ const loadBalancerCertificateBundleBaseURL string = "/load-balancer/certificate-
1212
// GetLoadBalancersRequest represents a request to list load balancers
1313
// List size can be filtered using optional Page object
1414
type GetLoadBalancersRequest struct {
15-
Page *Page
15+
Page *Page
16+
Filters []QueryFilter
1617
}
1718

1819
func (r *GetLoadBalancersRequest) RequestURL() string {
20+
u := "/load-balancer"
21+
f := make([]QueryFilter, len(r.Filters))
22+
copy(f, r.Filters)
1923
if r.Page != nil {
20-
return fmt.Sprintf("/load-balancer?%s", r.Page.String())
24+
f = append(f, r.Page)
2125
}
22-
return "/load-balancer"
26+
27+
if len(f) == 0 {
28+
return u
29+
}
30+
31+
return fmt.Sprintf("%s?%s", u, encodeQueryFilters(f))
2332
}
2433

2534
// GetLoadBalancerRequest represents a request to get load balancer details
@@ -50,6 +59,7 @@ type CreateLoadBalancerRequest struct {
5059
Frontends []LoadBalancerFrontend `json:"frontends"`
5160
Backends []LoadBalancerBackend `json:"backends"`
5261
Resolvers []LoadBalancerResolver `json:"resolvers"`
62+
Labels []upcloud.Label `json:"labels,omitempty"`
5363
}
5464

5565
func (r *CreateLoadBalancerRequest) RequestURL() string {
@@ -58,10 +68,11 @@ func (r *CreateLoadBalancerRequest) RequestURL() string {
5868

5969
// ModifyLoadBalancerRequest represents a request to modify load balancer
6070
type ModifyLoadBalancerRequest struct {
61-
UUID string `json:"-"`
62-
Name string `json:"name,omitempty"`
63-
Plan string `json:"plan,omitempty"`
64-
ConfiguredStatus string `json:"configured_status,omitempty"`
71+
UUID string `json:"-"`
72+
Name string `json:"name,omitempty"`
73+
Plan string `json:"plan,omitempty"`
74+
ConfiguredStatus string `json:"configured_status,omitempty"`
75+
Labels *[]upcloud.Label `json:"labels,omitempty"`
6576
}
6677

6778
func (r *ModifyLoadBalancerRequest) RequestURL() string {

upcloud/request/load_balancer_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,39 @@ import (
1313
func TestGetLoadBalancersRequest(t *testing.T) {
1414
assert.Equal(t, "/load-balancer", (&GetLoadBalancersRequest{}).RequestURL())
1515
assert.Equal(t, "/load-balancer?limit=50&offset=450", (&GetLoadBalancersRequest{Page: &Page{Number: 10, Size: 50}}).RequestURL())
16+
17+
assert.Equal(t, "/load-balancer?label=size&label=color%3Dred&limit=50&offset=450", (&GetLoadBalancersRequest{
18+
Page: &Page{
19+
Number: 10,
20+
Size: 50,
21+
},
22+
Filters: []QueryFilter{
23+
FilterLabelKey{Key: "size"},
24+
FilterLabel{
25+
upcloud.Label{
26+
Key: "color",
27+
Value: "red",
28+
},
29+
},
30+
},
31+
}).RequestURL())
32+
33+
// this should work also because Page is also QueryFilter
34+
assert.Equal(t, "/load-balancer?label=size&label=color%3Dred&limit=50&offset=450", (&GetLoadBalancersRequest{
35+
Filters: []QueryFilter{
36+
&Page{
37+
Number: 10,
38+
Size: 50,
39+
},
40+
FilterLabelKey{Key: "size"},
41+
FilterLabel{
42+
upcloud.Label{
43+
Key: "color",
44+
Value: "red",
45+
},
46+
},
47+
},
48+
}).RequestURL())
1649
}
1750

1851
func TestCreateLoadBalancerRequest(t *testing.T) {
@@ -1263,3 +1296,85 @@ func ExampleCreateLoadBalancerRequest() {
12631296
fmt.Println(string(js))
12641297
}
12651298
}
1299+
1300+
func TestCreateLoadBalancerLabelsRequest(t *testing.T) {
1301+
expected := `
1302+
{
1303+
"name": "example-service",
1304+
"plan": "development",
1305+
"zone": "fi-hel1",
1306+
"network_uuid": "03631160-d57a-4926-ad48-a2f828229dcb",
1307+
"configured_status": "started",
1308+
"frontends": [],
1309+
"backends": [],
1310+
"resolvers": [],
1311+
"labels": [
1312+
{
1313+
"key": "managedby",
1314+
"value": "upcloud-go-sdk-unit-test"
1315+
}
1316+
]
1317+
}
1318+
`
1319+
r := CreateLoadBalancerRequest{
1320+
Name: "example-service",
1321+
Plan: "development",
1322+
Zone: "fi-hel1",
1323+
NetworkUUID: "03631160-d57a-4926-ad48-a2f828229dcb",
1324+
ConfiguredStatus: upcloud.LoadBalancerConfiguredStatusStarted,
1325+
Frontends: []LoadBalancerFrontend{},
1326+
Backends: []LoadBalancerBackend{},
1327+
Resolvers: []LoadBalancerResolver{},
1328+
Labels: []upcloud.Label{
1329+
{
1330+
Key: "managedby",
1331+
Value: "upcloud-go-sdk-unit-test",
1332+
},
1333+
},
1334+
}
1335+
actual, err := json.Marshal(&r)
1336+
assert.NoError(t, err)
1337+
assert.JSONEq(t, expected, string(actual))
1338+
}
1339+
1340+
func TestModifyLoadBalancerLabelsRequest(t *testing.T) {
1341+
expected := `
1342+
{
1343+
"labels": [
1344+
{
1345+
"key": "managedby",
1346+
"value": "upcloud-go-sdk-unit-test"
1347+
}
1348+
]
1349+
}
1350+
`
1351+
r := ModifyLoadBalancerRequest{
1352+
Labels: &[]upcloud.Label{
1353+
{
1354+
Key: "managedby",
1355+
Value: "upcloud-go-sdk-unit-test",
1356+
},
1357+
},
1358+
}
1359+
actual, err := json.Marshal(&r)
1360+
assert.NoError(t, err)
1361+
assert.JSONEq(t, expected, string(actual))
1362+
1363+
expected = `
1364+
{
1365+
"labels": []
1366+
}
1367+
`
1368+
r = ModifyLoadBalancerRequest{
1369+
Labels: &[]upcloud.Label{},
1370+
}
1371+
actual, err = json.Marshal(&r)
1372+
assert.NoError(t, err)
1373+
assert.JSONEq(t, expected, string(actual))
1374+
1375+
expected = `{}`
1376+
r = ModifyLoadBalancerRequest{}
1377+
actual, err = json.Marshal(&r)
1378+
assert.NoError(t, err)
1379+
assert.JSONEq(t, expected, string(actual))
1380+
}

upcloud/request/page.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,7 @@ func (p *Page) Previous() *Page {
6565
func (p *Page) String() string {
6666
return p.Values().Encode()
6767
}
68+
69+
func (p *Page) ToQueryParam() string {
70+
return p.String()
71+
}

0 commit comments

Comments
 (0)