add ListDevices()

This commit is contained in:
Kalle Carlbark 2019-09-11 13:53:11 +02:00
parent 36129c8f88
commit 4b671c2ac4
No known key found for this signature in database
GPG key ID: 3FC0C93C5A5A0670

239
netbox_devices.go Normal file
View file

@ -0,0 +1,239 @@
package nbclient
import (
"crypto/tls"
"encoding/json"
"github.com/gorilla/schema"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"net/url"
"time"
)
type Dcim_Devices_List struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []struct {
ID int `json:"id"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
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"`
DeviceCount int `json:"device_count"`
} `json:"device_type"`
DeviceRole struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"device_role"`
Tenant struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"tenant"`
Platform struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"platform"`
Serial string `json:"serial"`
AssetTag string `json:"asset_tag"`
Site struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"site"`
Rack struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
DeviceCount int `json:"device_count"`
} `json:"rack"`
Position int `json:"position"`
Face struct {
Label string `json:"label"`
Value int `json:"value"`
} `json:"face"`
ParentDevice struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
} `json:"parent_device"`
Status struct {
Label string `json:"label"`
Value int `json:"value"`
} `json:"status"`
PrimaryIP struct {
ID int `json:"id"`
URL string `json:"url"`
Family int `json:"family"`
Address string `json:"address"`
} `json:"primary_ip"`
PrimaryIP4 struct {
ID int `json:"id"`
URL string `json:"url"`
Family int `json:"family"`
Address string `json:"address"`
} `json:"primary_ip4"`
PrimaryIP6 struct {
ID int `json:"id"`
URL string `json:"url"`
Family int `json:"family"`
Address string `json:"address"`
} `json:"primary_ip6"`
Cluster struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"cluster"`
VirtualChassis struct {
ID int `json:"id"`
URL string `json:"url"`
Master struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
} `json:"master"`
MemberCount int `json:"member_count"`
} `json:"virtual_chassis"`
VcPosition int `json:"vc_position"`
VcPriority int `json:"vc_priority"`
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"`
} `json:"results"`
}
type DeviceFilter struct {
Offset int64 `schema:"offset,omitempty"`
Limit int64 `schema:"limit,omitempty"`
//User specific filters
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"`
TenantGroup string `schema:"tenant_group"`
TenantId string `schema:"tenant_id,omitempty"`
Tenant string `schema:"tenant,omitempty"`
IdIn string `schema:"id__in,omitempty"`
Q string `schema:"q,omitempty"`
ManufacturerId string `schema:"manufacturer_id,omitempty"`
Manufacturer string `schema:"manufacturer,omitempty"`
DeviceTypeId string `schema:"device_type_id"`
RoleId string `schema:"role_id,omitempty"`
Role string `schema:"role,omitempty"`
RegionId string `schema:"region_id,omitempty"`
Region string `schema:"region,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"`
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"`
VirtualChassiMember string `schema:"virtual_chassis_member,omitempty"`
ConsolePorts string `schema:"console_ports,omitempty"`
ConsoleServerPorts string `schema:"console_server_ports,omitempty"`
PowerPorts string `schema:"power_ports,omitempty"`
PowerOutlets string `schema:"power_outlets,omitempty"`
Interfaces string `schema:"interfaces,omitempty"`
PassThroughPorts string `schema:"pass_through_ports,omitempty"`
Tag string `schema:"tag,omitempty"`
}
func (n *NetBox) ListDevices(d *Dcim_Devices_List, f *DeviceFilter) error {
var encoder = schema.NewEncoder()
form := url.Values{}
err := encoder.Encode(f, form)
if err != nil {
return err
}
query := form.Encode()
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: n.InsecureSkipVerify},
}
timeout := time.Duration(60 * time.Second)
client := &http.Client{
Timeout: timeout,
Transport: transport,
}
var request *http.Request
if len(query) > 0 {
request, err = http.NewRequest("GET", n.RootURL+"/api/dcim/devices/"+"?"+query, nil)
if err != nil {
return err
}
} else {
request, err = http.NewRequest("GET", n.RootURL+"/api/dcim/devices/", 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\n", response.StatusCode, http.StatusOK)
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
err = json.Unmarshal(data, &d)
if err != nil {
return err
}
return nil
}