add ListInterface() method.

This commit is contained in:
Kalle Carlbark 2019-09-11 11:04:53 +02:00
parent 491acef347
commit 45da04bec7
No known key found for this signature in database
GPG key ID: 3FC0C93C5A5A0670

View file

@ -5,9 +5,11 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/gorilla/schema"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"net/url"
"time"
)
@ -82,7 +84,44 @@ type Dcim_Interfaces_List struct {
} `json:"results,omitempty"`
}
func (i *Dcim_Interfaces_Create) CreateInterface(n *NetBox) error {
type InterfaceFilter struct {
Offset int64 `schema:"offset"`
Limit int64 `schema:"limit"`
//User specific filters
Id string `schema:"id"`
Name string `schema:"name"`
ConnectionStatus string `schema:"connection_status"`
Type string `schema:"type"`
Mtu string `schema:"mtu"`
MgmtOnly string `schema:"mgmt_only"`
Mode string `schema:"mode"`
Description string `schema:description"`
Q string `schema:"q"`
Device string `schema:"device"`
DeviceId string `schema:"device_id"`
Cabled string `schema:cabled"`
Kind string `schema:Kind"`
LagId string `schema:"lag_id"`
MacAddress string `schema:"mac_address"`
Tag string `schema:"tag"`
VlanId string `schema:"vlan_id"`
Vlan string `schema:"vlan"`
}
func (i *Dcim_Interfaces_List) ListInterfaces(n *NetBox, f *InterfaceFilter) error {
var encoder = schema.NewEncoder()
form := url.Values{}
err := encoder.Encode(f, form)
if err != nil {
return err
}
fmt.Println(form)
return nil
}
func (i *Dcim_Interfaces_Create) CreateInterfaces(n *NetBox) error {
interfaceData, err := json.Marshal(i)
if err != nil {
return err
@ -103,24 +142,15 @@ func (i *Dcim_Interfaces_Create) CreateInterface(n *NetBox) error {
request.Header.Add("Accept", "application/json")
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Authorization", " Token "+n.Token)
fmt.Println(request.Header)
response, err := client.Do(request)
if err != nil {
return err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
return err
}
if response.StatusCode == http.StatusCreated {
fmt.Println(string(data))
return nil
} else {
fmt.Println(string(data))
return errors.Errorf("Response was: %d\n", response.StatusCode)
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusCreated)
}
return nil
}