From ce9f27913f60b03859512df30c6796bc4e6a65fb Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Mon, 29 Nov 2021 10:20:00 +0100 Subject: [PATCH] Don't be dependent on static custom_fields Make custom_fields a interface{} --- devices.go | 94 +++++++++++-------------------------------------- devices_test.go | 36 +++++++++++++++++++ go.mod | 7 ++++ go.sum | 10 ++++++ 4 files changed, 74 insertions(+), 73 deletions(-) create mode 100644 devices_test.go diff --git a/devices.go b/devices.go index efb1fca..81f94dc 100644 --- a/devices.go +++ b/devices.go @@ -137,30 +137,7 @@ type Device struct { Slug string `json:"slug"` Color string `json:"color"` } `json:"tags"` - CustomFields struct { - 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"` + CustomFields interface{} `json:"custom_fields,omitempty"` ConfigContext struct { AdditionalProp1 string `json:"additionalProp1"` AdditionalProp2 string `json:"additionalProp2"` @@ -199,33 +176,7 @@ type NewDevice struct { Slug string `json:"slug"` Color string `json:"color"` } `json:"tags"` - CustomFields struct { - 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"` + CustomFields interface{} `json:"custom_fields,omitempty"` } type UpdateDevice struct { @@ -243,23 +194,17 @@ type UpdateDevice struct { ParentDevice *struct { Name string `json:"name,omitempty"` } `json:"parent_device,omitempty"` - Status string `json:"status,omitempty"` - PrimaryIP4 int `json:"primary_ip4,omitempty"` - PrimaryIP6 int `json:"primary_ip6,omitempty"` - Cluster int `json:"cluster,omitempty"` - VirtualChassis int `json:"virtual_chassis,omitempty"` - VcPosition int `json:"vc_position,omitempty"` - VcPriority int `json:"vc_priority,omitempty"` - Comments string `json:"comments,omitempty"` - LocalContextData string `json:"local_context_data,omitempty"` - Tags []string `json:"tags,omitempty"` - CustomFields struct { - 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"` + Status string `json:"status,omitempty"` + PrimaryIP4 int `json:"primary_ip4,omitempty"` + PrimaryIP6 int `json:"primary_ip6,omitempty"` + Cluster int `json:"cluster,omitempty"` + VirtualChassis int `json:"virtual_chassis,omitempty"` + VcPosition int `json:"vc_position,omitempty"` + VcPriority int `json:"vc_priority,omitempty"` + Comments string `json:"comments,omitempty"` + LocalContextData string `json:"local_context_data,omitempty"` + Tags []string `json:"tags,omitempty"` + CustomFields interface{} `json:"custom_fields,omitempty"` } // 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() - form := url.Values{} - err = encoder.Encode(f, form) - if err != nil { - return &devices, err + query = "" + if f != nil { + form := url.Values{} + 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) if err != nil { diff --git a/devices_test.go b/devices_test.go new file mode 100644 index 0000000..3765691 --- /dev/null +++ b/devices_test.go @@ -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) + } +} diff --git a/go.mod b/go.mod index fb2938b..4218472 100644 --- a/go.mod +++ b/go.mod @@ -6,3 +6,10 @@ require ( github.com/gorilla/schema v1.1.0 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 +) diff --git a/go.sum b/go.sum index 26a1f9b..c5fe4f5 100644 --- a/go.sum +++ b/go.sum @@ -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/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 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=