Skip to content

Commit cb45740

Browse files
authored
Feat/network labels (#201)
1 parent 3efa1bf commit cb45740

9 files changed

Lines changed: 535 additions & 138 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
77

88
### Added
99
- storage: possibility to control what to do with backups related to the storage that is about to be deleted
10-
- router: label support
11-
- network peering: label support
12-
- load-balancer: label support
10+
- labels support for: load-balancers, networks, network-peerings, routers
1311

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

upcloud/network.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ type Network struct {
163163
Zone string `json:"zone"`
164164
Router string `json:"router"`
165165
Servers NetworkServerSlice `json:"servers"`
166+
Labels []Label `json:"labels"`
166167
}
167168

168169
// UnmarshalJSON is a custom unmarshaller that deals with

upcloud/network_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ func TestUnmarshalNetworks(t *testing.T) {
3030
}
3131
]
3232
},
33+
"labels": [
34+
{
35+
"key": "managedBy",
36+
"value": "upcloud"
37+
}
38+
],
3339
"name": "Public 80.69.172.0/22",
3440
"type": "public",
3541
"uuid": "03000000-0000-4000-8001-000000000000",
@@ -93,6 +99,12 @@ func TestUnmarshalNetworks(t *testing.T) {
9399
Gateway: "80.69.172.1",
94100
},
95101
},
102+
Labels: []Label{
103+
{
104+
Key: "managedBy",
105+
Value: "upcloud",
106+
},
107+
},
96108
Name: "Public 80.69.172.0/22",
97109
Type: NetworkTypePublic,
98110
UUID: "03000000-0000-4000-8001-000000000000",
@@ -155,6 +167,16 @@ func TestUnmarshalNetwork(t *testing.T) {
155167
"uuid": "034c12bc-cf15-4b19-97b2-0ab4e51bb98d",
156168
"zone": "uk-lon1",
157169
"router": "04c0df35-2658-4b0c-8ac7-962090f4e92a",
170+
"labels": [
171+
{
172+
"key": "managedBy",
173+
"value": "upcloud"
174+
},
175+
{
176+
"key": "env",
177+
"value": "test"
178+
}
179+
],
158180
"ip_networks": {
159181
"ip_network": [
160182
{
@@ -190,6 +212,16 @@ func TestUnmarshalNetwork(t *testing.T) {
190212
UUID: "034c12bc-cf15-4b19-97b2-0ab4e51bb98d",
191213
Zone: "uk-lon1",
192214
Router: "04c0df35-2658-4b0c-8ac7-962090f4e92a",
215+
Labels: []Label{
216+
{
217+
Key: "managedBy",
218+
Value: "upcloud",
219+
},
220+
{
221+
Key: "env",
222+
Value: "test",
223+
},
224+
},
193225
IPNetworks: []IPNetwork{
194226
{
195227
Address: "172.16.0.0/22",

upcloud/request/network.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,33 @@ import (
77
"github.com/UpCloudLtd/upcloud-go-api/v5/upcloud"
88
)
99

10+
// GetNetworksRequest represents a rwquest to get all networks
11+
type GetNetworksRequest struct {
12+
Filters []QueryFilter
13+
}
14+
15+
func (r *GetNetworksRequest) RequestURL() string {
16+
if len(r.Filters) == 0 {
17+
return "/network"
18+
}
19+
20+
return fmt.Sprintf("/network?%s", encodeQueryFilters(r.Filters))
21+
}
22+
1023
// GetNetworksInZoneRequest represents a request to get all networks
1124
// within the specified zone.
1225
type GetNetworksInZoneRequest struct {
13-
Zone string
26+
Zone string
27+
Filters []QueryFilter
1428
}
1529

1630
// RequestURL implements the Request interface.
1731
func (r *GetNetworksInZoneRequest) RequestURL() string {
18-
return fmt.Sprintf("/network/?zone=%s", r.Zone)
32+
if len(r.Filters) == 0 {
33+
return fmt.Sprintf("/network/?zone=%s", r.Zone)
34+
}
35+
36+
return fmt.Sprintf("/network?zone=%s&%s", r.Zone, encodeQueryFilters(r.Filters))
1937
}
2038

2139
// GetNetworkDetailsRequest represents a request to the the details of
@@ -35,6 +53,7 @@ type CreateNetworkRequest struct {
3553
Zone string `json:"zone,omitempty"`
3654
Router string `json:"router,omitempty"`
3755
IPNetworks upcloud.IPNetworkSlice `json:"ip_networks,omitempty"`
56+
Labels []upcloud.Label `json:"labels,omitempty"`
3857
}
3958

4059
// RequestURL implements the Request interface.
@@ -61,6 +80,7 @@ type ModifyNetworkRequest struct {
6180
Name string `json:"name,omitempty"`
6281
Zone string `json:"zone,omitempty"`
6382
IPNetworks upcloud.IPNetworkSlice `json:"ip_networks,omitempty"`
83+
Labels *[]upcloud.Label `json:"labels,omitempty"`
6484
}
6585

6686
// RequestURL implements the Request interface.

upcloud/request/network_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,43 @@ import (
99
"github.com/UpCloudLtd/upcloud-go-api/v5/upcloud"
1010
)
1111

12+
func TestMarshalGetNetworks(t *testing.T) {
13+
request := GetNetworksRequest{}
14+
assert.Equal(t, "/network", request.RequestURL())
15+
16+
request = GetNetworksRequest{Filters: []QueryFilter{
17+
FilterLabel{Label: upcloud.Label{
18+
Key: "env",
19+
Value: "test",
20+
}},
21+
FilterLabelKey{Key: "managedBy"},
22+
}}
23+
24+
assert.Equal(t, "/network?label=env%3Dtest&label=managedBy", request.RequestURL())
25+
}
26+
1227
// TestMarshalGetNetworksInZoneRequest tests that GetNetworksInZoneRequest behaves correctly
1328
func TestMarshalGetNetworksInZoneRequest(t *testing.T) {
1429
request := GetNetworksInZoneRequest{
1530
Zone: "foo",
1631
}
1732

1833
assert.Equal(t, "/network/?zone=foo", request.RequestURL())
34+
35+
requestWithFilters := GetNetworksInZoneRequest{
36+
Zone: "fi-hel1",
37+
Filters: []QueryFilter{
38+
FilterLabel{Label: upcloud.Label{
39+
Key: "env",
40+
Value: "test",
41+
}},
42+
FilterLabelKey{
43+
Key: "managed",
44+
},
45+
},
46+
}
47+
48+
assert.Equal(t, "/network?zone=fi-hel1&label=env%3Dtest&label=managed", requestWithFilters.RequestURL())
1949
}
2050

2151
// TestMarshalGetNetworkDetailsRequest tests that GetNetworkDetailsRequest behaves correctly
@@ -33,6 +63,12 @@ func TestMarshalCreateNetworkRequest(t *testing.T) {
3363
Name: "Test private net",
3464
Zone: "uk-lon1",
3565
Router: "04c0df35-2658-4b0c-8ac7-962090f4e92a",
66+
Labels: []upcloud.Label{
67+
{
68+
Key: "env",
69+
Value: "test",
70+
},
71+
},
3672
IPNetworks: []upcloud.IPNetwork{
3773
{
3874
Address: "172.16.0.0/22",
@@ -54,6 +90,12 @@ func TestMarshalCreateNetworkRequest(t *testing.T) {
5490
"name": "Test private net",
5591
"zone": "uk-lon1",
5692
"router": "04c0df35-2658-4b0c-8ac7-962090f4e92a",
93+
"labels": [
94+
{
95+
"key": "env",
96+
"value": "test"
97+
}
98+
],
5799
"ip_networks" : {
58100
"ip_network" : [
59101
{
@@ -114,6 +156,36 @@ func TestMarshalModifyNetworkRequest(t *testing.T) {
114156
assert.NoError(t, err)
115157
assert.Equal(t, "/network/foo", request.RequestURL())
116158
assert.JSONEq(t, expectedJSON, string(actualJSON))
159+
160+
request = ModifyNetworkRequest{
161+
UUID: "foo",
162+
Name: "supername",
163+
Labels: &[]upcloud.Label{
164+
{
165+
Key: "env",
166+
Value: "test",
167+
},
168+
},
169+
}
170+
171+
expectedJSON = `
172+
{
173+
"network": {
174+
"name": "supername",
175+
"labels": [
176+
{
177+
"key": "env",
178+
"value": "test"
179+
}
180+
]
181+
}
182+
}
183+
`
184+
185+
actualJSON, err = json.Marshal(&request)
186+
assert.NoError(t, err)
187+
assert.Equal(t, "/network/foo", request.RequestURL())
188+
assert.JSONEq(t, expectedJSON, string(actualJSON))
117189
}
118190

119191
// TestMarshalDeleteNetwork tests the DeleteNetworkRequest behaves correctly

0 commit comments

Comments
 (0)