forked from netbox-community/go-netbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipam_ipaddresses.go
More file actions
226 lines (197 loc) · 5.7 KB
/
ipam_ipaddresses.go
File metadata and controls
226 lines (197 loc) · 5.7 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2016 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package netbox
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
)
// An IPAddress is an IPv4 or IPv6 address.
type IPAddress struct {
ID int
Family Family
Address *net.IPNet
VRF *VRFIdentifier
Interface *InterfaceIdentifier
Description string
NATInside *IPAddressIdentifier
NATOutside *IPAddressIdentifier
}
// An IPAddressIdentifier is a reduced version of a IPAddress, returned as
// nested object in some top-level objects. It contains information which can
// be used in subsequent API calls to identify and retrieve a full IPAddress.
type IPAddressIdentifier struct {
ID int
Family Family
Address *net.IPNet
}
// GetIPAddress retrieves an IPAddress object from NetBox by its ID.
func (s *IPAMService) GetIPAddress(id int) (*IPAddress, error) {
req, err := s.c.newRequest(
http.MethodGet,
fmt.Sprintf("/api/ipam/ip-addresses/%d", id),
nil,
)
if err != nil {
return nil, err
}
ip := new(IPAddress)
err = s.c.do(req, ip)
return ip, err
}
// ListIPAddresses retrives a list of IPAddress objects from NetBox, filtered according
// to the parameters specified in options.
//
// If options is nil, all IPAddresses will be retrieved.
func (s *IPAMService) ListIPAddresses(options *ListIPAddressesOptions) ([]*IPAddress, error) {
req, err := s.c.newRequest(http.MethodGet, "/api/ipam/ip-addresses/", options)
if err != nil {
return nil, err
}
var ips []*IPAddress
err = s.c.do(req, &ips)
return ips, err
}
// An ipAddress is the raw JSON representation of an IPAddress.
type ipAddress struct {
ID int `json:"id"`
Family Family `json:"family"`
Address string `json:"address"`
VRF *VRFIdentifier `json:"vrf"`
Interface *InterfaceIdentifier `json:"interface"`
Description string `json:"description"`
NATInside *IPAddressIdentifier `json:"nat_inside"`
NATOutside *IPAddressIdentifier `json:"nat_outside"`
}
// MarshalJSON marshals an IPAddress into JSON bytes.
func (ip *IPAddress) MarshalJSON() ([]byte, error) {
return json.Marshal(ipAddress{
ID: ip.ID,
Family: ip.Family,
Address: ip.Address.String(),
VRF: ip.VRF,
Interface: ip.Interface,
Description: ip.Description,
NATInside: ip.NATInside,
NATOutside: ip.NATOutside,
})
}
// UnmarshalJSON unmarshals JSON bytes into an IPAddress, and verifies that
// the contained IP address is valid.
func (ip *IPAddress) UnmarshalJSON(b []byte) error {
var raw ipAddress
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
_, ipNet, err := net.ParseCIDR(raw.Address)
if err != nil {
return err
}
*ip = IPAddress{
ID: raw.ID,
Family: raw.Family,
Address: ipNet,
VRF: raw.VRF,
Interface: raw.Interface,
Description: raw.Description,
NATInside: raw.NATInside,
NATOutside: raw.NATOutside,
}
return nil
}
// An ipAddressIdentifier is the raw JSON representation of an IPAddressIdentifier.
type ipAddressIdentifier struct {
ID int `json:"id"`
Family Family `json:"family"`
Address string `json:"address"`
}
// MarshalJSON marshals an IPAddressIdentifier into JSON bytes.
func (ip *IPAddressIdentifier) MarshalJSON() ([]byte, error) {
return json.Marshal(ipAddressIdentifier{
ID: ip.ID,
Family: ip.Family,
Address: ip.Address.String(),
})
}
// UnmarshalJSON unmarshals JSON bytes into an IPAddressIdentifier, and verifies that
// the contained IP address is valid.
func (ip *IPAddressIdentifier) UnmarshalJSON(b []byte) error {
var raw ipAddressIdentifier
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
_, ipNet, err := net.ParseCIDR(raw.Address)
if err != nil {
return err
}
*ip = IPAddressIdentifier{
ID: raw.ID,
Family: raw.Family,
Address: ipNet,
}
return nil
}
// ListIPAddressesOptions is used as an argument for Client.IPAM.ListIPAddresses.
// Integer fields with an *ID suffix are preferred over their string
// counterparts, and if both are set, only the *ID field will be used.
type ListIPAddressesOptions struct {
Family Family
VRFID []int
VRF string
InterfaceID []int
DeviceID []int
Device []string
// Query is a special option which enables free-form search.
// For example, Query could be an IP address such as "8.8.8.8".
Query string
}
// values generates a url.Values map from the data in ListIPAddressesOptions.
func (o *ListIPAddressesOptions) values() (url.Values, error) {
if o == nil {
return nil, nil
}
v := url.Values{}
if o.Family != 0 {
v.Set("family", strconv.Itoa(int(o.Family)))
}
for _, i := range o.InterfaceID {
v.Add("interface_id", strconv.Itoa(i))
}
// IDs should always be preferred over string names
switch {
case len(o.VRFID) > 0:
for _, vid := range o.VRFID {
v.Add("vrf_id", strconv.Itoa(vid))
}
case o.VRF != "":
v.Set("vrf", o.VRF)
}
switch {
case len(o.DeviceID) > 0:
for _, d := range o.DeviceID {
v.Add("device_id", strconv.Itoa(d))
}
case len(o.Device) > 0:
for _, d := range o.Device {
v.Add("device", d)
}
}
if o.Query != "" {
v.Set("q", o.Query)
}
return v, nil
}