v1.2.1
This commit is contained in:
96
pkg/cloudapi/k8s/filter.go
Normal file
96
pkg/cloudapi/k8s/filter.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package k8s
|
||||
|
||||
// FilterByID returns ListK8SClusters with specified ID.
|
||||
func (lkc ListK8SClusters) FilterByID(id uint64) ListK8SClusters {
|
||||
predicate := func(ikc ItemK8SCluster) bool {
|
||||
return ikc.ID == id
|
||||
}
|
||||
|
||||
return lkc.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByName returns ListK8SClusters with specified Name.
|
||||
func (lkc ListK8SClusters) FilterByName(name string) ListK8SClusters {
|
||||
predicate := func(ikc ItemK8SCluster) bool {
|
||||
return ikc.Name == name
|
||||
}
|
||||
|
||||
return lkc.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByAccountID returns ListK8SClusters with specified AccountID.
|
||||
func (lkc ListK8SClusters) FilterByAccountID(accountID uint64) ListK8SClusters {
|
||||
predicate := func(ikc ItemK8SCluster) bool {
|
||||
return ikc.AccountID == accountID
|
||||
}
|
||||
|
||||
return lkc.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByRGID returns ListK8SClusters with specified RGID.
|
||||
func (lkc ListK8SClusters) FilterByRGID(rgID uint64) ListK8SClusters {
|
||||
predicate := func(ikc ItemK8SCluster) bool {
|
||||
return ikc.RGID == rgID
|
||||
}
|
||||
|
||||
return lkc.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByStatus returns ListK8SClusters with specified Status.
|
||||
func (lkc ListK8SClusters) FilterByStatus(status string) ListK8SClusters {
|
||||
predicate := func(ikc ItemK8SCluster) bool {
|
||||
return ikc.Status == status
|
||||
}
|
||||
|
||||
return lkc.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByTechStatus returns ListK8SClusters with specified TechStatus.
|
||||
func (lkc ListK8SClusters) FilterByTechStatus(techStatus string) ListK8SClusters {
|
||||
predicate := func(ikc ItemK8SCluster) bool {
|
||||
return ikc.TechStatus == techStatus
|
||||
}
|
||||
|
||||
return lkc.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByCreatedBy returns ListK8SClusters created by specified user.
|
||||
func (lkc ListK8SClusters) FilterByCreatedBy(createdBy string) ListK8SClusters {
|
||||
predicate := func(ikc ItemK8SCluster) bool {
|
||||
return ikc.CreatedBy == createdBy
|
||||
}
|
||||
|
||||
return lkc.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByDeletedBy returns ListK8SClusters deleted by specified user.
|
||||
func (lkc ListK8SClusters) FilterByDeletedBy(deletedBy string) ListK8SClusters {
|
||||
predicate := func(ikc ItemK8SCluster) bool {
|
||||
return ikc.DeletedBy == deletedBy
|
||||
}
|
||||
|
||||
return lkc.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterFunc allows filtering ListK8SClusters based on a user-specified predicate.
|
||||
func (lkc ListK8SClusters) FilterFunc(predicate func(ItemK8SCluster) bool) ListK8SClusters {
|
||||
var result ListK8SClusters
|
||||
|
||||
for _, item := range lkc {
|
||||
if predicate(item) {
|
||||
result = append(result, item)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// FindOne returns first found ItemK8SCluster
|
||||
// If none was found, returns an empty struct.
|
||||
func (lkc ListK8SClusters) FindOne() ItemK8SCluster {
|
||||
if len(lkc) == 0 {
|
||||
return ItemK8SCluster{}
|
||||
}
|
||||
|
||||
return lkc[0]
|
||||
}
|
||||
188
pkg/cloudapi/k8s/filter_test.go
Normal file
188
pkg/cloudapi/k8s/filter_test.go
Normal file
@@ -0,0 +1,188 @@
|
||||
package k8s
|
||||
|
||||
import "testing"
|
||||
|
||||
var k8sItems = ListK8SClusters{
|
||||
ItemK8SCluster{
|
||||
AccountID: 1,
|
||||
AccountName: "test_1",
|
||||
ACL: []interface{}{},
|
||||
BServiceID: 1,
|
||||
CIID: 1,
|
||||
Config: nil,
|
||||
CreatedBy: "test_user",
|
||||
CreatedTime: 132454563,
|
||||
DeletedBy: "",
|
||||
DeletedTime: 0,
|
||||
Description: "",
|
||||
ExtNetID: 1,
|
||||
GID: 0,
|
||||
GUID: 1,
|
||||
ID: 1,
|
||||
LBID: 1,
|
||||
Milestones: 999999,
|
||||
Name: "k8s_1",
|
||||
RGID: 1,
|
||||
RGName: "rg_1",
|
||||
Status: "ENABLED",
|
||||
TechStatus: "STARTED",
|
||||
UpdatedBy: "",
|
||||
UpdatedTime: 0,
|
||||
VINSID: 0,
|
||||
},
|
||||
ItemK8SCluster{
|
||||
AccountID: 2,
|
||||
AccountName: "test_2",
|
||||
ACL: []interface{}{},
|
||||
BServiceID: 2,
|
||||
CIID: 2,
|
||||
Config: nil,
|
||||
CreatedBy: "test_user",
|
||||
CreatedTime: 132454638,
|
||||
DeletedBy: "",
|
||||
DeletedTime: 0,
|
||||
Description: "",
|
||||
ExtNetID: 2,
|
||||
GID: 0,
|
||||
GUID: 2,
|
||||
ID: 2,
|
||||
LBID: 2,
|
||||
Milestones: 999999,
|
||||
Name: "k8s_2",
|
||||
RGID: 2,
|
||||
RGName: "rg_2",
|
||||
Status: "ENABLED",
|
||||
TechStatus: "STARTED",
|
||||
UpdatedBy: "",
|
||||
UpdatedTime: 0,
|
||||
VINSID: 0,
|
||||
},
|
||||
ItemK8SCluster{
|
||||
AccountID: 3,
|
||||
AccountName: "test_3",
|
||||
ACL: []interface{}{},
|
||||
BServiceID: 3,
|
||||
CIID: 3,
|
||||
Config: nil,
|
||||
CreatedBy: "test_user",
|
||||
CreatedTime: 132454682,
|
||||
DeletedBy: "",
|
||||
DeletedTime: 0,
|
||||
Description: "",
|
||||
ExtNetID: 3,
|
||||
GID: 0,
|
||||
GUID: 3,
|
||||
ID: 3,
|
||||
LBID: 3,
|
||||
Milestones: 999999,
|
||||
Name: "k8s_3",
|
||||
RGID: 3,
|
||||
RGName: "rg_3",
|
||||
Status: "DISABLED",
|
||||
TechStatus: "STOPPED",
|
||||
UpdatedBy: "",
|
||||
UpdatedTime: 0,
|
||||
VINSID: 0,
|
||||
},
|
||||
}
|
||||
|
||||
func TestFilterByID(t *testing.T) {
|
||||
actual := k8sItems.FilterByID(1).FindOne()
|
||||
|
||||
if actual.ID != 1 {
|
||||
t.Fatal("expected 1 ID, found: ", actual.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByName(t *testing.T) {
|
||||
actual := k8sItems.FilterByName("k8s_3").FindOne()
|
||||
|
||||
if actual.Name != "k8s_3" {
|
||||
t.Fatal("expected Name 'k8s_3', found: ", actual.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByAccountID(t *testing.T) {
|
||||
actual := k8sItems.FilterByAccountID(2).FindOne()
|
||||
|
||||
if actual.AccountID != 2 {
|
||||
t.Fatal("expected AccountID 2, found: ", actual.AccountID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByRGID(t *testing.T) {
|
||||
actual := k8sItems.FilterByRGID(3).FindOne()
|
||||
|
||||
if actual.RGID != 3 {
|
||||
t.Fatal("expected RGID 3, found: ", actual.RGID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByStatus(t *testing.T) {
|
||||
actual := k8sItems.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 TestFilterByTechStatus(t *testing.T) {
|
||||
actual := k8sItems.FilterByTechStatus("STARTED")
|
||||
|
||||
if len(actual) != 2 {
|
||||
t.Fatal("expected 2 found, actual: ", len(actual))
|
||||
}
|
||||
|
||||
for _, item := range actual {
|
||||
if item.TechStatus != "STARTED" {
|
||||
t.Fatal("expected TechStatus 'STARTED', found: ", item.TechStatus)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByCreatedBy(t *testing.T) {
|
||||
actual := k8sItems.FilterByCreatedBy("test_user")
|
||||
|
||||
if len(actual) != 3 {
|
||||
t.Fatal("expected 3 found, actual: ", len(actual))
|
||||
}
|
||||
|
||||
for _, item := range actual {
|
||||
if item.CreatedBy != "test_user" {
|
||||
t.Fatal("expected CreatedBy 'test_user', found: ", item.CreatedBy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByDeletedBy(t *testing.T) {
|
||||
actual := k8sItems.FilterByDeletedBy("test_user")
|
||||
|
||||
if len(actual) != 0 {
|
||||
t.Fatal("expected 0 found, actual: ", len(actual))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterFunc(t *testing.T) {
|
||||
actual := k8sItems.FilterFunc(func(iks ItemK8SCluster) bool {
|
||||
return iks.AccountName == "test_2"
|
||||
}).
|
||||
FindOne()
|
||||
|
||||
if actual.AccountName != "test_2" {
|
||||
t.Fatal("expected AccountName 'test_2', found: ", actual.AccountName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortByCreatedTime(t *testing.T) {
|
||||
actual := k8sItems.SortByCreatedTime(false)
|
||||
|
||||
if actual[0].CreatedTime != 132454563 || actual[2].CreatedTime != 132454682 {
|
||||
t.Fatal("expected ascending sort, seems to be inversed")
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"github.com/rudecs/decort-sdk/interfaces"
|
||||
"repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces"
|
||||
)
|
||||
|
||||
// Structure for creating request to K8S
|
||||
|
||||
43
pkg/cloudapi/k8s/serialize.go
Normal file
43
pkg/cloudapi/k8s/serialize.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package k8s
|
||||
|
||||
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 (lkc ListK8SClusters) Serialize(params ...string) (serialization.Serialized, error) {
|
||||
if len(lkc) == 0 {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
if len(params) > 1 {
|
||||
prefix := params[0]
|
||||
indent := params[1]
|
||||
|
||||
return json.MarshalIndent(lkc, prefix, indent)
|
||||
}
|
||||
|
||||
return json.Marshal(lkc)
|
||||
}
|
||||
|
||||
// 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 (ikc ItemK8SCluster) Serialize(params ...string) (serialization.Serialized, error) {
|
||||
if len(params) > 1 {
|
||||
prefix := params[0]
|
||||
indent := params[1]
|
||||
|
||||
return json.MarshalIndent(ikc, prefix, indent)
|
||||
}
|
||||
|
||||
return json.Marshal(ikc)
|
||||
}
|
||||
60
pkg/cloudapi/k8s/sorting.go
Normal file
60
pkg/cloudapi/k8s/sorting.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package k8s
|
||||
|
||||
import "sort"
|
||||
|
||||
// SortByCreatedTime sorts ListK8SClusters by the CreatedTime field in ascending order.
|
||||
//
|
||||
// If inverse param is set to true, the order is reversed.
|
||||
func (lkc ListK8SClusters) SortByCreatedTime(inverse bool) ListK8SClusters {
|
||||
if len(lkc) < 2 {
|
||||
return lkc
|
||||
}
|
||||
|
||||
sort.Slice(lkc, func(i, j int) bool {
|
||||
if inverse {
|
||||
return lkc[i].CreatedTime > lkc[j].CreatedTime
|
||||
}
|
||||
|
||||
return lkc[i].CreatedTime < lkc[j].CreatedTime
|
||||
})
|
||||
|
||||
return lkc
|
||||
}
|
||||
|
||||
// SortByUpdatedTime sorts ListK8SClusters by the UpdatedTime field in ascending order.
|
||||
//
|
||||
// If inverse param is set to true, the order is reversed.
|
||||
func (lkc ListK8SClusters) SortByUpdatedTime(inverse bool) ListK8SClusters {
|
||||
if len(lkc) < 2 {
|
||||
return lkc
|
||||
}
|
||||
|
||||
sort.Slice(lkc, func(i, j int) bool {
|
||||
if inverse {
|
||||
return lkc[i].UpdatedTime > lkc[j].UpdatedTime
|
||||
}
|
||||
|
||||
return lkc[i].UpdatedTime < lkc[j].UpdatedTime
|
||||
})
|
||||
|
||||
return lkc
|
||||
}
|
||||
|
||||
// SortByDeletedTime sorts ListK8SClusters by the DeletedTime field in ascending order.
|
||||
//
|
||||
// If inverse param is set to true, the order is reversed.
|
||||
func (lkc ListK8SClusters) SortByDeletedTime(inverse bool) ListK8SClusters {
|
||||
if len(lkc) < 2 {
|
||||
return lkc
|
||||
}
|
||||
|
||||
sort.Slice(lkc, func(i, j int) bool {
|
||||
if inverse {
|
||||
return lkc[i].DeletedTime > lkc[j].DeletedTime
|
||||
}
|
||||
|
||||
return lkc[i].DeletedTime < lkc[j].DeletedTime
|
||||
})
|
||||
|
||||
return lkc
|
||||
}
|
||||
Reference in New Issue
Block a user