netboxgo/interfaces.go
Kalle Carlbark 236a9bb23e
When creating objects return the created object
TODO: Do this for all objects
2022-02-14 11:09:50 +01:00

254 lines
7.6 KiB
Go

package netboxgo
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
// "fmt"
"github.com/gorilla/schema"
)
type InterfacesService service
// NewInterface is used for creating a new interface
type NewInterface struct {
MacAddress string `json:"mac_address,omitempty"`
Name string `json:"name"`
Cable struct {
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"`
}
// Interfaces is a list of interfaces
type Interfaces struct {
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"`
}
// InterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list
type InterfaceFilter 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 interfacesPath = dcimPath + "/interfaces"
// 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
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", interfacesPath, 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 *InterfacesService) Create(ctx context.Context, c *NewInterface) (*Interface, error) {
var err error
var req *http.Request
var nic Interface
req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", 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
}