From af589e9fc0c4548a7b7edc536f53e645bf224a80 Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Mon, 29 Nov 2021 11:38:01 +0100 Subject: [PATCH] Move integration tests --- circuits.go | 10 +++---- clusters.go | 20 ++++++------- devices.go | 10 ++----- devices_integration_test.go | 59 +++++++++++++++++++++++++++++++++++++ devices_test.go | 36 ---------------------- netbox.go | 30 ------------------- prefixes.go | 28 +++++++++--------- secrets.go | 6 ++-- site_groups.go | 12 ++++---- sites.go | 18 +++++------ tenants.go | 48 ++++++++++++++---------------- virtual_machines.go | 24 +++++++-------- vlans.go | 34 ++++++++++----------- vrfs.go | 32 ++++++++++---------- 14 files changed, 174 insertions(+), 193 deletions(-) create mode 100644 devices_integration_test.go delete mode 100644 devices_test.go diff --git a/circuits.go b/circuits.go index 4919660..e80e771 100644 --- a/circuits.go +++ b/circuits.go @@ -96,10 +96,10 @@ type Circuits struct { UpstreamSpeed int `json:"upstream_speed"` XconnectID string `json:"xconnect_id"` } `json:"termination_z"` - Comments string `json:"comments"` - Tags []string `json:"tags"` - CustomFields struct{} `json:"custom_fields"` - Created string `json:"created"` - LastUpdated time.Time `json:"last_updated"` + Comments string `json:"comments"` + Tags []string `json:"tags"` + CustomFields interface{} `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` } `json:"results"` } diff --git a/clusters.go b/clusters.go index 3f591dc..bb88b9c 100644 --- a/clusters.go +++ b/clusters.go @@ -44,11 +44,11 @@ type NewCluster struct { 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"` - VirtualmachineCount int `json:"virtualmachine_count"` + CustomFields interface{} `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + DeviceCount int `json:"device_count"` + VirtualmachineCount int `json:"virtualmachine_count"` } // Clusters is used to list VirtualizationClusters @@ -87,11 +87,11 @@ type Clusters struct { 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"` - VirtualmachineCount int `json:"virtualmachine_count"` + CustomFields interface{} `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + DeviceCount int `json:"device_count"` + VirtualmachineCount int `json:"virtualmachine_count"` } `json:"results"` } diff --git a/devices.go b/devices.go index 81f94dc..b225ebb 100644 --- a/devices.go +++ b/devices.go @@ -138,13 +138,9 @@ type Device struct { Color string `json:"color"` } `json:"tags"` CustomFields interface{} `json:"custom_fields,omitempty"` - ConfigContext struct { - AdditionalProp1 string `json:"additionalProp1"` - AdditionalProp2 string `json:"additionalProp2"` - AdditionalProp3 string `json:"additionalProp3"` - } `json:"config_context"` - Created string `json:"created"` - LastUpdated time.Time `json:"last_updated"` + ConfigContext interface{} `json:"config_context"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` } type NewDevice struct { diff --git a/devices_integration_test.go b/devices_integration_test.go new file mode 100644 index 0000000..4ebe6d4 --- /dev/null +++ b/devices_integration_test.go @@ -0,0 +1,59 @@ +//go:build integration + +package netboxgo_test + +import ( + "context" + "fmt" + "os" + "path/filepath" + "runtime" + "testing" + + . "git.kcbark.net/kc/netboxgo" +) + +func TestListDevices(t *testing.T) { + var err error + + var ( + apiToken string + apiURL string + ) + + apiToken = os.Getenv("NETBOX_TOKEN") + apiURL = os.Getenv("NETBOX_URL") + assert(t, true, apiToken) + assert(t, true, apiURL) + + var nb *Client + nb, err = NewClient(apiURL, nil) + if err != nil { + ok(t, err) + } + nb.SetToken(apiToken) + + ctx := context.Background() + _, err = nb.Devices.List(ctx, nil) + if err != nil { + ok(t, err) + } +} + +// ok fails the test if an err is not nil. +func ok(tb testing.TB, err error) { + if err != nil { + _, file, line, _ := runtime.Caller(1) + fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error()) + tb.FailNow() + } +} + +// assert fails the test if the condition is false. +func assert(tb testing.TB, condition bool, msg string, v ...interface{}) { + if !condition { + _, file, line, _ := runtime.Caller(1) + fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...) + tb.FailNow() + } +} diff --git a/devices_test.go b/devices_test.go deleted file mode 100644 index 3765691..0000000 --- a/devices_test.go +++ /dev/null @@ -1,36 +0,0 @@ -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/netbox.go b/netbox.go index 1e9cd12..e639aac 100644 --- a/netbox.go +++ b/netbox.go @@ -16,36 +16,6 @@ import ( "github.com/pkg/errors" ) -type DCIM struct { - Devices *DevicesService - DeviceRoles *DeviceRolesService - DeviceTypes *DeviceTypesService - Interfaces *InterfacesService - InventoryItems *InventoryItemsService - RearPorts *RearPortsService -} - -type Virtualization struct { - Clusters *ClustersService - VirtualMachines *VirtualMachinesService -} - -type IPAM struct { - VLANs *VLANsService - VRFs *VRFsService - Prefixes *PrefixesService -} - -type Secret struct { - Secrets *SecretsService -} - -type Tenancy struct { - Tenants *TenantsService - Sites *SitesService - SiteGroups *SiteGroupsService -} - // Client struct is used to create a new NetBox endpoint type Client struct { // DCIM *DCIM diff --git a/prefixes.go b/prefixes.go index ee77658..a25ebbe 100644 --- a/prefixes.go +++ b/prefixes.go @@ -16,17 +16,17 @@ type PrefixesService service // NewPrefix is used for the return values from Netbox API ipam_prefixes_create type NewPrefix struct { - Prefix string `json:"prefix"` - Site int `json:"site"` - Vrf int `json:"vrf"` - Tenant int `json:"tenant"` - VLAN int `json:"vlan"` - Status int `json:"status"` - Role int `json:"role"` - IsPool bool `json:"is_pool"` - Description string `json:"description"` - Tags []string `json:"tags"` - CustomFields struct{} `json:"custom_fields"` + Prefix string `json:"prefix"` + Site int `json:"site"` + Vrf int `json:"vrf"` + Tenant int `json:"tenant"` + VLAN int `json:"vlan"` + Status int `json:"status"` + Role int `json:"role"` + IsPool bool `json:"is_pool"` + Description string `json:"description"` + Tags []string `json:"tags"` + CustomFields interface{} `json:"custom_fields"` } // Prefixes is used for the return value from NetBox API ipam_prefixes_list @@ -88,9 +88,9 @@ type Prefixes struct { 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"` + CustomFields interface{} `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` } `json:"results"` } diff --git a/secrets.go b/secrets.go index 59bdc95..72db6de 100644 --- a/secrets.go +++ b/secrets.go @@ -42,9 +42,9 @@ type Secrets struct { 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"` + CustomFields interface{} `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` } `json:"results"` } diff --git a/site_groups.go b/site_groups.go index d59fd57..ec53b5d 100644 --- a/site_groups.go +++ b/site_groups.go @@ -34,12 +34,12 @@ type SiteGroup struct { SiteCount int `json:"site_count"` Depth int `json:"_depth"` } `json:"parent"` - Description string `json:"description"` - CustomFields struct{} `json:"custom_fields"` - Created string `json:"created"` - LastUpdated time.Time `json:"last_updated"` - SiteCount int `json:"site_count"` - Depth int `json:"_depth"` + Description string `json:"description"` + CustomFields interface{} `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + SiteCount int `json:"site_count"` + Depth int `json:"_depth"` } // SiteFilter is used to filter out returned object from Netbox API tenancy_tenants_list diff --git a/sites.go b/sites.go index 7490e1b..f544bb6 100644 --- a/sites.go +++ b/sites.go @@ -74,15 +74,15 @@ type Site struct { 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"` - CircuitCount int `json:"circuit_count"` - DeviceCount int `json:"device_count"` - PrefixCount int `json:"prefix_count"` - RackCount int `json:"rack_count"` - VirtualmachineCount int `json:"virtualmachine_count"` - VlanCount int `json:"vlan_count"` + CustomFields interface{} `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + CircuitCount int `json:"circuit_count"` + DeviceCount int `json:"device_count"` + PrefixCount int `json:"prefix_count"` + RackCount int `json:"rack_count"` + VirtualmachineCount int `json:"virtualmachine_count"` + VlanCount int `json:"vlan_count"` } // SiteFilter is used to filter out returned object from Netbox API tenancy_tenants_list diff --git a/tenants.go b/tenants.go index 9663712..289fb18 100644 --- a/tenants.go +++ b/tenants.go @@ -14,13 +14,13 @@ type TenantsService service // NewTenant is used for the return values from Netbox API tenancy_tenants_create type NewTenant struct { - Name string `json:"name"` - Slug string `json:"slug"` - Group int `json:"group"` - Description string `json:"description"` - Comments string `json:"comments"` - Tags []string `json:"tags"` - CustomFields struct{} `json:"custom_fields"` + Name string `json:"name"` + Slug string `json:"slug"` + Group int `json:"group"` + Description string `json:"description"` + Comments string `json:"comments"` + Tags []string `json:"tags"` + CustomFields interface{} `json:"custom_fields"` } // Tenants is used for the return value from NetBox API tenancy_tenants_list @@ -38,25 +38,21 @@ type Tenants struct { Name string `json:"name"` Slug string `json:"slug"` } `json:"group"` - Description string `json:"description"` - Comments string `json:"comments"` - Tags []interface{} `json:"tags"` - CustomFields struct { - AccountNumber string `json:"account_number"` - AtlasCustNumber interface{} `json:"atlas_cust_number"` - TextField interface{} `json:"text_field"` - } `json:"custom_fields"` - Created string `json:"created"` - LastUpdated time.Time `json:"last_updated"` - CircuitCount interface{} `json:"circuit_count"` - DeviceCount int `json:"device_count"` - IpaddressCount interface{} `json:"ipaddress_count"` - PrefixCount int `json:"prefix_count"` - RackCount interface{} `json:"rack_count"` - SiteCount int `json:"site_count"` - VirtualmachineCount interface{} `json:"virtualmachine_count"` - VlanCount int `json:"vlan_count"` - VrfCount interface{} `json:"vrf_count"` + Description string `json:"description"` + Comments string `json:"comments"` + Tags []interface{} `json:"tags"` + CustomFields interface{} `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + CircuitCount interface{} `json:"circuit_count"` + DeviceCount int `json:"device_count"` + IpaddressCount interface{} `json:"ipaddress_count"` + PrefixCount int `json:"prefix_count"` + RackCount interface{} `json:"rack_count"` + SiteCount int `json:"site_count"` + VirtualmachineCount interface{} `json:"virtualmachine_count"` + VlanCount int `json:"vlan_count"` + VrfCount interface{} `json:"vrf_count"` } `json:"results"` } diff --git a/virtual_machines.go b/virtual_machines.go index cda07af..fdd0d2f 100644 --- a/virtual_machines.go +++ b/virtual_machines.go @@ -69,20 +69,16 @@ type NewVirtualMachine struct { Family int `json:"family"` Address string `json:"address"` } `json:"primary_ip6"` - Vcpus int `json:"vcpus"` - Memory int `json:"memory"` - Disk int `json:"disk"` - Comments string `json:"comments"` - LocalContextData string `json:"local_context_data"` - Tags []string `json:"tags"` - CustomFields struct{} `json:"custom_fields"` - ConfigContext struct { - AdditionalProp1 string `json:"additionalProp1"` - AdditionalProp2 string `json:"additionalProp2"` - AdditionalProp3 string `json:"additionalProp3"` - } `json:"config_context"` - Created string `json:"created"` - LastUpdated time.Time `json:"last_updated"` + Vcpus int `json:"vcpus"` + Memory int `json:"memory"` + Disk int `json:"disk"` + Comments string `json:"comments"` + LocalContextData string `json:"local_context_data"` + Tags []string `json:"tags"` + CustomFields interface{} `json:"custom_fields"` + ConfigContext interface{} `json:"config_context"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` } const virtualMachinesPath = virtualizationPath + "/virtual-machines" diff --git a/vlans.go b/vlans.go index 991da26..73aa8c6 100644 --- a/vlans.go +++ b/vlans.go @@ -14,16 +14,16 @@ type VLANsService service // NewVLAN is used for the return values from Netbox API ipam_vlans_create type NewVLAN struct { - Site int `json:"site"` - Group int `json:"group"` - Vid int `json:"vid"` - Name string `json:"name"` - Tenant int `json:"tenant"` - Status int `json:"status"` - Role int `json:"role"` - Description string `json:"description"` - Tags []string `json:"tags"` - CustomFields struct{} `json:"custom_fields"` + Site int `json:"site"` + Group int `json:"group"` + Vid int `json:"vid"` + Name string `json:"name"` + Tenant int `json:"tenant"` + Status int `json:"status"` + Role int `json:"role"` + Description string `json:"description"` + Tags []string `json:"tags"` + CustomFields interface{} `json:"custom_fields"` } type VLAN struct { @@ -61,13 +61,13 @@ type VLAN struct { PrefixCount int `json:"prefix_count"` VLANCount int `json:"vlan_count"` } `json:"role"` - Description string `json:"description"` - Tags []string `json:"tags"` - DisplayName string `json:"display_name"` - CustomFields struct{} `json:"custom_fields"` - Created string `json:"created"` - LastUpdated time.Time `json:"last_updated"` - PrefixCount int `json:"prefix_count"` + Description string `json:"description"` + Tags []string `json:"tags"` + DisplayName string `json:"display_name"` + CustomFields interface{} `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + PrefixCount int `json:"prefix_count"` } // IpamVLANsList is used for the return value from NetBox API ipam_vlans_list diff --git a/vrfs.go b/vrfs.go index de200c6..13ea258 100644 --- a/vrfs.go +++ b/vrfs.go @@ -14,13 +14,13 @@ type VRFsService service // NewVRF is used for the return values from Netbox API ipam_vrfs_create type NewVRF struct { - Name string `json:"name"` - Rd string `json:"rd"` - Tenant int `json:"tenant"` - EnforceUnique bool `json:"enforce_unique"` - Description string `json:"description"` - Tags []string `json:"tags"` - CustomFields struct{} `json:"custom_fields"` + Name string `json:"name"` + Rd string `json:"rd"` + Tenant int `json:"tenant"` + EnforceUnique bool `json:"enforce_unique"` + Description string `json:"description"` + Tags []string `json:"tags"` + CustomFields interface{} `json:"custom_fields"` } // VRFs is used for the return value from NetBox API ipam_vrfs_list @@ -38,15 +38,15 @@ type VRFs struct { Name string `json:"name"` Slug string `json:"slug"` } `json:"tenant"` - EnforceUnique bool `json:"enforce_unique"` - Description string `json:"description"` - Tags []string `json:"tags"` - DisplayName string `json:"display_name"` - CustomFields struct{} `json:"custom_fields"` - Created string `json:"created"` - LastUpdated time.Time `json:"last_updated"` - IpaddressCount int `json:"ipaddress_count"` - PrefixCount int `json:"prefix_count"` + EnforceUnique bool `json:"enforce_unique"` + Description string `json:"description"` + Tags []string `json:"tags"` + DisplayName string `json:"display_name"` + CustomFields interface{} `json:"custom_fields"` + Created string `json:"created"` + LastUpdated time.Time `json:"last_updated"` + IpaddressCount int `json:"ipaddress_count"` + PrefixCount int `json:"prefix_count"` } `json:"results"` }