netboxgo/interfaces.go

255 lines
7.6 KiB
Go
Raw Normal View History

2019-09-11 20:12:42 +02:00
package netboxgo
2019-09-10 22:32:20 +02:00
import (
2021-11-26 11:09:27 +01:00
"context"
"fmt"
2019-09-10 22:32:20 +02:00
"net/http"
2019-09-11 11:04:53 +02:00
"net/url"
"time"
2021-11-26 11:09:27 +01:00
// "fmt"
2019-09-17 09:53:24 +02:00
"github.com/gorilla/schema"
2019-09-10 22:32:20 +02:00
)
2019-09-10 22:05:25 +02:00
2021-11-26 11:09:27 +01:00
type InterfacesService service
// NewInterface is used for creating a new interface
2021-11-26 11:09:27 +01:00
type NewInterface struct {
MacAddress string `json:"mac_address,omitempty"`
Name string `json:"name"`
Cable struct {
2019-09-11 09:19:24 +02:00
Label string `json:"label,omitempty"`
} `json:"cable,omitempty"`
Description string `json:"description,omitempty"`
TaggedVLANs []int `json:"tagged_vlans,omitempty"`
Tags []string `json:"tags,omitempty"`
Device int `json:"device"`
Mtu int `json:"mtu,omitempty"`
Lag int `json:"lag,omitempty"`
UntaggedVLAN int `json:"untagged_vlan,omitempty"`
Type int `json:"type,omitempty"`
Mode int `json:"mode,omitempty"`
ConnectionStatus bool `json:"connection_status,omitempty"`
Enabled bool `json:"enabled,omitempty"`
MgmtOnly bool `json:"mgmt_only,omitempty"`
2019-09-10 22:05:25 +02:00
}
// Interfaces is a list of interfaces
2021-11-26 11:09:27 +01:00
type Interfaces struct {
2019-09-11 09:19:24 +02:00
Next interface{} `json:"next,omitempty"`
Previous interface{} `json:"previous,omitempty"`
Results []Interface `json:"results,omitempty"`
Count int `json:"count,omitempty"`
}
// Interface is one interface
type Interface struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Device struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
} `json:"device"`
Name string `json:"name"`
Label string `json:"label"`
Type struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"type"`
Enabled bool `json:"enabled"`
Parent struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Device struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
} `json:"device"`
Name string `json:"name"`
Cable int `json:"cable"`
Occupied string `json:"_occupied"`
} `json:"parent"`
Bridge struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Device struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
} `json:"device"`
Name string `json:"name"`
Cable int `json:"cable"`
Occupied string `json:"_occupied"`
} `json:"bridge"`
Lag struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Device struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
} `json:"device"`
Name string `json:"name"`
Cable int `json:"cable"`
Occupied string `json:"_occupied"`
} `json:"lag"`
Mtu int `json:"mtu"`
MacAddress string `json:"mac_address"`
Wwn string `json:"wwn"`
MgmtOnly bool `json:"mgmt_only"`
Description string `json:"description"`
Mode struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"mode"`
RFRole struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"rf_role"`
RFChannel struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"rf_channel"`
RFChannelFrequency int `json:"rf_channel_frequency"`
RFChannelWidth int `json:"rf_channel_width"`
TXPower int `json:"tx_power"`
UntaggedVlan struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Vid int `json:"vid"`
Name string `json:"name"`
} `json:"untagged_vlan"`
TaggedVlans []struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Vid int `json:"vid"`
Name string `json:"name"`
} `json:"tagged_vlans"`
MarkConnected bool `json:"mark_connected"`
Cable struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Label string `json:"label"`
} `json:"cable"`
WirelessLink struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Ssid string `json:"ssid"`
} `json:"wireless_link"`
LinkPeer interface{} `json:"link_peer"`
LinkPeerType string `json:"link_peer_type"`
WirelessLans []struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Ssid string `json:"ssid"`
} `json:"wireless_lans"`
ConnectedEndpoint interface{} `json:"connected_endpoint"`
ConnectedEndpointType string `json:"connected_endpoint_type"`
ConnectedEndpointReachable bool `json:"connected_endpoint_reachable"`
Tags []struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
} `json:"tags"`
CustomFields struct{} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
CountIpaddresses int `json:"count_ipaddresses"`
CountFhrpGroups int `json:"count_fhrp_groups"`
Occupied bool `json:"_occupied"`
2019-09-10 22:05:25 +02:00
}
2019-09-17 09:53:24 +02:00
// InterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list
2019-09-11 11:04:53 +02:00
type InterfaceFilter struct {
2021-11-26 11:09:27 +01:00
// User specific filters
2019-09-17 09:53:24 +02:00
ID string `schema:"id,omitempty"`
2019-09-11 11:17:20 +02:00
Name string `schema:"name,omitempty"`
ConnectionStatus string `schema:"connection_status,omitempty"`
Type string `schema:"type,omitempty"`
Mtu string `schema:"mtu,omitempty"`
2019-09-11 11:18:45 +02:00
MgmtOnly string `schema:"mgmt_only,omitempty"`
Mode string `schema:"mode,omitempty"`
Description string `schema:"description,omitempty"`
Q string `schema:"q,omitempty"`
Device string `schema:"device,omitempty"`
2019-09-17 09:53:24 +02:00
DeviceID string `schema:"device_id,omitempty"`
2019-09-11 11:18:45 +02:00
Cabled string `schema:"cabled,omitempty"`
Kind string `schema:"kind,omitempty"`
2019-09-17 09:53:24 +02:00
LagID string `schema:"lag_id,omitempty"`
2019-09-11 11:18:45 +02:00
MacAddress string `schema:"mac_address,omitempty"`
Tag string `schema:"tag,omitempty"`
2020-03-02 10:00:40 +01:00
VLANID string `schema:"vlan_id,omitempty"`
VLAN string `schema:"vlan,omitempty"`
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
2019-09-11 11:04:53 +02:00
}
const interfacesPath = dcimPath + "/interfaces"
2019-09-11 11:04:53 +02:00
2021-11-26 11:09:27 +01:00
// List devices. DeviceFilter is used to list based on filter queries.
func (s *InterfacesService) List(ctx context.Context, f *InterfaceFilter) (*Interfaces, error) {
var interfaces Interfaces
var query string
var req *http.Request
var err error
2019-09-11 11:39:31 +02:00
2021-11-26 11:09:27 +01:00
encoder := schema.NewEncoder()
2019-09-11 11:39:31 +02:00
2021-11-26 11:09:27 +01:00
form := url.Values{}
err = encoder.Encode(f, form)
2019-09-11 11:39:31 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &interfaces, err
2019-09-11 11:39:31 +02:00
}
2021-11-26 11:09:27 +01:00
query = form.Encode()
2019-09-11 11:39:31 +02:00
2021-11-26 11:09:27 +01:00
req, err = s.client.newRequest(ctx, "GET", interfacesPath, query, nil)
2019-09-17 09:53:24 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &interfaces, err
2019-09-17 09:53:24 +02:00
}
2021-11-26 11:09:27 +01:00
_, err = s.client.do(req, &interfaces)
2019-09-11 11:39:31 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &interfaces, err
2019-09-11 11:39:31 +02:00
}
2021-11-26 11:09:27 +01:00
return &interfaces, nil
2019-09-11 11:04:53 +02:00
}
2021-11-26 11:09:27 +01:00
// Create a interfaces
func (s *InterfacesService) Create(ctx context.Context, c *NewInterface) (*Interface, error) {
2021-11-26 11:09:27 +01:00
var err error
var req *http.Request
var nic Interface
2021-11-26 11:09:27 +01:00
req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c)
2019-09-10 22:32:20 +02:00
if err != nil {
return &nic, fmt.Errorf("unable to create request: %w", err)
2019-09-10 22:32:20 +02:00
}
2021-11-26 11:09:27 +01:00
_, err = s.client.do(req, &nic)
2019-09-17 09:53:24 +02:00
if err != nil {
return &nic, fmt.Errorf("unable to do request: %w", err)
2019-09-17 09:53:24 +02:00
}
2019-09-10 22:05:25 +02:00
return &nic, nil
2019-09-10 22:32:20 +02:00
}