package nbclient import ( "bytes" "crypto/tls" "encoding/json" "fmt" "github.com/gorilla/schema" "github.com/pkg/errors" "net/http" "net/url" "time" ) type Dcim_Interfaces_Create struct { Device int `json:"device"` Name string `json:"name"` 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"` Cable struct { 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"` } type Dcim_Interfaces_List struct { Count int `json:"count,omitempty"` Next interface{} `json:"next,omitempty"` Previous interface{} `json:"previous,omitempty"` Results []struct { ID int `json:"id,omitempty"` Device struct { 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"` Type struct { Value int `json:"value,omitempty"` Label string `json:"label,omitempty"` } `json:"type,omitempty"` FormFactor struct { Value int `json:"value,omitempty"` Label string `json:"label,omitempty"` } `json:"form_factor,omitempty"` Enabled bool `json:"enabled,omitempty"` Lag struct { ID int `json:"id,omitempty"` URL string `json:"url,omitempty"` Device struct { 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"` } type InterfaceFilter struct { Offset int64 `schema:"offset"` Limit int64 `schema:"limit"` //User specific filters Id string `schema:"id,omitempty"` Name string `schema:"name,omitempty"` ConnectionStatus string `schema:"connection_status,omitempty"` Type string `schema:"type,omitempty"` Mtu string `schema:"mtu,omitempty"` 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 } query := form.Encode() fmt.Println(query) return nil } func (i *Dcim_Interfaces_Create) CreateInterfaces(n *NetBox) error { interfaceData, err := json.Marshal(i) if err != nil { return err } transport := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify}, } timeout := time.Duration(60 * time.Second) client := &http.Client{ Timeout: timeout, Transport: transport, } fmt.Println(bytes.NewBuffer(interfaceData)) request, err := http.NewRequest("POST", n.RootURL+"/api/dcim/interfaces/", bytes.NewBuffer(interfaceData)) if err != nil { return err } request.Header.Add("Accept", "application/json") request.Header.Add("Content-Type", "application/json") request.Header.Add("Authorization", " Token "+n.Token) response, err := client.Do(request) if err != nil { return err } if response.StatusCode == http.StatusCreated { return nil } else { return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusCreated) } return nil }