forked from netbox-community/go-netbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipam_prefixes.go
More file actions
213 lines (187 loc) · 4.92 KB
/
ipam_prefixes.go
File metadata and controls
213 lines (187 loc) · 4.92 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
// 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"
)
// A Prefix is an IPv4 or IPv6 address prefix.
type Prefix struct {
ID int
Family Family
Prefix *net.IPNet
Site *SiteIdentifier
VRF *VRFIdentifier
VLAN *VLANIdentifier
Status Status
Role *RoleIdentifier
Description string
}
// GetPrefix retrieves a Prefix object from NetBox by its ID.
func (s *IPAMService) GetPrefix(id int) (*Prefix, error) {
req, err := s.c.newRequest(
http.MethodGet,
fmt.Sprintf("/api/ipam/prefixes/%d", id),
nil,
)
if err != nil {
return nil, err
}
p := new(Prefix)
err = s.c.do(req, p)
return p, err
}
// ListPrefixes retrives a list of Prefix objects from NetBox, filtered
// according to the parameters specified in options.
//
// If options is nil, all Prefixes will be retrieved.
func (s *IPAMService) ListPrefixes(options *ListPrefixesOptions) ([]*Prefix, error) {
req, err := s.c.newRequest(http.MethodGet, "/api/ipam/prefixes/", options)
if err != nil {
return nil, err
}
var ps []*Prefix
err = s.c.do(req, &ps)
return ps, err
}
// ListPrefixesOptions is used as an argument for Client.IPAM.ListPrefixes.
// Integer fields with an *ID suffix are preferred over their string
// counterparts, and if both are set, only the *ID field will be used.
// In addition, VLANID is preferred over the site-specific VLANVID.
type ListPrefixesOptions struct {
Family Family
SiteID []int
Site []string
VRFID int
VRF string
VLANID []int
VLANVID VLANID
Status int
RoleID []int
Role []string
Parent *net.IPNet
// 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 ListPrefixesOptions.
func (o *ListPrefixesOptions) 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)))
}
// IDs should always be preferred over string names
switch {
case len(o.SiteID) > 0:
for _, s := range o.SiteID {
v.Add("site_id", strconv.Itoa(s))
}
case len(o.Site) > 0:
for _, s := range o.Site {
v.Add("site", s)
}
}
switch {
case o.VRFID != 0:
v.Set("vrf_id", strconv.Itoa(o.VRFID))
case o.VRF != "":
v.Set("vrf", o.VRF)
}
// Also prefer VLAN's NetBox ID over its VLAN VID
switch {
case len(o.VLANID) > 0:
for _, vid := range o.VLANID {
v.Add("vlan_id", strconv.Itoa(vid))
}
case o.VLANVID != 0:
v.Set("vlan_vid", strconv.Itoa(int(o.VLANVID)))
}
if o.Status != 0 {
v.Set("status", strconv.Itoa(o.Status))
}
switch {
case len(o.RoleID) > 0:
for _, r := range o.RoleID {
v.Add("role_id", strconv.Itoa(r))
}
case len(o.Role) > 0:
for _, r := range o.Role {
v.Add("role", r)
}
}
if o.Parent != nil {
v.Set("parent", o.Parent.String())
}
if o.Query != "" {
v.Set("q", o.Query)
}
return v, nil
}
// A prefix is the raw JSON representation of a Prefix.
type prefix struct {
ID int `json:"id"`
Family Family `json:"family"`
Prefix string `json:"prefix"`
Site *SiteIdentifier `json:"site"`
VRF *VRFIdentifier `json:"vrf"`
VLAN *VLANIdentifier `json:"vlan"`
Status Status `json:"status"`
Role *RoleIdentifier `json:"role"`
Description string `json:"description"`
}
// MarshalJSON marshals an Prefix into JSON bytes.
func (p *Prefix) MarshalJSON() ([]byte, error) {
return json.Marshal(prefix{
ID: p.ID,
Family: p.Family,
Prefix: p.Prefix.String(),
Site: p.Site,
VRF: p.VRF,
VLAN: p.VLAN,
Status: p.Status,
Role: p.Role,
Description: p.Description,
})
}
// UnmarshalJSON unmarshals JSON bytes into an Prefix, and verifies that
// the contained IP address is valid.
func (p *Prefix) UnmarshalJSON(b []byte) error {
var raw prefix
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
_, prefix, err := net.ParseCIDR(raw.Prefix)
if err != nil {
return err
}
*p = Prefix{
ID: raw.ID,
Family: raw.Family,
Prefix: prefix,
Site: raw.Site,
VRF: raw.VRF,
VLAN: raw.VLAN,
Status: raw.Status,
Role: raw.Role,
Description: raw.Description,
}
return nil
}