Skip to content

Commit d9918f0

Browse files
authored
feat(storage): labels support (#214)
1 parent 7725803 commit d9918f0

7 files changed

Lines changed: 763 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
55

66
## [Unreleased]
77

8+
### Added
9+
- labels support for storages
10+
811
### Changed
912
- errors: all service method now return `Problem` type in case of errors (*BREAKING CHANGE*)
1013

upcloud/request/storage.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,29 @@ type GetStoragesRequest struct {
2828
Type string
2929
// If specified, only storages marked as favorite will be retrieved
3030
Favorite bool
31+
32+
Filters []QueryFilter
3133
}
3234

3335
// RequestURL implements the Request interface
3436
func (r *GetStoragesRequest) RequestURL() string {
37+
url := "/storage"
3538
if r.Access != "" {
36-
return fmt.Sprintf("/storage/%s", r.Access)
39+
url = fmt.Sprintf("%s/%s", url, r.Access)
3740
}
3841

3942
if r.Type != "" {
40-
return fmt.Sprintf("/storage/%s", r.Type)
43+
url = fmt.Sprintf("%s/%s", url, r.Type)
4144
}
4245

4346
if r.Favorite {
44-
return "/storage/favorite"
47+
url = url + "/favorite"
4548
}
4649

47-
return "/storage"
50+
if len(r.Filters) == 0 {
51+
return url
52+
}
53+
return fmt.Sprintf("%s?%s", url, encodeQueryFilters(r.Filters))
4854
}
4955

5056
// GetStorageDetailsRequest represents a request for retrieving details about a piece of storage
@@ -64,6 +70,7 @@ type CreateStorageRequest struct {
6470
Title string `json:"title,omitempty"`
6571
Zone string `json:"zone"`
6672
BackupRule *upcloud.BackupRule `json:"backup_rule,omitempty"`
73+
Labels []upcloud.Label `json:"labels,omitempty"`
6774
}
6875

6976
// RequestURL implements the Request interface
@@ -90,6 +97,7 @@ type ModifyStorageRequest struct {
9097
Title string `json:"title,omitempty"`
9198
Size int `json:"size,omitempty,string"`
9299
BackupRule *upcloud.BackupRule `json:"backup_rule,omitempty"`
100+
Labels *[]upcloud.Label `json:"labels,omitempty"`
93101
}
94102

95103
// MarshalJSON is a custom marshaller that deals with

upcloud/request/storage_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ func TestGetStoragesRequest(t *testing.T) {
2121
request.Favorite = false
2222
request.Type = upcloud.StorageTypeDisk
2323
assert.Equal(t, "/storage/disk", request.RequestURL())
24+
25+
request.Filters = []QueryFilter{
26+
FilterLabel{
27+
Label: upcloud.Label{
28+
Key: "color",
29+
Value: "green",
30+
},
31+
},
32+
FilterLabelKey{Key: "size"},
33+
}
34+
assert.Equal(t, "/storage/disk"+"?label=color%3Dgreen&label=size", request.RequestURL())
35+
36+
r := GetStoragesRequest{
37+
Filters: []QueryFilter{
38+
FilterLabel{
39+
Label: upcloud.Label{
40+
Key: "color",
41+
Value: "green",
42+
},
43+
},
44+
FilterLabelKey{Key: "size"},
45+
},
46+
}
47+
assert.Equal(t, "/storage?label=color%3Dgreen&label=size", r.RequestURL())
2448
}
2549

2650
// TestGetStorageDetailsRequest tests that GetStorageDetailsRequest objects behave correctly

0 commit comments

Comments
 (0)