2019-09-11 09:19:24 +02:00
|
|
|
package nbclient
|
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-11 09:32:15 +02:00
|
|
|
"fmt"
|
2019-09-10 22:32:20 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
2019-09-10 22:05:25 +02:00
|
|
|
|
2019-09-11 09:25:23 +02:00
|
|
|
type Dcim_Interfaces_Create 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"`
|
|
|
|
UntaggedVlan int `json:"untagged_vlan,omitempty"`
|
|
|
|
TaggedVlans []int `json:"tagged_vlans,omitempty"`
|
|
|
|
Tags []string `json:"tags,omitempty"`
|
2019-09-10 22:05:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:23 +02:00
|
|
|
type Dcim_Interfaces_List 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"`
|
|
|
|
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"`
|
2019-09-10 22:05:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-11 09:25:23 +02:00
|
|
|
func (i *Dcim_Interfaces_Create) CreateInterface(n *NetBox) 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+"/dcim/interfaces/", bytes.NewBuffer(interfaceData))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
request.Header.Add("Content-Type", "application/json")
|
2019-09-11 09:19:24 +02:00
|
|
|
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-10 22:05:25 +02:00
|
|
|
|
2019-09-10 22:32:20 +02:00
|
|
|
if response.StatusCode == http.StatusCreated {
|
2019-09-11 09:32:51 +02:00
|
|
|
fmt.Println(response.Body)
|
2019-09-10 22:32:20 +02:00
|
|
|
return nil
|
|
|
|
} else {
|
2019-09-11 09:39:44 +02:00
|
|
|
fmt.Println(response.Body)
|
2019-09-10 22:32:20 +02:00
|
|
|
return errors.Errorf("Response was: %d\n", response.StatusCode)
|
|
|
|
}
|
2019-09-11 09:32:51 +02:00
|
|
|
return nil
|
|
|
|
|
2019-09-10 22:32:20 +02:00
|
|
|
}
|