Update netbox_inventory.
This commit is contained in:
parent
6daf77093a
commit
adfcea80d0
1 changed files with 152 additions and 0 deletions
152
netbox_inventory.go
Normal file
152
netbox_inventory.go
Normal file
|
@ -0,0 +1,152 @@
|
|||
package netboxgo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/schema"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type AddInventory struct {
|
||||
Device int `json:"device"`
|
||||
Parent int `json:"parent"`
|
||||
Name string `json:"name"`
|
||||
Manufacturer int `json:"manufacturer"`
|
||||
PartID string `json:"part_id"`
|
||||
Serial string `json:"serial"`
|
||||
AssetTag string `json:"asset_tag"`
|
||||
Discovered bool `json:"discovered"`
|
||||
Description string `json:"description"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
type DcimInventoryItemsList struct {
|
||||
Count int `json:"count"`
|
||||
Next string `json:"next"`
|
||||
Previous string `json:"previous"`
|
||||
Results []struct {
|
||||
ID int `json:"id"`
|
||||
Device struct {
|
||||
ID int `json:"id"`
|
||||
URL string `json:"url"`
|
||||
Name string `json:"name"`
|
||||
DisplayName string `json:"display_name"`
|
||||
} `json:"device"`
|
||||
Parent int `json:"parent"`
|
||||
Name string `json:"name"`
|
||||
Manufacturer struct {
|
||||
ID int `json:"id"`
|
||||
URL string `json:"url"`
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
DevicetypeCount int `json:"devicetype_count"`
|
||||
} `json:"manufacturer"`
|
||||
PartID string `json:"part_id"`
|
||||
Serial string `json:"serial"`
|
||||
AssetTag string `json:"asset_tag"`
|
||||
Discovered bool `json:"discovered"`
|
||||
Description string `json:"description"`
|
||||
Tags []string `json:"tags"`
|
||||
} `json:"results"`
|
||||
}
|
||||
|
||||
type InventoryFilter struct {
|
||||
Device string `schema:"device"`
|
||||
}
|
||||
|
||||
// ListDevices method returns dcim_device_list from Netbox API
|
||||
func (n *NetBox) ListInventory(d *DcimInventoryItemsList, f *InventoryFilter) error {
|
||||
encoder := schema.NewEncoder()
|
||||
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
|
||||
}
|
||||
timeout := time.Duration(60 * time.Second)
|
||||
client := &http.Client{
|
||||
Timeout: timeout,
|
||||
Transport: transport,
|
||||
}
|
||||
deviceurl := n.RootURL + "/api/dcim/inventory-items/"
|
||||
if f != nil {
|
||||
form := url.Values{}
|
||||
err := encoder.Encode(f, form)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
query := form.Encode()
|
||||
deviceurl = deviceurl + "?" + query
|
||||
}
|
||||
var request *http.Request
|
||||
var err error
|
||||
request, err = http.NewRequest("GET", deviceurl, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.Header.Add("Accept", "application/json")
|
||||
request.Header.Add("Authorization", " Token "+n.Token)
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return errors.Errorf("Error: response was: %d should be %d (%s)\n", response.StatusCode, http.StatusOK, deviceurl)
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = response.Body.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NetBox) AddInventory(v *AddInventory) error {
|
||||
data, err := json.Marshal(v)
|
||||
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,
|
||||
}
|
||||
request, err := http.NewRequest("POST", n.RootURL+"/api/dcim/inventory-items/", bytes.NewBuffer(data))
|
||||
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
|
||||
}
|
||||
err = response.Body.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if response.StatusCode == http.StatusCreated {
|
||||
return nil
|
||||
}
|
||||
return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusCreated)
|
||||
}
|
Loading…
Reference in a new issue