Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 227656e28e | |||
| 63ef233357 | |||
| b4f49be2c6 |
16
CHANGELOG.md
16
CHANGELOG.md
@@ -2,6 +2,22 @@
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## v0.5.0 - 2026-01-19
|
||||
### Added
|
||||
- `ToughSwitch` consolidated type combining Device, System, Interfaces, VLANs, Services, Statistics, and Neighbors
|
||||
- `GetToughSwitch(ctx, host)` method to fetch all data for a single ToughSwitch device in parallel
|
||||
- `GetAllToughSwitches(ctx)` method to fetch consolidated data for all ToughSwitch devices
|
||||
- `EdgeOS` consolidated type combining AuthInfo and Config
|
||||
- `GetEdgeOS(ctx, host)` method to fetch all data for a single EdgeOS device in parallel
|
||||
- `GetAllEdgeOS(ctx)` method to fetch consolidated data for all EdgeOS devices
|
||||
|
||||
### Changed
|
||||
- CLI `get device` and `get devices` commands now return full consolidated data for both device types
|
||||
- EdgeOS CLI output now includes device model, ports, PoE capability, and features from AuthInfo
|
||||
|
||||
### Fixed
|
||||
- Typo in ToughSwitch struct field name (`Statistucs` → `Statistics`)
|
||||
|
||||
## v0.4.0 - 2026-01-18
|
||||
### Added
|
||||
- CLI tool (`cmd/`) for quick device queries using cobra
|
||||
|
||||
56
README.md
56
README.md
@@ -15,6 +15,7 @@ ToughSwitch POE Pro (TS-8-PRO)) via their internal REST API.
|
||||
|
||||
- **Authentication**: Handles login and session token management automatically (or explicitly via `Login`).
|
||||
- **Multi-Device Support**: Manage multiple devices with a single client instance.
|
||||
- **Consolidated Data**: `GetToughSwitch` fetches all device data in a single call (Device, System, Interfaces, VLANs, Services, Statistics, Neighbors).
|
||||
- **Data Retrieval**:
|
||||
- **System Information**: Hostname, uptime, firmware version, etc.
|
||||
- **Interfaces**: Status, POE settings, link speed, statistics.
|
||||
@@ -31,6 +32,7 @@ A client library for interacting with Ubiquiti EdgeOS devices (EdgeRouter, EdgeS
|
||||
|
||||
- **Authentication**: Handles login and session management automatically (or explicitly via `Login`).
|
||||
- **Multi-Device Support**: Manage multiple devices with a single client instance.
|
||||
- **Consolidated Data**: `GetEdgeOS` fetches all device data in a single call (AuthInfo and Config).
|
||||
- **Data Retrieval**:
|
||||
- **System Configuration**: Hostname, domain name, and other system settings.
|
||||
- **Interface Configuration**: Ethernet and switch interface settings, including PoE.
|
||||
@@ -257,6 +259,34 @@ for _, stat := range stats {
|
||||
}
|
||||
```
|
||||
|
||||
### Fetching All Device Data
|
||||
|
||||
Use the consolidated methods to fetch all device information in a single call:
|
||||
|
||||
```go
|
||||
// ToughSwitch: Get everything at once
|
||||
ts, err := client.GetToughSwitch(ctx, "192.168.1.1")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Model: %s\n", ts.Device.Identification.Model)
|
||||
fmt.Printf("Hostname: %s\n", ts.System.Hostname)
|
||||
fmt.Printf("Interfaces: %d\n", len(ts.Interfaces))
|
||||
fmt.Printf("VLANs: %d\n", len(ts.VLANs.Vlans))
|
||||
fmt.Printf("Neighbors: %d\n", len(ts.Neighbors))
|
||||
|
||||
// EdgeOS: Get everything at once
|
||||
eos, err := edgeClient.GetEdgeOS(ctx, "192.168.2.1")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Model: %s (%s)\n", eos.AuthInfo.ModelName, eos.AuthInfo.Model)
|
||||
fmt.Printf("Ports: %d, PoE: %v\n", eos.AuthInfo.Ports, eos.AuthInfo.PoE)
|
||||
fmt.Printf("Hostname: %s\n", eos.Config.System.HostName)
|
||||
```
|
||||
|
||||
### Working with Multiple Devices
|
||||
|
||||
Both clients are designed to handle multiple devices concurrently.
|
||||
@@ -269,14 +299,14 @@ configs := []toughswitch.Config{
|
||||
}
|
||||
client := toughswitch.MustNew(ctx, configs)
|
||||
|
||||
// Get info for all configured devices in parallel
|
||||
allSystems, err := client.GetAllSystems(ctx)
|
||||
// Get all data for all devices in parallel
|
||||
allSwitches, err := client.GetAllToughSwitches(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Error fetching some systems: %v", err)
|
||||
log.Printf("Error fetching some devices: %v", err)
|
||||
}
|
||||
|
||||
for host, sys := range allSystems {
|
||||
fmt.Printf("[%s] Hostname: %s\n", host, sys.Hostname)
|
||||
for host, ts := range allSwitches {
|
||||
fmt.Printf("[%s] %s - %d interfaces\n", host, ts.System.Hostname, len(ts.Interfaces))
|
||||
}
|
||||
|
||||
// EdgeOS example
|
||||
@@ -286,14 +316,14 @@ edgeConfigs := []edgeos.Config{
|
||||
}
|
||||
edgeClient := edgeos.MustNew(ctx, edgeConfigs)
|
||||
|
||||
// Get config for all configured devices in parallel
|
||||
allConfigs, err := edgeClient.GetAllConfigs(ctx)
|
||||
// Get all data for all devices in parallel
|
||||
allEdge, err := edgeClient.GetAllEdgeOS(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Error fetching some configs: %v", err)
|
||||
log.Printf("Error fetching some devices: %v", err)
|
||||
}
|
||||
|
||||
for host, cfg := range allConfigs {
|
||||
fmt.Printf("[%s] Hostname: %s\n", host, cfg.System.HostName)
|
||||
for host, eos := range allEdge {
|
||||
fmt.Printf("[%s] %s (%d ports)\n", host, eos.AuthInfo.ModelName, eos.AuthInfo.Ports)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -304,13 +334,14 @@ for host, cfg := range allConfigs {
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `Login` | Explicit authentication (also happens automatically on 401) |
|
||||
| `GetToughSwitch` | All device data combined (Device, System, Interfaces, VLANs, Services, Statistics, Neighbors) |
|
||||
| `GetDevice` | Hardware and capabilities info |
|
||||
| `GetSystem` | General system configuration and status |
|
||||
| `GetInterfaces` | Interface configuration and status |
|
||||
| `GetVLANs` | VLAN and Trunk configuration |
|
||||
| `GetServices` | State of running services (SSH, NTP, etc.) |
|
||||
| `GetStatistics` | Performance metrics |
|
||||
| `GetNeighbors` | Discovered UBNT neighbors |
|
||||
| `GetDevice` | Hardware and capabilities info |
|
||||
|
||||
All `Get*` methods have corresponding `GetAll*` variants for multi-device operations.
|
||||
|
||||
@@ -319,8 +350,9 @@ All `Get*` methods have corresponding `GetAll*` variants for multi-device operat
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `Login` | Explicit authentication (also happens automatically on 401) |
|
||||
| `GetConfig` | Complete device configuration |
|
||||
| `GetEdgeOS` | All device data combined (AuthInfo and Config) |
|
||||
| `GetAuthInfo` | Device authentication and feature information |
|
||||
| `GetConfig` | Complete device configuration |
|
||||
| `GetInterfaces` | Interface configuration (ethernet and switch) |
|
||||
| `GetSystem` | System configuration (hostname, domain) |
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ func fetchToughSwitchDevice(ctx context.Context, clientConf *config.ClientConfig
|
||||
return nil, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
return client.GetDevice(ctx, clientConf.Host)
|
||||
return client.GetToughSwitch(ctx, clientConf.Host)
|
||||
}
|
||||
|
||||
func fetchEdgeOSDevice(ctx context.Context, clientConf *config.ClientConfig) (any, error) {
|
||||
@@ -44,5 +44,5 @@ func fetchEdgeOSDevice(ctx context.Context, clientConf *config.ClientConfig) (an
|
||||
return nil, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
return client.GetConfig(ctx, clientConf.Host)
|
||||
return client.GetEdgeOS(ctx, clientConf.Host)
|
||||
}
|
||||
|
||||
@@ -203,3 +203,79 @@ func (c *Client) GetAllSystems(ctx context.Context) (map[string]*SystemConfig, e
|
||||
wg.Wait()
|
||||
return results, errs
|
||||
}
|
||||
|
||||
// GetEdgeOS retrieves all information for a specific device,
|
||||
// combining AuthInfo and Config.
|
||||
func (c *Client) GetEdgeOS(ctx context.Context, host string) (*EdgeOS, error) {
|
||||
_, err := c.getDeviceByHost(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
eos := &EdgeOS{}
|
||||
var (
|
||||
mu sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
errs error
|
||||
)
|
||||
|
||||
// Fetch auth info and config in parallel
|
||||
wg.Go(func() {
|
||||
res, err := c.GetAuthInfo(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
eos.AuthInfo = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetConfig(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
eos.Config = res
|
||||
})
|
||||
|
||||
wg.Wait()
|
||||
return eos, errs
|
||||
}
|
||||
|
||||
// GetAllEdgeOS retrieves all information for all devices.
|
||||
func (c *Client) GetAllEdgeOS(ctx context.Context) (map[string]*EdgeOS, error) {
|
||||
results := make(map[string]*EdgeOS)
|
||||
var (
|
||||
mu sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
errs error
|
||||
)
|
||||
|
||||
c.mu.RLock()
|
||||
hosts := make([]string, 0, len(c.devices))
|
||||
for h := range c.devices {
|
||||
hosts = append(hosts, h)
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
|
||||
for _, host := range hosts {
|
||||
wg.Go(func() {
|
||||
res, err := c.GetEdgeOS(ctx, host)
|
||||
if err != nil {
|
||||
mu.Lock()
|
||||
errs = errors.Join(errs, err)
|
||||
mu.Unlock()
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
results[host] = res
|
||||
mu.Unlock()
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
return results, errs
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
package edgeos
|
||||
|
||||
// EdgeOS combines all device information into one response.
|
||||
type EdgeOS struct {
|
||||
AuthInfo *AuthResponse `json:"authInfo,omitempty"`
|
||||
Config *ConfigData `json:"config,omitempty"`
|
||||
}
|
||||
|
||||
// AuthResponse represents the authentication response from login2 endpoint.
|
||||
type AuthResponse struct {
|
||||
Username string `json:"username"`
|
||||
|
||||
@@ -348,3 +348,134 @@ func (c *Client) GetAllNeighbors(ctx context.Context) (map[string][]Neighbor, er
|
||||
wg.Wait()
|
||||
return results, errs
|
||||
}
|
||||
|
||||
// GetToughSwitch retrieves all information for a specific device,
|
||||
// combining Device, System, Interfaces, VLANs, Services, Statistics, and Neighbors.
|
||||
func (c *Client) GetToughSwitch(ctx context.Context, host string) (*ToughSwitch, error) {
|
||||
_, err := c.getDeviceByHost(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ts := &ToughSwitch{}
|
||||
var (
|
||||
mu sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
errs error
|
||||
)
|
||||
|
||||
// Fetch all resources in parallel
|
||||
wg.Go(func() {
|
||||
res, err := c.GetDevice(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.Device = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetSystem(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.System = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetInterfaces(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.Interfaces = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetVLANs(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.VLANs = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetServices(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.Services = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetStatistics(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.Statistics = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetNeighbors(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.Neighbors = res
|
||||
})
|
||||
|
||||
wg.Wait()
|
||||
return ts, errs
|
||||
}
|
||||
|
||||
// GetAllToughSwitches retrieves all information for all devices.
|
||||
func (c *Client) GetAllToughSwitches(ctx context.Context) (map[string]*ToughSwitch, error) {
|
||||
results := make(map[string]*ToughSwitch)
|
||||
var (
|
||||
mu sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
errs error
|
||||
)
|
||||
|
||||
c.mu.RLock()
|
||||
hosts := make([]string, 0, len(c.devices))
|
||||
for h := range c.devices {
|
||||
hosts = append(hosts, h)
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
|
||||
for _, host := range hosts {
|
||||
wg.Go(func() {
|
||||
res, err := c.GetToughSwitch(ctx, host)
|
||||
if err != nil {
|
||||
mu.Lock()
|
||||
errs = errors.Join(errs, err)
|
||||
mu.Unlock()
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
results[host] = res
|
||||
mu.Unlock()
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
return results, errs
|
||||
}
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
package toughswitch
|
||||
|
||||
// ToughSwitch combines all types into one response.
|
||||
type ToughSwitch struct {
|
||||
Device *Device `json:"device,omitempty"`
|
||||
System *System `json:"system,omitempty"`
|
||||
Interfaces []Interface `json:"interfaces,omitempty"`
|
||||
Neighbors []Neighbor `json:"neighbors,omitempty"`
|
||||
Statistics []Statistics `json:"statistics,omitempty"`
|
||||
Services *Services `json:"services,omitempty"`
|
||||
VLANs *VLANs `json:"vlans,omitempty"`
|
||||
}
|
||||
|
||||
// LoginResponse represents the response from the login endpoint.
|
||||
type LoginResponse struct {
|
||||
StatusCode int `json:"statusCode"`
|
||||
|
||||
Reference in New Issue
Block a user