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 create disk

View File

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

View File

@@ -0,0 +1,60 @@
package disks
// FilterByID returns ListDisks with specified ID.
func (ld ListDisks) FilterByID(id uint64) ListDisks {
predicate := func(idisk ItemDisk) bool {
return idisk.ID == id
}
return ld.FilterFunc(predicate)
}
// FilterByName returns ListDisks with specified Name.
func (ld ListDisks) FilterByName(name string) ListDisks {
predicate := func(idisk ItemDisk) bool {
return idisk.Name == name
}
return ld.FilterFunc(predicate)
}
// FilterByStatus returns ListDisks with specified Status.
func (ld ListDisks) FilterByStatus(status string) ListDisks {
predicate := func(idisk ItemDisk) bool {
return idisk.Status == status
}
return ld.FilterFunc(predicate)
}
// FilterByTechStatus returns ListDisks with specified TechStatus.
func (ld ListDisks) FilterByTechStatus(techStatus string) ListDisks {
predicate := func(idisk ItemDisk) bool {
return idisk.TechStatus == techStatus
}
return ld.FilterFunc(predicate)
}
// FilterFunc allows filtering ListDisks based on a user-specified predicate.
func (ld ListDisks) FilterFunc(predicate func(ItemDisk) bool) ListDisks {
var result ListDisks
for _, item := range ld {
if predicate(item) {
result = append(result, item)
}
}
return result
}
// FindOne returns first found ItemDisk
// If none was found, returns an empty struct.
func (ld ListDisks) FindOne() ItemDisk {
if len(ld) == 0 {
return ItemDisk{}
}
return ld[0]
}

View File

@@ -0,0 +1,177 @@
package disks
import "testing"
var disks = ListDisks{
ItemDisk{
MachineID: 0,
MachineName: "",
DeviceName: "vda",
AccountID: 132847,
AccountName: "std_2",
ACL: map[string]interface{}{},
Computes: map[string]string{
"48500": "test",
},
CreatedTime: 1676975177,
DeletedTime: 0,
Description: "",
DestructionTime: 0,
GID: 212,
ID: 65191,
ImageID: 9884,
Images: []uint64{},
IOTune: IOTune{
TotalIOPSSec: 2000,
},
Name: "bootdisk",
Order: 0,
Params: "",
ParentID: 0,
PCISlot: 6,
Pool: "vmstor",
PresentTo: []uint64{
27,
},
PurgeTime: 0,
ResID: "sample",
ResName: "sample",
Role: "",
Shareable: false,
SizeMax: 2,
SizeUsed: 2,
Snapshots: []ItemSnapshot{},
Status: "ASSIGNED",
TechStatus: "ALLOCATED",
Type: "B",
VMID: 48500,
},
ItemDisk{
MachineID: 0,
MachineName: "",
DeviceName: "vda",
AccountID: 132852,
AccountName: "std",
ACL: map[string]interface{}{},
Computes: map[string]string{
"48502": "stdvm2",
},
CreatedTime: 1676982606,
DeletedTime: 0,
Description: "",
DestructionTime: 0,
GID: 212,
ID: 65193,
ImageID: 9885,
Images: []uint64{},
IOTune: IOTune{
TotalIOPSSec: 2000,
},
Name: "bootdisk",
Order: 0,
Params: "",
ParentID: 0,
PCISlot: 6,
Pool: "vmstor",
PresentTo: []uint64{
27,
27,
},
PurgeTime: 0,
ResID: "sample",
ResName: "sample",
Role: "",
Shareable: false,
SizeMax: 4,
SizeUsed: 4,
Snapshots: []ItemSnapshot{},
Status: "ASSIGNED",
TechStatus: "ALLOCATED",
Type: "B",
VMID: 48502,
},
}
func TestFilterByID(t *testing.T) {
actual := disks.FilterByID(65193)
if len(actual) == 0 {
t.Fatal("No elements were found")
}
actualItem := actual.FindOne()
if actualItem.ID != 65193 {
t.Fatal("expected ID 65193, found: ", actualItem.ID)
}
}
func TestFilterByName(t *testing.T) {
actual := disks.FilterByName("bootdisk")
if len(actual) != 2 {
t.Fatal("expected 2 elements, found: ", len(actual))
}
for _, item := range actual {
if item.Name != "bootdisk" {
t.Fatal("expected 'bootdisk' name, found: ", item.Name)
}
}
}
func TestFilterByStatus(t *testing.T) {
actual := disks.FilterByStatus("ASSIGNED")
if len(actual) == 0 {
t.Fatal("No elements were found")
}
for _, item := range actual {
if item.Status != "ASSIGNED" {
t.Fatal("expected 'ASSIGNED' status, found: ", item.Status)
}
}
}
func TestFilterByTechStatus(t *testing.T) {
actual := disks.FilterByTechStatus("ALLOCATED")
if len(actual) == 0 {
t.Fatal("No elements were found")
}
for _, item := range actual {
if item.TechStatus != "ALLOCATED" {
t.Fatal("expected 'ALLOCATED' techStatus, found: ", item.TechStatus)
}
}
}
func TestFilterFunc(t *testing.T) {
actual := disks.FilterFunc(func(id ItemDisk) bool {
return len(id.PresentTo) == 2
})
if len(actual) == 0 {
t.Fatal("No elements were found")
}
if len(actual[0].PresentTo) != 2 {
t.Fatal("expected 2 elements in PresentTo, found: ", len(actual[0].PresentTo))
}
}
func TestSortByCreatedTime(t *testing.T) {
actual := disks.SortByCreatedTime(false)
if actual[0].ID != 65191 {
t.Fatal("expected ID 65191, found: ", actual[0].ID)
}
actual = disks.SortByCreatedTime(true)
if actual[0].ID != 65193 {
t.Fatal("expected ID 65193, found: ", actual[0].ID)
}
}

View File

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

View File

@@ -0,0 +1,60 @@
package disks
import "sort"
// SortByCreatedTime sorts ListDisks by the CreatedTime field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (ld ListDisks) SortByCreatedTime(inverse bool) ListDisks {
if len(ld) < 2 {
return ld
}
sort.Slice(ld, func(i, j int) bool {
if inverse {
return ld[i].CreatedTime > ld[j].CreatedTime
}
return ld[i].CreatedTime < ld[j].CreatedTime
})
return ld
}
// SortByDestructionTime sorts ListDisks by the DestructionTime field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (ld ListDisks) SortByDestructionTime(inverse bool) ListDisks {
if len(ld) < 2 {
return ld
}
sort.Slice(ld, func(i, j int) bool {
if inverse {
return ld[i].DestructionTime > ld[j].DestructionTime
}
return ld[i].DestructionTime < ld[j].DestructionTime
})
return ld
}
// SortByDeletedTime sorts ListDisks by the DeletedTime field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (ld ListDisks) SortByDeletedTime(inverse bool) ListDisks {
if len(ld) < 2 {
return ld
}
sort.Slice(ld, func(i, j int) bool {
if inverse {
return ld[i].DeletedTime > ld[j].DeletedTime
}
return ld[i].DeletedTime < ld[j].DeletedTime
})
return ld
}