This commit is contained in:
asteam
2024-10-01 11:15:36 +03:00
commit a25a3c2e5c
81 changed files with 5143 additions and 0 deletions

15
pkg/cluster/cluster.go Normal file
View File

@@ -0,0 +1,15 @@
package cluster
import "repository.basistech.ru/BASIS/dynamix-standart-go-sdk/interfaces"
// Structure for creating request to cluster
type Cluster struct {
client interfaces.Caller
}
// Builder for vms endpoints
func New(client interfaces.Caller) *Cluster {
return &Cluster{
client,
}
}

42
pkg/cluster/list.go Normal file
View File

@@ -0,0 +1,42 @@
package cluster
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/internal/constants"
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/internal/validators"
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/pkg/cluster/models"
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/pkg/cluster/requests"
)
// List gets a list of all clusters
func (c Cluster) List(ctx context.Context, req requests.ListClusterRequest) (*models.ListClusterResponse, error) {
res, err := c.ListRaw(ctx, req)
if err != nil {
return nil, err
}
list := models.ListClusterResponse{}
err = json.Unmarshal(res, &list)
if err != nil {
return nil, err
}
return &list, nil
}
// ListRaw gets a list of all clusters as an array of bytes
func (c Cluster) ListRaw(ctx context.Context, req requests.ListClusterRequest) ([]byte, error) {
if err := validators.ValidateRequest(req); err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := constants.APIv0 + "/cluster"
res, err := c.client.ApiCall(ctx, http.MethodGet, url, req)
return res, err
}

View File

@@ -0,0 +1,43 @@
package cluster
import (
"context"
"encoding/json"
"fmt"
"net/http"
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/internal/constants"
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/internal/validators"
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/pkg/cluster/models"
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/pkg/cluster/requests"
)
// ListVNetworks Returns a list of v_networks for a specific cluster
func (c Cluster) ListVNetworks(ctx context.Context, req requests.ListVNetworksRequest) (*models.ListVNetwork, error) {
res, err := c.ListVNetworksRaw(ctx, req)
if err != nil {
return nil, err
}
list := models.ListVNetwork{}
err = json.Unmarshal(res, &list)
if err != nil {
return nil, err
}
return &list, nil
}
// ListVNetworksRaw gets a list of all v_network the user has access to as an array of bytes
func (c Cluster) ListVNetworksRaw(ctx context.Context, req requests.ListVNetworksRequest) ([]byte, error) {
if err := validators.ValidateRequest(req); err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := constants.APIv1 + "/cluster/" + fmt.Sprint(req.ClusterID) + "/v_network"
res, err := c.client.ApiCall(ctx, http.MethodGet, url, req)
return res, err
}

View File

