-
Notifications
You must be signed in to change notification settings - Fork 3
/
serverroute.go
165 lines (140 loc) · 5.09 KB
/
serverroute.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
155
156
157
158
159
160
161
162
163
164
165
package pritunl
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// ServerRouteRequest represents a request to create or update a server route
type ServerRouteRequest struct {
ID string `json:"id"`
Server string `json:"server"`
Network string `json:"network"`
Comment string `json:"comment"`
Metric int `json:"metric"`
Nat bool `json:"nat"`
NatInterface string `json:"nat_interface"`
NatNetmap string `json:"nat_netmap"`
Advertise bool `json:"advertise"`
VpcRegion string `json:"vpc_region"`
VpcId string `json:"vpc_id"`
NetGateway bool `json:"net_gateway"`
VirtualNetwork bool `json:"virtual_network"`
NetworkLink bool `json:"network_link"`
ServerLink bool `json:"server_link"` // Addition for Put Method
LinkVirtualNetwork bool `json:"link_virtual_network"`
WgNetwork bool `json:"wg_network"`
}
// ServerRouteResponse represents a server route response
type ServerRouteResponse struct {
ID string `json:"id"`
Server string `json:"server"`
Network string `json:"network"`
Comment string `json:"comment"`
Metric int `json:"metric"`
Nat bool `json:"nat"`
NatInterface string `json:"nat_interface"`
NatNetmap string `json:"nat_netmap"`
Advertise bool `json:"advertise"`
VpcRegion string `json:"vpc_region"`
VpcId string `json:"vpc_id"`
NetGateway bool `json:"net_gateway"`
}
// ServerRouteGet retrieves the server routes
func (c *Client) ServerRouteGet(ctx context.Context, srvId string) ([]ServerRouteResponse, error) {
// Construct the API path using the server ID
path := fmt.Sprintf("/server/%s/route", srvId)
// Send an authenticated GET request to retrieve server routes
response, err := c.AuthRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of ServerRouteResponse
var serverroutes []ServerRouteResponse
if err := handleUnmarshal(body, &serverroutes); err != nil {
return nil, err
}
// Return the slice of serverroutes
return serverroutes, nil
}
// ServerRouteCreate adds a route to a network
func (c *Client) ServerRouteCreate(ctx context.Context, srvId string, newServerRoute ServerRouteRequest) ([]ServerRouteResponse, error) {
// Marshal the ServerRouteRequest struct into JSON data
serverRouteData, err := json.Marshal(newServerRoute)
if err != nil {
return nil, fmt.Errorf("failed to marshal serverroute data: %w", err)
}
// Construct the API path using the server ID
path := fmt.Sprintf("/server/%s/route", srvId)
// Send an authenticated POST request to create a new server route
response, err := c.AuthRequest(ctx, http.MethodPost, path, serverRouteData)
if err != nil {
return nil, err
}
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of ServerRouteResponse
var serverroutes []ServerRouteResponse
if err := handleUnmarshal(body, &serverroutes); err != nil {
return nil, err
}
// Return the slice of serverroutes
return serverroutes, nil
}
// ServerRouteUpdate updates a server route
func (c *Client) ServerRouteUpdate(ctx context.Context, srvId string, routeId string, newServerRoute ServerRouteRequest) ([]ServerRouteResponse, error) {
// Marshal the ServerRouteRequest struct into JSON data
serverRouteData, err := json.Marshal(newServerRoute)
if err != nil {
return nil, fmt.Errorf("failed to marshal serverroute data: %w", err)
}
// Construct the API path using the server ID and route ID
path := fmt.Sprintf("/server/%s/route/%s", srvId, routeId)
// Send an authenticated PUT request to update an existing server route
response, err := c.AuthRequest(ctx, http.MethodPut, path, serverRouteData)
if err != nil {
return nil, err
}
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of ServerRouteResponse
var serverroutes []ServerRouteResponse
if err := handleUnmarshal(body, &serverroutes); err != nil {
return nil, err
}
// Return the slice of serverroutes
return serverroutes, nil
}
// ServerRouteDelete removes a server route
func (c *Client) ServerRouteDelete(ctx context.Context, srvId string, routeId string) ([]ServerRouteResponse, error) {
// Construct the API path using the server ID and route ID
path := fmt.Sprintf("/server/%s/route/%s", srvId, routeId)
// Send an authenticated DELETE request to remove a server route
response, err := c.AuthRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
body, err := handleResponse(response)
if err != nil {
return nil, err
}
defer body.Close()
// Unmarshal the JSON data into a slice of ServerRouteResponse
var serverroutes []ServerRouteResponse
if err := handleUnmarshal(body, &serverroutes); err != nil {
return nil, err
}
// Return the slice of serverroutes
return serverroutes, nil
}