162 lines
5.6 KiB
Go
162 lines
5.6 KiB
Go
package netboxgo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
// "fmt"
|
|
|
|
"github.com/gorilla/schema"
|
|
)
|
|
|
|
type InterfacesService service
|
|
|
|
// NewInterface is used for the return values from Netbox API dcim_interfaces_create
|
|
type NewInterface struct {
|
|
Device int `json:"device"`
|
|
Name string `json:"name"`
|
|
Type int `json:"type,omitempty"`
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
Lag int `json:"lag,omitempty"`
|
|
Mtu int `json:"mtu,omitempty"`
|
|
MacAddress string `json:"mac_address,omitempty"`
|
|
MgmtOnly bool `json:"mgmt_only,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
ConnectionStatus bool `json:"connection_status,omitempty"`
|
|
Cable struct {
|
|
Label string `json:"label,omitempty"`
|
|
} `json:"cable,omitempty"`
|
|
Mode int `json:"mode,omitempty"`
|
|
UntaggedVLAN int `json:"untagged_vlan,omitempty"`
|
|
TaggedVLANs []int `json:"tagged_vlans,omitempty"`
|
|
Tags []string `json:"tags,omitempty"`
|
|
}
|
|
|
|
// Interfaces is used for the return value from NetBox API dcim_interfaces_list
|
|
type Interfaces struct {
|
|
Count int `json:"count,omitempty"`
|
|
Next interface{} `json:"next,omitempty"`
|
|
Previous interface{} `json:"previous,omitempty"`
|
|
Results []struct {
|
|
ID int `json:"id,omitempty"`
|
|
Device struct {
|
|
ID int `json:"id,omitempty"`
|
|
URL string `json:"url,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
DisplayName string `json:"display_name,omitempty"`
|
|
} `json:"device,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
Type struct {
|
|
Value int `json:"value,omitempty"`
|
|
Label string `json:"label,omitempty"`
|
|
} `json:"type,omitempty"`
|
|
FormFactor struct {
|
|
Value int `json:"value,omitempty"`
|
|
Label string `json:"label,omitempty"`
|
|
} `json:"form_factor,omitempty"`
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
Lag struct {
|
|
ID int `json:"id,omitempty"`
|
|
URL string `json:"url,omitempty"`
|
|
Device struct {
|
|
ID int `json:"id,omitempty"`
|
|
URL string `json:"url,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
DisplayName string `json:"display_name,omitempty"`
|
|
} `json:"device,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
Cable interface{} `json:"cable,omitempty"`
|
|
ConnectionStatus interface{} `json:"connection_status,omitempty"`
|
|
} `json:"lag,omitempty"`
|
|
Mtu interface{} `json:"mtu,omitempty"`
|
|
MacAddress interface{} `json:"mac_address,omitempty"`
|
|
MgmtOnly bool `json:"mgmt_only,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
ConnectedEndpointType interface{} `json:"connected_endpoint_type,omitempty"`
|
|
ConnectedEndpoint interface{} `json:"connected_endpoint,omitempty"`
|
|
ConnectionStatus interface{} `json:"connection_status,omitempty"`
|
|
Cable interface{} `json:"cable,omitempty"`
|
|
Mode interface{} `json:"mode,omitempty"`
|
|
UntaggedVLAN interface{} `json:"untagged_vlan,omitempty"`
|
|
TaggedVLANs []interface{} `json:"tagged_vlans,omitempty"`
|
|
Tags []interface{} `json:"tags,omitempty"`
|
|
CountIpaddresses int `json:"count_ipaddresses,omitempty"`
|
|
} `json:"results,omitempty"`
|
|
}
|
|
|
|
// InterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list
|
|
type InterfaceFilter struct {
|
|
Offset int64 `schema:"offset,omitempty"`
|
|
Limit int64 `schema:"limit,omitempty"`
|
|
|
|
// 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"`
|
|
}
|
|
|
|
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) error {
|
|
var err error
|
|
var req *http.Request
|
|
|
|
req, err = s.client.newRequest(ctx, "POST", interfacesPath, "", c)
|
|
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
|
|
}
|