forked from cherryservers/cherrygo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipaddresses.go
193 lines (156 loc) · 6.21 KB
/
ipaddresses.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package cherrygo
import (
"fmt"
)
const baseIpPath = "/v1/ips"
// IpAddressesService is an interface for interfacing with the the Server endpoints of the CherryServers API
// See: https://api.cherryservers.com/doc/#tag/Ip-Addresses
type IpAddressesService interface {
List(projectID int, opts *GetOptions) ([]IPAddress, *Response, error)
Get(ipID string, opts *GetOptions) (IPAddress, *Response, error)
Create(projectID int, request *CreateIPAddress) (IPAddress, *Response, error)
Remove(ipID string) (*Response, error)
Update(ipID string, request *UpdateIPAddress) (IPAddress, *Response, error)
Assign(ipID string, request *AssignIPAddress) (IPAddress, *Response, error)
Unassign(ipID string) (*Response, error)
}
// IPAddresses fields
type IPAddress struct {
ID string `json:"id,omitempty"`
Address string `json:"address,omitempty"`
AddressFamily int `json:"address_family,omitempty"`
Cidr string `json:"cidr,omitempty"`
Gateway string `json:"gateway,omitempty"`
Type string `json:"type,omitempty"`
Region Region `json:"region,omitempty"`
RoutedTo RoutedTo `json:"routed_to,omitempty"`
AssignedTo AssignedTo `json:"assigned_to,omitempty"`
TargetedTo AssignedTo `json:"targeted_to,omitempty"`
Project Project `json:"project,omitempty"`
PtrRecord string `json:"ptr_record,omitempty"`
ARecord string `json:"a_record,omitempty"`
Tags *map[string]string `json:"tags,omitempty"`
DDoSScrubbing bool `json:"ddos_scrubbing,omitempty"`
Href string `json:"href,omitempty"`
}
// RoutedTo fields
type RoutedTo struct {
ID string `json:"id,omitempty"`
Address string `json:"address,omitempty"`
AddressFamily int `json:"address_family,omitempty"`
Cidr string `json:"cidr,omitempty"`
Gateway string `json:"gateway,omitempty"`
Type string `json:"type,omitempty"`
Region Region `json:"region,omitempty"`
}
// AssignedTo fields
type AssignedTo struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Href string `json:"href,omitempty"`
Hostname string `json:"hostname,omitempty"`
Image string `json:"image,omitempty"`
Region Region `json:"region,omitempty"`
State string `json:"state,omitempty"`
Pricing Pricing `json:"pricing,omitempty"`
}
// IPClient paveldi client
type IPsClient struct {
client *Client
}
// CreateIPAddress fields for adding addition IP address
type CreateIPAddress struct {
Region string `json:"region,omitempty"`
PtrRecord string `json:"ptr_record,omitempty"`
ARecord string `json:"a_record,omitempty"`
RoutedTo string `json:"routed_to,omitempty"`
AssignedTo string `json:"assigned_to,omitempty"`
TargetedTo string `json:"targeted_to,omitempty"`
Tags *map[string]string `json:"tags,omitempty"`
DDoSScrubbing bool `json:"ddos_scrubbing,omitempty"`
}
// UpdateIPAddress fields for updating IP address
type UpdateIPAddress struct {
PtrRecord string `json:"ptr_record,omitempty"`
ARecord string `json:"a_record,omitempty"`
RoutedTo string `json:"routed_to,omitempty"`
AssignedTo string `json:"assigned_to,omitempty"`
TargetedTo string `json:"targeted_to,omitempty"`
Tags *map[string]string `json:"tags,omitempty"`
}
// Subnet type IP addresses can be only assigned to a server.
// Floating IP address can be assigned directly to a server or routed to subnet type IP address.
type AssignIPAddress struct {
ServerID int `json:"targeted_to,omitempty"`
IpID string `json:"routed_to,omitempty"`
}
// List func lists ip addresses
func (i *IPsClient) List(projectID int, opts *GetOptions) ([]IPAddress, *Response, error) {
path := opts.WithQuery(fmt.Sprintf("%s/%d/ips", baseProjectPath, projectID))
var trans []IPAddress
resp, err := i.client.MakeRequest("GET", path, nil, &trans)
if err != nil {
err = fmt.Errorf("Error: %v", err)
}
return trans, resp, err
}
// List func lists teams
func (i *IPsClient) Get(ipID string, opts *GetOptions) (IPAddress, *Response, error) {
path := opts.WithQuery(fmt.Sprintf("%s/%s", baseIpPath, ipID))
var trans IPAddress
resp, err := i.client.MakeRequest("GET", path, nil, &trans)
if err != nil {
err = fmt.Errorf("Error: %v", err)
}
return trans, resp, err
}
// Create function orders new floating IP address
func (i *IPsClient) Create(projectID int, request *CreateIPAddress) (IPAddress, *Response, error) {
var trans IPAddress
path := fmt.Sprintf("%s/%d/ips", baseProjectPath, projectID)
resp, err := i.client.MakeRequest("POST", path, request, &trans)
if err != nil {
err = fmt.Errorf("Error: %v", err)
}
return trans, resp, err
}
// Update function updates existing IP address
func (i *IPsClient) Update(ipID string, request *UpdateIPAddress) (IPAddress, *Response, error) {
var trans IPAddress
path := fmt.Sprintf("%s/%s", baseIpPath, ipID)
resp, err := i.client.MakeRequest("PUT", path, request, &trans)
if err != nil {
err = fmt.Errorf("Error: %v", err)
}
return trans, resp, err
}
// Remove function removes existing project IP address
func (i *IPsClient) Remove(ipID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", baseIpPath, ipID)
resp, err := i.client.MakeRequest("DELETE", path, nil, nil)
if err != nil {
err = fmt.Errorf("Error: %v", err)
}
return resp, err
}
func (i *IPsClient) Assign(ipID string, request *AssignIPAddress) (IPAddress, *Response, error) {
var trans IPAddress
path := fmt.Sprintf("%s/%s", baseIpPath, ipID)
resp, err := i.client.MakeRequest("PUT", path, request, &trans)
if err != nil {
err = fmt.Errorf("Error: %v", err)
}
return trans, resp, err
}
func (i *IPsClient) Unassign(ipID string) (*Response, error) {
var trans IPAddress
path := fmt.Sprintf("%s/%s", baseIpPath, ipID)
request := UpdateIPAddress{
TargetedTo: "0",
}
resp, err := i.client.MakeRequest("PUT", path, request, &trans)
if err != nil {
err = fmt.Errorf("Error: %v", err)
}
return resp, err
}