Don't be dependent on static custom_fields

Make custom_fields a interface{}
This commit is contained in:
Kalle Carlbark 2021-11-29 10:20:00 +01:00
parent 6a496accb3
commit ce9f27913f
No known key found for this signature in database
4 changed files with 74 additions and 73 deletions

View file

@ -137,30 +137,7 @@ type Device struct {
Slug string `json:"slug"` Slug string `json:"slug"`
Color string `json:"color"` Color string `json:"color"`
} `json:"tags"` } `json:"tags"`
CustomFields struct { CustomFields interface{} `json:"custom_fields,omitempty"`
DcimDeviceBelongsToService struct {
Value int `json:"value"`
Label string `json:"label"`
} `json:"dcim_device_belongs_to_service"`
DcimDeviceCendotid string `json:"dcim_device_cendotid"`
DcimDeviceExposedToInternet struct {
Value int `json:"value"`
Label string `json:"label"`
} `json:"dcim_device_exposed_to_internet"`
DcimDeviceImportOsVersion bool `json:"dcim_device_import_os_version"`
DcimDeviceOsVersion interface{} `json:"dcim_device_os_version"`
DcimDevicesImportInterfaces interface{} `json:"dcim_devices_import_interfaces"`
DcimDeviceImportvlan bool `json:"dcim_device_importvlan"`
DcimDeviceL2Domain interface{} `json:"dcim_device_l2domain"`
DcimDeviceCountTrunkVlans interface{} `json:"dcim_device_count_trunk_vlans"`
DcimDeviceLogicalportsDeviceLimit interface{} `json:"dcim_device_logicalports_device_limit"`
DcimDeviceLogicalportsActiveNum interface{} `json:"dcim_device_logicalports_active_num"`
DcimDeviceLogicalportsConfiguredNum interface{} `json:"dcim_device_logicalports_configured_num"`
DcimDeviceCountTimesLastRun interface{} `json:"dcim_device_count_times_last_run"`
DcimDeviceImportVrfs interface{} `json:"dcim_device_import_vrfs"`
DcimDeviceVrfTag interface{} `json:"dcim_device_vrf_tag"`
DcimDevicePod interface{} `json:"dcim_device_pod"`
} `json:"custom_fields"`
ConfigContext struct { ConfigContext struct {
AdditionalProp1 string `json:"additionalProp1"` AdditionalProp1 string `json:"additionalProp1"`
AdditionalProp2 string `json:"additionalProp2"` AdditionalProp2 string `json:"additionalProp2"`
@ -199,33 +176,7 @@ type NewDevice struct {
Slug string `json:"slug"` Slug string `json:"slug"`
Color string `json:"color"` Color string `json:"color"`
} `json:"tags"` } `json:"tags"`
CustomFields struct { CustomFields interface{} `json:"custom_fields,omitempty"`
DcimDeviceBelongsToService struct {
Value int `json:"value,omitempty"`
Label string `json:"label,omitempty"`
} `json:"dcim_device_belongs_to_service,omitempty"`
DcimDeviceCendotid string `json:"dcim_device_cendotid,omitempty"`
DcimDeviceExposedToInternet struct {
Value int `json:"value,omitempty"`
Label string `json:"label,omitempty"`
} `json:"dcim_device_exposed_to_internet,omitempty"`
DcimDeviceImportOsVersion bool `json:"dcim_device_import_os_version,omitempty"`
DcimDevicePod struct {
Value int `json:"value,omitempty"`
Label string `json:"label,omitempty"`
} `json:"dcim_device_pod,omitempty"`
DcimDeviceOsVersion interface{} `json:"dcim_device_os_version,omitempty"`
DcimDevicesImportInterfaces interface{} `json:"dcim_devices_import_interfaces,omitempty"`
DcimDeviceImportvlan bool `json:"dcim_device_importvlan,omitempty"`
DcimDeviceL2Domain interface{} `json:"dcim_device_l2domain,omitempty"`
DcimDeviceCountTrunkVlans interface{} `json:"dcim_device_count_trunk_vlans,omitempty"`
DcimDeviceLogicalportsDeviceLimit interface{} `json:"dcim_device_logicalports_device_limit,omitempty"`
DcimDeviceLogicalportsActiveNum interface{} `json:"dcim_device_logicalports_active_num,omitempty"`
DcimDeviceLogicalportsConfiguredNum interface{} `json:"dcim_device_logicalports_configured_num,omitempty"`
DcimDeviceCountTimesLastRun interface{} `json:"dcim_device_count_times_last_run,omitempty"`
DcimDeviceImportVrfs interface{} `json:"dcim_device_import_vrfs,omitempty"`
DcimDeviceVrfTag interface{} `json:"dcim_device_vrf_tag,omitempty"`
} `json:"custom_fields,omitempty"`
} }
type UpdateDevice struct { type UpdateDevice struct {
@ -243,23 +194,17 @@ type UpdateDevice struct {
ParentDevice *struct { ParentDevice *struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
} `json:"parent_device,omitempty"` } `json:"parent_device,omitempty"`
Status string `json:"status,omitempty"` Status string `json:"status,omitempty"`
PrimaryIP4 int `json:"primary_ip4,omitempty"` PrimaryIP4 int `json:"primary_ip4,omitempty"`
PrimaryIP6 int `json:"primary_ip6,omitempty"` PrimaryIP6 int `json:"primary_ip6,omitempty"`
Cluster int `json:"cluster,omitempty"` Cluster int `json:"cluster,omitempty"`
VirtualChassis int `json:"virtual_chassis,omitempty"` VirtualChassis int `json:"virtual_chassis,omitempty"`
VcPosition int `json:"vc_position,omitempty"` VcPosition int `json:"vc_position,omitempty"`
VcPriority int `json:"vc_priority,omitempty"` VcPriority int `json:"vc_priority,omitempty"`
Comments string `json:"comments,omitempty"` Comments string `json:"comments,omitempty"`
LocalContextData string `json:"local_context_data,omitempty"` LocalContextData string `json:"local_context_data,omitempty"`
Tags []string `json:"tags,omitempty"` Tags []string `json:"tags,omitempty"`
CustomFields struct { CustomFields interface{} `json:"custom_fields,omitempty"`
DcimDeviceBelongsToService int `json:"dcim_device_belongs_to_service,omitempty"`
DcimDeviceCendotid string `json:"dcim_device_cendotid,omitempty"`
DcimDeviceExposedToInternet int `json:"dcim_device_exposed_to_internet,omitempty"`
DcimDeviceImportOsVersion bool `json:"dcim_device_import_os_version,omitempty"`
DcimDevicePod int `json:"dcim_device_pod,omitempty"`
} `json:"custom_fields,omitempty"`
} }
// DeviceFilter is used to filter dcim_device_list query to the Netbox API // DeviceFilter is used to filter dcim_device_list query to the Netbox API
@ -321,12 +266,15 @@ func (s *DevicesService) List(ctx context.Context, f *DeviceFilter) (*Devices, e
encoder := schema.NewEncoder() encoder := schema.NewEncoder()
form := url.Values{} query = ""
err = encoder.Encode(f, form) if f != nil {
if err != nil { form := url.Values{}
return &devices, err err = encoder.Encode(f, form)
if err != nil {
return &devices, err
}
query = form.Encode()
} }
query = form.Encode()
req, err = s.client.newRequest(ctx, "GET", devicesPath, query, nil) req, err = s.client.newRequest(ctx, "GET", devicesPath, query, nil)
if err != nil { if err != nil {

36
devices_test.go Normal file
View file

@ -0,0 +1,36 @@
package netboxgo
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestListDevices(t *testing.T) {
var err error
var (
apiToken string
apiURL string
)
apiToken = os.Getenv("NETBOX_TOKEN")
apiURL = os.Getenv("NETBOX_URL")
assert.NotEmpty(t, apiToken)
assert.NotEmpty(t, apiURL)
var nb *Client
nb, err = NewClient(apiURL, nil)
if err != nil {
assert.Nil(t, err)
}
nb.SetToken(apiToken)
ctx := context.Background()
_, err = nb.Devices.List(ctx, nil)
if err != nil {
assert.Nil(t, err)
}
}

7
go.mod
View file

@ -6,3 +6,10 @@ require (
github.com/gorilla/schema v1.1.0 github.com/gorilla/schema v1.1.0
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
) )
require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

10
go.sum
View file

@ -1,4 +1,14 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY= github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=