@@ -0,0 +1,85 @@
package models
import (
"encoding/json"
"time"
)
// ListClusterResponse represents the response for a list of clusters.
type ListClusterResponse struct {
List ListCluster `json:"cluster_list"`
RequestID string `json:"request_id"`
}
type ListCluster struct {
Total int `json:"total"`
Page int `json:"page"`
PerPage int `json:"per_page"`
IsApproximateTotal bool `json:"is_approximate_total"`
Items []ItemCluster `json:"items"`
HasMore bool `json:"has_more"`
}
// ItemCluster represents the details of a specific cluster.
type ItemCluster struct {
FenixClusterStatus string `json:"fenix_cluster_status"`
ClusterID int `json:"cluster_id"`
MemoryOvercommit float64 `json:"memory_overcommit"`
CPUOvercommit float64 `json:"cpu_overcommit"`
Created time.Time `json:"created"`
StorageName string `json:"storage_name"`
StorageType string `json:"storage_type"`
FenixBroadcastIP string `json:"fenix_broadcast_ip"`
Modified time.Time `json:"modified"`
FenixPingIP string `json:"fenix_ping_ip"`
Deleted time.Time `json:"deleted"`
DrsMode string `json:"drs_mode"`
Name string `json:"name"`
FenixClusterID int `json:"fenix_cluster_id"`
VstorageClusterID int `json:"vstorage_cluster_id"`
FenixBalancingStrategy string `json:"fenix_balancing_strategy"`
FenixEnabled bool `json:"fenix_enabled"`
NodeCount int `json:"node_count"`
VMTotalRAM int `json:"vm_total_ram"`
VMCount int `json:"vm_count"`
ResourceMetrics interface{} `json:"resource_metrics"`
}
type ResourceMetrics struct {
CPU int `json:"cpu"`
MemoryAvailable float64 `json:"memory_available"`
AvgUtilization float64 `json:"avg_utilization"`
MaxMemoryAvailable float64 `json:"max_memory_available"`
MaxCPUAvailable float64 `json:"max_cpu_available"`
MigrationsCount int `json:"migrations_count"`
CPUAvailable float64 `json:"cpu_available"`
Memory int `json:"memory"`
StandardDeviation float64 `json:"standard_deviation"`
MaxStandardDeviation float64 `json:"max_standard_deviation"`
}
func (ic *ItemCluster) UnmarshalJSON(data []byte) error {
type Alias ItemCluster
temp := &struct {
ResourceMetrics json.RawMessage `json:"resource_metrics,omitempty"`
*Alias
}{
Alias: (*Alias)(ic),
}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
if len(temp.ResourceMetrics) > 0 {
var metrics ResourceMetrics
if err := json.Unmarshal(temp.ResourceMetrics, &metrics); err != nil {
return err
}
ic.ResourceMetrics = metrics
} else {
ic.ResourceMetrics = nil
}
return nil
}

View File

