Add ability to list device types

This commit is contained in:
Kalle Carlbark 2021-05-26 10:58:32 +02:00
parent 8383ef9bb9
commit 3f5f9eff7d
No known key found for this signature in database
GPG key ID: 5E4BAB01A57CDD62

129
device_types.go Normal file
View file

@ -0,0 +1,129 @@
package netboxgo
import (
"crypto/tls"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
type ListDeviceTypes struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []DeviceType `json:"results"`
}
type DeviceType struct {
ID int `json:"id"`
URL string `json:"url"`
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"`
Model string `json:"model"`
Slug string `json:"slug"`
DisplayName string `json:"display_name"`
PartNumber string `json:"part_number"`
UHeight int `json:"u_height"`
IsFullDepth bool `json:"is_full_depth"`
SubdeviceRole struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"subdevice_role"`
FrontImage string `json:"front_image"`
RearImage string `json:"rear_image"`
Comments string `json:"comments"`
Tags []struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
} `json:"tags"`
CustomFields struct {
} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
DeviceCount int `json:"device_count"`
}
// DeviceTypeFilter is used to filter dcim_device_types_list query to the Netbox API
type DeviceTypeFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
// User specific filters
ID string `schema:"id,omitempty"`
Model string `schema:"model,omitempty"`
Slug string `schema:"slug,omitempty"`
PartNumber string `schema:"part_number,omitempty"`
AssetTag string `schema:"asset_tag,omitempty"`
UHeight string `schema:"u_height,omitempty"`
IsFullDepth string `schema:"is_full_depth,omitempty"`
Tag string `schema:"tag,omitempty"`
}
// ListDeviceTypes method returns dcim_device_types_list from Netbox API
func (n *NetBox) ListDeviceTypes(d *ListDeviceTypes, f *DeviceTypeFilter) 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/device-types/"
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
}