-
Notifications
You must be signed in to change notification settings - Fork 0
/
ports.go
154 lines (126 loc) · 4.95 KB
/
ports.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package edgecloud
import (
"context"
"fmt"
"net"
"net/http"
)
const (
portsBasePathV1 = "/v1/ports"
)
const (
portsAllowAddressPairs = "allow_address_pairs"
portsEnableSecurity = "enable_port_security"
portsDisableSecurity = "disable_port_security"
)
// PortsService is an interface for creating and managing Ports with the EdgecenterCloud API.
// See: https://apidocs.edgecenter.ru/cloud#tag/ports
type PortsService interface {
Assign(context.Context, string, *PortsAllowedAddressPairsRequest) (*Port, *Response, error)
EnablePortSecurity(context.Context, string) (*InstancePortInterface, *Response, error)
DisablePortSecurity(context.Context, string) (*InstancePortInterface, *Response, error)
}
// PortsServiceOp handles communication with Ports methods of the EdgecenterCloud API.
type PortsServiceOp struct {
client *Client
}
var _ PortsService = &PortsServiceOp{}
// Port represents an EdgecenterCloud Port.
type Port struct {
NetworkID string `json:"network_id"`
AllowedAddressPairs []PortsAllowedAddressPairs `json:"allowed_address_pairs"`
InstanceID string `json:"instance_id"`
PortID string `json:"port_id"`
}
type InstanceSubPort struct {
PortID string `json:"port_id"`
MacAddress string `json:"mac_address"`
NetworkID string `json:"network_id"`
IPAssignments []PortIP `json:"ip_assignments"`
NetworkDetails NetworkSubnetwork `json:"network_details"`
FloatingIPDetails []FloatingIP `json:"floatingip_details"`
SegmentationID int `json:"segmentation_id"`
SegmentationType string `json:"segmentation_type"`
}
// PortsAllowedAddressPairs represents allowed port address pair and/or subnet masks.
type PortsAllowedAddressPairs struct {
IPAddress string `json:"ip_address"`
MacAddress string `json:"mac_address"`
}
// PortsAllowedAddressPairsRequest represents a request to assign allowed address pairs for an instance port.
type PortsAllowedAddressPairsRequest struct {
AllowedAddressPairs []PortsAllowedAddressPairs `json:"allowed_address_pairs"`
}
// InstancePortInterface represents an instance port interface.
type InstancePortInterface struct {
FloatingIPDetails []FloatingIP `json:"floatingip_details"`
NetworkDetails NetworkSubnetwork `json:"network_details"`
PortSecurityEnabled bool `json:"port_security_enabled"`
PortID string `json:"port_id"`
MacAddress string `json:"mac_address"`
NetworkID string `json:"network_id"`
IPAssignments []PortIP `json:"ip_assignments"`
SubPorts []InstanceSubPort `json:"sub_ports,omitempty"`
}
// PortIP represents an IPAddress and a SubnetID.
type PortIP struct {
IPAddress net.IP `json:"ip_address"`
SubnetID string `json:"subnet_id"`
}
// Assign allowed address pairs for an instance port.
func (s *PortsServiceOp) Assign(ctx context.Context, portID string, reqBody *PortsAllowedAddressPairsRequest) (*Port, *Response, error) {
if reqBody == nil {
return nil, nil, NewArgError("reqBody", "cannot be nil")
}
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := s.client.addProjectRegionPath(portsBasePathV1)
path = fmt.Sprintf("%s/%s/%s", path, portID, portsAllowAddressPairs)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, reqBody)
if err != nil {
return nil, nil, err
}
port := new(Port)
resp, err := s.client.Do(ctx, req, port)
if err != nil {
return nil, resp, err
}
return port, resp, err
}
// EnablePortSecurity for an instance interface.
func (s *PortsServiceOp) EnablePortSecurity(ctx context.Context, portID string) (*InstancePortInterface, *Response, error) {
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := s.client.addProjectRegionPath(portsBasePathV1)
path = fmt.Sprintf("%s/%s/%s", path, portID, portsEnableSecurity)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return nil, nil, err
}
instancePortInterface := new(InstancePortInterface)
resp, err := s.client.Do(ctx, req, instancePortInterface)
if err != nil {
return nil, resp, err
}
return instancePortInterface, resp, err
}
// DisablePortSecurity for an instance interface.
func (s *PortsServiceOp) DisablePortSecurity(ctx context.Context, portID string) (*InstancePortInterface, *Response, error) {
if resp, err := s.client.Validate(); err != nil {
return nil, resp, err
}
path := s.client.addProjectRegionPath(portsBasePathV1)
path = fmt.Sprintf("%s/%s/%s", path, portID, portsDisableSecurity)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return nil, nil, err
}
instancePortInterface := new(InstancePortInterface)
resp, err := s.client.Do(ctx, req, instancePortInterface)
if err != nil {
return nil, resp, err
}
return instancePortInterface, resp, err
}