@@ -0,0 +1,57 @@
package models
import "time"
type ListVNetwork struct {
Total int `json:"total"`
Page int `json:"page"`
PerPage int `json:"per_page"`
IsApproximateTotal bool `json:"is_approximate_total"`
Items []VNetworkItem `json:"items"`
HasMore bool `json:"has_more"`
RequestID string `json:"request_id"`
}
type VNetworkItem struct {
AliasID int `json:"alias_id"`
NetworkType string `json:"network_type"`
ClusterID int `json:"cluster_id"`
VirtualNetworkID int `json:"virtual_network_id"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
ExternalUUID int `json:"external_uuid"`
Deleted time.Time `json:"deleted"`
Name string `json:"name"`
BridgeInterfaceID int `json:"bridge_interface_id"`
VlanID int `json:"vlan_id"`
Description string `json:"description"`
VlanInterfaceID int `json:"vlan_interface_id"`
NetworkInterfaceID int `json:"network_interface_id"`
ExternalResourceID int `json:"external_resource_id"`
NodeID int `json:"node_id"`
NodeCount int `json:"node_count"`
VMCount int `json:"vm_count"`
RunningVMCount int `json:"running_vm_count"`
Alias string `json:"alias"`
Node Node `json:"node"`
}
type Node struct {
MaintenanceStatus string `json:"maintenance_status"`
ClusterID int `json:"cluster_id"`
Created time.Time `json:"created"`
VstorageStatus string `json:"vstorage_status"`
IPAddress string `json:"ip_address"`
Status string `json:"status"`
Modified time.Time `json:"modified"`
Deleted time.Time `json:"deleted"`
ServerUUID string `json:"server_uuid"`
Name string `json:"name"`
VstorageHostID int `json:"vstorage_host_id"`
Compute bool `json:"compute"`
VstorageClusterID int `json:"vstorage_cluster_id"`
SdkStatus string `json:"sdk_status"`
Description string `json:"description"`
StorageStatus string `json:"storage_status"`
NodeID int `json:"node_id"`
}

View File

@@ -0,0 +1,68 @@
package requests
import "time"
// ListClusterRequest struct to get a list of clusters
type ListClusterRequest struct {
// Cluster name. Searching by partial matching. Ignored if NameExact is provided.
// Required: false
Name string `url:"name,omitempty" json:"name,omitempty"`
// Exact cluster name.
// Required: false
NameExact string `url:"name_exact,omitempty" json:"name_exact,omitempty"`
// VStorage Cluster ID.
// Required: false
VStorageClusterID int `url:"vstorage_cluster_id,omitempty" json:"vstorage_cluster_id,omitempty"`
// External Storage ID.
// Required: false
ExternalStorageID int `url:"external_storage_id,omitempty" json:"external_storage_id,omitempty"`
// List of cluster IDs to exclude from listing.
// Required: false
ExcludeIDs []int `url:"exclude_ids,omitempty" json:"exclude_ids,omitempty"`
// Template for filtering by any text field.
// Required: false
FilterText string `url:"filter_text,omitempty" json:"filter_text,omitempty"`
// Filter clusters created before this date.
// Required: false
CreatedBefore time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
// Filter clusters created after this date.
// Required: false
CreatedAfter time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
// List columns which will be used by FilterText.
// Required: false
FilterColumns string `url:"filter_columns,omitempty" json:"filter_columns,omitempty"`
// Field name or list of field names used for sorting. Ascending sort is default. For descending sort, use "-" before name.
// Default sorting field: "name".
// Required: false
Sort []string `url:"sort,omitempty" json:"sort,omitempty"`
// Visibility options: "visible" - Only active clusters, "deleted" - Only removed clusters, "all" - All clusters.
// Required: false
Visibility string `url:"visibility,omitempty" json:"visibility,omitempty" validate:"omitempty,visibility"`
// Add resource metrics.
// Required: false
WithResourceMetrics bool `url:"with_resource_metrics,omitempty" json:"with_resource_metrics,omitempty"`
// Include external resource's clusters.
// Default: false
// Required: false
IncludeExternal bool `url:"include_external,omitempty" json:"include_external,omitempty"`
// Number of returning page.
// Required: false
Page int `url:"page,omitempty" json:"page,omitempty"`
// Number of items on page.
// Required: false
Limit int `url:"limit,omitempty" json:"limit,omitempty"`
}

View File

@@ -0,0 +1,57 @@
package requests
import "time"
type ListVNetworksRequest struct {
// Cluster id where virtual net is attached
// Required: true
ClusterID int `url:"cluster_id" json:"cluster_id" validate:"required"`
// ID of alias
// Required: false
AliasID int `url:"alias_id,omitempty" json:"alias_id,omitempty"`
// Name of the vnet
// Required: false
Name string `url:"name,omitempty" json:"name,omitempty"`
// Virtual networks for filtering by and text field
// Required: false
FilterText string `url:"filter_text,omitempty" json:"filter_text,omitempty"`
// List columns which will be used by filter_text
// Required: false
FilterColumns string `url:"filter_columns,omitempty" json:"filter_columns,omitempty"`
// Filter entities created before this date.
// Required: false
CreatedBefore time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
// Filter entities created after this date.
// Required: false
CreatedAfter time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
// List of columns for sorting.
// Required: false
Sort []string `url:"sort,omitempty" json:"sort,omitempty"`
// Visibility status of the interface (visible, all, deleted).
// Required: false
Visibility string `url:"visibility,omitempty" json:"visibility,omitempty" validate:"omitempty,visibility"`
// NetworkType type of virtual network (HOST_ONLY, CLUSTER_NETWORK, BRIDGED_ETHERNET).
// Required: false
NetworkType string `url:"network_type,omitempty" json:"network_type,omitempty" validate:"omitempty,network_type"`
// NetworkType type of virtual network (HOST_ONLY, CLUSTER_NETWORK, BRIDGED_ETHERNET).
// Required: false
FilterByUsePermission bool `url:"filter_by_use_permission,omitempty" json:"filter_by_use_permission,omitempty"`
// Number of the page to return.
// Required: false
Page int `url:"page,omitempty" json:"page,omitempty"`
// Number of items to return per page.
// Required: false
Limit int `url:"limit,omitempty" json:"limit,omitempty"`
}