This commit is contained in:
dayterr
2026-04-10 16:38:00 +03:00
parent 30e464e4d2
commit 5cdae8520f
16 changed files with 458 additions and 62 deletions

View File

@@ -0,0 +1,30 @@
package hypervisors
import (
"context"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/constants"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// ConnectNodeRequest to connect a node
type ConnectNodeRequest struct {
// Node to connect
// Required: true
NodeID uint64 `url:"node_id" json:"node_id" validate:"required"`
}
func (hv Hypervisors) ConnectNode(ctx context.Context, req ConnectNodeRequest) (string, error) {
err := validators.ValidateRequest(req)
if err != nil {
return "", validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/hypervisor/connect_node"
result, err := hv.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, req)
if err != nil {
return "", err
}
return string(result), nil
}

View File

@@ -0,0 +1,43 @@
package hypervisors
import (
"context"
"net/http"
"strconv"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/constants"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// DeleteRequest to delete a hypervisor
type DeleteRequest struct {
// Name of a hypervisor
// Required: true
Name string `url:"name" json:"name" validate:"required"`
}
// Delete a hypervisor
func (hv Hypervisors) Delete(ctx context.Context, req DeleteRequest) (bool, error) {
err := validators.ValidateRequest(req)
if err != nil {
return false, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/hypervisor/delete"
res, err := hv.client.DecortApiCallCtype(ctx, http.MethodDelete, url, constants.MIMEJSON, req)
if err != nil {
return false, err
}
if string(res) == "" {
return true, nil
}
result, err := strconv.ParseBool(string(res))
if err != nil {
return false, err
}
return result, nil
}

View File

@@ -0,0 +1,50 @@
package hypervisors
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// GetRequest struct to get information about hypervisor
type GetRequest struct {
// Name
// Required: true
Name string `url:"name" json:"name" validate:"required"`
// Port info (available options: detailed, general)
// Required: false
PortInfo string `url:"port_info,omitempty" json:"port_info,omitempty"`
}
// Get gets current configuration of a hypervisor as a RecordHypervisor
func (hv Hypervisors) Get(ctx context.Context, req GetRequest) (*RecordHypervisor, error) {
res, err := hv.GetRaw(ctx, req)
if err != nil {
return nil, err
}
info := RecordHypervisor{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}
// GetRaw gets information about a hypervisor as an array of bytes
func (hv Hypervisors) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/hypervisor/get"
res, err := hv.client.DecortApiCall(ctx, http.MethodGet, url, req)
return res, err
}

View File

@@ -0,0 +1,17 @@
package hypervisors
import (
"repository.basistech.ru/BASIS/decort-golang-sdk/interfaces"
)
// Structure for creating request to hypervisors
type Hypervisors struct {
client interfaces.Caller
}
// Builder for hypervisors endpoints
func New(client interfaces.Caller) *Hypervisors {
return &Hypervisors{
client,
}
}

View File

@@ -0,0 +1,90 @@
package hypervisors
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// ListRequest struct to get a list of hypervisors
type ListRequest struct {
// Page
// Required: false
Page uint64 `url:"page,omitempty" json:"page,omitempty"`
// Per page
// Required: false
PerPage uint64 `url:"per_page,omitempty" json:"per_page,omitempty"`
// Sort by (available options: name, hostname, last_sync, display_name, ip, created_at, updated_at)
// Required: false
SortBy string `url:"sort_by,omitempty" json:"sort_by,omitempty"`
// Sort order (available options: asc, desc)
// Required: false
SortOrder string `url:"sort_order,omitempty" json:"sort_order,omitempty"`
// Port info (available options: detailed, general)
// Required: false
PortInfo string `url:"port_info,omitempty" json:"port_info,omitempty"`
// Hostname
// Required: false
Hostname string `url:"hostname,omitempty" json:"hostname,omitempty"`
// Display name
// Required: false
DisplayName string `url:"display_name,omitempty" json:"display_name,omitempty"`
// IP
// Required: false
IP string `url:"ip,omitempty" json:"ip,omitempty"`
// Created from
// Required: false
CreatedFrom string `url:"created_from,omitempty" json:"created_from,omitempty"`
// Created to
// Required: false
CreatedTo string `url:"created_to,omitempty" json:"created_to,omitempty"`
// Updated from
// Required: false
UpdatedFrom string `url:"updated_from,omitempty" json:"updated_from,omitempty"`
// Updated to
// Required: false
UpdatedTo string `url:"updated_to,omitempty" json:"updated_to,omitempty"`
}
// List of hypervisors
func (hv Hypervisors) List(ctx context.Context, req ListRequest) (HypervisorsList, error) {
res, err := hv.ListRaw(ctx, req)
if err != nil {
return nil, err
}
hvs := HypervisorsList{}
err = json.Unmarshal(res, &hvs)
if err != nil {
return nil, err
}
return hvs, nil
}
// ListRaw gets a list of all hypervisors as an array of bytes
func (hv Hypervisors) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {
if err := validators.ValidateRequest(req); err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/hypervisor/list"
res, err := hv.client.DecortApiCall(ctx, http.MethodGet, url, req)
return res, err
}

View File

@@ -0,0 +1,31 @@
package hypervisors
// Main information about hypervisor
type RecordHypervisor struct {
// Created at
CreatedAt string `json:"created_at"`
// Display name
DisplayName string `json:"display_name"`
// Hostname
Hostname string `json:"hostname"`
// IP
IP string `json:"ip"`
// Synced at
SyncedAt string `json:"synced_at"`
// Name
Name string `json:"name"`
// Ports
Ports []string `json:"ports"`
// Status
Status string `json:"status"`
}
// List of hypervisors
type HypervisorsList []RecordHypervisor

View File

@@ -0,0 +1,50 @@
package hypervisors
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// UpdateDisplayNameRequest struct to update display name for a hypervisor
type UpdateDisplayNameRequest struct {
// Current name of the hypervisor
// Required: true
Name string `url:"name" json:"name" validate:"required"`
// New display name to set
// Required: true
DisplayName string `url:"display_name" json:"display_name" validate:"required"`
}
// UpdateDisplayName updates display name for a hypervisor
func (hv Hypervisors) UpdateDisplayName(ctx context.Context, req UpdateDisplayNameRequest) (*RecordHypervisor, error) {
res, err := hv.UpdateDisplayNameRaw(ctx, req)
if err != nil {
return nil, err
}
info := RecordHypervisor{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}
// UpdateDisplayNameRaw update display name for a hypervisor and get its information as an array of bytes
func (hv Hypervisors) UpdateDisplayNameRaw(ctx context.Context, req UpdateDisplayNameRequest) ([]byte, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/hypervisor/update_display_name"
res, err := hv.client.DecortApiCall(ctx, http.MethodPut, url, req)
return res, err
}