forked from netbox-community/go-netbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipam_vlans.go
More file actions
150 lines (130 loc) · 3.72 KB
/
ipam_vlans.go
File metadata and controls
150 lines (130 loc) · 3.72 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
// 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 (
"fmt"
"net/http"
"net/url"
"strconv"
)
// A VLANID is a 12 bit integer VLAN ID. Use its Valid method to determine
// if the contained value is a valid VLAN ID.
type VLANID int
// Valid determines if a VLANID contains a valid VLAN ID.
func (v VLANID) Valid() bool {
// Cannot be less than 0, cannot be greater than 4096.
return v >= 0 && v <= 4096
}
// A VLAN is a Virtual LAN object which can be assigned to a Site.
type VLAN struct {
ID int `json:"id"`
Site *SiteIdentifier `json:"site"`
VID VLANID `json:"vid"`
Name string `json:"name"`
Status Status `json:"status"`
Role *RoleIdentifier `json:"role"`
DisplayName string `json:"display_name"`
}
// A VLANIdentifier is a reduced version of a VLAN, returned as a nested
// object in some top-level objects. It contains information which can
// be used in subsequent API calls to identify and retrieve a full VLAN.
type VLANIdentifier struct {
ID int `json:"id"`
VID VLANID `json:"vid"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
}
// GetVLAN retrieves a VLAN object from NetBox by its ID.
func (s *IPAMService) GetVLAN(id int) (*VLAN, error) {
req, err := s.c.newRequest(
http.MethodGet,
fmt.Sprintf("/api/ipam/vlans/%d", id),
nil,
)
if err != nil {
return nil, err
}
vlan := new(VLAN)
err = s.c.do(req, vlan)
return vlan, err
}
// ListVLANs retrives a list of VLAN objects from NetBox, filtered according
// to the parameters specified in options.
//
// If options is nil, all VLANs will be retrieved.
func (s *IPAMService) ListVLANs(options *ListVLANsOptions) ([]*VLAN, error) {
req, err := s.c.newRequest(http.MethodGet, "/api/ipam/vlans/", options)
if err != nil {
return nil, err
}
var vlans []*VLAN
err = s.c.do(req, &vlans)
return vlans, err
}
// ListVLANsOptions is used as an argument for Client.IPAM.ListVLANs.
// 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 ListVLANsOptions struct {
Name string
Role []string
RoleID []int
Site []string
SiteID []int
Status []string
StatusID int
VID VLANID
}
// values generates a url.Values map from the data in ListVLANsOptions.
func (o *ListVLANsOptions) values() (url.Values, error) {
if o == nil {
return nil, nil
}
v := url.Values{}
if o.Name != "" {
v.Set("name", o.Name)
}
// IDs should always be preferred over string names
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)
}
}
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.StatusID != 0:
v.Set("status_id", strconv.Itoa(o.StatusID))
case len(o.Status) > 0:
for _, s := range o.Status {
v.Add("status", s)
}
}
if o.VID != 0 {
v.Set("vid", strconv.Itoa(int(o.VID)))
}
return v, nil
}