This commit is contained in:
2023-03-14 14:45:51 +03:00
parent 42800ac4fe
commit f3a1a4c83f
202 changed files with 6199 additions and 155 deletions

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"strconv"
"github.com/rudecs/decort-sdk/internal/validators"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
)
// Request struct for add affinity rule

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"strconv"
"github.com/rudecs/decort-sdk/internal/validators"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
)
// Request struct for remove affinity rule

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"strconv"
"github.com/rudecs/decort-sdk/internal/validators"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
)
// Request struct for add anti affinity rule

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"strconv"
"github.com/rudecs/decort-sdk/internal/validators"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
)
// Request struct for remove anti affinity rule

View File

@@ -2,7 +2,7 @@
package compute
import (
"github.com/rudecs/decort-sdk/interfaces"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/interfaces"
)
// Structure for creating request to compute

View File

@@ -0,0 +1,74 @@
package compute
// FilterByID returns ListComputes with specified ID.
func (lc ListComputes) FilterByID(id uint64) ListComputes {
predicate := func(ic ItemCompute) bool {
return ic.ID == id
}
return lc.FilterFunc(predicate)
}
// FilterByName returns ListComputes with specified Name.
func (lc ListComputes) FilterByName(name string) ListComputes {
predicate := func(ic ItemCompute) bool {
return ic.Name == name
}
return lc.FilterFunc(predicate)
}
// FilterByStatus returns ListComputes with specified Status.
func (lc ListComputes) FilterByStatus(status string) ListComputes {
predicate := func(ic ItemCompute) bool {
return ic.Status == status
}
return lc.FilterFunc(predicate)
}
// FilterByTechStatus returns ListComputes with specified TechStatus.
func (lc ListComputes) FilterByTechStatus(techStatus string) ListComputes {
predicate := func(ic ItemCompute) bool {
return ic.TechStatus == techStatus
}
return lc.FilterFunc(predicate)
}
// FilterByDiskID return ListComputes with specified DiskID.
func (lc ListComputes) FilterByDiskID(diskID uint64) ListComputes {
predicate := func(ic ItemCompute) bool {
for _, disk := range ic.Disks {
if disk.ID == diskID {
return true
}
}
return false
}
return lc.FilterFunc(predicate)
}
// FilterFunc allows filtering ListComputes based on a user-specified predicate.
func (lc ListComputes) FilterFunc(predicate func(ItemCompute) bool) ListComputes {
var result ListComputes
for _, item := range lc {
if predicate(item) {
result = append(result, item)
}
}
return result
}
// FindOne returns first found ItemCompute
// If none was found, returns an empty struct.
func (lc ListComputes) FindOne() ItemCompute {
if len(lc) == 0 {
return ItemCompute{}
}
return lc[0]
}

View File

