This commit is contained in:
asteam
2025-09-23 14:34:24 +03:00
parent b924e85e49
commit f1ffb4c0fd
1108 changed files with 72020 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
package k8ci
// FilterByID returns ListK8CI with specified ID.
func (lkc ListK8CI) FilterByID(id uint64) ListK8CI {
predicate := func(ikc ItemK8CI) bool {
return ikc.ID == id
}
return lkc.FilterFunc(predicate)
}
// FilterByName returns ListK8CI with specified Name.
func (lkc ListK8CI) FilterByName(name string) ListK8CI {
predicate := func(ikc ItemK8CI) bool {
return ikc.Name == name
}
return lkc.FilterFunc(predicate)
}
// FilterFunc allows filtering ListK8CI based on a user-specified predicate.
func (lkc ListK8CI) FilterFunc(predicate func(ItemK8CI) bool) ListK8CI {
var result ListK8CI
for _, item := range lkc.Data {
if predicate(item) {
result.Data = append(result.Data, item)
}
}
result.EntryCount = uint64(len(result.Data))
return result
}
// FindOne returns first found ItemK8CI
// If none was found, returns an empty struct.
func (lkc ListK8CI) FindOne() ItemK8CI {
if len(lkc.Data) == 0 {
return ItemK8CI{}
}
return lkc.Data[0]
}

View File

@@ -0,0 +1,90 @@
package k8ci
import "testing"
var k8ciItems = ListK8CI{
Data: []ItemK8CI{
{
CreatedTime: 123902139,
Status: "ENABLED",
Description: "",
ID: 1,
Name: "purple_snake",
Version: "1",
LBImageID: 654,
NetworkPlugins: []string{
"flannel",
"calico",
"weavenet",
},
},
{
CreatedTime: 123902232,
Status: "ENABLED",
Description: "",
ID: 2,
Name: "green_giant",
Version: "2",
LBImageID: 654,
NetworkPlugins: []string{
"flannel",
"calico",
"weavenet",
},
},
{
CreatedTime: 123902335,
Status: "DISABLED",
Description: "",
ID: 3,
Name: "magenta_cloud",
Version: "3",
NetworkPlugins: []string{
"flannel",
"calico",
"weavenet",
},
},
},
EntryCount: 3,
}
func TestFilterByID(t *testing.T) {
actual := k8ciItems.FilterByID(2).FindOne()
if actual.ID != 2 {
t.Fatal("expected ID 2, found: ", actual.ID)
}
}
func TestFilterByName(t *testing.T) {
actual := k8ciItems.FilterByName("magenta_cloud").FindOne()
if actual.Name != "magenta_cloud" {
t.Fatal("expected Name 'magenta_cloud', found: ", actual.Name)
}
}
func TestFilterFunc(t *testing.T) {
actual := k8ciItems.FilterFunc(func(ikc ItemK8CI) bool {
return ikc.CreatedTime > 123902139
})
if len(actual.Data) != 2 {
t.Fatal("expected 2 found, actual: ", len(actual.Data))
}
for _, item := range actual.Data {
if item.CreatedTime < 123902139 {
t.Fatal("expected CreatedTime greater than 123902139, found: ", item.CreatedTime)
}
}
}
func TestSortingByCreatedTime(t *testing.T) {
actual := k8ciItems.SortByCreatedTime(true)
if actual.Data[0].CreatedTime != 123902335 && actual.Data[2].CreatedTime != 123902139 {
t.Fatal("expected inverse sort, found normal")
}
}

45
pkg/cloudapi/k8ci/get.go Normal file
View File

@@ -0,0 +1,45 @@
package k8ci
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/internal/validators"
)
// GetRequest struct to get information about K8CI
type GetRequest struct {
// ID of the K8 catalog item to get
// Required: true
K8CIID uint64 `url:"k8ciId" json:"k8ciId" validate:"required"`
}
// Get gets details of the specified K8 catalog item as a RecordK8CI struct
func (k K8CI) Get(ctx context.Context, req GetRequest) (*RecordK8CI, error) {
res, err := k.GetRaw(ctx, req)
if err != nil {
return nil, err
}
info := RecordK8CI{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}
// GetRaw gets details of the specified K8 catalog item as an array of bytes
func (k K8CI) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/cloudapi/k8ci/get"
res, err := k.client.DecortApiCall(ctx, http.MethodPost, url, req)
return res, err
}

10
pkg/cloudapi/k8ci/ids.go Normal file
View File

@@ -0,0 +1,10 @@
package k8ci
// IDs gets array of K8CIIDs from ListK8CI struct
func (lk ListK8CI) IDs() []uint64 {
res := make([]uint64, 0, len(lk.Data))
for _, k := range lk.Data {
res = append(res, k.ID)
}
return res
}

18
pkg/cloudapi/k8ci/k8ci.go Normal file
View File

@@ -0,0 +1,18 @@
// API to manage K8CI instances
package k8ci
import (
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/interfaces"
)
// Structure for creating request to K8CI
type K8CI struct {
client interfaces.Caller
}
// Builder for K8CI endpoints
func New(client interfaces.Caller) *K8CI {
return &K8CI{
client,
}
}

