netboxgo/device_types.go

196 lines
5.1 KiB
Go
Raw Normal View History

2021-05-26 10:58:32 +02:00
package netboxgo
import (
2021-11-26 11:09:27 +01:00
"context"
2021-05-26 10:58:32 +02:00
"net/http"
"net/url"
"time"
"github.com/gorilla/schema"
)
2021-11-26 11:09:27 +01:00
type DeviceTypesService service
type DeviceTypes struct {
2021-05-26 10:58:32 +02:00
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"`
2021-11-26 11:09:27 +01:00
CustomFields struct{} `json:"custom_fields"`
Created string `json:"created"`
LastUpdated time.Time `json:"last_updated"`
DeviceCount int `json:"device_count"`
2021-05-26 10:58:32 +02:00
}
// 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"
2021-05-26 10:58:32 +02:00
2021-11-26 11:09:27 +01:00
// 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
2021-05-26 10:58:32 +02:00
var err error
2021-11-26 11:09:27 +01:00
encoder := schema.NewEncoder()
2021-05-26 10:58:32 +02:00
2021-11-26 11:09:27 +01:00
form := url.Values{}
err = encoder.Encode(f, form)
2021-05-26 10:58:32 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &devicetypes, err
2021-05-26 10:58:32 +02:00
}
2021-11-26 11:09:27 +01:00
query = form.Encode()
2021-05-26 10:58:32 +02:00
2021-11-26 11:09:27 +01:00
req, err = s.client.newRequest(ctx, "GET", deviceTypesPath, query, nil)
2021-05-26 10:58:32 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &devicetypes, err
2021-05-26 10:58:32 +02:00
}
2021-11-26 11:09:27 +01:00
_, err = s.client.do(req, &devicetypes)
2021-05-26 10:58:32 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &devicetypes, err
2021-05-26 10:58:32 +02:00
}
2021-11-26 11:09:27 +01:00
return &devicetypes, nil
2021-05-26 10:58:32 +02:00
}
2021-11-26 11:09:27 +01:00
// // 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
// }