@@ -0,0 +1,241 @@
package compute
import "testing"
var computes = ListComputes{
ItemCompute{
ACL: []interface{}{},
AccountID: 132847,
AccountName: "std_2",
AffinityLabel: "",
AffinityRules: []ItemRule{
{
GUID: "",
Key: "aff_key",
Mode: "ANY",
Policy: "RECOMMENDED",
Topology: "compute",
Value: "aff_val",
},
},
AffinityWeight: 0,
AntiAffinityRules: []ItemRule{
{
GUID: "",
Key: "antiaff_key",
Mode: "ANY",
Policy: "RECOMMENDED",
Topology: "compute",
Value: "antiaff_val",
},
},
Architecture: "X86_64",
BootOrder: []string{
"hd", "cdrom",
},
BootDiskSize: 0,
CloneReference: 0,
Clones: []uint64{},
ComputeCIID: 0,
CPU: 4,
CreatedBy: "timofey_tkachev_1@decs3o",
CreatedTime: 1676975175,
CustomFields: map[string]interface{}{},
DeletedBy: "",
DeletedTime: 0,
Description: "",
Devices: nil,
Disks: []InfoDisk{
{
ID: 65191,
PCISlot: 6,
},
},
Driver: "KVM_X86",
GID: 212,
GUID: 48500,
ID: 48500,
ImageID: 9884,
Interfaces: []ItemVNFInterface{},
LockStatus: "UNLOCKED",
ManagerID: 0,
ManagerType: "",
MigrationJob: 0,
Milestones: 363500,
Name: "test",
Pinned: false,
RAM: 4096,
ReferenceID: "c7cb19ac-af4a-4067-852f-c5572949207e",
Registered: true,
ResName: "compute-48500",
RGID: 79724,
RGName: "std_broker2",
SnapSets: []ItemSnapSet{},
StatelessSepID: 0,
StatelessSepType: "",
Status: "ENABLED",
Tags: map[string]string{},
TechStatus: "STOPPED",
TotalDiskSize: 2,
UpdatedBy: "",
UpdatedTime: 1677058904,
UserManaged: true,
VGPUs: []uint64{},
VINSConnected: 0,
VirtualImageID: 0,
},
ItemCompute{
ACL: []interface{}{},
AccountID: 132848,
AccountName: "std_broker",
AffinityLabel: "",
AffinityRules: []ItemRule{},
AffinityWeight: 0,
AntiAffinityRules: []ItemRule{},
Architecture: "X86_64",
BootOrder: []string{
"hd", "cdrom",
},
BootDiskSize: 0,
CloneReference: 0,
Clones: []uint64{},
ComputeCIID: 0,
CPU: 6,
CreatedBy: "timofey_tkachev_1@decs3o",
CreatedTime: 1677579436,
CustomFields: map[string]interface{}{},
DeletedBy: "",
DeletedTime: 0,
Description: "",
Devices: nil,
Disks: []InfoDisk{
{
ID: 65248,
PCISlot: 6,
},
},
Driver: "KVM_X86",
GID: 212,
GUID: 48556,
ID: 48556,
ImageID: 9884,
Interfaces: []ItemVNFInterface{},
LockStatus: "UNLOCKED",
ManagerID: 0,
ManagerType: "",
MigrationJob: 0,
Milestones: 363853,
Name: "compute_2",
Pinned: false,
RAM: 4096,
ReferenceID: "a542c449-5b1c-4f90-88c5-7bb5f8ae68ff",
Registered: true,
ResName: "compute-48556",
RGID: 79727,
RGName: "sdk_negative_fields_test",
SnapSets: []ItemSnapSet{},
StatelessSepID: 0,
StatelessSepType: "",
Status: "ENABLED",
Tags: map[string]string{},
TechStatus: "STARTED",
TotalDiskSize: 1,
UpdatedBy: "",
UpdatedTime: 1677579436,
UserManaged: true,
VGPUs: []uint64{},
VINSConnected: 0,
VirtualImageID: 0,
},
}
func TestFilterByID(t *testing.T) {
actual := computes.FilterByID(48500).FindOne()
if actual.ID != 48500 {
t.Fatal("expected ID 48500, found: ", actual.ID)
}
actualEmpty := computes.FilterByID(0)
if len(actualEmpty) != 0 {
t.Fatal("expected empty, actual: ", len(actualEmpty))
}
}
func TestFilterByName(t *testing.T) {
actual := computes.FilterByName("test").FindOne()
if actual.Name != "test" {
t.Fatal("expected compute with name 'test', found: ", actual.Name)
}
}
func TestFilterByStatus(t *testing.T) {
actual := computes.FilterByStatus("ENABLED")
for _, item := range actual {
if item.Status != "ENABLED" {
t.Fatal("expected ENABLED status, found: ", item.Status)
}
}
}
func TestFilterByTechStatus(t *testing.T) {
actual := computes.FilterByTechStatus("STARTED").FindOne()
if actual.ID != 48556 {
t.Fatal("expected 48556 with STARTED techStatus, found: ", actual.ID)
}
}
func TestFilterByDiskID(t *testing.T) {
actual := computes.FilterByDiskID(65248).FindOne()
if actual.ID != 48556 {
t.Fatal("expected 48556 with DiskID 65248, found: ", actual.ID)
}
}
func TestFilterFunc(t *testing.T) {
actual := computes.FilterFunc(func(ic ItemCompute) bool {
return ic.Registered == true
})
if len(actual) != 2 {
t.Fatal("expected 2 elements found, actual: ", len(actual))
}
for _, item := range actual {
if item.Registered != true {
t.Fatal("expected Registered to be true, actual: ", item.Registered)
}
}
}
func TestSortingByCreatedTime(t *testing.T) {
actual := computes.SortByCreatedTime(false)
if actual[0].Name != "test" {
t.Fatal("expected 'test', found: ", actual[0].Name)
}
actual = computes.SortByCreatedTime(true)
if actual[0].Name != "compute_2" {
t.Fatal("expected 'compute_2', found: ", actual[0].Name)
}
}
func TestSortingByCPU(t *testing.T) {
actual := computes.SortByCPU(false)
if actual[0].CPU != 4{
t.Fatal("expected 4 CPU cores, found: ", actual[0].CPU)
}
actual = computes.SortByCPU(true)
if actual[0].CPU != 6 {
t.Fatal("expected 6 CPU cores, found: ", actual[0].CPU)
}
}

