v1.8.0
This commit is contained in:
99
pkg/cloudapi/vfpool/filter.go
Normal file
99
pkg/cloudapi/vfpool/filter.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package vfpool
|
||||
|
||||
// FilterByID returns ListVFPool with specified ID.
|
||||
func (lvfp ListVFPool) FilterByID(id uint64) ListVFPool {
|
||||
predicate := func(ivfp ItemVFPool) bool {
|
||||
return ivfp.ID == id
|
||||
}
|
||||
|
||||
return lvfp.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByGID returns ListVFPool with specified GID.
|
||||
func (lvfp ListVFPool) FilterByGID(gid uint64) ListVFPool {
|
||||
predicate := func(ivfp ItemVFPool) bool {
|
||||
return ivfp.GID == gid
|
||||
}
|
||||
|
||||
return lvfp.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByName returns ListVFPool with specified Name.
|
||||
func (lvfp ListVFPool) FilterByName(name string) ListVFPool {
|
||||
predicate := func(ivfp ItemVFPool) bool {
|
||||
return ivfp.Name == name
|
||||
}
|
||||
|
||||
return lvfp.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByDescription returns ListVFPool with specified Description.
|
||||
func (lvfp ListVFPool) FilterByDescription(description string) ListVFPool {
|
||||
predicate := func(ivfp ItemVFPool) bool {
|
||||
return ivfp.Description == description
|
||||
}
|
||||
|
||||
return lvfp.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByStatus returns ListVFPool with specified Status.
|
||||
func (lvfp ListVFPool) FilterByStatus(status string) ListVFPool {
|
||||
predicate := func(ivfp ItemVFPool) bool {
|
||||
return ivfp.Status == status
|
||||
}
|
||||
|
||||
return lvfp.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByAccountAccess returns ListVFPool with specified AccountAccess.
|
||||
func (lvfp ListVFPool) FilterByAccountAccess(accountAccess uint64) ListVFPool {
|
||||
predicate := func(ivfp ItemVFPool) bool {
|
||||
for _, i := range ivfp.AccountAccess {
|
||||
if i == accountAccess {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
return lvfp.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByRGAccess returns ListVFPool with specified RGAccess.
|
||||
func (lvfp ListVFPool) FilterByRGAccess(rgAccess uint64) ListVFPool {
|
||||
predicate := func(ivfp ItemVFPool) bool {
|
||||
for _, i := range ivfp.RGAccess {
|
||||
if i == rgAccess {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
return lvfp.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterFunc allows filtering ListVFPool based on a user-specified predicate.
|
||||
func (lvfp ListVFPool) FilterFunc(predicate func(ItemVFPool) bool) ListVFPool {
|
||||
var result ListVFPool
|
||||
|
||||
for _, item := range lvfp.Data {
|
||||
if predicate(item) {
|
||||
result.Data = append(result.Data, item)
|
||||
}
|
||||
}
|
||||
|
||||
result.EntryCount = uint64(len(result.Data))
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// FindOne returns first found ItemVFPool
|
||||
// If none was found, returns an empty struct.
|
||||
func (lvfp ListVFPool) FindOne() ItemVFPool {
|
||||
if lvfp.EntryCount == 0 {
|
||||
return ItemVFPool{}
|
||||
}
|
||||
|
||||
return lvfp.Data[0]
|
||||
}
|
||||
138
pkg/cloudapi/vfpool/filter_test.go
Normal file
138
pkg/cloudapi/vfpool/filter_test.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package vfpool
|
||||
|
||||
import "testing"
|
||||
|
||||
var vfpools = ListVFPool{
|
||||
Data: []ItemVFPool{
|
||||
{
|
||||
AccountAccess: []uint64{1, 2},
|
||||
Description: "descr",
|
||||
GID: 1,
|
||||
ID: 1,
|
||||
Name: "name",
|
||||
RGAccess: []uint64{3, 4},
|
||||
Status: "ENABLED",
|
||||
},
|
||||
{
|
||||
AccountAccess: []uint64{},
|
||||
Description: "",
|
||||
GID: 2,
|
||||
ID: 2,
|
||||
Name: "name2",
|
||||
RGAccess: []uint64{},
|
||||
Status: "DISABLED",
|
||||
},
|
||||
{
|
||||
AccountAccess: []uint64{7, 8},
|
||||
Description: "",
|
||||
GID: 215,
|
||||
ID: 3,
|
||||
Name: "name3",
|
||||
RGAccess: []uint64{5, 6},
|
||||
Status: "DISABLED",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestFilterByID(t *testing.T) {
|
||||
actual := vfpools.FilterByID(1).FindOne()
|
||||
|
||||
if actual.ID != 1 {
|
||||
t.Fatal("expected ID 1, found: ", actual.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByGID(t *testing.T) {
|
||||
var gid uint64 = 1
|
||||
actual := vfpools.FilterByGID(gid).FindOne()
|
||||
|
||||
if actual.GID != gid {
|
||||
t.Fatal("expected ", gid, " found: ", actual.GID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByName(t *testing.T) {
|
||||
name := "name"
|
||||
actual := vfpools.FilterByName(name).FindOne()
|
||||
|
||||
if actual.Name != name {
|
||||
t.Fatal("expected ", name, " found: ", actual.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByDescription(t *testing.T) {
|
||||
description := "descr"
|
||||
actual := vfpools.FilterByDescription(description).FindOne()
|
||||
|
||||
if actual.Description != description {
|
||||
t.Fatal("expected ", description, " found: ", actual.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByStatus(t *testing.T) {
|
||||
actual := vfpools.FilterByStatus("ENABLED")
|
||||
|
||||
if len(actual.Data) != 1 {
|
||||
t.Fatal("expected 1 found, actual: ", len(actual.Data))
|
||||
}
|
||||
|
||||
for _, item := range actual.Data {
|
||||
if item.Status != "ENABLED" {
|
||||
t.Fatal("expected Status 'ENABLED', found: ", item.Status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByAccountAccess(t *testing.T) {
|
||||
var account uint64 = 1
|
||||
actual := vfpools.FilterByAccountAccess(account)
|
||||
|
||||
if len(actual.Data) != 1 {
|
||||
t.Fatal("expected 1 found, actual: ", len(actual.Data))
|
||||
}
|
||||
|
||||
for _, item := range actual.Data {
|
||||
var found bool
|
||||
for _, a := range item.AccountAccess {
|
||||
if a == account {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Fatalf("expected account access %d, found: %v", account, item.AccountAccess)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByRGAccess(t *testing.T) {
|
||||
var rg uint64 = 3
|
||||
actual := vfpools.FilterByRGAccess(rg)
|
||||
|
||||
if len(actual.Data) != 1 {
|
||||
t.Fatal("expected 1 found, actual: ", len(actual.Data))
|
||||
}
|
||||
|
||||
for _, item := range actual.Data {
|
||||
var found bool
|
||||
for _, r := range item.RGAccess {
|
||||
if r == rg {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Fatalf("expected account access %d, found: %v", rg, item.RGAccess)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterFunc(t *testing.T) {
|
||||
actual := vfpools.FilterFunc(func(ivfpool ItemVFPool) bool {
|
||||
return ivfpool.GID == ivfpool.ID
|
||||
})
|
||||
|
||||
if len(actual.Data) != 2 {
|
||||
t.Fatal("expected 2 elements, found: ", len(actual.Data))
|
||||
}
|
||||
}
|
||||
46
pkg/cloudapi/vfpool/get.go
Normal file
46
pkg/cloudapi/vfpool/get.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package vfpool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||
)
|
||||
|
||||
// GetRequest struct to get detailed information about vfpool device
|
||||
type GetRequest struct {
|
||||
// ID of vfpool device
|
||||
// Required: true
|
||||
VFPoolID uint64 `url:"id" json:"id" validate:"required"`
|
||||
}
|
||||
|
||||
// Get gets detailed information about vfpool device as a RecordVFPool struct
|
||||
func (v VFPool) Get(ctx context.Context, req GetRequest) (*RecordVFPool, error) {
|
||||
res, err := v.GetRaw(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := RecordVFPool{}
|
||||
|
||||
err = json.Unmarshal(res, &info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
// GetRaw gets detailed information about vfpool device as an array of bytes
|
||||
func (v VFPool) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
|
||||
err := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
url := "/cloudapi/vfpool/get"
|
||||
|
||||
res, err := v.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
return res, err
|
||||
}
|
||||
19
pkg/cloudapi/vfpool/ids.go
Normal file
19
pkg/cloudapi/vfpool/ids.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package vfpool
|
||||
|
||||
// IDs gets array of VFPool IDs from ListVFPool struct
|
||||
func (lv ListVFPool) IDs() []uint64 {
|
||||
res := make([]uint64, 0, len(lv.Data))
|
||||
for _, e := range lv.Data {
|
||||
res = append(res, e.ID)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// IDs gets array of VF IDs from VFSInfoList struct
|
||||
func (lv VFSInfoList) IDs() []uint64 {
|
||||
res := make([]uint64, 0, len(lv))
|
||||
for _, e := range lv {
|
||||
res = append(res, e.ID)
|
||||
}
|
||||
return res
|
||||
}
|
||||
83
pkg/cloudapi/vfpool/list.go
Normal file
83
pkg/cloudapi/vfpool/list.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package vfpool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
||||
)
|
||||
|
||||
// ListRequest struct to get list of vfpool devices
|
||||
type ListRequest struct {
|
||||
// Find by ID
|
||||
// Required: false
|
||||
ByID uint64 `url:"by_id,omitempty" json:"by_id,omitempty"`
|
||||
|
||||
// Find by Grid ID
|
||||
// Required: false
|
||||
GID uint64 `url:"gid,omitempty" json:"gid,omitempty"`
|
||||
|
||||
// Find by name
|
||||
// Required: false
|
||||
Name string `url:"name,omitempty" json:"name,omitempty"`
|
||||
|
||||
// Find by description
|
||||
// Required: false
|
||||
Description string `url:"description,omitempty" json:"description,omitempty"`
|
||||
|
||||
// Find by status
|
||||
// Required: false
|
||||
Status string `url:"status,omitempty" json:"status,omitempty"`
|
||||
|
||||
// Find by account Access
|
||||
// Required: false
|
||||
AccountAccess uint64 `url:"accountAccess,omitempty" json:"accountAccess,omitempty"`
|
||||
|
||||
// Find by resource group Access
|
||||
// Required: false
|
||||
RGAccess uint64 `url:"rgAccess,omitempty" json:"rgAccess,omitempty"`
|
||||
|
||||
// Sort by one of supported fields, format +|-(field)
|
||||
// Required: false
|
||||
SortBy string `url:"sortBy,omitempty" json:"sortBy,omitempty" validate:"omitempty,sortBy"`
|
||||
|
||||
// Page number
|
||||
// Required: false
|
||||
Page uint64 `url:"page,omitempty" json:"page,omitempty"`
|
||||
|
||||
// Page size
|
||||
// Required: false
|
||||
Size uint64 `url:"size,omitempty" json:"size,omitempty"`
|
||||
}
|
||||
|
||||
// List gets list of all available vfpool devices as a ListVFPool struct
|
||||
func (v VFPool) List(ctx context.Context, req ListRequest) (*ListVFPool, error) {
|
||||
|
||||
res, err := v.ListRaw(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := ListVFPool{}
|
||||
|
||||
err = json.Unmarshal(res, &list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &list, nil
|
||||
}
|
||||
|
||||
// ListRaw gets list of all available vfpool devices as an array of bytes
|
||||
func (v VFPool) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {
|
||||
|
||||
if err := validators.ValidateRequest(req); err != nil {
|
||||
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
url := "/cloudapi/vfpool/list"
|
||||
|
||||
res, err := v.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
return res, err
|
||||
}
|
||||
116
pkg/cloudapi/vfpool/models.go
Normal file
116
pkg/cloudapi/vfpool/models.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package vfpool
|
||||
|
||||
// Main information about vfpool device
|
||||
type ItemVFPool struct {
|
||||
// AccountAccess
|
||||
AccountAccess []uint64 `json:"accountAccess"`
|
||||
|
||||
// CreatedTime
|
||||
CreatedTime uint64 `json:"createdTime"`
|
||||
|
||||
// Description
|
||||
Description string `json:"description"`
|
||||
|
||||
// GID
|
||||
GID uint64 `json:"gid"`
|
||||
|
||||
// GUID
|
||||
GUID uint64 `json:"guid"`
|
||||
|
||||
// ID
|
||||
ID uint64 `json:"id"`
|
||||
|
||||
// Name
|
||||
Name string `json:"name"`
|
||||
|
||||
// RGAccess
|
||||
RGAccess []uint64 `json:"rgAccess"`
|
||||
|
||||
// Status
|
||||
Status string `json:"status"`
|
||||
|
||||
// UpdatedTime
|
||||
UpdatedTime uint64 `json:"updatedTime"`
|
||||
|
||||
// VFS
|
||||
VFS []VFS `json:"vfs"`
|
||||
}
|
||||
|
||||
// List of information about vfpool devices
|
||||
type ListVFPool struct {
|
||||
Data []ItemVFPool `json:"data"`
|
||||
|
||||
EntryCount uint64 `json:"entryCount"`
|
||||
}
|
||||
|
||||
// Detailed information about vfpool device
|
||||
type RecordVFPool struct {
|
||||
// AccountAccess
|
||||
AccountAccess []uint64 `json:"accountAccess"`
|
||||
|
||||
// CreatedTime
|
||||
CreatedTime uint64 `json:"createdTime"`
|
||||
|
||||
// Description
|
||||
Description string `json:"description"`
|
||||
|
||||
// GID
|
||||
GID uint64 `json:"gid"`
|
||||
|
||||
// GUID
|
||||
GUID uint64 `json:"guid"`
|
||||
|
||||
// ID
|
||||
ID uint64 `json:"id"`
|
||||
|
||||
// Name
|
||||
Name string `json:"name"`
|
||||
|
||||
// RGAccess
|
||||
RGAccess []uint64 `json:"rgAccess"`
|
||||
|
||||
// Status
|
||||
Status string `json:"status"`
|
||||
|
||||
// UpdatedTime
|
||||
UpdatedTime uint64 `json:"updatedTime"`
|
||||
|
||||
// VFS
|
||||
VFS []VFS `json:"vfs"`
|
||||
}
|
||||
|
||||
// VFS struct
|
||||
type VFS struct {
|
||||
// NodeID
|
||||
NodeID uint64 `json:"nodeId"`
|
||||
|
||||
// UpdatedTime
|
||||
VFList VFList `json:"vfList"`
|
||||
}
|
||||
|
||||
// VFList struct
|
||||
type VFList []VFItem
|
||||
|
||||
// VFItem struct
|
||||
type VFItem struct {
|
||||
// NicName
|
||||
NicName string `json:"nicName"`
|
||||
|
||||
// VFSInfo list
|
||||
VFSInfo VFSInfoList `json:"vfsInfo"`
|
||||
}
|
||||
|
||||
// VFSInfoList struct
|
||||
type VFSInfoList []VFSInfoItem
|
||||
|
||||
// VFSInfoItem struct
|
||||
type VFSInfoItem struct {
|
||||
// ID
|
||||
ID uint64 `json:"id"`
|
||||
|
||||
// Claimed
|
||||
Claimed bool `json:"claimed"`
|
||||
|
||||
// VM ID
|
||||
VMID uint64 `json:"vmId"`
|
||||
}
|
||||
59
pkg/cloudapi/vfpool/serialize.go
Normal file
59
pkg/cloudapi/vfpool/serialize.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package vfpool
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"repository.basistech.ru/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 (lvfpool ListVFPool) Serialize(params ...string) (serialization.Serialized, error) {
|
||||
if lvfpool.EntryCount == 0 {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
if len(params) > 1 {
|
||||
prefix := params[0]
|
||||
indent := params[1]
|
||||
|
||||
return json.MarshalIndent(lvfpool, prefix, indent)
|
||||
}
|
||||
|
||||
return json.Marshal(lvfpool)
|
||||
}
|
||||
|
||||
// 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 (rvfpool RecordVFPool) Serialize(params ...string) (serialization.Serialized, error) {
|
||||
if len(params) > 1 {
|
||||
prefix := params[0]
|
||||
indent := params[1]
|
||||
|
||||
return json.MarshalIndent(rvfpool, prefix, indent)
|
||||
}
|
||||
|
||||
return json.Marshal(rvfpool)
|
||||
}
|
||||
|
||||
// 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 (ivfpool ItemVFPool) Serialize(params ...string) (serialization.Serialized, error) {
|
||||
if len(params) > 1 {
|
||||
prefix := params[0]
|
||||
indent := params[1]
|
||||
|
||||
return json.MarshalIndent(ivfpool, prefix, indent)
|
||||
}
|
||||
|
||||
return json.Marshal(ivfpool)
|
||||
}
|
||||
18
pkg/cloudapi/vfpool/vfpool.go
Normal file
18
pkg/cloudapi/vfpool/vfpool.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// API Actor for managing vfpool device
|
||||
package vfpool
|
||||
|
||||
import (
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/interfaces"
|
||||
)
|
||||
|
||||
// Structure for creating request to vfpool
|
||||
type VFPool struct {
|
||||
client interfaces.Caller
|
||||
}
|
||||
|
||||
// Builder for vfpool endpoints
|
||||
func New(client interfaces.Caller) *VFPool {
|
||||
return &VFPool{
|
||||
client,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user