netboxgo/interfaces.go

289 lines
8.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 {
CustomFields interface{} `json:"custom_fields"`
WWN string `json:"wwn"`
Label string `json:"label"`
Type string `json:"type"`
Cable struct {
Label string `json:"label"`
} `json:"cable"`
RFChannel string `json:"rf_channel"`
RFRole string `json:"rf_role"`
Name string `json:"name"`
Description string `json:"description"`
MACAddress string `json:"mac_address"`
Mode string `json:"mode"`
TaggedVLANs []int `json:"tagged_vlans"`
WirelessLANs []int `json:"wireless_lans"`
Tags []struct {
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
} `json:"tags"`
Device int `json:"device"`
Parent int `json:"parent"`
RFChannelFrequency int `json:"rf_channel_frequency"`
RFChannelWidth int `json:"rf_channel_width"`
TXPower int `json:"tx_power"`
UntaggedVLAN int `json:"untagged_vlan"`
Bridge int `json:"bridge"`
LAG int `json:"lag"`
MTU int `json:"mtu"`
WirelessLink int `json:"wireless_link"`
Enabled bool `json:"enabled"`
MarkConnected bool `json:"mark_connected"`
MGMTOnly bool `json:"mgmt_only"`
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 {
CustomFields struct{} `json:"custom_fields"`
LastUpdated time.Time `json:"last_updated"`
LinkPeer interface{} `json:"link_peer"`
ConnectedEndpoint interface{} `json:"connected_endpoint"`
Parent struct {
Occupied string `json:"_occupied"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Device struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"device"`
Cable int `json:"cable"`
ID int `json:"id"`
} `json:"parent"`
Device struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"device"`
RFChannel struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"rf_channel"`
RFRole struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"rf_role"`
Type struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"type"`
Mode struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"mode"`
Display string `json:"display"`
LinkPeerType string `json:"link_peer_type"`
MACAddress string `json:"mac_address"`
WWN string `json:"wwn"`
URL string `json:"url"`
Description string `json:"description"`
ConnectedEndpointType string `json:"connected_endpoint_type"`
Label string `json:"label"`
Name string `json:"name"`
Created string `json:"created"`
Cable struct {
URL string `json:"url"`
Display string `json:"display"`
Label string `json:"label"`
ID int `json:"id"`
} `json:"cable"`
WirelessLink struct {
URL string `json:"url"`
Display string `json:"display"`
Ssid string `json:"ssid"`
ID int `json:"id"`
} `json:"wireless_link"`
WirelessLANs []struct {
URL string `json:"url"`
Display string `json:"display"`
Ssid string `json:"ssid"`
ID int `json:"id"`
} `json:"wireless_lans"`
TaggedVLANs []struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
Vid int `json:"vid"`
} `json:"tagged_vlans"`
Tags []struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
ID int `json:"id"`
} `json:"tags"`
UntaggedVLAN struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
Vid int `json:"vid"`
} `json:"untagged_vlan"`
LAG struct {
2022-02-14 13:33:39 +01:00
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Device struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"device"`
2022-02-14 13:33:39 +01:00
ID int `json:"id"`
Cable int `json:"cable"`
Occupied bool `json:"_occupied"`
} `json:"lag"`
Bridge struct {
Occupied string `json:"_occupied"`
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
Device struct {
URL string `json:"url"`
Display string `json:"display"`
Name string `json:"name"`
ID int `json:"id"`
} `json:"device"`
Cable int `json:"cable"`
ID int `json:"id"`
} `json:"bridge"`
CountIPAddresses int `json:"count_ipaddresses"`
MTU int `json:"mtu"`
TXPower int `json:"tx_power"`
CountFHRPGroups int `json:"count_fhrp_groups"`
RFChannelFrequency int `json:"rf_channel_frequency"`
ID int `json:"id"`
RFChannelWidth int `json:"rf_channel_width"`
ConnectedEndpointReachable bool `json:"connected_endpoint_reachable"`
MarkConnected bool `json:"mark_connected"`
MGMTOnly bool `json:"mgmt_only"`
Enabled bool `json:"enabled"`
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
}
2022-02-14 12:19:02 +01:00
// Delete a interface
func (s *InterfacesService) Delete(ctx context.Context, i string) error {
var err error
var req *http.Request
req, err = s.client.newRequest(ctx, "DELETE", interfacesPath+"/"+i+"/", "", nil)
if err != nil {
return fmt.Errorf("unable to create request: %w", err)
}
_, err = s.client.do(req, nil)
if err != nil {
return fmt.Errorf("unable to do request: %w", err)
}
return nil
}