From adfcea80d0515162f416b25a6541b05113decf8a Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Mon, 14 Sep 2020 09:40:30 +0200 Subject: [PATCH] Update netbox_inventory. --- netbox_inventory.go | 152 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 netbox_inventory.go diff --git a/netbox_inventory.go b/netbox_inventory.go new file mode 100644 index 0000000..73cc03a --- /dev/null +++ b/netbox_inventory.go @@ -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) +}