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"
|
2022-02-14 11:09:50 +01:00
|
|
|
"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
|
|
|
|
|
2022-02-14 11:09:50 +01:00
|
|
|
// NewInterface is used for creating a new interface
|
2021-11-26 11:09:27 +01:00
|
|
|
type NewInterface struct {
|
2022-02-14 11:21:57 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-14 11:09:50 +01: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"`
|
2022-02-14 11:09:50 +01:00
|
|
|
Results []Interface `json:"results,omitempty"`
|
|
|
|
Count int `json:"count,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface is one interface
|
|
|
|
type Interface struct {
|
2022-02-14 11:21:57 +01:00
|
|
|
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 {
|
2022-02-14 11:09:50 +01:00
|
|
|
URL string `json:"url"`
|
|
|
|
Display string `json:"display"`
|
|
|
|
Name string `json:"name"`
|
2022-02-14 11:21:57 +01:00
|
|
|
ID int `json:"id"`
|
2022-02-14 11:09:50 +01:00
|
|
|
} `json:"device"`
|
2022-02-14 11:21:57 +01:00
|
|
|
Cable int `json:"cable"`
|
|
|
|
ID int `json:"id"`
|
2022-02-14 11:09:50 +01:00
|
|
|
} `json:"parent"`
|
2022-02-14 11:21:57 +01:00
|
|
|
Device struct {
|
2022-02-14 11:09:50 +01:00
|
|
|
URL string `json:"url"`
|
|
|
|
Display string `json:"display"`
|
2022-02-14 11:21:57 +01:00
|
|
|
Name string `json:"name"`
|
2022-02-14 11:09:50 +01:00
|
|
|
ID int `json:"id"`
|
2022-02-14 11:21:57 +01:00
|
|
|
} `json:"device"`
|
|
|
|
RFChannel struct {
|
2022-02-14 11:09:50 +01:00
|
|
|
Label string `json:"label"`
|
|
|
|
Value string `json:"value"`
|
2022-02-14 11:21:57 +01:00
|
|
|
} `json:"rf_channel"`
|
2022-02-14 11:09:50 +01:00
|
|
|
RFRole struct {
|
|
|
|
Label string `json:"label"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"rf_role"`
|
2022-02-14 11:21:57 +01:00
|
|
|
Type struct {
|
2022-02-14 11:09:50 +01:00
|
|
|
Label string `json:"label"`
|
|
|
|
Value string `json:"value"`
|
2022-02-14 11:21:57 +01:00
|
|
|
} `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 {
|
2022-02-14 11:09:50 +01:00
|
|
|
URL string `json:"url"`
|
|
|
|
Display string `json:"display"`
|
|
|
|
Label string `json:"label"`
|
2022-02-14 11:21:57 +01:00
|
|
|
ID int `json:"id"`
|
2022-02-14 11:09:50 +01:00
|
|
|
} `json:"cable"`
|
|
|
|
WirelessLink struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Display string `json:"display"`
|
|
|
|
Ssid string `json:"ssid"`
|
|
|
|
ID int `json:"id"`
|
2022-02-14 11:21:57 +01:00
|
|
|
} `json:"wireless_link"`
|
|
|
|
WirelessLANs []struct {
|
2022-02-14 11:09:50 +01:00
|
|
|
URL string `json:"url"`
|
|
|
|
Display string `json:"display"`
|
|
|
|
Ssid string `json:"ssid"`
|
2022-02-14 11:21:57 +01:00
|
|
|
ID int `json:"id"`
|
2022-02-14 11:09:50 +01:00
|
|
|
} `json:"wireless_lans"`
|
2022-02-14 11:21:57 +01:00
|
|
|
TaggedVLANs []struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Display string `json:"display"`
|
|
|
|
Name string `json:"name"`
|
2022-02-14 11:09:50 +01:00
|
|
|
ID int `json:"id"`
|
2022-02-14 11:21:57 +01:00
|
|
|
Vid int `json:"vid"`
|
|
|
|
} `json:"tagged_vlans"`
|
|
|
|
Tags []struct {
|
2022-02-14 11:09:50 +01:00
|
|
|
URL string `json:"url"`
|
|
|
|
Display string `json:"display"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
Color string `json:"color"`
|
2022-02-14 11:21:57 +01:00
|
|
|
ID int `json:"id"`
|
2022-02-14 11:09:50 +01:00
|
|
|
} `json:"tags"`
|
2022-02-14 11:21:57 +01:00
|
|
|
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 {
|
|
|
|
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:"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"`
|
2022-02-14 11:09:50 +01:00
|
|
|
|
|
|
|
Offset int64 `schema:"offset,omitempty"`
|
|
|
|
Limit int64 `schema:"limit,omitempty"`
|
2019-09-11 11:04:53 +02:00
|
|
|
}
|
|
|
|
|
2021-11-27 23:31:44 +01: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
|
2022-02-14 11:09:50 +01:00
|
|
|
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
|
2022-02-14 11:09:50 +01:00
|
|
|
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 {
|
2022-02-14 11:09:50 +01:00
|
|
|
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
|
|
|
|
2022-02-14 11:09:50 +01:00
|
|
|
_, err = s.client.do(req, &nic)
|
2019-09-17 09:53:24 +02:00
|
|
|
if err != nil {
|
2022-02-14 11:09:50 +01:00
|
|
|
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
|
|
|
|
2022-02-14 11:09:50 +01:00
|
|
|
return &nic, nil
|
2019-09-10 22:32:20 +02:00
|
|
|
}
|