netboxgo/devices.go

324 lines
11 KiB
Go
Raw Normal View History

2019-09-11 20:12:42 +02:00
package netboxgo
2019-09-11 13:53:11 +02:00
import (
2021-11-26 11:09:27 +01:00
"context"
"fmt"
2019-09-11 13:53:11 +02:00
"net/http"
"net/url"
"time"
2019-09-17 09:53:24 +02:00
"github.com/gorilla/schema"
2019-09-11 13:53:11 +02:00
)
2021-11-26 11:09:27 +01:00
type DevicesService service
// Devices is used for Netbox dcim_device_list return struct
type Devices struct {
2021-05-24 15:10:20 +02:00
Next string `json:"next"`
Previous string `json:"previous"`
Results []Device `json:"results"`
Count int `json:"count"`
2021-05-24 15:10:20 +02:00
}
type Device struct {
LastUpdated time.Time `json:"last_updated"`
CustomFields interface{} `json:"custom_fields,omitempty"`
ConfigContext interface{} `json:"config_context"`
Status struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"status"`
Face struct {
Label string `json:"label"`
Value string `json:"value"`
} `json:"face"`
Name string `json:"name"`
AssetTag string `json:"asset_tag"`
Comments string `json:"comments"`
DisplayName string `json:"display_name"`
Created string `json:"created"`
Serial string `json:"serial"`
ParentDevice struct {
URL string `json:"url"`
Name string `json:"name"`
2021-05-24 15:10:20 +02:00
DisplayName string `json:"display_name"`
ID int `json:"id"`
} `json:"parent_device"`
2021-05-24 15:10:20 +02:00
Tenant struct {
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
} `json:"tenant"`
Site struct {
2021-05-24 15:10:20 +02:00
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
2021-05-24 15:10:20 +02:00
} `json:"site"`
Tags []struct {
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
ID int `json:"id"`
} `json:"tags"`
2021-05-24 15:10:20 +02:00
Rack struct {
URL string `json:"url"`
2019-09-11 13:53:11 +02:00
Name string `json:"name"`
DisplayName string `json:"display_name"`
ID int `json:"id"`
2021-05-24 15:10:20 +02:00
DeviceCount int `json:"device_count"`
} `json:"rack"`
PrimaryIP4 struct {
2021-05-24 15:10:20 +02:00
URL string `json:"url"`
Address string `json:"address"`
ID int `json:"id"`
Family int `json:"family"`
} `json:"primary_ip4"`
PrimaryIP struct {
2021-05-24 15:10:20 +02:00
URL string `json:"url"`
Address string `json:"address"`
ID int `json:"id"`
Family int `json:"family"`
} `json:"primary_ip"`
2021-05-24 15:10:20 +02:00
Cluster struct {
URL string `json:"url"`
Name string `json:"name"`
ID int `json:"id"`
2021-05-24 15:10:20 +02:00
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"cluster"`
PrimaryIP6 struct {
URL string `json:"url"`
Address string `json:"address"`
ID int `json:"id"`
Family int `json:"family"`
} `json:"primary_ip6"`
2021-05-24 15:10:20 +02:00
VirtualChassis struct {
URL string `json:"url"`
Master struct {
2019-09-11 13:53:11 +02:00
URL string `json:"url"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
ID int `json:"id"`
2021-05-24 15:10:20 +02:00
} `json:"master"`
ID int `json:"id"`
2021-05-24 15:10:20 +02:00
MemberCount int `json:"member_count"`
} `json:"virtual_chassis"`
DeviceRole struct {
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"device_role"`
Platform struct {
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
DeviceCount int `json:"device_count"`
VirtualmachineCount int `json:"virtualmachine_count"`
} `json:"platform"`
DeviceType struct {
URL string `json:"url"`
Model string `json:"model"`
Slug string `json:"slug"`
DisplayName string `json:"display_name"`
Manufacturer struct {
URL string `json:"url"`
Name string `json:"name"`
Slug string `json:"slug"`
ID int `json:"id"`
DevicetypeCount int `json:"devicetype_count"`
} `json:"manufacturer"`
ID int `json:"id"`
DeviceCount int `json:"device_count"`
} `json:"device_type"`
Position int `json:"position"`
VcPriority int `json:"vc_priority"`
VcPosition int `json:"vc_position"`
ID int `json:"id"`
2019-09-11 13:53:11 +02:00
}
2021-11-26 11:09:27 +01:00
type NewDevice struct {
CustomFields interface{} `json:"custom_fields,omitempty"`
Face string `json:"face,omitempty"`
LocalContextData string `json:"local_context_data,omitempty"`
Comments string `json:"comments,omitempty"`
Status string `json:"status,omitempty"`
Serial string `json:"serial,omitempty"`
AssetTag string `json:"asset_tag,omitempty"`
ParentDevice struct {
2020-08-10 10:28:22 +02:00
Name string `json:"name,omitempty"`
} `json:"parent_device,omitempty"`
Name string `json:"name,omitempty"`
Tags []struct {
2020-10-15 13:01:55 +02:00
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
} `json:"tags"`
Cluster int `json:"cluster,omitempty"`
Platform int `json:"platform,omitempty"`
Tenant int `json:"tenant,omitempty"`
PrimaryIP4 int `json:"primary_ip4,omitempty"`
PrimaryIP6 int `json:"primary_ip6,omitempty"`
Site int `json:"site,omitempty"`
VirtualChassis int `json:"virtual_chassis,omitempty"`
VcPosition int `json:"vc_position,omitempty"`
VcPriority int `json:"vc_priority,omitempty"`
DeviceRole int `json:"device_role,omitempty"`
DeviceType int `json:"device_type,omitempty"`
Position int `json:"position,omitempty"`
Rack int `json:"rack,omitempty"`
2020-08-10 10:28:22 +02:00
}
2021-11-26 11:09:27 +01:00
type UpdateDevice struct {
CustomFields interface{} `json:"custom_fields,omitempty"`
2020-09-03 13:52:52 +02:00
ParentDevice *struct {
Name string `json:"name,omitempty"`
} `json:"parent_device,omitempty"`
Face string `json:"face,omitempty"`
LocalContextData string `json:"local_context_data,omitempty"`
Comments string `json:"comments,omitempty"`
Serial string `json:"serial,omitempty"`
AssetTag string `json:"asset_tag,omitempty"`
Status string `json:"status,omitempty"`
Name string `json:"name,omitempty"`
Tags []string `json:"tags,omitempty"`
Position int `json:"position,omitempty"`
DeviceType int `json:"device_type,omitempty"`
Site int `json:"site,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"`
Platform int `json:"platform,omitempty"`
Tenant int `json:"tenant,omitempty"`
Rack int `json:"rack,omitempty"`
DeviceRole int `json:"device_role,omitempty"`
2020-09-03 13:52:52 +02:00
}
2019-09-17 09:53:24 +02:00
// DeviceFilter is used to filter dcim_device_list query to the Netbox API
2019-09-11 13:53:11 +02:00
type DeviceFilter struct {
2020-08-10 10:28:22 +02:00
// User specific filters
2019-09-17 09:53:24 +02:00
ID string `schema:"id,omitempty"`
2019-09-11 13:53:11 +02:00
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"`
2019-09-17 09:53:24 +02:00
TenantGroupID string `schema:"tenant_group_id,omitempty"`
TenantGroup string `schema:"tenant_group,omitempty"`
2019-09-17 09:53:24 +02:00
TenantID string `schema:"tenant_id,omitempty"`
2019-09-11 13:53:11 +02:00
Tenant string `schema:"tenant,omitempty"`
2019-09-17 09:53:24 +02:00
IDIn string `schema:"id__in,omitempty"`
2019-09-11 13:53:11 +02:00
Q string `schema:"q,omitempty"`
2019-09-17 09:53:24 +02:00
ManufacturerID string `schema:"manufacturer_id,omitempty"`
2019-09-11 13:53:11 +02:00
Manufacturer string `schema:"manufacturer,omitempty"`
2019-09-17 09:53:24 +02:00
DeviceTypeID string `schema:"device_type_id,omitempty"`
RoleID string `schema:"role_id,omitempty"`
2019-09-11 13:53:11 +02:00
Role string `schema:"role,omitempty"`
2019-09-17 09:53:24 +02:00
RegionID string `schema:"region_id,omitempty"`
2019-09-11 13:53:11 +02:00
Region string `schema:"region,omitempty"`
2019-09-17 09:53:24 +02:00
SiteID string `schema:"site_id,omitempty"`
2019-09-11 13:53:11 +02:00
Site string `schema:"site,omitempty"`
2019-09-17 09:53:24 +02:00
RackGroupID string `schema:"rack_group_id,omitempty"`
RackID string `schema:"rack_id,omitempty"`
ClusterID string `schema:"cluster_id,omitempty"`
2019-09-11 13:53:11 +02:00
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"`
2019-09-17 09:53:24 +02:00
HasPrimaryIP string `schema:"has_primary_ip,omitempty"`
VirtualChassiID string `schema:"virtual_chassis_id,omitempty"`
2019-09-11 13:53:11 +02:00
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"`
Offset int `schema:"offset,omitempty"`
Limit int `schema:"limit,omitempty"`
2019-09-11 13:53:11 +02:00
}
const devicesPath = dcimPath + "/devices"
2019-09-11 13:53:11 +02:00
2021-11-26 11:09:27 +01:00
// List devices. DeviceFilter is used to list based on filter queries.
func (s *DevicesService) List(ctx context.Context, f *DeviceFilter) (*Devices, error) {
var devices Devices
var query string
var req *http.Request
var err error
2019-09-11 13:53:11 +02:00
2021-11-26 11:09:27 +01:00
encoder := schema.NewEncoder()
2019-09-11 13:53:11 +02:00
query = ""
if f != nil {
form := url.Values{}
err = encoder.Encode(f, form)
if err != nil {
return &devices, err
}
query = form.Encode()
2019-09-11 13:53:11 +02:00
}
2021-11-26 11:09:27 +01:00
req, err = s.client.newRequest(ctx, "GET", devicesPath, query, nil)
2019-09-17 09:53:24 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &devices, err
2019-09-17 09:53:24 +02:00
}
2021-11-26 11:09:27 +01:00
_, err = s.client.do(req, &devices)
2019-09-11 13:53:11 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return &devices, err
2019-09-11 13:53:11 +02:00
}
2021-11-26 11:09:27 +01:00
return &devices, nil
2019-09-11 13:53:11 +02:00
}
2020-08-10 10:28:22 +02:00
2021-11-26 11:09:27 +01:00
// Create a device
func (s *DevicesService) Create(ctx context.Context, c *NewDevice) error {
var err error
var req *http.Request
req, err = s.client.newRequest(ctx, "POST", devicesPath, "", c)
2020-08-10 10:28:22 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return fmt.Errorf("unable to create request: %w", err)
2020-08-10 10:28:22 +02:00
}
2021-11-26 11:09:27 +01:00
_, err = s.client.do(req, nil)
2020-08-10 10:28:22 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return fmt.Errorf("unable to do request: %w", err)
2020-08-10 10:28:22 +02:00
}
2021-11-26 11:09:27 +01:00
return nil
2020-08-10 10:28:22 +02:00
}
2021-11-26 11:09:27 +01:00
// Update a device
func (s *DevicesService) Update(ctx context.Context, id string, c *UpdateDevice) error {
var req *http.Request
var err error
path := fmt.Sprintf("%s/%s", devicesPath, id)
req, err = s.client.newRequest(ctx, "PATCH", path, "", c)
2020-08-10 10:28:22 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return fmt.Errorf("unable to create request: %w", err)
2020-08-10 10:28:22 +02:00
}
2020-09-03 13:52:52 +02:00
2021-11-26 11:09:27 +01:00
_, err = s.client.do(req, nil)
2020-08-10 10:28:22 +02:00
if err != nil {
2021-11-26 11:09:27 +01:00
return fmt.Errorf("unable to do request: %w", err)
2020-08-10 10:28:22 +02:00
}
2021-06-01 22:53:19 +02:00
return nil
2020-08-10 10:28:22 +02:00
}