add ListInterface() method.
This commit is contained in:
parent
491acef347
commit
45da04bec7
1 changed files with 41 additions and 11 deletions
|
@ -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
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue