netboxgo/device_types.go
2021-11-27 23:31:44 +01:00

195 lines
5.1 KiB
Go

package netboxgo
import (
"context"
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
type DeviceTypesService service
type DeviceTypes 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"`
}
const deviceTypesPath = dcimPath + "/device-types"
// List device-types. DeviceTypeFilter is used to list based on filter queries.
func (s *DeviceTypesService) List(ctx context.Context, f *DeviceTypeFilter) (*DeviceTypes, error) {
var devicetypes DeviceTypes
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 &devicetypes, err
}
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", deviceTypesPath, query, nil)
if err != nil {
return &devicetypes, err
}
_, err = s.client.do(req, &devicetypes)
if err != nil {
return &devicetypes, err
}
return &devicetypes, nil
}
// // Create a device
// func (s *DeviceTypesService) Create(ctx context.Context, c *NewDeviceType) error {
// var err error
// var req *http.Request
// req, err = s.client.newRequest(ctx, "POST", deviceTypesPath, "", 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
// }
// // Update a device
// func (s *DeviceTypesService) Update(ctx context.Context, id string, c *UpdateDevice) error {
// var req *http.Request
// var err error
// path := fmt.Sprintf("%s/%s", deviceTypesPath, id)
// req, err = s.client.newRequest(ctx, "PATCH", path, "", 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
// }
// // ListDeviceTypes method returns dcim_device_types_list from Netbox API
// func (n *NetBox) ListDeviceTypes(d *DeviceTypes, 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
// }