forked from equinixmetal-archive/packngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgp_sessions.go
131 lines (110 loc) · 3.88 KB
/
bgp_sessions.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
package packngo
import (
"path"
)
var bgpSessionBasePath = "/bgp/sessions"
var bgpNeighborsBasePath = "/bgp/neighbors"
var bgpDiscoverBasePath = "/bgp/discover"
// BGPSessionService interface defines available BGP session methods
type BGPSessionService interface {
Get(string, *GetOptions) (*BGPSession, *Response, error)
Create(string, CreateBGPSessionRequest) (*BGPSession, *Response, error)
Update(string, UpdateBGPSessionRequest) (*BGPSession, *Response, error)
Delete(string) (*Response, error)
}
type bgpSessionsRoot struct {
Sessions []BGPSession `json:"bgp_sessions"`
Meta meta `json:"meta"`
}
// BGPSessionServiceOp implements BgpSessionService
type BGPSessionServiceOp struct {
client *Client
}
// BGPSession represents an Equinix Metal BGP Session
type BGPSession struct {
ID string `json:"id,omitempty"`
Status string `json:"status,omitempty"`
LearnedRoutes []string `json:"learned_routes,omitempty"`
AddressFamily string `json:"address_family,omitempty"`
Device Device `json:"device,omitempty"`
Href string `json:"href,omitempty"`
DefaultRoute *bool `json:"default_route,omitempty"`
}
type bgpNeighborsRoot struct {
BGPNeighbors []BGPNeighbor `json:"bgp_neighbors"`
}
// BGPNeighor is struct for listing BGP neighbors of a device
type BGPNeighbor struct {
AddressFamily int `json:"address_family"`
CustomerAs int `json:"customer_as"`
CustomerIP string `json:"customer_ip"`
Md5Enabled bool `json:"md5_enabled"`
Md5Password string `json:"md5_password"`
Multihop bool `json:"multihop"`
PeerAs int `json:"peer_as"`
PeerIps []string `json:"peer_ips"`
RoutesIn []BGPRoute `json:"routes_in"`
RoutesOut []BGPRoute `json:"routes_out"`
}
// BGPRoute is a struct for Route in BGP neighbor listing
type BGPRoute struct {
Route string `json:"route"`
Exact bool `json:"exact"`
}
// CreateBGPSessionRequest struct
type CreateBGPSessionRequest struct {
AddressFamily string `json:"address_family"`
DefaultRoute *bool `json:"default_route,omitempty"`
}
// UpdateBGPSessionRequest struct
type UpdateBGPSessionRequest struct {
DefaultRoute bool `json:"default_route"`
}
// Create function
func (s *BGPSessionServiceOp) Create(deviceID string, request CreateBGPSessionRequest) (*BGPSession, *Response, error) {
if validateErr := ValidateUUID(deviceID); validateErr != nil {
return nil, nil, validateErr
}
apiPath := path.Join(deviceBasePath, deviceID, bgpSessionBasePath)
session := new(BGPSession)
resp, err := s.client.DoRequest("POST", apiPath, request, session)
if err != nil {
return nil, resp, err
}
return session, resp, err
}
// Update function
func (s *BGPSessionServiceOp) Update(sessionID string, request UpdateBGPSessionRequest) (*BGPSession, *Response, error) {
if validateErr := ValidateUUID(sessionID); validateErr != nil {
return nil, nil, validateErr
}
apiPath := path.Join(bgpSessionBasePath, sessionID)
session := new(BGPSession)
resp, err := s.client.DoRequest("PUT", apiPath, request, session)
if err != nil {
return nil, resp, err
}
return session, resp, err
}
// Delete function
func (s *BGPSessionServiceOp) Delete(id string) (*Response, error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, validateErr
}
apiPath := path.Join(bgpSessionBasePath, id)
return s.client.DoRequest("DELETE", apiPath, nil, nil)
}
// Get function
func (s *BGPSessionServiceOp) Get(id string, opts *GetOptions) (session *BGPSession, response *Response, err error) {
if validateErr := ValidateUUID(id); validateErr != nil {
return nil, nil, validateErr
}
endpointPath := path.Join(bgpSessionBasePath, id)
apiPathQuery := opts.WithQuery(endpointPath)
session = new(BGPSession)
response, err = s.client.DoRequest("GET", apiPathQuery, nil, session)
if err != nil {
return nil, response, err
}
return session, response, err
}