2019-09-10 22:32:20 +02:00
|
|
|
package netboxclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
2019-09-10 22:05:25 +02:00
|
|
|
|
|
|
|
type dcim_interfaces_create struct {
|
|
|
|
Device int `json:"device"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type int `json:"type"`
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
Lag int `json:"lag"`
|
|
|
|
Mtu int `json:"mtu"`
|
|
|
|
MacAddress string `json:"mac_address"`
|
|
|
|
MgmtOnly bool `json:"mgmt_only"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
ConnectionStatus bool `json:"connection_status"`
|
|
|
|
Cable struct {
|
|
|
|
Label string `json:"label"`
|
|
|
|
} `json:"cable"`
|
|
|
|
Mode int `json:"mode"`
|
|
|
|
UntaggedVlan int `json:"untagged_vlan"`
|
|
|
|
TaggedVlans []int `json:"tagged_vlans"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type dcim_interfaces_list struct {
|
|
|
|
Count int `json:"count"`
|
|
|
|
Next interface{} `json:"next"`
|
|
|
|
Previous interface{} `json:"previous"`
|
|
|
|
Results []struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Device struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
} `json:"device"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type struct {
|
|
|
|
Value int `json:"value"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
} `json:"type"`
|
|
|
|
FormFactor struct {
|
|
|
|
Value int `json:"value"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
} `json:"form_factor"`
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
Lag struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Device struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
} `json:"device"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Cable interface{} `json:"cable"`
|
|
|
|
ConnectionStatus interface{} `json:"connection_status"`
|
|
|
|
} `json:"lag"`
|
|
|
|
Mtu interface{} `json:"mtu"`
|
|
|
|
MacAddress interface{} `json:"mac_address"`
|
|
|
|
MgmtOnly bool `json:"mgmt_only"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
ConnectedEndpointType interface{} `json:"connected_endpoint_type"`
|
|
|
|
ConnectedEndpoint interface{} `json:"connected_endpoint"`
|
|
|
|
ConnectionStatus interface{} `json:"connection_status"`
|
|
|
|
Cable interface{} `json:"cable"`
|
|
|
|
Mode interface{} `json:"mode"`
|
|
|
|
UntaggedVlan interface{} `json:"untagged_vlan"`
|
|
|
|
TaggedVlans []interface{} `json:"tagged_vlans"`
|
|
|
|
Tags []interface{} `json:"tags"`
|
|
|
|
CountIpaddresses int `json:"count_ipaddresses"`
|
|
|
|
} `json:"results"`
|
|
|
|
}
|
|
|
|
|
2019-09-10 22:32:20 +02:00
|
|
|
func (i *dcim_interfaces_create) CreateInterface(n *NetBox) error {
|
|
|
|
interfaceData, err := json.Marshal(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
timeout := time.Duration(60 * time.Second)
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
|
|
|
request, err := http.NewRequest("POST", n.RootURL+"/dcim/interfaces/", bytes.NewBuffer(interfaceData))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
request.Header.Add("Content-Type", "application/json")
|
|
|
|
response, err := client.Do(request)
|
|
|
|
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
|
|
|
|
} else {
|
|
|
|
return errors.Errorf("Response was: %d\n", response.StatusCode)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-10 22:05:25 +02:00
|
|
|
|
2019-09-10 22:32:20 +02:00
|
|
|
func (i *dcim_interface_list) ListInterface(n *NetBox) error {
|