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

121
pkg/sdn/segments/create.go Normal file
View File

@@ -0,0 +1,121 @@
package segments
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 for creating segment
type CreateRequest struct {
// Identifier of the parent access group
// Required: false
AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`
// Detailed description of the segment
// Required: true
Description string `url:"description" json:"description" validate:"required"`
// User-friendly name for the segment
// Required: true
DisplayName string `url:"display_name" json:"display_name" validate:"required"`
// Whether the network is enabled
// Required: true
Enabled bool `url:"enabled" json:"enabled"`
// IPv4 subnet in CIDR notation (Either subnet_v4 or subnet_v6 must be specified)
// Required: false
SubnetV4 string `url:"subnet_v4,omitempty" json:"subnet_v4,omitempty"`
// IPv6 subnet in CIDR notation (Either subnet_v4 or subnet_v6 must be specified)
// Required: false
SubnetV6 string `url:"subnet_v6,omitempty" json:"subnet_v6,omitempty"`
// DHCP IPv4
// Required: false
DHCPv4 *DHCPv4ConfigRequest `url:"-" json:"dhcp_v4,omitempty"`
// DHCP IPv6
// Required: false
DHCPv6 *DHCPv6ConfigRequest `url:"-" json:"dhcp_v6,omitempty"`
}
type DHCPv4ConfigRequest struct {
// DNS
// Required: false
DNS []string `url:"dns,omitempty" json:"dns,omitempty"`
// Excluded address ranges
// Required: false
ExcludedAddressRanges []string `url:"excluded_address_ranges,omitempty" json:"excluded_address_ranges,omitempty"`
// Gateway
// Required: true
Gateway string `url:"gateway" json:"gateway" validate:"required"`
// Lease time
// Required: false
LeaseTime uint64 `url:"lease_time,omitempty" json:"lease_time,omitempty"`
// Server IP
// Required: true
ServerIP string `url:"server_ip" json:"server_ip" validate:"required"`
// Server MAC
// Required: false
ServerMAC string `url:"server_mac,omitempty" json:"server_mac,omitempty"`
// Whether the config is enabled
// Required: true
Enabled bool `url:"enabled" json:"enabled"`
}
type DHCPv6ConfigRequest struct {
// Address prefix
// Required: true
AddressPrefix string `url:"address_prefix" json:"address_prefix" validate:"required"`
// DNS
// Required: false
DNS []string `url:"dns,omitempty" json:"dns,omitempty"`
// Lease time
// Required: true
LeaseTime uint64 `url:"lease_time,omitempty" json:"lease_time,omitempty"`
// Server MAC
// Required: true
ServerMAC string `url:"server_mac,omitempty" json:"server_mac,omitempty"`
// Whether the config is enabled
// Required: true
Enabled bool `url:"enabled" json:"enabled"`
}
// Create creates segment
func (s Segments) Create(ctx context.Context, req CreateRequest) (*SegmentResponse, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/segment/create"
res, err := s.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, req)
if err != nil {
return nil, err
}
info := SegmentResponse{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}

View File

@@ -0,0 +1,42 @@
package segments
import (
"context"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/constants"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// DeleteRequest struct for delete segment
type DeleteRequest struct {
// ID of segment
// Required: true
SegmentID string `url:"segment_id" json:"segment_id" validate:"required"`
// ID of version
// Required: true
VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`
// Force delete
// Required: false
Force bool `url:"force,omitempty" json:"force,omitempty"`
}
// Delete delete an segment
func (s Segments) Delete(ctx context.Context, req DeleteRequest) error {
err := validators.ValidateRequest(req)
if err != nil {
return validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/segment/delete"
_, err = s.client.DecortApiCallCtype(ctx, http.MethodDelete, url, constants.MIMEJSON, req)
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,60 @@
package segments
// FilterByID returns ListSegment with specified ID.
func (list ListSegment) FilterByID(id string) ListSegment {
predicate := func(segment SegmentResponse) bool {
return segment.ID == id
}
return list.FilterFunc(predicate)
}
// FilterByName returns ListSegment with specified display name.
func (list ListSegment) FilterByName(name string) ListSegment {
predicate := func(segment SegmentResponse) bool {
return segment.DisplayName == name
}
return list.FilterFunc(predicate)
}
// FilterBySubnetIPv4 returns ListSegment with specified subnet IPv4.
func (list ListSegment) FilterBySubnetIPv4(IPv4 string) ListSegment {
predicate := func(segment SegmentResponse) bool {
return segment.SubnetV4 == IPv4
}
return list.FilterFunc(predicate)
}
// FilterBySubnetIPv6 returns ListSegment with specified subnet IPv4.
func (list ListSegment) FilterBySubnetIPv6(IPv6 string) ListSegment {
predicate := func(segment SegmentResponse) bool {
return segment.SubnetV6 == IPv6
}
return list.FilterFunc(predicate)
}
// FilterFunc allows filtering ListSegment based on a user-specified predicate.
func (list ListSegment) FilterFunc(predicate func(response SegmentResponse) bool) ListSegment {
var result ListSegment
for _, item := range list {
if predicate(item) {
result = append(result, item)
}
}
return result
}
// FindOne returns first element.
// If none was found, returns an empty struct.
func (list ListSegment) FindOne() SegmentResponse {
if len(list) == 0 {
return SegmentResponse{}
}
return list[0]
}

View File

@@ -0,0 +1,153 @@
package segments
import (
"testing"
"time"
)
var testSegmentList = ListSegment{
{
AccessGroupID: "a1b2c3d4-1234-5678-9101-abcdef123456",
AccessGroupName: "default-access-group",
CreatedAt: time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC),
Description: "Test1",
DHCPv4: DHCPv4Config{},
DHCPv6: DHCPv6Config{},
DisplayName: "Test1",
Enabled: true,
ID: "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
LogicalPortsInfo: []EntityInfo{},
RoutersInfo: []EntityInfo{},
Status: Status{},
SubnetV4: "192.168.1.0/24",
SubnetV6: "2001:db8::/64",
UpdatedAt: time.Date(2024, 1, 20, 14, 45, 0, 0, time.UTC),
VersionID: 1,
},
{
AccessGroupID: "b2c3d4e5-2345-6789-0123-bcdefa654321",
AccessGroupName: "restricted-group",
CreatedAt: time.Date(2024, 2, 1, 9, 0, 0, 0, time.UTC),
Description: "Test2",
DHCPv4: DHCPv4Config{},
DHCPv6: DHCPv6Config{},
DisplayName: "Test2",
Enabled: false,
ID: "c3d4e5f6-3456-7890-1234-cdefab098765",
LogicalPortsInfo: []EntityInfo{},
RoutersInfo: []EntityInfo{},
Status: Status{},
SubnetV4: "10.0.0.0/24",
SubnetV6: "2001:db8:1::/64",
UpdatedAt: time.Date(2024, 2, 5, 16, 20, 0, 0, time.UTC),
VersionID: 2,
},
{
AccessGroupID: "d4e5f6a7-4567-8901-2345-defabc123456",
AccessGroupName: "minimal-group",
CreatedAt: time.Date(2024, 3, 10, 12, 0, 0, 0, time.UTC),
Description: "Test3",
DHCPv4: DHCPv4Config{},
DHCPv6: DHCPv6Config{},
DisplayName: "Test3",
Enabled: true,
ID: "e5f6a7b8-5678-9012-3456-efabcd789012",
LogicalPortsInfo: []EntityInfo{},
RoutersInfo: []EntityInfo{},
Status: Status{},
SubnetV4: "192.15.1.0/24",
SubnetV6: "",
UpdatedAt: time.Date(2024, 3, 10, 12, 0, 0, 0, time.UTC),
VersionID: 1,
},
}
func TestFilterByID(t *testing.T) {
actual := testSegmentList.FilterByID("a1b2c3d4-e5f6-7890-abcd-ef1234567890").FindOne()
if actual.ID != "a1b2c3d4-e5f6-7890-abcd-ef1234567890" {
t.Fatal("actual:", actual.ID, "> expected: a1b2c3d4-e5f6-7890-abcd-ef1234567890")
}
}
func TestFilterByName(t *testing.T) {
actual := testSegmentList.FilterByName("Test1").FindOne()
if actual.DisplayName != "Test1" {
t.Fatal("actual:", actual.DisplayName, ">> expected: Test1")
}
}
func TestFilterBySubnetIPv4(t *testing.T) {
actual := testSegmentList.FilterBySubnetIPv4("192.168.1.0/24").FindOne()
if actual.SubnetV4 != "192.168.1.0/24" {
t.Fatal("actual:", actual.SubnetV4, ">> expected: 192.168.1.0/24")
}
}
func TestFilterBySubnetIPv6(t *testing.T) {
actual := testSegmentList.FilterBySubnetIPv6("2001:db8::/64").FindOne()
if actual.SubnetV6 != "2001:db8::/64" {
t.Fatal("actual:", actual.SubnetV6, ">> expected: 2001:db8::/64")
}
}
func TestFilterFunc(t *testing.T) {
actual := testSegmentList.FilterFunc(func(response SegmentResponse) bool {
return response.DisplayName == "Test2"
})
if len(actual) != 1 || actual[0].ID != "c3d4e5f6-3456-7890-1234-cdefab098765" {
t.Fatal("Expected 1 extnet with name 'Test2', found:", len(actual))
}
}
func TestFindOneWithResults(t *testing.T) {
result := testSegmentList.FilterByID("c3d4e5f6-3456-7890-1234-cdefab098765").FindOne()
if result.ID != "c3d4e5f6-3456-7890-1234-cdefab098765" {
t.Fatal("Expected c3d4e5f6-3456-7890-1234-cdefab098765, got:", result.ID)
}
}
func TestFindOneEmpty(t *testing.T) {
emptyList := ListSegment{}
result := emptyList.FindOne()
if result.ID != "" || result.DisplayName != "" {
t.Fatal("Expected empty segment, got:", result)
}
}
func TestFilterByIDNotFound(t *testing.T) {
actual := ListSegment{}.FilterByID("nonexistent")
if len(actual) != 0 {
t.Fatal("Expected 0 extNet, found:", len(actual))
}
}
func TestFilterByNameNotFound(t *testing.T) {
actual := ListSegment{}.FilterByName("Nonexistent")
if len(actual) != 0 {
t.Fatal("Expected 0 extNet, found:", len(actual))
}
}
func TestFilterBySegmentIPv4NotFound(t *testing.T) {
actual := testSegmentList.FilterBySubnetIPv4("nonexistent-ip")
if len(actual) != 0 {
t.Fatal("Expected 0 extNet, found:", len(actual))
}
}
func TestFilterBySubnetIPv6NotFound(t *testing.T) {
actual := testSegmentList.FilterBySubnetIPv6("nonexistent-ipv6")
if len(actual) != 0 {
t.Fatal("Expected 0 extNet, found:", len(actual))
}
}

48
pkg/sdn/segments/get.go Normal file
View File

@@ -0,0 +1,48 @@
package segments
import (
"context"
"encoding/json"
"net/http"
)
// GetRequest struct to get information about segment
type GetRequest struct {
// ID of segment
// Required: true
SegmentID string `url:"segment_id" json:"segment_id" validate:"required"`
// ID of access group
// Required: false
AccessGroupID string `url:"access_group_id,omitempty" json:"access_group_id,omitempty"`
}
// Get gets segment details as a SegmentResponse struct
func (s Segments) Get(ctx context.Context, req GetRequest) (*SegmentResponse, error) {
res, err := s.GetRaw(ctx, req)
if err != nil {
return nil, err
}
info := SegmentResponse{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}
// GetRaw gets segment details as an array of bytes
func (s Segments) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
//if err := validators.ValidateRequest(req); err != nil {
// return nil, validators.ValidationErrors(validators.GetErrors(err))
//}
url := "/sdn/segment/get"
res, err := s.client.DecortApiCall(ctx, http.MethodGet, url, req)
return res, err
}

View File

@@ -0,0 +1,51 @@
package segments
import (
"context"
"encoding/json"
"net/http"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
// GetStatusRequest struct to get information about segment status
type GetStatusRequest struct {
// ID of segment
// Required: true
SegmentID string `url:"segment_id" json:"segment_id" validate:"required"`
// ID of version
// Required: false
VersionID uint64 `url:"version_id,omitempty" json:"version_id,omitempty"`
// Get detailed status or not
// Required: false
Detailed interface{} `url:"enabled,omitempty" json:"enabled,omitempty" validate:"omitempty,isBool"`
}
// GetStatus gets segment status
func (s Segments) GetStatus(ctx context.Context, req GetStatusRequest) (string, error) {
type temp struct {
Status string `json:"status"`
}
err := validators.ValidateRequest(req)
if err != nil {
return "", validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/segment/get_status"
res, err := s.client.DecortApiCall(ctx, http.MethodGet, url, req)
if err != nil {
return "", err
}
info := temp{}
err = json.Unmarshal(res, &info)
if err != nil {
return "", err
}
return info.Status, nil
}

10
pkg/sdn/segments/ids.go Normal file
View File

@@ -0,0 +1,10 @@
package segments
// IDs gets array of IDs from ListSegment struct
func (list ListSegment) IDs() []string {
res := make([]string, 0, len(list))
for _, item := range list {
res = append(res, item.ID)
}
return res
}

92
pkg/sdn/segments/list.go Normal file
View File

@@ -0,0 +1,92 @@
package segments
import (
"context"
"encoding/json"
"net/http"
)
// ListRequest struct to get a list of segments
type ListRequest struct {
// Filter by access group ID
// Required: false
AccessGroupID string `url:"access_group_id,omitempty" json:"access_group_id,omitempty"`
// Filter by display name
// Required: false
DisplayName string `url:"display_name,omitempty" json:"display_name,omitempty"`
// Filter by IP version (v4 or v6)
// Required: false
Subnet string `url:"subnet,omitempty" json:"subnet,omitempty" validate:"omitempty,ipTypes"`
// Filter by enabled status
// Required: false
Enabled interface{} `url:"enabled,omitempty" json:"enabled,omitempty" validate:"omitempty,isBool"`
// Does Core currently believe that its data is synchronized with the data in the OVN?
// Required: false
IsSynced interface{} `url:"is_synced,omitempty" json:"is_synced,omitempty" validate:"omitempty,isBool"`
// Filter by update date from
// Required: false
UpdatedFrom string `url:"updated_from,omitempty" json:"updated_from,omitempty"`
// Filter lby update date to
// Required: false
UpdatedTo string `url:"updated_to,omitempty" json:"updated_to,omitempty"`
// Filter by create date from
// Required: false
CreatedFrom string `url:"created_from,omitempty" json:"created_from,omitempty"`
// Filter lby create date to
// 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, subnet)
// 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 gets list of all available segments as a ListSegment struct
func (s Segments) List(ctx context.Context, req ListRequest) (ListSegment, error) {
res, err := s.ListRaw(ctx, req)
if err != nil {
return nil, err
}
list := ListSegment{}
err = json.Unmarshal(res, &list)
if err != nil {
return nil, err
}
return list, nil
}
// ListRaw gets list of all available segments as an array of bytes
func (s Segments) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {
//if err := validators.ValidateRequest(req); err != nil {
// return nil, validators.ValidationErrors(validators.GetErrors(err))
//}
url := "/sdn/segment/list"
res, err := s.client.DecortApiCall(ctx, http.MethodGet, url, req)
return res, err
}

136
pkg/sdn/segments/models.go Normal file
View File

@@ -0,0 +1,136 @@
package segments
import "time"
// List segments
type ListSegment []SegmentResponse
// Main information about network segment
type SegmentResponse struct {
// Access group ID
AccessGroupID string `json:"access_group_id"`
// Access group name
AccessGroupName string `json:"access_group_name"`
// Created time
CreatedAt time.Time `json:"created_at"`
// Detailed description of the router port
Description string `json:"description"`
// DHCP IPv4
DHCPv4 DHCPv4Config `json:"dhcp_v4"`
// DHCP IPv6
DHCPv6 DHCPv6Config `json:"dhcp_v6"`
// User-friendly name for the segment
DisplayName string `json:"display_name"`
// Whether the segment is enabled
Enabled bool `json:"enabled"`
// ID of segment
ID string `json:"id"`
// Logical ports info
LogicalPortsInfo []EntityInfo `json:"logical_ports_info"`
// Routers info
RoutersInfo []EntityInfo `json:"routers_info"`
// Status
Status Status `json:"status"`
// IPv4 subnet in CIDR notation
SubnetV4 string `json:"subnet_v4"`
// IPv6 subnet in CIDR notation
SubnetV6 string `json:"subnet_v6"`
// Update time
UpdatedAt time.Time `json:"updated_at"`
// ID of version
VersionID uint64 `json:"version_id"`
}
type DHCPv4Config struct {
// DNS
DNS []string `json:"dns"`
// Excluded address ranges
ExcludedAddressRanges []string `json:"excluded_address_ranges"`
// Gateway
Gateway string `json:"gateway"`
// ID of config
ID string `json:"id"`
// Lease time
LeaseTime uint64 `json:"lease_time"`
// Server IP
ServerIP string `json:"server_ip"`
// Server MAC
ServerMAC string `json:"server_mac"`
// Whether the config is enabled
Enabled bool `json:"enabled"`
}
type DHCPv6Config struct {
// Address prefix
AddressPrefix string `json:"address_prefix"`
// DNS
DNS []string `json:"dns"`
// ID of config
ID string `json:"id"`
// Lease time
LeaseTime uint64 `json:"lease_time"`
// Server MAC
ServerMAC string `json:"server_mac"`
// Whether the config is enabled
Enabled bool `json:"enabled"`
}
type EntityInfo struct {
// User-friendly name for entity
DisplayName string `json:"display_name"`
// ID of entity
ID string `json:"id"`
}
type Status struct {
// Common
Common string `json:"common"`
// Hypervisors status
Hypervisors []HypervisorStatus `json:"hypervisors"`
}
type HypervisorStatus struct {
// Status
Status string `json:"status"`
// Name of hypervisor
Name string `json:"name"`
// User-friendly name for the hypervisor
DisplayName string `json:"display_name"`
// Hypervisor status
HypervisorStatus string `json:"hypervisor_status"`
// Synced time
SyncedAt time.Time `json:"synced_at"`
}

View File

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

View File

@@ -0,0 +1,77 @@
package segments
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 for updating segment
type UpdateRequest struct {
// ID of segment
// Required: true
SegmentID string `url:"segment_id" json:"segment_id" validate:"required"`
// ID of version
// Required: true
VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`
// Identifier of the parent access group
// Required: true
AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`
// Detailed description of the segment
// Required: true
Description string `url:"description" json:"description" validate:"required"`
// User-friendly name for the segment
// Required: true
DisplayName string `url:"display_name" json:"display_name" validate:"required"`
// Whether the network is enabled
// Required: true
Enabled bool `url:"enabled" json:"enabled"`
// IPv4 subnet in CIDR notation (Either subnet_v4 or subnet_v6 must be specified)
// Required: false
SubnetV4 string `url:"subnet_v4,omitempty" json:"subnet_v4,omitempty"`
// IPv6 subnet in CIDR notation (Either subnet_v4 or subnet_v6 must be specified)
// Required: false
SubnetV6 string `url:"subnet_v6,omitempty" json:"subnet_v6,omitempty"`
// DHCP IPv4
// Required: false
DHCPv4 *DHCPv4ConfigRequest `url:"-" json:"dhcp_v4,omitempty"`
// DHCP IPv6
// Required: false
DHCPv6 *DHCPv6ConfigRequest `url:"-" json:"dhcp_v6,omitempty"`
}
// Update updates segment
func (s Segments) Update(ctx context.Context, req UpdateRequest) (*SegmentResponse, error) {
err := validators.ValidateRequest(req)
if err != nil {
return nil, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/sdn/segment/update"
res, err := s.client.DecortApiCallCtype(ctx, http.MethodPut, url, constants.MIMEJSON, req)
if err != nil {
return nil, err
}
info := SegmentResponse{}
err = json.Unmarshal(res, &info)
if err != nil {
return nil, err
}
return &info, nil
}