netboxgo/dcim_interfaces.go

289 lines
8.7 KiB
Go

package netboxgo
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
// "fmt"
"github.com/gorilla/schema"
)
type DCIMInterfacesService service
// NewDCIMInterface is used for creating a new interface
type NewDCIMInterface 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"`
}
// DCIMInterfaces is a list of interfaces
type DCIMInterfaces struct {
Next interface{} `json:"next,omitempty"`
Previous interface{} `json:"previous,omitempty"`
Results []DCIMInterface `json:"results,omitempty"`
Count int `json:"count,omitempty"`
}
// DCIMInterface is one interface
type DCIMInterface 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 {
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"`
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"`
}
// DCIMInterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list
type DCIMInterfaceFilter struct {
// User specific filters
ID string `schema:"id,omitempty"`
Name string `schema:"name,omitempty"`
ConnectionStatus string `schema:"connection_status,omitempty"`
Type string `schema:"type,omitempty"`
Mtu string `schema:"mtu,omitempty"`
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"`
DeviceID string `schema:"device_id,omitempty"`
Cabled string `schema:"cabled,omitempty"`
Kind string `schema:"kind,omitempty"`
LagID string `schema:"lag_id,omitempty"`
MacAddress string `schema:"mac_address,omitempty"`
Tag string `schema:"tag,omitempty"`
VLANID string `schema:"vlan_id,omitempty"`
VLAN string `schema:"vlan,omitempty"`
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
}
const dcimInterfacesPath = dcimPath + "/interfaces"
// List devices. DeviceFilter is used to list based on filter queries.
func (s *DCIMInterfacesService) List(ctx context.Context, f *DCIMInterfaceFilter) (*DCIMInterfaces, error) {
var interfaces DCIMInterfaces
var query string
var req *http.Request
var err error
encoder := schema.NewEncoder()
form := url.Values{}
err = encoder.Encode(f, form)
if err != nil {
return &interfaces, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", dcimInterfacesPath, query, nil)
if err != nil {
return &interfaces, err
}
_, err = s.client.do(req, &interfaces)
if err != nil {
return &interfaces, err
}
return &interfaces, nil
}
// Create a interfaces
func (s *DCIMInterfacesService) Create(ctx context.Context, c *NewDCIMInterface) (*DCIMInterface, error) {
var err error
var req *http.Request
var nic DCIMInterface
req, err = s.client.newRequest(ctx, "POST", dcimInterfacesPath, "", c)
if err != nil {
return &nic, fmt.Errorf("unable to create request: %w", err)
}
_, err = s.client.do(req, &nic)
if err != nil {
return &nic, fmt.Errorf("unable to do request: %w", err)
}
return &nic, nil
}
// Delete a interface
func (s *DCIMInterfacesService) Delete(ctx context.Context, i string) error {
var err error
var req *http.Request
req, err = s.client.newRequest(ctx, "DELETE", dcimInterfacesPath+"/"+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
}