This commit is contained in:
asteam
2025-11-14 17:38:59 +03:00
parent 562b6019d0
commit 0bf073da93
149 changed files with 11080 additions and 38 deletions

View File

@@ -0,0 +1,112 @@
package logicalports
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/constants"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// CreateRequest struct to create logical port
type CreateRequest struct {
// ID of the logical port
// Required: true
LogicalPortID string `url:"logical_port_id" json:"logical_port_id" validate:"required"`
// ID of the version
// Required: true
VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`
// ID of the access group
// Required: true
AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`
// Description
// Required: true
Description string `url:"description" json:"description" validate:"required"`
// Display name
// Required: true
DisplayName string `url:"display_name" json:"display_name" validate:"required"`
// Enabled. True or False
// Required: true
Enabled interface{} `url:"enabled" json:"enabled" validate:"required,isBool"`
// Is excluded from firewall. True or False
// Required: true
IsExcludedFromFirewall interface{} `url:"is_excluded_from_firewall" json:"is_excluded_from_firewall" validate:"required,isBool"`
// Hypervisor
// Required: true
Hypervisor string `url:"hypervisor" json:"hypervisor" validate:"required"`
// Port security. True or False
// Required: true
PortSecurity interface{} `url:"port_security" json:"port_security" validate:"required,isBool"`
// Segment ID
// Required: true
SegmentID string `url:"segment_id" json:"segment_id" validate:"required"`
// Adapter MAC
// Required: false
AdapterMAC string `url:"adapter_mac,omitempty" json:"adapter_mac,omitempty"`
// Unique identifier
// Required: false
UniqueIdentifier string `url:"unique_identifier,omitempty" json:"unique_identifier,omitempty"`
// Logical port addresses
// Required: false
LogicalPortAddresses []LogicalPortAddress `url:"logical_port_addresses,omitempty" json:"logical_port_addresses,omitempty" validate:"dive"`
}
// LogicalPortAddressRequest struct representing logical port address
type LogicalPortAddressRequest struct {
// IP address
// Required: true
IP string `url:"ip" json:"ip" validate:"required"`
// IP type
// Required: true
IPType string `url:"ip_type" json:"ip_type" validate:"required,oneof=IPv4 IPv6"`
// Is primary. True or False
// Required: true
IsPrimary interface{} `url:"is_primary" json:"is_primary" validate:"required,isBool"`
// MAC address
// Required: false
MAC string `url:"mac,omitempty" json:"mac,omitempty"`
// Is discovered. True or False
// Required: false
IsDiscovered interface{} `url:"is_discovered,omitempty" json:"is_discovered,omitempty" validate:"omitempty,isBool"`
}
// Create creates a logical port
func (l LogicalPorts) Create(ctx context.Context, req CreateRequest) (*LogicalPort, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/logical_port/create"
res, err := l.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, req)
if err != nil {
return nil, err
}
info := LogicalPort{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}

View File

