From 3f5f9eff7d790f97a4a95eb3296bcd20665ebac4 Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Wed, 26 May 2021 10:58:32 +0200 Subject: [PATCH] Add ability to list device types --- device_types.go | 129 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 device_types.go diff --git a/device_types.go b/device_types.go new file mode 100644 index 0000000..923c1dd --- /dev/null +++ b/device_types.go @@ -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 +}