83
pkg/cloudapi/k8ci/list.go Normal file
View File

@@ -0,0 +1,83 @@
package k8ci
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/internal/validators"
)
// ListRequest struct to get list of information about images
type ListRequest struct {
// Find by ID
// Required: false
ByID uint64 `url:"by_id,omitempty" json:"by_id,omitempty"`
// Find by name
// Required: false
Name string `url:"name,omitempty" json:"name,omitempty"`
// Find by status
// Required: false
Status string `url:"status,omitempty" json:"status,omitempty"`
// Find by worker driver
// Required: false
WorkerDriver string `url:"workerDriver,omitempty" json:"workerDriver,omitempty"`
// Find by master driver
// Required: false
MasterDriver string `url:"masterDriver,omitempty" json:"masterDriver,omitempty"`
// Find by network plugin
// Required: false
NetworkPlugins string `url:"netPlugins,omitempty" json:"netPlugins,omitempty"`
// List disabled items as well
// Required: false
IncludeDisabled bool `url:"includeDisabled,omitempty" json:"includeDisabled,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 k8ci catalog items available to the current user as a ListK8CI struct
func (k K8CI) List(ctx context.Context, req ListRequest) (*ListK8CI, error) {
res, err := k.ListRaw(ctx, req)
if err != nil {
return nil, err
}
list := ListK8CI{}
err = json.Unmarshal(res, &list)
if err != nil {
return nil, err
}
return &list, nil
}
// ListRaw gets list of all k8ci catalog items available to the current user as an array of bytes
func (k K8CI) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {
if err := validators.ValidateRequest(req); err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/cloudapi/k8ci/list"
res, err := k.client.DecortApiCall(ctx, http.MethodPost, url, req)
return res, err
}

View File

@@ -0,0 +1,68 @@
package k8ci
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/internal/validators"
)
// ListDeletedRequest struct to get list information about deleted k8ci items
type ListDeletedRequest struct {
// Find by ID
// Required: false
ByID uint64 `url:"k8cId,omitempty" json:"k8cId,omitempty"`
// Find by name
// Required: false
Name string `url:"name,omitempty" json:"name,omitempty"`
// Find by worker driver
// Required: false
WorkerDriver string `url:"workerDriver,omitempty" json:"workerDriver,omitempty"`
// Find by master driver
// Required: false
MasterDriver string `url:"masterDriver,omitempty" json:"masterDriver,omitempty"`
// Find by network plugin
// Required: false
NetworkPlugins string `url:"netPlugins,omitempty" json:"netPlugins,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"`
}
// ListDeleted gets list all deleted k8ci catalog items available to the current user
func (k K8CI) ListDeleted(ctx context.Context, req ListDeletedRequest) (*ListK8CI, error) {
if err := validators.ValidateRequest(req); err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/cloudapi/k8ci/listDeleted"
res, err := k.client.DecortApiCall(ctx, http.MethodPost, url, req)
if err != nil {
return nil, err
}
list := ListK8CI{}
err = json.Unmarshal(res, &list)
if err != nil {
return nil, err
}
return &list, nil
}

View File

@@ -0,0 +1,53 @@
package k8ci
// Detailed information about K8CI
type ItemK8CI struct {
// Created time
CreatedTime uint64 `json:"createdTime"`
// Description
Description string `json:"desc"`
// ID
ID uint64 `json:"id"`
// LB image ID
LBImageID uint64 `json:"lbImageId"`
// Name
Name string `json:"name"`
// Network plugins
NetworkPlugins []string `json:"networkPlugins"`
// Status
Status string `json:"status"`
// Version
Version string `json:"version"`
}
// List of K8CI
type ListK8CI struct {
Data []ItemK8CI `json:"data"`
EntryCount uint64 `json:"entryCount"`
}
// Main information about K8CI
type RecordK8CI struct {
// Description
Description string `json:"desc"`
// ID
ID uint64 `json:"id"`
// Name
Name string `json:"name"`
// Network plugins
NetworkPlugins []string `json:"networkPlugins"`
// Version
Version string `json:"version"`
}

View File

@@ -0,0 +1,43 @@
package k8ci
import (
"encoding/json"
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/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 ListK8CI) Serialize(params ...string) (serialization.Serialized, error) {
if len(lkc.Data) == 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 ItemK8CI) 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)
}

View File

@@ -0,0 +1,22 @@
package k8ci
import "sort"
// SortByCreatedTime sorts ListK8CI by the CreatedTime field in ascending order.
//
// If inverse param is set to true, the order is reversed.
func (lkc ListK8CI) SortByCreatedTime(inverse bool) ListK8CI {
if len(lkc.Data) < 2 {
return lkc
}
sort.Slice(lkc.Data, func(i, j int) bool {
if inverse {
return lkc.Data[i].CreatedTime > lkc.Data[j].CreatedTime
}
return lkc.Data[i].CreatedTime < lkc.Data[j].CreatedTime
})
return lkc
}