-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipBlocks.go
143 lines (119 loc) · 4.56 KB
/
ipBlocks.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
package solus
import (
"context"
"fmt"
)
// IPBlocksService handles all available methods with IP blocks.
type IPBlocksService service
// IPBlock represents an IP block.
type IPBlock struct {
ID int `json:"id"`
Name string `json:"name"`
Type IPVersion `json:"type"`
ListType ListType `json:"list_type"`
Gateway string `json:"gateway"`
Netmask string `json:"netmask"`
Ns1 string `json:"ns_1"`
Ns2 string `json:"ns_2"`
From string `json:"from"`
To string `json:"to"`
Subnet int `json:"subnet"`
Range string `json:"range"`
ComputeResources []ComputeResource `json:"compute_resources[]"`
IPs []IPBlockIPAddress `json:"ips[]"`
ReverseDNS IPBlockReverseDNS `json:"reverse_dns"`
}
// IPVersion represents available IP versions.
type IPVersion string
const (
// IPv4 indicates IP v4 address.
IPv4 IPVersion = "IPv4"
// IPv6 indicates IP v6 address.
IPv6 IPVersion = "IPv6"
)
// ListType represents the type of list of IP addresses
type ListType string
const (
// IpBlockListTypeRange indicates a range of IP addresses.
IpBlockListTypeRange ListType = "range"
// IpBlockListTypeSet indicates a set of IP addresses.
IpBlockListTypeSet ListType = "set"
)
// IPBlockRequest represents available properties for creating new or updating
// existing IP block.
type IPBlockRequest struct {
ComputeResources []int `json:"compute_resources,omitempty"`
Name string `json:"name"`
Type IPVersion `json:"type"`
ListType ListType `json:"list_type"`
Gateway string `json:"gateway"`
Ns1 string `json:"ns_1"`
Ns2 string `json:"ns_2"`
ReverseDNS IPBlockReverseDNS `json:"reverse_dns"`
// IPv4 related fields
Netmask string `json:"netmask,omitempty"`
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
// IPv6 related fields
Range string `json:"range,omitempty"`
Subnet int `json:"subnet,omitempty"`
}
// IPBlockReverseDNS represents an IP block's reverse DNS settings.
type IPBlockReverseDNS struct {
Zone string `json:"zone"`
Enabled bool `json:"enabled"`
}
// IPBlockIPAddress represents an IP block's IP address.
type IPBlockIPAddress struct {
ID int `json:"id"`
IP string `json:"ip"`
IPBlock IPBlock `json:"ip_block"`
}
// IPBlocksResponse represents paginated list of IP blocks.
// This cursor can be used for iterating over all available IP blocks.
type IPBlocksResponse struct {
paginatedResponse
Data []IPBlock `json:"data"`
}
type ipBlockResponse struct {
Data IPBlock `json:"data"`
}
// List lists IP blocks.
func (s *IPBlocksService) List(ctx context.Context, filter *FilterIPBlocks) (IPBlocksResponse, error) {
resp := IPBlocksResponse{
paginatedResponse: paginatedResponse{
service: (*service)(s),
},
}
return resp, s.client.list(ctx, "ip_blocks", &resp, withFilter(filter.data))
}
// Get gets specified IP block.
func (s *IPBlocksService) Get(ctx context.Context, id int) (IPBlock, error) {
var resp ipBlockResponse
return resp.Data, s.client.get(ctx, fmt.Sprintf("ip_blocks/%d", id), &resp)
}
// Create creates new IP block.
func (s *IPBlocksService) Create(ctx context.Context, data IPBlockRequest) (IPBlock, error) {
var resp ipBlockResponse
return resp.Data, s.client.create(ctx, "ip_blocks", data, &resp)
}
// Update updates specified IP block.
func (s *IPBlocksService) Update(ctx context.Context, id int, data IPBlockRequest) (IPBlock, error) {
var resp ipBlockResponse
return resp.Data, s.client.update(ctx, fmt.Sprintf("ip_blocks/%d", id), data, &resp)
}
// Delete deletes specified IP block.
func (s *IPBlocksService) Delete(ctx context.Context, id int) error {
return s.client.syncDelete(ctx, fmt.Sprintf("ip_blocks/%d", id))
}
// IPAddressCreate creates a new IP address in the specified IP block.
func (s *IPBlocksService) IPAddressCreate(ctx context.Context, ipBlockID int) (IPBlockIPAddress, error) {
var resp struct {
Data IPBlockIPAddress `json:"data"`
}
return resp.Data, s.client.create(ctx, fmt.Sprintf("ip_blocks/%d/ips", ipBlockID), nil, &resp)
}
// IPAddressDelete deletes a provided IP address in the specified IP block.
func (s *IPBlocksService) IPAddressDelete(ctx context.Context, id int) error {
return s.client.syncDelete(ctx, fmt.Sprintf("ips/%d", id))
}