@@ -0,0 +1,41 @@
package logicalports
import (
"context"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/constants"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// DeleteRequest struct to delete logical port
type DeleteRequest struct {
// Port ID
// Required: true
ID string `url:"logical_port_id" json:"logical_port_id" validate:"required"`
// Version
// Required: true
Version uint64 `url:"version_id" json:"version_id" validate:"required"`
// Force delete. True or false
// Required: false
Force interface{} `url:"force,omitempty" json:"force,omitempty" validate:"omitempty,isBool"`
}
// Delete a logical port
func (i LogicalPorts) Delete(ctx context.Context, req DeleteRequest) (bool, error) {
err := validators.ValidateRequest(req)
if err != nil {
return false, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/logical_port/delete"
_, err = i.client.DecortApiCallCtype(ctx, http.MethodDelete, url, constants.MIMEJSON, req)
if err != nil {
return false, err
}
return true, nil
}

View File

@@ -0,0 +1,42 @@
package logicalports
// FilterByID returns LogicalPortsList with specified ID.
func (agl LogicalPortsList) FilterByID(id string) LogicalPortsList {
predicate := func(ia LogicalPort) bool {
return ia.ID == id
}
return agl.FilterFunc(predicate)
}
// FilterByName returns LogicalPortsList with specified Name.
func (agl LogicalPortsList) FilterByName(name string) LogicalPortsList {
predicate := func(ia LogicalPort) bool {
return ia.DisplayName == name
}
return agl.FilterFunc(predicate)
}
// FilterFunc allows filtering LogicalPortsList based on a user-specified predicate.
func (agl LogicalPortsList) FilterFunc(predicate func(LogicalPort) bool) LogicalPortsList {
var result LogicalPortsList
for _, acc := range agl.Ports {
if predicate(acc) {
result.Ports = append(result.Ports, acc)
}
}
return result
}
// FindOne returns first element.
// If none was found, returns an empty struct.
func (agl LogicalPortsList) FindOne() LogicalPort {
if len(agl.Ports) == 0 {
return LogicalPort{}
}
return agl.Ports[0]
}

View File

@@ -0,0 +1,47 @@
package logicalports
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// GetRequest struct to get information about logical port
type GetRequest struct {
// ID a logical port
// Required: true
ID string `url:"logical_port_id" json:"logical_port_id" validate:"required"`
}
// Get gets logical port details as a LogicalPort struct
func (a LogicalPorts) Get(ctx context.Context, req GetRequest) (*LogicalPort, error) {
res, err := a.GetRaw(ctx, req)
if err != nil {
return nil, err
}
info := LogicalPort{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}
// GetRaw gets logical port details as an array of bytes
func (a LogicalPorts) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/logical_port/get"
res, err := a.client.DecortApiCall(ctx, http.MethodGet, url, req)
return res, err
}

View File

@@ -0,0 +1,47 @@
package logicalports
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// GetByUniqueIdentifierRequest struct to get information about logical port
type GetByUniqueIdentifierRequest struct {
// ID a logical port
// Required: true
ID string `url:"unique_identifier" json:"unique_identifier" validate:"required"`
}
// GetByUniqueIdentifier gets logical port details as a LogicalPort struct
func (a LogicalPorts) GetByUniqueIdentifier(ctx context.Context, req GetByUniqueIdentifierRequest) (*LogicalPort, error) {
res, err := a.GetByUniqueIdentifierRaw(ctx, req)
if err != nil {
return nil, err
}
info := LogicalPort{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}
// GetByUniqueIdentifier gets logical port details as an array of bytes
func (a LogicalPorts) GetByUniqueIdentifierRaw(ctx context.Context, req GetByUniqueIdentifierRequest) ([]byte, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/logical_port/get_by_unique_identifier"
res, err := a.client.DecortApiCall(ctx, http.MethodGet, url, req)
return res, err
}

View File

@@ -0,0 +1,10 @@
package logicalports
// IDs gets array of IDs from LogicalPortList struct
func (pl LogicalPortsList) IDs() []string {
res := make([]string, 0, len(pl.Ports))
for _, c := range pl.Ports {
res = append(res, c.ID)
}
return res
}

View File

@@ -0,0 +1,120 @@
package logicalports
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// ListRequest struct to get a list of logical ports
type ListRequest struct {
// Find by access group ID
// Required: false
AccessGroupID string `url:"access_group_id,omitempty" json:"access_group_id,omitempty"`
// Find by segment ID
// Required: false
SegmentID string `url:"segment_id,omitempty" json:"segment_id,omitempty"`
// Find by segment display name
// Required: false
SegmentDisplayName string `url:"segment_display_name,omitempty" json:"segment_display_name,omitempty"`
// Find by external network ID
// Required: false
ExternalNetworkID string `url:"external_network_id,omitempty" json:"external_network_id,omitempty"`
// Find by unique identifier
// Required: false
UniqueIdentifier string `url:"unique_identifier,omitempty" json:"unique_identifier,omitempty"`
// Find by display name
// Required: false
DisplayName string `url:"display_name,omitempty" json:"display_name,omitempty"`
// Find by adapter MAC address
// Required: false
AdapterMAC string `url:"adapter_mac,omitempty" json:"adapter_mac,omitempty"`
// Find by hypervisor
// Required: false
Hypervisor string `url:"hypervisor,omitempty" json:"hypervisor,omitempty"`
// Find by hypervisor display name
// Required: false
HypervisorDisplayName string `url:"hypervisor_display_name,omitempty" json:"hypervisor_display_name,omitempty"`
// Find by live migration target hypervisor
// Required: false
LiveMigrationTargetHv string `url:"live_migration_target_hv,omitempty" json:"live_migration_target_hv,omitempty"`
// Find by port security status, true or false
// Required: false
PortSecurity interface{} `url:"port_security,omitempty" json:"port_security,omitempty" validate:"omitempty,isBool"`
// Find by address detection status, true or false
// Required: false
AddressDetection interface{} `url:"address_detection,omitempty" json:"address_detection,omitempty" validate:"omitempty,isBool"`
// Find by enabled status, true or false
// Required: false
Enabled interface{} `url:"enabled,omitempty" json:"enabled,omitempty" validate:"omitempty,isBool"`
// Creation date lower bound (inclusive)
// Required: false
CreatedFrom string `url:"created_from,omitempty" json:"created_from,omitempty"`
// Creation date upper bound (inclusive)
// Required: false
CreatedTo string `url:"created_to,omitempty" json:"created_to,omitempty"`
// Page number for pagination
// Required: false
Page uint64 `url:"page,omitempty" json:"page,omitempty"`
// Number of results per page
// Required: false
PerPage uint64 `url:"per_page,omitempty" json:"per_page,omitempty"`
// Field to sort by (display_name, created_at, updated_at, deleted_at, segment_id, hypervisor, port_security, segment_display_name, primary_address, hypervisor_display_name)
// Required: false
SortBy string `url:"sort_by,omitempty" json:"sort_by,omitempty"`
// Sort order (asc/desc)
// Required: false
SortOrder string `url:"sort_order,omitempty" json:"sort_order,omitempty"`
}
// List of logical portts
func (i LogicalPorts) List(ctx context.Context, req ListRequest) (*LogicalPortsList, error) {
res, err := i.ListRaw(ctx, req)
if err != nil {
return nil, err
}
groups := []LogicalPort{}
err = json.Unmarshal(res, &groups)
if err != nil {
return nil, err
}
result := LogicalPortsList{Ports: groups}
return &result, nil
}
// ListRaw gets a list of all logical portts as an array of bytes
func (a LogicalPorts) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {
if err := validators.ValidateRequest(req); err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/logical_port/list"
res, err := a.client.DecortApiCall(ctx, http.MethodGet, url, req)
return res, err
}

View File

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

View File

@@ -0,0 +1,44 @@
package logicalports
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/constants"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// MigrateCancelRequest struct to cancel migrate
type MigrateCancelRequest struct {
// ID of the logical port
// Required: true
LogicalPortID string `url:"logical_port_id" json:"logical_port_id" validate:"required"`
// ID of the version
// Required: true
VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`
}
func (i LogicalPorts) CancelMigrate(ctx context.Context, req MigrateCancelRequest) (*MigrationStatus, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/logical_port/migration_cancel"
res, err := i.client.DecortApiCallCtype(ctx, http.MethodDelete, url, constants.MIMEJSON, req)
if err != nil {
return nil, err
}
info := MigrationStatus{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}

View File

@@ -0,0 +1,48 @@
package logicalports
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/constants"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// MigrateStartRequest struct to start migrate
type MigrateStartRequest struct {
// ID of the logical port
// Required: true
LogicalPortID string `url:"logical_port_id" json:"logical_port_id" validate:"required"`
// ID of the version
// Required: true
VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`
// Hypervisor
// Required: true
TargetHypervisor string `url:"target_hypervisor" json:"target_hypervisor" validate:"required"`
}
func (i LogicalPorts) StartMigrate(ctx context.Context, req MigrateStartRequest) (*MigrationStatus, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/logical_port/migration_start"
res, err := i.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, req)
if err != nil {
return nil, err
}
info := MigrationStatus{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}

View File

@@ -0,0 +1,158 @@
package logicalports
// LogicalPortsList represents a list of logical ports
type LogicalPortsList struct {
Ports []LogicalPort `json:"ports"`
}
// Main information about logical port
type LogicalPort struct {
// ID
ID string `json:"id"`
// Access group ID
AccessGroupID string `json:"access_group_id"`
// Access group name
AccessGroupName string `json:"access_group_name"`
// Adapter MAC
AdapterMAC string `json:"adapter_mac"`
// Address detection
AddressDetection bool `json:"address_detection"`
// Description
Description string `json:"description"`
// Created time
CreatedAt string `json:"created_at"`
// Display name
DisplayName string `json:"display_name"`
// Enabled
Enabled bool `json:"enabled"`
// External network ID
ExternalNetworkID string `json:"external_network_id"`
// Hypervisor
Hypervisor string `json:"hypervisor"`
// Hypervisor display name
HypervisorDisplayName string `json:"hypervisor_display_name"`
// Live migration target hypervisor
LiveMigrationTargetHV string `json:"live_migration_target_hv"`
// Status information
Status Status `json:"status"`
// Bindings information
Bindings Bindings `json:"bindings"`
// Unique identifier
UniqueIdentifier string `json:"unique_identifier"`
// Updated time
UpdatedAt string `json:"updated_at"`
// Version ID
VersionID uint64 `json:"version_id"`
}
// Status information
type Status struct {
// Common status
Common string `json:"common"`
// Hypervisors status
Hypervisors []HypervisorStatus `json:"hypervisors"`
}
// HypervisorStatus information
type HypervisorStatus struct {
// Status
Status string `json:"status"`
// Name
Name string `json:"name"`
// Display name
DisplayName string `json:"display_name"`
// Hypervisor status
HypervisorStatus string `json:"hypervisor_status"`
// Synced time
SyncedAt string `json:"synced_at"`
}
// Bindings information
type Bindings struct {
// ID
ID string `json:"id"`
// Segment display name
SegmentDisplayName string `json:"segment_display_name"`
// Segment ID
SegmentID string `json:"segment_id"`
// Port security
PortSecurity bool `json:"port_security"`
// Address detection
AddressDetection bool `json:"address_detection"`
// Is excluded from firewall
IsExcludedFromFirewall bool `json:"is_excluded_from_firewall"`
// Version ID
VersionID uint64 `json:"version_id"`
// Created time
CreatedAt string `json:"created_at"`
// Updated time
UpdatedAt string `json:"updated_at"`
// Logical port addresses
LogicalPortAddresses []LogicalPortAddress `json:"logical_port_addresses"`
}
// LogicalPortAddress information
type LogicalPortAddress struct {
// IP address
IP string `json:"ip"`
// IP type
IPType string `json:"ip_type"`
// Is discovered
IsDiscovered bool `json:"is_discovered"`
// Is primary
IsPrimary bool `json:"is_primary"`
// MAC address
MAC string `json:"mac"`
// ID
ID string `json:"id"`
// Logical port ID
LogicalPortID string `json:"logical_port_id"`
// Assigned time
AssignedAt string `json:"assigned_at"`
}
type MigrationStatus struct {
// ID
ID string `json:"id"`
// Version ID
VersionID uint64 `json:"version_id"`
}

View File

@@ -0,0 +1,43 @@
package logicalports
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 (la LogicalPortsList) Serialize(params ...string) (serialization.Serialized, error) {
if len(la.Ports) == 0 {
return []byte{}, nil
}
if len(params) > 1 {
prefix := params[0]
indent := params[1]
return json.MarshalIndent(la, prefix, indent)
}
return json.Marshal(la)
}
// 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 (ia LogicalPort) Serialize(params ...string) (serialization.Serialized, error) {
if len(params) > 1 {
prefix := params[0]
indent := params[1]
return json.MarshalIndent(ia, prefix, indent)
}
return json.Marshal(ia)
}

View File

@@ -0,0 +1,135 @@
package logicalports
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/constants"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// UpdateRequest struct to update logical port
type UpdateRequest struct {
// ID of the logical port
// Required: true
LogicalPortID string `url:"logical_port_id" json:"logical_port_id" validate:"required"`
// ID of the version
// Required: true
VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`
// Adapter MAC
// Required: true
AdapterMAC string `url:"adapter_mac" json:"adapter_mac" validate:"required"`
// Description
// Required: true
Description string `url:"description" json:"description" validate:"required"`
// Display name
// Required: true
DisplayName string `url:"display_name" json:"display_name" validate:"required"`
// Enabled. True or False
// Required: true
Enabled interface{} `url:"enabled" json:"enabled" validate:"required,isBool"`
// Remove addresses
// Required: false
RemoveAddresses []string `url:"remove_addresses,omitempty" json:"remove_addresses,omitempty"`
// Hypervisor
// Required: true
Hypervisor string `url:"hypervisor" json:"hypervisor" validate:"required"`
// Port security. True or False
// Required: true
PortSecurity interface{} `url:"port_security" json:"port_security" validate:"required,isBool"`
// Is excluded from firewall. True or False
// Required: true
IsExcludedFromFirewall interface{} `url:"is_excluded_from_firewall" json:"is_excluded_from_firewall" validate:"required,isBool"`
// Segment ID
// Required: true
SegmentID string `url:"segment_id" json:"segment_id" validate:"required"`
// Update addresses
// Required: false
UpdateAddresses []UpdateAddress `url:"update_addresses,omitempty" json:"update_addresses,omitempty" validate:"dive"`
// Add addresses
// Required: false
AddAddresses []AddAddress `url:"add_addresses,omitempty" json:"add_addresses,omitempty" validate:"dive"`
}
// UpdateAddress struct representing update address
type UpdateAddress struct {
// IP address
// Required: true
IP string `url:"ip" json:"ip" validate:"required"`
// IP type
// Required: true
IPType string `url:"ip_type" json:"ip_type" validate:"required,oneof=IPv4 IPv6"`
// Is discovered. True or False
// Required: false
IsDiscovered interface{} `url:"is_discovered,omitempty" json:"is_discovered,omitempty" validate:"omitempty,isBool"`
// Is primary. True or False
// Required: true
IsPrimary interface{} `url:"is_primary" json:"is_primary" validate:"required,isBool"`
// MAC address
// Required: false
MAC string `url:"mac,omitempty" json:"mac,omitempty"`
}
// AddAddress struct representing add address
type AddAddress struct {
// IP address
// Required: true
IP string `url:"ip" json:"ip" validate:"required"`
// IP type
// Required: true
IPType string `url:"ip_type" json:"ip_type" validate:"required,oneof=IPv4 IPv6"`
// Is discovered. True or False
// Required: false
IsDiscovered interface{} `url:"is_discovered,omitempty" json:"is_discovered,omitempty" validate:"omitempty,isBool"`
// Is primary. True or False
// Required: true
IsPrimary interface{} `url:"is_primary" json:"is_primary" validate:"required,isBool"`
// MAC address
// Required: false
MAC string `url:"mac,omitempty" json:"mac,omitempty"`
}
// Update updates a logical port
func (i LogicalPorts) Update(ctx context.Context, req UpdateRequest) (*LogicalPort, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/logical_port/update"
res, err := i.client.DecortApiCallCtype(ctx, http.MethodPut, url, constants.MIMEJSON, req)
if err != nil {
return nil, err
}
info := LogicalPort{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}