View File

@@ -6,7 +6,7 @@ import (
"errors"
"net/http"
"github.com/rudecs/decort-sdk/internal/validators"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
)
// Request struct for attach network

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"strconv"
"github.com/rudecs/decort-sdk/internal/validators"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
)
// Request struct for add port forward rule

View File

@@ -0,0 +1,43 @@
package compute
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 (lc ListComputes) Serialize(params ...string) (serialization.Serialized, error) {
if len(lc) == 0 {
return []byte{}, nil
}
if len(params) > 1 {
prefix := params[0]
indent := params[1]
return json.MarshalIndent(lc, prefix, indent)
}
return json.Marshal(lc)
}
// 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 (ic ItemCompute) Serialize(params ...string) (serialization.Serialized, error) {
if len(params) > 1 {
prefix := params[0]
indent := params[1]
return json.MarshalIndent(ic, prefix, indent)
}
return json.Marshal(ic)
}

View File

@@ -0,0 +1,98 @@
package compute
import "sort"
// SortByCPU sorts ListComputes by the CPU core amount in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (lc ListComputes) SortByCPU(inverse bool) ListComputes {
if len(lc) < 2 {
return lc
}
sort.Slice(lc, func(i, j int) bool {
if inverse {
return lc[i].CPU > lc[j].CPU
}
return lc[i].CPU < lc[j].CPU
})
return lc
}
// SortByRAM sorts ListComputes by the RAM amount in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (lc ListComputes) SortByRAM(inverse bool) ListComputes {
if len(lc) < 2 {
return lc
}
sort.Slice(lc, func(i, j int) bool {
if inverse {
return lc[i].RAM > lc[j].RAM
}
return lc[i].RAM < lc[j].RAM
})
return lc
}
// SortByCreatedTime sorts ListComputes by the CreatedTime field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (lc ListComputes) SortByCreatedTime(inverse bool) ListComputes {
if len(lc) < 2 {
return lc
}
sort.Slice(lc, func(i, j int) bool {
if inverse {
return lc[i].CreatedTime > lc[j].CreatedTime
}
return lc[i].CreatedTime < lc[j].CreatedTime
})
return lc
}
// SortByUpdatedTime sorts ListComputes by the UpdatedTime field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (lc ListComputes) SortByUpdatedTime(inverse bool) ListComputes {
if len(lc) < 2 {
return lc
}
sort.Slice(lc, func(i, j int) bool {
if inverse {
return lc[i].UpdatedTime > lc[j].UpdatedTime
}
return lc[i].UpdatedTime < lc[j].UpdatedTime
})
return lc
}
// SortByDeletedTime sorts ListComputes by the DeletedTime field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (lc ListComputes) SortByDeletedTime(inverse bool) ListComputes {
if len(lc) < 2 {
return lc
}
sort.Slice(lc, func(i, j int) bool {
if inverse {
return lc[i].DeletedTime > lc[j].DeletedTime
}
return lc[i].DeletedTime < lc[j].DeletedTime
})
return lc
}

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"strconv"
"github.com/rudecs/decort-sdk/internal/validators"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
)
// Request struct for grant access to compute

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"strconv"
"github.com/rudecs/decort-sdk/internal/validators"
"repos.digitalenergy.online/BASIS/decort-golang-sdk/internal/validators"
)
// Request struct for update user access