v1.2.1
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
package extnet
|
||||
|
||||
import (
|
||||
"github.com/rudecs/decort-sdk/interfaces"
|
||||
"repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces"
|
||||
)
|
||||
|
||||
// Structure for creating request to extnet
|
||||
|
||||
51
pkg/cloudapi/extnet/filter.go
Normal file
51
pkg/cloudapi/extnet/filter.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package extnet
|
||||
|
||||
// FilterByID returns ListExtNets with specified ID.
|
||||
func (lenet ListExtNets) FilterByID(id uint64) ListExtNets {
|
||||
predicate := func(iexnet ItemExtNet) bool {
|
||||
return iexnet.ID == id
|
||||
}
|
||||
|
||||
return lenet.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByName returns ListExtNets with specified Name.
|
||||
func (lenet ListExtNets) FilterByName(name string) ListExtNets {
|
||||
predicate := func(iexnet ItemExtNet) bool {
|
||||
return iexnet.Name == name
|
||||
}
|
||||
|
||||
return lenet.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByStatus returns ListExtNets with specified Status.
|
||||
func (lenet ListExtNets) FilterByStatus(status string) ListExtNets {
|
||||
predicate := func(iexnet ItemExtNet) bool {
|
||||
return iexnet.Status == status
|
||||
}
|
||||
|
||||
return lenet.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterFunc allows filtering ListExtNets based on a user-specified predicate.
|
||||
func (lenet ListExtNets) FilterFunc(predicate func(ItemExtNet) bool) ListExtNets {
|
||||
var result ListExtNets
|
||||
|
||||
for _, item := range lenet {
|
||||
if predicate(item) {
|
||||
result = append(result, item)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// FindOne returns first found ItemExtNet
|
||||
// If none was found, returns an empty struct.
|
||||
func (lenet ListExtNets) FindOne() ItemExtNet {
|
||||
if len(lenet) == 0 {
|
||||
return ItemExtNet{}
|
||||
}
|
||||
|
||||
return lenet[0]
|
||||
}
|
||||
65
pkg/cloudapi/extnet/filter_test.go
Normal file
65
pkg/cloudapi/extnet/filter_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package extnet
|
||||
|
||||
import "testing"
|
||||
|
||||
var extnets = ListExtNets{
|
||||
ItemExtNet{
|
||||
ID: 3,
|
||||
IPCIDR: "176.118.164.0/24",
|
||||
Name: "176.118.164.0/24",
|
||||
Status: "ENABLED",
|
||||
},
|
||||
ItemExtNet{
|
||||
ID: 10,
|
||||
IPCIDR: "45.134.255.0/24",
|
||||
Name: "45.134.255.0/24",
|
||||
Status: "ENABLED",
|
||||
},
|
||||
ItemExtNet{
|
||||
ID: 13,
|
||||
IPCIDR: "88.218.249.0/24",
|
||||
Name: "88.218.249.0/24",
|
||||
Status: "DISABLED",
|
||||
},
|
||||
}
|
||||
|
||||
func TestFilterByID(t *testing.T) {
|
||||
actual := extnets.FilterByID(10).FindOne()
|
||||
|
||||
if actual.ID != 10 {
|
||||
t.Fatal("expected ID 10, found: ", actual.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByName(t *testing.T) {
|
||||
name := "88.218.249.0/24"
|
||||
actual := extnets.FilterByName(name).FindOne()
|
||||
|
||||
if actual.Name != name {
|
||||
t.Fatal("expected ", name, " found: ", actual.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByStatus(t *testing.T) {
|
||||
actual := extnets.FilterByStatus("ENABLED")
|
||||
|
||||
if len(actual) != 2 {
|
||||
t.Fatal("expected 2 found, actual: ", len(actual))
|
||||
}
|
||||
|
||||
for _, item := range actual {
|
||||
if item.Status != "ENABLED" {
|
||||
t.Fatal("expected Status 'ENABLED', found: ", item.Status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterFunc(t *testing.T) {
|
||||
actual := extnets.FilterFunc(func(ien ItemExtNet) bool {
|
||||
return ien.IPCIDR == ien.Name
|
||||
})
|
||||
|
||||
if len(actual) != 3 {
|
||||
t.Fatal("expected 3 elements, found: ", len(actual))
|
||||
}
|
||||
}
|
||||
43
pkg/cloudapi/extnet/serialize.go
Normal file
43
pkg/cloudapi/extnet/serialize.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package extnet
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/serialization"
|
||||
)
|
||||
|
||||
// Serialize returns JSON-serialized []byte. Used as a wrapper over json.Marshal and json.MarshalIndent functions.
|
||||
//
|
||||
// In order to serialize with indent make sure to follow these guidelines:
|
||||
// - First argument -> prefix
|
||||
// - Second argument -> indent
|
||||
func (lenet ListExtNets) Serialize(params ...string) (serialization.Serialized, error) {
|
||||
if len(lenet) == 0 {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
if len(params) > 1 {
|
||||
prefix := params[0]
|
||||
indent := params[1]
|
||||
|
||||
return json.MarshalIndent(lenet, prefix, indent)
|
||||
}
|
||||
|
||||
return json.Marshal(lenet)
|
||||
}
|
||||
|
||||
// Serialize returns JSON-serialized []byte. Used as a wrapper over json.Marshal and json.MarshalIndent functions.
|
||||
//
|
||||
// In order to serialize with indent make sure to follow these guidelines:
|
||||
// - First argument -> prefix
|
||||
// - Second argument -> indent
|
||||
func (ienet ItemExtNet) Serialize(params ...string) (serialization.Serialized, error) {
|
||||
if len(params) > 1 {
|
||||
prefix := params[0]
|
||||
indent := params[1]
|
||||
|
||||
return json.MarshalIndent(ienet, prefix, indent)
|
||||
}
|
||||
|
||||
return json.Marshal(ienet)
|
||||
}
|
||||
Reference in New Issue
Block a user