forked from floshodan/hrobot-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subnet.go
209 lines (165 loc) · 5.07 KB
/
subnet.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package hrobot
import (
"context"
"fmt"
"net/url"
"github.com/Mats4k/hrobot-go/hrobot/schema"
"github.com/google/go-querystring/query"
)
type Subnet struct {
IP string
Mask int
Gateway string
ServerIP string
ServerNumber int
Failover bool
Locked bool
TrafficWarnings bool
TrafficHourly int
TrafficDaily int
TrafficMonthly int
}
type SubnetClient struct {
client *Client
}
type SubnetMac struct {
IP string
Mask int
Mac string
PossibleMac interface{}
}
type SubnetCancellation struct {
IP string
Mask int
ServerNumber int
EarliestCancellationDate string
Cancelled bool
CancellationDate interface{}
}
func (c *SubnetClient) List(ctx context.Context) ([]*Subnet, *Response, error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/subnet/"), nil)
// fmt.Println(req)
if err != nil {
return nil, nil, err
}
var body schema.SubnetList
//fmt.Println(body)
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
//fmt.Println(len(body))
subnetlist := make([]*Subnet, 0, len(body))
for i := range body {
subnet := SubnetFromSchema(body[i])
subnetlist = append(subnetlist, subnet)
}
return subnetlist, resp, nil
}
// requires the net ip of the subnet
func (c *SubnetClient) GetSubnetByIP(ctx context.Context, netip string) (*Subnet, *Response, error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/subnet/%s", netip), nil)
if err != nil {
return nil, nil, err
}
var body schema.Subnet
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return SubnetFromSchema(body), resp, nil
}
// requires the net ip of the subnet
func (c *SubnetClient) UpdateTraffic(ctx context.Context, netip string, opt *SubnetOps) (*Subnet, *Response, error) {
params, _ := query.Values(opt)
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("/subnet/%s", netip), params)
if err != nil {
return nil, nil, err
}
var body schema.Subnet
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return SubnetFromSchema(body), resp, nil
}
func (c *SubnetClient) GetMac(ctx context.Context, netip string) (*SubnetMac, *Response, error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/subnet/%s/mac", netip), nil)
if err != nil {
return nil, nil, err
}
var body schema.SubnetMac
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return SubnetMacFromSchema(body), resp, nil
}
func (c *SubnetClient) GenerateMac(ctx context.Context, netip string) (*SubnetMac, *Response, error) {
req, err := c.client.NewRequest(ctx, "PUT", fmt.Sprintf("/subnet/%s/mac", netip), nil)
if err != nil {
return nil, nil, err
}
var body schema.SubnetMac
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return SubnetMacFromSchema(body), resp, nil
}
func (c *SubnetClient) DeleteMAC(ctx context.Context, netip string) (*SubnetMac, *Response, error) {
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/subnet/%s/mac", netip), nil)
if err != nil {
return nil, nil, err
}
var body schema.SubnetMac
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return SubnetMacFromSchema(body), resp, nil
}
func (c *SubnetClient) GetCancellation(ctx context.Context, netip string) (*SubnetCancellation, *Response, error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/subnet/%s/cancellation", netip), nil)
if err != nil {
return nil, nil, err
}
var body schema.SubnetCancellation
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return SubnetCancellationFromSchema(body), resp, nil
}
func (c *SubnetClient) PostCancellation(ctx context.Context, netip string, cancellation_date string) (*SubnetCancellation, *Response, error) {
data := url.Values{}
data.Set("cancellation_date", cancellation_date)
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("/subnet/%s/cancellation", netip), data)
if err != nil {
return nil, nil, err
}
var body schema.SubnetCancellation
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return SubnetCancellationFromSchema(body), resp, nil
}
func (c *SubnetClient) DeleteCancellation(ctx context.Context, netip string) (*SubnetCancellation, *Response, error) {
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/subnet/%s/cancellation", netip), nil)
if err != nil {
return nil, nil, err
}
var body schema.SubnetCancellation
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return SubnetCancellationFromSchema(body), resp, nil
}
type SubnetOps struct {
TrafficWarnings bool `url:"traffic_warnings"` //enable/disable traffic warnings (true,false)
TrafficHourly string `url:"traffic_hourly"` // hourly traffic limit in MB
TrafficDaily string `url:"traffic_daily"` // daily traffic limit in MB
TrafficMonthly string `url:"traffic_monthly"` // monthly traffic limit in GB
}