This commit is contained in:
asteam
2025-07-15 17:39:18 +03:00
parent 1f8637400f
commit 7dacf35cd6
163 changed files with 4322 additions and 504 deletions

View File

@@ -0,0 +1,53 @@
package zone
// FilterByID returns ListZones with specified ID.
func (list ListZones) FilterByID(id uint64) ListZones {
predicate := func(izone RecordZone) bool {
return izone.ID == id
}
return list.FilterFunc(predicate)
}
// FilterByName returns ListZones with specified Name.
func (list ListZones) FilterByName(name string) ListZones {
predicate := func(izone RecordZone) bool {
return izone.Name == name
}
return list.FilterFunc(predicate)
}
// FilterByStatus returns ListZones with specified Status.
func (list ListZones) FilterByStatus(status string) ListZones {
predicate := func(izone RecordZone) bool {
return izone.Status == status
}
return list.FilterFunc(predicate)
}
// FilterFunc allows filtering ListZones based on a user-specified predicate.
func (list ListZones) FilterFunc(predicate func(RecordZone) bool) ListZones {
var result ListZones
for _, item := range list.Data {
if predicate(item) {
result.Data = append(result.Data, item)
}
}
result.EntryCount = uint64(len(result.Data))
return result
}
// FindOne returns first found RecordZone
// If none was found, returns an empty struct.
func (list ListZones) FindOne() RecordZone {
if list.EntryCount == 0 {
return RecordZone{}
}
return list.Data[0]
}

View File

@@ -0,0 +1,86 @@
package zone
import "testing"
var zones = ListZones{
Data: []RecordZone{
{
ID: 2,
GUID: 0,
GID: 0,
Name: "System Config",
Description: "",
Deletable: true,
Status: "LOCKED",
CreatedTime: 1640995200, // 2022-01-01
UpdatedTime: 1640995200,
NodeIDs: nil,
},
{
ID: 5,
GUID: 5500,
GID: 6600,
Name: "ssss Nodes",
Description: " infrastructure",
Deletable: true,
Status: "DISABLED",
CreatedTime: 1577836800, // 2020-01-01
UpdatedTime: 1580515200, // 2020-02-01
NodeIDs: []uint64{777, 888, 999},
},
{
ID: 10,
GUID: 5500,
GID: 6600,
Name: "node",
Description: "infrastructure",
Deletable: true,
Status: "DISABLED",
CreatedTime: 1577836800,
UpdatedTime: 1580515200,
NodeIDs: []uint64{777, 888, 999},
},
},
}
func TestFilterByID(t *testing.T) {
actual := zones.FilterByID(10).FindOne()
if actual.ID != 10 {
t.Fatal("expected ID 10, found: ", actual.ID)
}
}
func TestFilterByName(t *testing.T) {
name := "node"
actual := zones.FilterByName(name).FindOne()
if actual.Name != name {
t.Fatal("expected ", name, " found: ", actual.Name)
}
}
func TestFilterByStatus(t *testing.T) {
actual := zones.FilterByStatus("DISABLED")
if len(actual.Data) != 2 {
t.Fatal("expected 2 found, actual: ", len(actual.Data))
}
for _, item := range actual.Data {
if item.Status != "DISABLED" {
t.Fatal("expected Status 'DISABLED', found: ", item.Status)
}
}
}
func TestFilterFunc(t *testing.T) {
actual := zones.FilterFunc(func(ien RecordZone) bool {
return ien.Deletable == true
})
if len(actual.Data) != 3 {
t.Fatal("expected 3 elements, found: ", len(actual.Data))
}
}

46
pkg/cloudapi/zone/get.go Normal file
View File

@@ -0,0 +1,46 @@
package zone
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// GetRequest struct to get detailed information about zone
type GetRequest struct {
// ID of zone
// Required: true
ID uint64 `url:"id" json:"id" validate:"required"`
}
// Get gets detailed information about zone struct
func (e Zone) Get(ctx context.Context, req GetRequest) (*RecordZone, error) {
res, err := e.GetRaw(ctx, req)
if err != nil {
return nil, err
}
info := RecordZone{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}
// GetRaw gets detailed information about zone as an array of bytes
func (e Zone) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/cloudapi/zone/get"
res, err := e.client.DecortApiCall(ctx, http.MethodPost, url, req)
return res, err
}

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

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

84
pkg/cloudapi/zone/list.go Normal file
View File

@@ -0,0 +1,84 @@
package zone
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// ListRequest struct to get list of zones
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 deletable
// Required: false
Deletable bool `url:"deletable,omitempty" json:"deletable,omitempty"`
// Find by node ID
// Required: false
NodeID uint64 `url:"nodeId,omitempty" json:"nodeId,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 zones as a ListZones struct
func (e Zone) List(ctx context.Context, req ListRequest) (*ListZones, error) {
res, err := e.ListRaw(ctx, req)
if err != nil {
return nil, err
}
list := ListZones{}
err = json.Unmarshal(res, &list)
if err != nil {
return nil, err
}
return &list, nil
}
// ListRaw gets list of all available zones as an array of bytes
func (e Zone) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {
if err := validators.ValidateRequest(req); err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/cloudapi/zone/list"
res, err := e.client.DecortApiCall(ctx, http.MethodPost, url, req)
return res, err
}

View File

@@ -0,0 +1 @@
package zone

View File

@@ -0,0 +1,42 @@
package zone
type ListZones struct {
// Entry count
EntryCount uint64 `json:"entryCount"`
// Data
Data []RecordZone `json:"data"`
}
// Detailed information about the zone record
type RecordZone struct {
// ID
ID uint64 `json:"id"`
// GUID
GUID uint64 `json:"guid"`
// GID
GID uint64 `json:"gid"`
// Name
Name string `json:"name"`
// Description
Description string `json:"description"`
// Deletable flag
Deletable bool `json:"deletable"`
// Status
Status string `json:"status"`
// Created timestamp
CreatedTime uint64 `json:"createdTime"`
// Updated timestamp
UpdatedTime uint64 `json:"updatedTime"`
// List of associated Node IDs
NodeIDs []uint64 `json:"nodeIds"`
}

View File

@@ -0,0 +1,43 @@
package zone
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 (list ListZones) Serialize(params ...string) (serialization.Serialized, error) {
if list.EntryCount == 0 {
return []byte{}, nil
}
if len(params) > 1 {
prefix := params[0]
indent := params[1]
return json.MarshalIndent(list, prefix, indent)
}
return json.Marshal(list)
}
// 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 (item RecordZone) Serialize(params ...string) (serialization.Serialized, error) {
if len(params) > 1 {
prefix := params[0]
indent := params[1]
return json.MarshalIndent(item, prefix, indent)
}
return json.Marshal(item)
}

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

@@ -0,0 +1,18 @@
// API Actor for use zones
package zone
import (
"repository.basistech.ru/BASIS/decort-golang-sdk/interfaces"
)
// Structure for creating request to zone
type Zone struct {
client interfaces.Caller
}
// Builder for zone endpoints
func New(client interfaces.Caller) *Zone {
return &Zone{
client,
}
}