Netbox librarararary.

This commit is contained in:
Kalle Carlbark 2019-09-10 22:32:20 +02:00
parent 066a3e5251
commit 44968a9d6f
No known key found for this signature in database
GPG key ID: 3FC0C93C5A5A0670
3 changed files with 40 additions and 27 deletions

5
netbox.go Normal file
View file

@ -0,0 +1,5 @@
package netboxclient
type NetBox struct {
RootURL string
}

View file

@ -1,4 +1,12 @@
package main package netboxclient
import (
"bytes"
"encoding/json"
"github.com/pkg/errors"
"net/http"
"time"
)
type dcim_interfaces_create struct { type dcim_interfaces_create struct {
Device int `json:"device"` Device int `json:"device"`
@ -71,6 +79,31 @@ type dcim_interfaces_list struct {
} `json:"results"` } `json:"results"`
} }
func (i *NbInterfaces) CreateInterface() { 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
}
if response.StatusCode == http.StatusCreated {
return nil
} else {
return errors.Errorf("Response was: %d\n", response.StatusCode)
}
return nil
}
func (i *dcim_interface_list) ListInterface(n *NetBox) error {

View file

@ -1,25 +0,0 @@
package main
import (
"fmt"
"net/http"
"net/url"
"io/ioutil"
)
type DeviceInterface struct {
Id int `json:"id"`
Device int `json:"device"`
Name string `json:"name"`
Type int `json:"type"`
FormFactor string `json:"form_factor"`
Enabled bool `json:"enabled"`
Lag int `json:"lag"`
Mtu int `json:"mtu"`
MacAddress string `json:"mac_address"`
func main() {
}
func (i * InterfacesCreate