diff --git a/netbox.go b/netbox.go index 097c710..5ee3965 100644 --- a/netbox.go +++ b/netbox.go @@ -1,13 +1,15 @@ package netboxgo +// NetBox settings struct type NetBox struct { RootURL string Token string InsecureSkipVerify bool } -func (n *NetBox) New(root, token string, TlsSkipVerify bool) { +// New populates the NetBox settings struct +func (n *NetBox) New(root, token string, TLSSkipVerify bool) { n.RootURL = root n.Token = token - n.InsecureSkipVerify = TlsSkipVerify + n.InsecureSkipVerify = TLSSkipVerify } diff --git a/netbox_devices.go b/netbox_devices.go index c782216..ae50fb7 100644 --- a/netbox_devices.go +++ b/netbox_devices.go @@ -3,15 +3,17 @@ package netboxgo import ( "crypto/tls" "encoding/json" - "github.com/gorilla/schema" - "github.com/pkg/errors" "io/ioutil" "net/http" "net/url" "time" + + "github.com/gorilla/schema" + "github.com/pkg/errors" ) -type Dcim_Devices_List struct { +// DcimDevicesList is used for Netbox dcim_device_list return struct +type DcimDevicesList struct { Count int `json:"count"` Next string `json:"next"` Previous string `json:"previous"` @@ -138,43 +140,44 @@ type Dcim_Devices_List struct { } `json:"results"` } +// DeviceFilter is used to filter dcim_device_list query to the Netbox API type DeviceFilter struct { Offset int64 `schema:"offset,omitempty"` Limit int64 `schema:"limit,omitempty"` //User specific filters - Id string `schema:"id,omitempty"` + ID string `schema:"id,omitempty"` Name string `schema:"name,omitempty"` AssetTag string `schema:"asset_tag,omitempty"` Face string `schema:"face,omitempty"` Position string `schema:"position,omitempty"` VcPosition string `schema:"vc_position,omitempty"` VcPriority string `schema:"vc_priority,omitempty"` - TenantGroupId string `schema:"tenant_group_id,omitempty"` + TenantGroupID string `schema:"tenant_group_id,omitempty"` TenantGroup string `schema:"tenant_group,omitempty"` - TenantId string `schema:"tenant_id,omitempty"` + TenantID string `schema:"tenant_id,omitempty"` Tenant string `schema:"tenant,omitempty"` - IdIn string `schema:"id__in,omitempty"` + IDIn string `schema:"id__in,omitempty"` Q string `schema:"q,omitempty"` - ManufacturerId string `schema:"manufacturer_id,omitempty"` + ManufacturerID string `schema:"manufacturer_id,omitempty"` Manufacturer string `schema:"manufacturer,omitempty"` - DeviceTypeId string `schema:"device_type_id,omitempty"` - RoleId string `schema:"role_id,omitempty"` + DeviceTypeID string `schema:"device_type_id,omitempty"` + RoleID string `schema:"role_id,omitempty"` Role string `schema:"role,omitempty"` - RegionId string `schema:"region_id,omitempty"` + RegionID string `schema:"region_id,omitempty"` Region string `schema:"region,omitempty"` - SiteId string `schema:"site_id,omitempty"` + SiteID string `schema:"site_id,omitempty"` Site string `schema:"site,omitempty"` - RackGroupId string `schema:"rack_group_id,omitempty"` - RackId string `schema:"rack_id,omitempty"` - ClusterId string `schema:"cluster_id,omitempty"` + RackGroupID string `schema:"rack_group_id,omitempty"` + RackID string `schema:"rack_id,omitempty"` + ClusterID string `schema:"cluster_id,omitempty"` Model string `schema:"model,omitempty"` Status string `schema:"status,omitempty"` IsfullDepth string `schema:"is_full_depth,omitempty"` MacAddress string `schema:"mac_address,omitempty"` Serial string `schema:"serial,omitempty"` - HasPrimaryIp string `schema:"has_primary_ip,omitempty"` - VirtualChassiId string `schema:"virtual_chassis_id,omitempty"` + HasPrimaryIP string `schema:"has_primary_ip,omitempty"` + VirtualChassiID string `schema:"virtual_chassis_id,omitempty"` VirtualChassiMember string `schema:"virtual_chassis_member,omitempty"` ConsolePorts string `schema:"console_ports,omitempty"` ConsoleServerPorts string `schema:"console_server_ports,omitempty"` @@ -185,7 +188,8 @@ type DeviceFilter struct { Tag string `schema:"tag,omitempty"` } -func (n *NetBox) ListDevices(d *Dcim_Devices_List, f *DeviceFilter) error { +// ListDevices method returns dcim_device_list from Netbox API +func (n *NetBox) ListDevices(d *DcimDevicesList, f *DeviceFilter) error { var encoder = schema.NewEncoder() transport := &http.Transport{ @@ -218,7 +222,6 @@ func (n *NetBox) ListDevices(d *Dcim_Devices_List, f *DeviceFilter) error { if err != nil { return err } - defer response.Body.Close() if response.StatusCode != http.StatusOK { return errors.Errorf("Error: response was: %d should be %d (%s)\n", response.StatusCode, http.StatusOK, deviceurl) @@ -229,6 +232,11 @@ func (n *NetBox) ListDevices(d *Dcim_Devices_List, f *DeviceFilter) error { return err } + err = response.Body.Close() + if err != nil { + return err + } + err = json.Unmarshal(data, &d) if err != nil { return err diff --git a/netbox_interfaces.go b/netbox_interfaces.go index a6ebb03..8131622 100644 --- a/netbox_interfaces.go +++ b/netbox_interfaces.go @@ -4,16 +4,19 @@ import ( "bytes" "crypto/tls" "encoding/json" + // "fmt" - "github.com/gorilla/schema" - "github.com/pkg/errors" "io/ioutil" "net/http" "net/url" "time" + + "github.com/gorilla/schema" + "github.com/pkg/errors" ) -type Dcim_Interfaces_Create struct { +// DcimInterfacesCreate is used for the return values from Netbox API dcim_interfaces_create +type DcimInterfacesCreate struct { Device int `json:"device"` Name string `json:"name"` Type int `json:"type,omitempty"` @@ -33,7 +36,8 @@ type Dcim_Interfaces_Create struct { Tags []string `json:"tags,omitempty"` } -type Dcim_Interfaces_List struct { +// DcimInterfacesList is used for the return value from NetBox API dcim_interfaces_list +type DcimInterfacesList struct { Count int `json:"count,omitempty"` Next interface{} `json:"next,omitempty"` Previous interface{} `json:"previous,omitempty"` @@ -84,12 +88,13 @@ type Dcim_Interfaces_List struct { } `json:"results,omitempty"` } +// InterfaceFilter is used to filter out returned object from Netbox API dcim_interfaces_list type InterfaceFilter struct { Offset int64 `schema:"offset,omitempty"` Limit int64 `schema:"limit,omitempty"` //User specific filters - Id string `schema:"id,omitempty"` + ID string `schema:"id,omitempty"` Name string `schema:"name,omitempty"` ConnectionStatus string `schema:"connection_status,omitempty"` Type string `schema:"type,omitempty"` @@ -99,17 +104,18 @@ type InterfaceFilter struct { Description string `schema:"description,omitempty"` Q string `schema:"q,omitempty"` Device string `schema:"device,omitempty"` - DeviceId string `schema:"device_id,omitempty"` + DeviceID string `schema:"device_id,omitempty"` Cabled string `schema:"cabled,omitempty"` Kind string `schema:"kind,omitempty"` - LagId string `schema:"lag_id,omitempty"` + LagID string `schema:"lag_id,omitempty"` MacAddress string `schema:"mac_address,omitempty"` Tag string `schema:"tag,omitempty"` - VlanId string `schema:"vlan_id,omitempty"` + VlanID string `schema:"vlan_id,omitempty"` Vlan string `schema:"vlan,omitempty"` } -func (n *NetBox) ListInterfaces(i *Dcim_Interfaces_List, f *InterfaceFilter) error { +// ListInterfaces returns Netbox dcim_interfaces_list +func (n *NetBox) ListInterfaces(i *DcimInterfacesList, f *InterfaceFilter) error { var encoder = schema.NewEncoder() form := url.Values{} @@ -137,7 +143,6 @@ func (n *NetBox) ListInterfaces(i *Dcim_Interfaces_List, f *InterfaceFilter) err if err != nil { return err } - defer response.Body.Close() if response.StatusCode != http.StatusOK { return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusOK) @@ -148,6 +153,11 @@ func (n *NetBox) ListInterfaces(i *Dcim_Interfaces_List, f *InterfaceFilter) err return err } + err = response.Body.Close() + if err != nil { + return err + } + err = json.Unmarshal(data, &i) if err != nil { return err @@ -155,7 +165,8 @@ func (n *NetBox) ListInterfaces(i *Dcim_Interfaces_List, f *InterfaceFilter) err return nil } -func (n *NetBox) CreateInterfaces(i *Dcim_Interfaces_Create) error { +// CreateInterfaces creates interfaces via Netbox API dcim_interfaces_create +func (n *NetBox) CreateInterfaces(i *DcimInterfacesCreate) error { interfaceData, err := json.Marshal(i) if err != nil { return err @@ -179,12 +190,13 @@ func (n *NetBox) CreateInterfaces(i *Dcim_Interfaces_Create) error { if err != nil { return err } - defer response.Body.Close() + err = response.Body.Close() + if err != nil { + return err + } if response.StatusCode == http.StatusCreated { return nil - } else { - return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusCreated) } - return nil + return errors.Errorf("Error: response was: %d should be %d\n", response.StatusCode, http.StatusCreated) }