2021-11-26 11:09:27 +01:00
|
|
|
package netboxgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/gorilla/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InventoryItemsService service
|
|
|
|
|
|
|
|
type NewInventoryItem 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 InventoryItems 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 InventoryItemFilter struct {
|
|
|
|
Device string `schema:"device"`
|
|
|
|
}
|
|
|
|
|
2021-11-27 23:31:44 +01:00
|
|
|
const inventoryItemsPath = dcimPath + "/inventory-items"
|
2021-11-26 11:09:27 +01:00
|
|
|
|
|
|
|
// List inventory-items. InventoryItemFilter is used to list based on filter queries.
|
|
|
|
func (s *InventoryItemsService) List(ctx context.Context, f *InventoryItemFilter) (*InventoryItems, error) {
|
|
|
|
var inventoryitems InventoryItems
|
|
|
|
var query string
|
|
|
|
var req *http.Request
|
|
|
|
var err error
|
|
|
|
|
|
|
|
encoder := schema.NewEncoder()
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
err = encoder.Encode(f, form)
|
|
|
|
if err != nil {
|
|
|
|
return &inventoryitems, err
|
|
|
|
}
|
|
|
|
query = form.Encode()
|
|
|
|
|
|
|
|
req, err = s.client.newRequest(ctx, "GET", inventoryItemsPath, query, nil)
|
|
|
|
if err != nil {
|
|
|
|
return &inventoryitems, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.client.do(req, &inventoryitems)
|
|
|
|
if err != nil {
|
|
|
|
return &inventoryitems, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &inventoryitems, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a inventory-item
|
|
|
|
func (s *InventoryItemsService) Create(ctx context.Context, c *NewInventoryItem) error {
|
|
|
|
var err error
|
|
|
|
var req *http.Request
|
|
|
|
|
|
|
|
req, err = s.client.newRequest(ctx, "POST", inventoryItemsPath, "", c)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.client.do(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to do request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|