v1.5.0
This commit is contained in:
@@ -22,21 +22,23 @@ func (ls ListSizes) FilterByName(name string) ListSizes {
|
||||
func (ls ListSizes) FilterFunc(predicate func(ItemSize) bool) ListSizes {
|
||||
var result ListSizes
|
||||
|
||||
for _, item := range ls {
|
||||
for _, item := range ls.Data {
|
||||
if predicate(item) {
|
||||
result = append(result, item)
|
||||
result.Data = append(result.Data, item)
|
||||
}
|
||||
}
|
||||
|
||||
result.EntryCount = uint64(len(result.Data))
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// FindOne returns first found ItemSize
|
||||
// If none was found, returns an empty struct.
|
||||
func (ls ListSizes) FindOne() ItemSize {
|
||||
if len(ls) == 0 {
|
||||
if len(ls.Data) == 0 {
|
||||
return ItemSize{}
|
||||
}
|
||||
|
||||
return ls[0]
|
||||
return ls.Data[0]
|
||||
}
|
||||
|
||||
@@ -3,30 +3,33 @@ package sizes
|
||||
import "testing"
|
||||
|
||||
var sizeItems = ListSizes{
|
||||
{
|
||||
Description: "",
|
||||
Disks: []uint64{},
|
||||
ID: 1,
|
||||
Memory: 512,
|
||||
Name: "size_1",
|
||||
VCPUs: 2,
|
||||
},
|
||||
{
|
||||
Description: "",
|
||||
Disks: []uint64{},
|
||||
ID: 2,
|
||||
Memory: 1024,
|
||||
Name: "size_2",
|
||||
VCPUs: 4,
|
||||
},
|
||||
{
|
||||
Description: "",
|
||||
Disks: []uint64{},
|
||||
ID: 2,
|
||||
Memory: 2048,
|
||||
Name: "size_3",
|
||||
VCPUs: 6,
|
||||
Data: []ItemSize{
|
||||
{
|
||||
Description: "",
|
||||
Disks: []uint64{},
|
||||
ID: 1,
|
||||
Memory: 512,
|
||||
Name: "size_1",
|
||||
VCPUs: 2,
|
||||
},
|
||||
{
|
||||
Description: "",
|
||||
Disks: []uint64{},
|
||||
ID: 2,
|
||||
Memory: 1024,
|
||||
Name: "size_2",
|
||||
VCPUs: 4,
|
||||
},
|
||||
{
|
||||
Description: "",
|
||||
Disks: []uint64{},
|
||||
ID: 2,
|
||||
Memory: 2048,
|
||||
Name: "size_3",
|
||||
VCPUs: 6,
|
||||
},
|
||||
},
|
||||
EntryCount: 3,
|
||||
}
|
||||
|
||||
func TestFilterByID(t *testing.T) {
|
||||
@@ -50,11 +53,11 @@ func TestFilterFunc(t *testing.T) {
|
||||
return is.Memory > 512
|
||||
})
|
||||
|
||||
if len(actual) != 2 {
|
||||
t.Fatal("expected 2 found, actual: ", len(actual))
|
||||
if len(actual.Data) != 2 {
|
||||
t.Fatal("expected 2 found, actual: ", len(actual.Data))
|
||||
}
|
||||
|
||||
for _, item := range actual {
|
||||
for _, item := range actual.Data {
|
||||
if item.Memory <= 512 {
|
||||
t.Fatal("expected Memory greater than 512, found: ", item.Memory)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ type ListRequest struct {
|
||||
}
|
||||
|
||||
// List gets list the available flavors, filtering can be based on the user which is doing the request
|
||||
func (s Sizes) List(ctx context.Context, req ListRequest) (ListSizes, error) {
|
||||
func (s Sizes) List(ctx context.Context, req ListRequest) (*ListSizes, error) {
|
||||
url := "/cloudapi/sizes/list"
|
||||
|
||||
res, err := s.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
@@ -34,7 +34,7 @@ func (s Sizes) List(ctx context.Context, req ListRequest) (ListSizes, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := ListSizes{}
|
||||
list := &ListSizes{}
|
||||
|
||||
err = json.Unmarshal(res, &list)
|
||||
if err != nil {
|
||||
|
||||
@@ -22,4 +22,10 @@ type ItemSize struct {
|
||||
}
|
||||
|
||||
// List of configured available flavors
|
||||
type ListSizes []ItemSize
|
||||
type ListSizes struct {
|
||||
// Data
|
||||
Data []ItemSize `json:"data"`
|
||||
|
||||
// Entry count
|
||||
EntryCount uint64 `json:"entryCount"`
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
// - First argument -> prefix
|
||||
// - Second argument -> indent
|
||||
func (ls ListSizes) Serialize(params ...string) (serialization.Serialized, error) {
|
||||
if len(ls) == 0 {
|
||||
if len(ls.Data) == 0 {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user