netboxgo/netbox_interfaces.go

203 lines
6.8 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 (
"bytes"
2019-09-11 09:38:29 +02:00
"crypto/tls"
2019-09-10 22:32:20 +02:00
"encoding/json"
2019-09-17 09:53:24 +02:00
2019-09-11 11:39:31 +02:00
// "fmt"
"io/ioutil"
2019-09-10 22:32:20 +02:00
"net/http"
2019-09-11 11:04:53 +02:00
"net/url"
2019-09-10 22:32:20 +02:00
"time"
2019-09-17 09:53:24 +02:00
"github.com/gorilla/schema"
"github.com/pkg/errors"
2019-09-10 22:32:20 +02:00
)
2019-09-10 22:05:25 +02:00
2019-09-17 09:53:24 +02:00
// DcimInterfacesCreate is used for the return values from Netbox API dcim_interfaces_create
type DcimInterfacesCreate struct {
2019-09-10 22:05:25 +02:00
Device int `json:"device"`
Name string `json:"name"`
2019-09-11 09:19:24 +02:00
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"`
2019-09-10 22:05:25 +02:00
Cable struct {
2019-09-11 09:19:24 +02:00
Label string `json:"label,omitempty"`
} `json:"cable,omitempty"`
Mode int `json:"mode,omitempty"`
2020-03-02 10:00:40 +01:00
UntaggedVLAN int `json:"untagged_vlan,omitempty"`
TaggedVLANs []int `json:"tagged_vlans,omitempty"`
2019-09-11 09:19:24 +02:00
Tags []string `json:"tags,omitempty"`
2019-09-10 22:05:25 +02:00
}
2019-09-17 09:53:24 +02:00
// DcimInterfacesList is used for the return value from NetBox API dcim_interfaces_list
type DcimInterfacesList struct {
2019-09-11 09:19:24 +02:00
Count int `json:"count,omitempty"`
Next interface{} `json:"next,omitempty"`
Previous interface{} `json:"previous,omitempty"`
2019-09-10 22:05:25 +02:00
Results []struct {
2019-09-11 09:19:24 +02:00
ID int `json:"id,omitempty"`
2019-09-10 22:05:25 +02:00
Device struct {
2019-09-11 09:19:24 +02:00
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"`
2019-09-10 22:05:25 +02:00
Type struct {
2019-09-11 09:19:24 +02:00
Value int `json:"value,omitempty"`
Label string `json:"label,omitempty"`
} `json:"type,omitempty"`
2019-09-10 22:05:25 +02:00
FormFactor struct {
2019-09-11 09:19:24 +02:00
Value int `json:"value,omitempty"`
Label string `json:"label,omitempty"`
} `json:"form_factor,omitempty"`
Enabled bool `json:"enabled,omitempty"`
2019-09-10 22:05:25 +02:00
Lag struct {
2019-09-11 09:19:24 +02:00
ID int `json:"id,omitempty"`
URL string `json:"url,omitempty"`
2019-09-10 22:05:25 +02:00
Device struct {
2019-09-11 09:19:24 +02:00
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"`
2020-03-02 10:00:40 +01:00
UntaggedVLAN interface{} `json:"untagged_vlan,omitempty"`
TaggedVLANs []interface{} `json:"tagged_vlans,omitempty"`
2019-09-11 09:19:24 +02:00
Tags []interface{} `json:"tags,omitempty"`
CountIpaddresses int `json:"count_ipaddresses,omitempty"`
} `json:"results,omitempty"`
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 {
2019-09-11 11:19:36 +02:00
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
2019-09-11 11:04:53 +02: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"`
2019-09-11 11:04:53 +02:00
}
2019-09-17 09:53:24 +02:00
// ListInterfaces returns Netbox dcim_interfaces_list
func (n *NetBox) ListInterfaces(i *DcimInterfacesList, f *InterfaceFilter) error {
2019-09-11 11:04:53 +02:00
var encoder = schema.NewEncoder()
form := url.Values{}
err := encoder.Encode(f, form)
if err != nil {
return err
}
2019-09-11 11:16:13 +02:00
query := form.Encode()
2019-09-11 11:39:31 +02:00
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
}
timeout := time.Duration(60 * time.Second)
client := &http.Client{
Timeout: timeout,
Transport: transport,
}
request, err := http.NewRequest("GET", n.RootURL+"/api/dcim/interfaces/?"+query, nil)
if err != nil {
return err
}
request.Header.Add("Accept", "application/json")
request.Header.Add("Authorization", " Token "+n.Token)
response, err := client.Do(request)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusOK)
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
2019-09-17 09:53:24 +02:00
err = response.Body.Close()
if err != nil {
return err
}
2019-09-11 11:39:31 +02:00
err = json.Unmarshal(data, &i)
if err != nil {
return err
}
2019-09-11 11:04:53 +02:00
return nil
}
2019-09-17 09:53:24 +02:00
// CreateInterfaces creates interfaces via Netbox API dcim_interfaces_create
func (n *NetBox) CreateInterfaces(i *DcimInterfacesCreate) error {
2019-09-10 22:32:20 +02:00
interfaceData, err := json.Marshal(i)
if err != nil {
return err
}
2019-09-11 09:38:29 +02:00
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
}
2019-09-10 22:32:20 +02:00
timeout := time.Duration(60 * time.Second)
client := &http.Client{
2019-09-11 09:38:29 +02:00
Timeout: timeout,
Transport: transport,
2019-09-10 22:32:20 +02:00
}
request, err := http.NewRequest("POST", n.RootURL+"/api/dcim/interfaces/", bytes.NewBuffer(interfaceData))
2019-09-10 22:32:20 +02:00
if err != nil {
return err
}
2019-09-11 09:55:15 +02:00
request.Header.Add("Accept", "application/json")
2019-09-11 09:59:24 +02:00
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Authorization", " Token "+n.Token)
2019-09-10 22:32:20 +02:00
response, err := client.Do(request)
if err != nil {
return err
}
2019-09-17 09:53:24 +02:00
err = response.Body.Close()
if err != nil {
return err
}
2019-09-10 22:05:25 +02:00
2019-09-10 22:32:20 +02:00
if response.StatusCode == http.StatusCreated {
return nil
}
2019-09-17 09:53:24 +02:00
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusCreated)
2019-09-10 22:32:20 +02:00
}