v9.0.0
This commit is contained in:
18
pkg/cloudapi/extnet/extnet.go
Normal file
18
pkg/cloudapi/extnet/extnet.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// API Actor for use external networks
|
||||
package extnet
|
||||
|
||||
import (
|
||||
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/interfaces"
|
||||
)
|
||||
|
||||
// Structure for creating request to extnet
|
||||
type ExtNet struct {
|
||||
client interfaces.Caller
|
||||
}
|
||||
|
||||
// Builder for extnet endpoints
|
||||
func New(client interfaces.Caller) *ExtNet {
|
||||
return &ExtNet{
|
||||
client,
|
||||
}
|
||||
}
|
||||
53
pkg/cloudapi/extnet/filter.go
Normal file
53
pkg/cloudapi/extnet/filter.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package extnet
|
||||
|
||||
// FilterByID returns ListExtNets with specified ID.
|
||||
func (lenet ListExtNets) FilterByID(id uint64) ListExtNets {
|
||||
predicate := func(iexnet ItemExtNet) bool {
|
||||
return iexnet.ID == id
|
||||
}
|
||||
|
||||
return lenet.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByName returns ListExtNets with specified Name.
|
||||
func (lenet ListExtNets) FilterByName(name string) ListExtNets {
|
||||
predicate := func(iexnet ItemExtNet) bool {
|
||||
return iexnet.Name == name
|
||||
}
|
||||
|
||||
return lenet.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterByStatus returns ListExtNets with specified Status.
|
||||
func (lenet ListExtNets) FilterByStatus(status string) ListExtNets {
|
||||
predicate := func(iexnet ItemExtNet) bool {
|
||||
return iexnet.Status == status
|
||||
}
|
||||
|
||||
return lenet.FilterFunc(predicate)
|
||||
}
|
||||
|
||||
// FilterFunc allows filtering ListExtNets based on a user-specified predicate.
|
||||
func (lenet ListExtNets) FilterFunc(predicate func(ItemExtNet) bool) ListExtNets {
|
||||
var result ListExtNets
|
||||
|
||||
for _, item := range lenet.Data {
|
||||
if predicate(item) {
|
||||
result.Data = append(result.Data, item)
|
||||
}
|
||||
}
|
||||
|
||||
result.EntryCount = uint64(len(result.Data))
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// FindOne returns first found ItemExtNet
|
||||
// If none was found, returns an empty struct.
|
||||
func (lenet ListExtNets) FindOne() ItemExtNet {
|
||||
if lenet.EntryCount == 0 {
|
||||
return ItemExtNet{}
|
||||
}
|
||||
|
||||
return lenet.Data[0]
|
||||
}
|
||||
67
pkg/cloudapi/extnet/filter_test.go
Normal file
67
pkg/cloudapi/extnet/filter_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package extnet
|
||||
|
||||
import "testing"
|
||||
|
||||
var extnets = ListExtNets{
|
||||
Data: []ItemExtNet{
|
||||
{
|
||||
ID: 3,
|
||||
IPCIDR: "176.118.164.0/24",
|
||||
Name: "176.118.164.0/24",
|
||||
Status: "ENABLED",
|
||||
},
|
||||
{
|
||||
ID: 10,
|
||||
IPCIDR: "45.134.255.0/24",
|
||||
Name: "45.134.255.0/24",
|
||||
Status: "ENABLED",
|
||||
},
|
||||
{
|
||||
ID: 13,
|
||||
IPCIDR: "88.218.249.0/24",
|
||||
Name: "88.218.249.0/24",
|
||||
Status: "DISABLED",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestFilterByID(t *testing.T) {
|
||||
actual := extnets.FilterByID(10).FindOne()
|
||||
|
||||
if actual.ID != 10 {
|
||||
t.Fatal("expected ID 10, found: ", actual.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByName(t *testing.T) {
|
||||
name := "88.218.249.0/24"
|
||||
actual := extnets.FilterByName(name).FindOne()
|
||||
|
||||
if actual.Name != name {
|
||||
t.Fatal("expected ", name, " found: ", actual.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterByStatus(t *testing.T) {
|
||||
actual := extnets.FilterByStatus("ENABLED")
|
||||
|
||||
if len(actual.Data) != 2 {
|
||||
t.Fatal("expected 2 found, actual: ", len(actual.Data))
|
||||
}
|
||||
|
||||
for _, item := range actual.Data {
|
||||
if item.Status != "ENABLED" {
|
||||
t.Fatal("expected Status 'ENABLED', found: ", item.Status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterFunc(t *testing.T) {
|
||||
actual := extnets.FilterFunc(func(ien ItemExtNet) bool {
|
||||
return ien.IPCIDR == ien.Name
|
||||
})
|
||||
|
||||
if len(actual.Data) != 3 {
|
||||
t.Fatal("expected 3 elements, found: ", len(actual.Data))
|
||||
}
|
||||
}
|
||||
46
pkg/cloudapi/extnet/get.go
Normal file
46
pkg/cloudapi/extnet/get.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package extnet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/internal/validators"
|
||||
)
|
||||
|
||||
// GetRequest struct to get detailed information about external network
|
||||
type GetRequest struct {
|
||||
// ID of external network
|
||||
// Required: true
|
||||
NetID uint64 `url:"net_id" json:"net_id" validate:"required"`
|
||||
}
|
||||
|
||||
// Get gets detailed information about external network as a RecordExtNet struct
|
||||
func (e ExtNet) Get(ctx context.Context, req GetRequest) (*RecordExtNet, error) {
|
||||
res, err := e.GetRaw(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := RecordExtNet{}
|
||||
|
||||
err = json.Unmarshal(res, &info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
// GetRaw gets detailed information about external network as an array of bytes
|
||||
func (e ExtNet) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
|
||||
err := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
url := "/cloudapi/extnet/get"
|
||||
|
||||
res, err := e.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
return res, err
|
||||
}
|
||||
24
pkg/cloudapi/extnet/get_default.go
Normal file
24
pkg/cloudapi/extnet/get_default.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package extnet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// GetDefault get default external network ID
|
||||
func (e ExtNet) GetDefault(ctx context.Context) (uint64, error) {
|
||||
url := "/cloudapi/extnet/getDefault"
|
||||
|
||||
res, err := e.client.DecortApiCall(ctx, http.MethodPost, url, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
result, err := strconv.ParseUint(string(res), 10, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
19
pkg/cloudapi/extnet/ids.go
Normal file
19
pkg/cloudapi/extnet/ids.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package extnet
|
||||
|
||||
// IDs gets array of ExtNetIDs from ListExtNets struct
|
||||
func (le ListExtNets) IDs() []uint64 {
|
||||
res := make([]uint64, 0, len(le.Data))
|
||||
for _, e := range le.Data {
|
||||
res = append(res, e.ID)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// IDs gets array of ComputeIDs from ListExtNetComputes struct
|
||||
func (le ListExtNetComputes) IDs() []uint64 {
|
||||
res := make([]uint64, 0, len(le.Data))
|
||||
for _, e := range le.Data {
|
||||
res = append(res, e.ID)
|
||||
}
|
||||
return res
|
||||
}
|
||||
83
pkg/cloudapi/extnet/list.go
Normal file
83
pkg/cloudapi/extnet/list.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package extnet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/internal/validators"
|
||||
)
|
||||
|
||||
// ListRequest struct to get list of external network
|
||||
type ListRequest struct {
|
||||
// Find by account ID
|
||||
// Required: false
|
||||
AccountID uint64 `url:"accountId,omitempty" json:"accountId,omitempty"`
|
||||
|
||||
// Find by ID
|
||||
// Required: false
|
||||
ByID uint64 `url:"by_id,omitempty" json:"by_id,omitempty"`
|
||||
|
||||
// Find by name
|
||||
// Required: false
|
||||
Name string `url:"name,omitempty" json:"name,omitempty"`
|
||||
|
||||
// Find by network ip address
|
||||
// Required: false
|
||||
Network string `url:"network,omitempty" json:"network,omitempty"`
|
||||
|
||||
// Find by vlan ID
|
||||
// Required: false
|
||||
VLANID uint64 `url:"vlanId,omitempty" json:"vlanId,omitempty"`
|
||||
|
||||
// Find by vnfDevices ID
|
||||
// Required: false
|
||||
VNFDevID uint64 `url:"vnfDevId,omitempty" json:"vnfDevId,omitempty"`
|
||||
|
||||
// Find by status
|
||||
// Required: false
|
||||
Status string `url:"status,omitempty" json:"status,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 available external networks as a ListExtNets struct
|
||||
func (e ExtNet) List(ctx context.Context, req ListRequest) (*ListExtNets, error) {
|
||||
|
||||
res, err := e.ListRaw(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := ListExtNets{}
|
||||
|
||||
err = json.Unmarshal(res, &list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &list, nil
|
||||
}
|
||||
|
||||
// ListRaw gets list of all available external networks as an array of bytes
|
||||
func (e ExtNet) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {
|
||||
|
||||
if err := validators.ValidateRequest(req); err != nil {
|
||||
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
url := "/cloudapi/extnet/list"
|
||||
|
||||
res, err := e.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
return res, err
|
||||
}
|
||||
61
pkg/cloudapi/extnet/list_computes.go
Normal file
61
pkg/cloudapi/extnet/list_computes.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package extnet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/internal/validators"
|
||||
)
|
||||
|
||||
// ListComputesRequest struct to get list computes
|
||||
type ListComputesRequest struct {
|
||||
// Filter by account ID
|
||||
// Required: true
|
||||
AccountID uint64 `url:"accountId" json:"accountId" validate:"required"`
|
||||
|
||||
// Find by rg ID
|
||||
// Required: false
|
||||
RGID uint64 `url:"rgId,omitempty" json:"rgId,omitempty"`
|
||||
|
||||
// Find by compute ID
|
||||
// Required: false
|
||||
ComputeID uint64 `url:"computeId,omitempty" json:"computeId,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"`
|
||||
}
|
||||
|
||||
// ListComputes gets computes from account with extnets
|
||||
func (e ExtNet) ListComputes(ctx context.Context, req ListComputesRequest) (*ListExtNetComputes, error) {
|
||||
|
||||
err := validators.ValidateRequest(req)
|
||||
if err != nil {
|
||||
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
url := "/cloudapi/extnet/listComputes"
|
||||
|
||||
res, err := e.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := ListExtNetComputes{}
|
||||
|
||||
err = json.Unmarshal(res, &list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &list, nil
|
||||
}
|
||||
228
pkg/cloudapi/extnet/models.go
Normal file
228
pkg/cloudapi/extnet/models.go
Normal file
@@ -0,0 +1,228 @@
|
||||
package extnet
|
||||
|
||||
// Main information about external network
|
||||
type ItemExtNet struct {
|
||||
// ID
|
||||
ID uint64 `json:"id"`
|
||||
|
||||
// IPCIDR
|
||||
IPCIDR string `json:"ipcidr"`
|
||||
|
||||
// Name
|
||||
Name string `json:"name"`
|
||||
|
||||
// Status
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// Extend information about external network
|
||||
type ItemExtNetExtend struct {
|
||||
// Main information about external network
|
||||
ItemExtNet
|
||||
|
||||
// IP address
|
||||
IPAddr string `json:"ipaddr"`
|
||||
}
|
||||
|
||||
// List of information about external network
|
||||
type ListExtNets struct {
|
||||
Data []ItemExtNet `json:"data"`
|
||||
|
||||
EntryCount uint64 `json:"entryCount"`
|
||||
}
|
||||
|
||||
// List of extend information about external network
|
||||
type ListExtNetExtends []ItemExtNetExtend
|
||||
|
||||
// Main information about compute with external network
|
||||
type ItemExtNetCompute struct {
|
||||
// Account ID
|
||||
AccountID uint64 `json:"accountId"`
|
||||
|
||||
// Account name
|
||||
AccountName string `json:"accountName"`
|
||||
|
||||
// List of extend information about external network
|
||||
ExtNets ListExtNetExtends `json:"extnets"`
|
||||
|
||||
// ID
|
||||
ID uint64 `json:"id"`
|
||||
|
||||
// Name
|
||||
Name string `json:"name"`
|
||||
|
||||
// Resource group ID
|
||||
RGID uint64 `json:"rgId"`
|
||||
|
||||
// Resource group name
|
||||
RGName string `json:"rgName"`
|
||||
}
|
||||
|
||||
// List of information about computes with external network
|
||||
type ListExtNetComputes struct {
|
||||
// Data
|
||||
Data []ItemExtNetCompute `json:"data"`
|
||||
|
||||
// Entry count
|
||||
EntryCount uint64 `json:"entryCount"`
|
||||
}
|
||||
|
||||
// QOS
|
||||
type QOS struct {
|
||||
// EBurst
|
||||
EBurst uint64 `json:"eBurst"`
|
||||
|
||||
// ERate
|
||||
ERate uint64 `json:"eRate"`
|
||||
|
||||
// GUID
|
||||
GUID string `json:"guid"`
|
||||
|
||||
// InBurst
|
||||
InBurst uint64 `json:"inBurst"`
|
||||
|
||||
// InRate
|
||||
InRate uint64 `json:"inRate"`
|
||||
}
|
||||
|
||||
// Main information about reservations
|
||||
type ItemReservation struct {
|
||||
// ClientType
|
||||
ClientType string `json:"clientType"`
|
||||
|
||||
// Description
|
||||
Description string `json:"desc"`
|
||||
|
||||
// Domain name
|
||||
DomainName string `json:"domainname"`
|
||||
|
||||
// Hostname
|
||||
Hostname string `json:"hostname"`
|
||||
|
||||
// IP
|
||||
IP string `json:"ip"`
|
||||
|
||||
// MAC
|
||||
MAC string `json:"mac"`
|
||||
|
||||
// Type
|
||||
Type string `json:"type"`
|
||||
|
||||
// Virtual machine ID
|
||||
VMID uint64 `json:"vmId"`
|
||||
}
|
||||
|
||||
// List of information about reservations
|
||||
type ListReservations []ItemReservation
|
||||
|
||||
// VNFs
|
||||
type VNFs struct {
|
||||
DHCP uint64 `json:"dhcp"`
|
||||
}
|
||||
|
||||
type Excluded struct {
|
||||
// ClientType
|
||||
ClientType string `json:"clientType"`
|
||||
|
||||
// Domain name
|
||||
DomainName string `json:"domainname"`
|
||||
|
||||
// Host name
|
||||
HostName string `json:"hostname"`
|
||||
|
||||
// IP
|
||||
IP string `json:"ip"`
|
||||
|
||||
// MAC
|
||||
MAC string `json:"mac"`
|
||||
|
||||
// Type
|
||||
Type string `json:"type"`
|
||||
|
||||
// VMID
|
||||
VMID uint64 `json:"vmId"`
|
||||
}
|
||||
|
||||
// Detailed information about external network
|
||||
type RecordExtNet struct {
|
||||
// CKey
|
||||
CKey string `json:"_ckey"`
|
||||
|
||||
// Meta
|
||||
Meta []interface{} `json:"_meta"`
|
||||
|
||||
// CheckIps
|
||||
CheckIPs []string `json:"checkIps"`
|
||||
|
||||
// Default
|
||||
Default bool `json:"default"`
|
||||
|
||||
// Default QOS
|
||||
DefaultQOS QOS `json:"defaultQos"`
|
||||
|
||||
// Description
|
||||
Description string `json:"desc"`
|
||||
|
||||
// list of DNS
|
||||
DNS []string `json:"dns"`
|
||||
|
||||
// Excluded
|
||||
Excluded []Excluded `json:"excluded"`
|
||||
|
||||
// Free IPs
|
||||
FreeIPs int64 `json:"free_ips"`
|
||||
|
||||
// Gateway
|
||||
Gateway string `json:"gateway"`
|
||||
|
||||
// Grid ID
|
||||
GID uint64 `json:"gid"`
|
||||
|
||||
// GUID
|
||||
GUID uint64 `json:"guid"`
|
||||
|
||||
// ID
|
||||
ID uint64 `json:"id"`
|
||||
|
||||
// IPCIDR
|
||||
IPCIDR string `json:"ipcidr"`
|
||||
|
||||
// Milestones
|
||||
Milestones uint64 `json:"milestones"`
|
||||
|
||||
// Name
|
||||
Name string `json:"name"`
|
||||
|
||||
// Network
|
||||
Network string `json:"network"`
|
||||
|
||||
// Network ID
|
||||
NetworkID uint64 `json:"networkId"`
|
||||
|
||||
// OVS Bridge
|
||||
OVSBridge string `json:"ovsBridge"`
|
||||
|
||||
// PreReservation IP num
|
||||
PreReservationsNum uint64 `json:"preReservationsNum"`
|
||||
|
||||
// Prefix
|
||||
Prefix uint64 `json:"prefix"`
|
||||
|
||||
// PriVNFDevID
|
||||
PriVNFDevID uint64 `json:"priVnfDevId"`
|
||||
|
||||
// List reservations
|
||||
Reservations ListReservations `json:"reservations"`
|
||||
|
||||
// Shared with
|
||||
SharedWith []uint64 `json:"sharedWith"`
|
||||
|
||||
// Status
|
||||
Status string `json:"status"`
|
||||
|
||||
// VLAN ID
|
||||
VLANID uint64 `json:"vlanId"`
|
||||
|
||||
// VNFs
|
||||
VNFs VNFs `json:"vnfs"`
|
||||
}
|
||||
43
pkg/cloudapi/extnet/serialize.go
Normal file
43
pkg/cloudapi/extnet/serialize.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package extnet
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"repository.basistech.ru/BASIS/dynamix-golang-sdk/v9/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 (lenet ListExtNets) Serialize(params ...string) (serialization.Serialized, error) {
|
||||
if lenet.EntryCount == 0 {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
if len(params) > 1 {
|
||||
prefix := params[0]
|
||||
indent := params[1]
|
||||
|
||||
return json.MarshalIndent(lenet, prefix, indent)
|
||||
}
|
||||
|
||||
return json.Marshal(lenet)
|
||||
}
|
||||
|
||||
// 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 (ienet ItemExtNet) Serialize(params ...string) (serialization.Serialized, error) {
|
||||
if len(params) > 1 {
|
||||
prefix := params[0]
|
||||
indent := params[1]
|
||||
|
||||
return json.MarshalIndent(ienet, prefix, indent)
|
||||
}
|
||||
|
||||
return json.Marshal(ienet)
|
||||
}
|
||||
Reference in New Issue
Block a user