Compare commits
6 Commits
868ab64bc9
...
v0.5.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 227656e28e | |||
| 63ef233357 | |||
| b4f49be2c6 | |||
| 4964317b6b | |||
| baf321ece0 | |||
| 5e8e7cd41d |
34
CHANGELOG.md
34
CHANGELOG.md
@@ -2,6 +2,40 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
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
|
||||||
|
- `get device <name>` - fetch info from a single device
|
||||||
|
- `get devices` - fetch info from all configured devices in parallel
|
||||||
|
- YAML/JSON config file support with environment variable overlay
|
||||||
|
- Pretty print and colorized YAML output options
|
||||||
|
- `Login(ctx, host)` method to both toughswitch and edgeos clients for explicit pre-authentication
|
||||||
|
- Context helpers for storing/retrieving clients (`ToughSwitchClientFromContext`, `EdgeOSClientFromContext`)
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- CLI uses zerolog's built-in context methods for logger storage
|
||||||
|
- CLI prerun creates shared clients from config and stores in context
|
||||||
|
- Updated documentation (README.md, CLAUDE.md) with CLI usage and new Login method
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Config default flag handling in CLI
|
||||||
|
|
||||||
## v0.3.0 - 2026-01-14
|
## v0.3.0 - 2026-01-14
|
||||||
### Changed
|
### Changed
|
||||||
- Renamed project from toughswitch to ubiquiti-clients
|
- Renamed project from toughswitch to ubiquiti-clients
|
||||||
|
|||||||
29
CLAUDE.md
29
CLAUDE.md
@@ -15,9 +15,6 @@ go build ./cmd/...
|
|||||||
go test ./...
|
go test ./...
|
||||||
|
|
||||||
# Run tests for a specific package
|
# Run tests for a specific package
|
||||||
go test ./pkg/toughswitch/...
|
|
||||||
|
|
||||||
# Run a single test
|
|
||||||
go test ./pkg/toughswitch/... -run TestClient_AddDel
|
go test ./pkg/toughswitch/... -run TestClient_AddDel
|
||||||
|
|
||||||
# Tidy module dependencies
|
# Tidy module dependencies
|
||||||
@@ -26,17 +23,17 @@ go mod tidy
|
|||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
This repository provides Go client libraries for interacting with Ubiquiti network devices via reverse-engineered REST APIs.
|
This repository provides Go client libraries for interacting with Ubiquiti network devices via reverse-engineered REST APIs, plus a CLI tool.
|
||||||
|
|
||||||
### Package Structure
|
### Package Structure
|
||||||
|
|
||||||
- `pkg/toughswitch/` - Client for ToughSwitch devices (e.g., TS-8-PRO)
|
- `pkg/toughswitch/` - Client for ToughSwitch devices (e.g., TS-8-PRO)
|
||||||
- `pkg/edgeos/` - Client for EdgeOS devices (EdgeRouter, EdgeSwitch)
|
- `pkg/edgeos/` - Client for EdgeOS devices (EdgeRouter, EdgeSwitch)
|
||||||
- `cmd/` - Optional CLI tool using cobra (work in progress)
|
- `cmd/` - CLI tool using cobra
|
||||||
|
|
||||||
### Client Design Pattern
|
### Library Client Design Pattern
|
||||||
|
|
||||||
Both packages follow the same multi-device client pattern:
|
Both `pkg/toughswitch` and `pkg/edgeos` follow the same multi-device client pattern:
|
||||||
|
|
||||||
1. **Client** - Top-level struct holding a map of `deviceClient` instances keyed by host
|
1. **Client** - Top-level struct holding a map of `deviceClient` instances keyed by host
|
||||||
2. **deviceClient** - Per-device HTTP client with authentication state (token for ToughSwitch, cookies for EdgeOS)
|
2. **deviceClient** - Per-device HTTP client with authentication state (token for ToughSwitch, cookies for EdgeOS)
|
||||||
@@ -45,16 +42,28 @@ Both packages follow the same multi-device client pattern:
|
|||||||
Key characteristics:
|
Key characteristics:
|
||||||
- Thread-safe: Uses `sync.RWMutex` for device map access and `sync.Mutex` for per-device operations
|
- Thread-safe: Uses `sync.RWMutex` for device map access and `sync.Mutex` for per-device operations
|
||||||
- Auto-login: Automatically authenticates on 401 responses and retries the request
|
- Auto-login: Automatically authenticates on 401 responses and retries the request
|
||||||
|
- Explicit login: `Login(ctx, host)` can be called to pre-authenticate
|
||||||
- Concurrent multi-device: `GetAll*` methods use `sync.WaitGroup.Go()` for parallel queries
|
- Concurrent multi-device: `GetAll*` methods use `sync.WaitGroup.Go()` for parallel queries
|
||||||
|
|
||||||
### API Pattern
|
API pattern for each package:
|
||||||
|
|
||||||
Each package exposes:
|
|
||||||
- `MustNew(ctx, []Config)` - Constructor that accepts multiple device configs
|
- `MustNew(ctx, []Config)` - Constructor that accepts multiple device configs
|
||||||
|
- `Login(ctx, host)` - Explicit authentication (also happens automatically on 401)
|
||||||
- `Add(cfg)` / `Del(host)` - Dynamic device management
|
- `Add(cfg)` / `Del(host)` - Dynamic device management
|
||||||
- `Get<Resource>(ctx, host)` - Single device query
|
- `Get<Resource>(ctx, host)` - Single device query
|
||||||
- `GetAll<Resources>(ctx)` - Parallel query across all devices, returns `map[string]*Resource`
|
- `GetAll<Resources>(ctx)` - Parallel query across all devices, returns `map[string]*Resource`
|
||||||
|
|
||||||
|
### CLI Architecture (`cmd/`)
|
||||||
|
|
||||||
|
The CLI uses cobra with a prerun chain pattern:
|
||||||
|
|
||||||
|
- `cmd/cmd/root.go` - Root command with `PersistentPreRunE` hook
|
||||||
|
- `cmd/cmd/prerun.go` - Prerun functions executed in order: `validateConfigFile` → `prepareConfig` → `prepareLogger` → `setEnvironment` → `prepareClients`
|
||||||
|
- `cmd/cmd/client.go` - Shared `fetchDevice()` helper that retrieves clients from context
|
||||||
|
- `cmd/internal/util/context.go` - Context helpers for config, logger, and clients
|
||||||
|
- `cmd/internal/config/config.go` - Config loading from YAML/JSON files with env overlay
|
||||||
|
|
||||||
|
Context flow: Prerun creates clients based on config and stores them in context. Commands retrieve clients via `util.ToughSwitchClientFromContext(ctx)` / `util.EdgeOSClientFromContext(ctx)`.
|
||||||
|
|
||||||
### Authentication
|
### Authentication
|
||||||
|
|
||||||
- **ToughSwitch**: Token-based via `x-auth-token` header from `/api/v1.0/user/login`
|
- **ToughSwitch**: Token-based via `x-auth-token` header from `/api/v1.0/user/login`
|
||||||
|
|||||||
143
README.md
143
README.md
@@ -1,6 +1,6 @@
|
|||||||
# ubiquiti-clients
|
# ubiquiti-clients
|
||||||
|
|
||||||
Go client libraries for interacting with Ubiquiti network devices via their REST APIs.
|
Go client libraries for interacting with Ubiquiti network devices via their REST APIs, plus a CLI tool.
|
||||||
|
|
||||||
**⚠️ Disclaimer: These libraries are based on reverse-engineered API calls. They are not official Ubiquiti products and are subject to change if the device firmware changes.**
|
**⚠️ Disclaimer: These libraries are based on reverse-engineered API calls. They are not official Ubiquiti products and are subject to change if the device firmware changes.**
|
||||||
|
|
||||||
@@ -13,8 +13,9 @@ ToughSwitch POE Pro (TS-8-PRO)) via their internal REST API.
|
|||||||
|
|
||||||
#### Features
|
#### Features
|
||||||
|
|
||||||
- **Authentication**: Handles login and session token management automatically.
|
- **Authentication**: Handles login and session token management automatically (or explicitly via `Login`).
|
||||||
- **Multi-Device Support**: Manage multiple devices with a single client instance.
|
- **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**:
|
- **Data Retrieval**:
|
||||||
- **System Information**: Hostname, uptime, firmware version, etc.
|
- **System Information**: Hostname, uptime, firmware version, etc.
|
||||||
- **Interfaces**: Status, POE settings, link speed, statistics.
|
- **Interfaces**: Status, POE settings, link speed, statistics.
|
||||||
@@ -29,25 +30,79 @@ A client library for interacting with Ubiquiti EdgeOS devices (EdgeRouter, EdgeS
|
|||||||
|
|
||||||
#### Features
|
#### Features
|
||||||
|
|
||||||
- **Authentication**: Handles login and session management automatically.
|
- **Authentication**: Handles login and session management automatically (or explicitly via `Login`).
|
||||||
- **Multi-Device Support**: Manage multiple devices with a single client instance.
|
- **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**:
|
- **Data Retrieval**:
|
||||||
- **System Configuration**: Hostname, domain name, and other system settings.
|
- **System Configuration**: Hostname, domain name, and other system settings.
|
||||||
- **Interface Configuration**: Ethernet and switch interface settings, including PoE.
|
- **Interface Configuration**: Ethernet and switch interface settings, including PoE.
|
||||||
- **VLAN Configuration**: VLAN assignments, PVID, and tagged VLANs.
|
- **VLAN Configuration**: VLAN assignments, PVID, and tagged VLANs.
|
||||||
- **Device Information**: Model, ports, PoE capabilities, and features.
|
- **Device Information**: Model, ports, PoE capabilities, and features.
|
||||||
|
|
||||||
## Installation
|
## CLI Tool
|
||||||
|
|
||||||
|
A command-line tool is included for quick device queries.
|
||||||
|
|
||||||
|
### Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go install gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd@latest
|
||||||
|
```
|
||||||
|
|
||||||
|
### Configuration
|
||||||
|
|
||||||
|
Create a config file (YAML or JSON):
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
logLevel: info
|
||||||
|
logFormat: console
|
||||||
|
|
||||||
|
clients:
|
||||||
|
- name: switch1
|
||||||
|
type: toughswitch
|
||||||
|
host: 192.168.1.1
|
||||||
|
user: ubnt
|
||||||
|
pass: password
|
||||||
|
insecure: true
|
||||||
|
timeout: 10s
|
||||||
|
|
||||||
|
- name: router1
|
||||||
|
type: edgeos
|
||||||
|
host: 192.168.1.2
|
||||||
|
user: ubnt
|
||||||
|
pass: password
|
||||||
|
insecure: true
|
||||||
|
timeout: 10s
|
||||||
|
```
|
||||||
|
|
||||||
|
Environment variables can also be used (and take priority over config file):
|
||||||
|
- `LOG_LEVEL`, `LOG_FORMAT` for top-level settings
|
||||||
|
- `CLIENT_0_NAME`, `CLIENT_0_HOST`, `CLIENT_0_TYPE`, etc. for client array
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Get info from a single device
|
||||||
|
cmd --config config.yaml get device switch1
|
||||||
|
|
||||||
|
# Get info from all configured devices
|
||||||
|
cmd --config config.yaml get devices
|
||||||
|
|
||||||
|
# With flags
|
||||||
|
cmd --config config.yaml get device switch1 --pretty --color
|
||||||
|
```
|
||||||
|
|
||||||
|
## Library Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# For ToughSwitch
|
# For ToughSwitch
|
||||||
go get gitea.libretechconsulting.com/rmcguire/toughswitch-client/pkg/toughswitch
|
go get gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/toughswitch
|
||||||
|
|
||||||
# For EdgeOS
|
# For EdgeOS
|
||||||
go get gitea.libretechconsulting.com/rmcguire/toughswitch-client/pkg/edgeos
|
go get gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/edgeos
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Library Usage
|
||||||
|
|
||||||
### ToughSwitch Basic Example
|
### ToughSwitch Basic Example
|
||||||
|
|
||||||
@@ -60,7 +115,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.libretechconsulting.com/rmcguire/toughswitch-client/pkg/toughswitch"
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/toughswitch"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -80,6 +135,11 @@ func main() {
|
|||||||
// Initialize the client
|
// Initialize the client
|
||||||
client := toughswitch.MustNew(ctx, configs)
|
client := toughswitch.MustNew(ctx, configs)
|
||||||
|
|
||||||
|
// Optionally pre-authenticate (otherwise happens automatically on first request)
|
||||||
|
if err := client.Login(ctx, "192.168.1.1"); err != nil {
|
||||||
|
log.Fatalf("Failed to login: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch system information
|
// Fetch system information
|
||||||
deviceHost := "192.168.1.1"
|
deviceHost := "192.168.1.1"
|
||||||
system, err := client.GetSystem(ctx, deviceHost)
|
system, err := client.GetSystem(ctx, deviceHost)
|
||||||
@@ -116,7 +176,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.libretechconsulting.com/rmcguire/toughswitch-client/pkg/edgeos"
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/edgeos"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -136,6 +196,11 @@ func main() {
|
|||||||
// Initialize the client
|
// Initialize the client
|
||||||
client := edgeos.MustNew(ctx, configs)
|
client := edgeos.MustNew(ctx, configs)
|
||||||
|
|
||||||
|
// Optionally pre-authenticate (otherwise happens automatically on first request)
|
||||||
|
if err := client.Login(ctx, "192.168.1.1"); err != nil {
|
||||||
|
log.Fatalf("Failed to login: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch device information
|
// Fetch device information
|
||||||
deviceHost := "192.168.1.1"
|
deviceHost := "192.168.1.1"
|
||||||
authInfo, err := client.GetAuthInfo(ctx, deviceHost)
|
authInfo, err := client.GetAuthInfo(ctx, deviceHost)
|
||||||
@@ -194,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
|
### Working with Multiple Devices
|
||||||
|
|
||||||
Both clients are designed to handle multiple devices concurrently.
|
Both clients are designed to handle multiple devices concurrently.
|
||||||
@@ -206,14 +299,14 @@ configs := []toughswitch.Config{
|
|||||||
}
|
}
|
||||||
client := toughswitch.MustNew(ctx, configs)
|
client := toughswitch.MustNew(ctx, configs)
|
||||||
|
|
||||||
// Get info for all configured devices in parallel
|
// Get all data for all devices in parallel
|
||||||
allSystems, err := client.GetAllSystems(ctx)
|
allSwitches, err := client.GetAllToughSwitches(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error fetching some systems: %v", err)
|
log.Printf("Error fetching some devices: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for host, sys := range allSystems {
|
for host, ts := range allSwitches {
|
||||||
fmt.Printf("[%s] Hostname: %s\n", host, sys.Hostname)
|
fmt.Printf("[%s] %s - %d interfaces\n", host, ts.System.Hostname, len(ts.Interfaces))
|
||||||
}
|
}
|
||||||
|
|
||||||
// EdgeOS example
|
// EdgeOS example
|
||||||
@@ -223,14 +316,14 @@ edgeConfigs := []edgeos.Config{
|
|||||||
}
|
}
|
||||||
edgeClient := edgeos.MustNew(ctx, edgeConfigs)
|
edgeClient := edgeos.MustNew(ctx, edgeConfigs)
|
||||||
|
|
||||||
// Get config for all configured devices in parallel
|
// Get all data for all devices in parallel
|
||||||
allConfigs, err := edgeClient.GetAllConfigs(ctx)
|
allEdge, err := edgeClient.GetAllEdgeOS(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error fetching some configs: %v", err)
|
log.Printf("Error fetching some devices: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for host, cfg := range allConfigs {
|
for host, eos := range allEdge {
|
||||||
fmt.Printf("[%s] Hostname: %s\n", host, cfg.System.HostName)
|
fmt.Printf("[%s] %s (%d ports)\n", host, eos.AuthInfo.ModelName, eos.AuthInfo.Ports)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -240,26 +333,30 @@ for host, cfg := range allConfigs {
|
|||||||
|
|
||||||
| Method | Description |
|
| 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 |
|
| `GetSystem` | General system configuration and status |
|
||||||
| `GetInterfaces` | Interface configuration and status |
|
| `GetInterfaces` | Interface configuration and status |
|
||||||
| `GetVLANs` | VLAN and Trunk configuration |
|
| `GetVLANs` | VLAN and Trunk configuration |
|
||||||
| `GetServices` | State of running services (SSH, NTP, etc.) |
|
| `GetServices` | State of running services (SSH, NTP, etc.) |
|
||||||
| `GetStatistics` | Performance metrics |
|
| `GetStatistics` | Performance metrics |
|
||||||
| `GetNeighbors` | Discovered UBNT neighbors |
|
| `GetNeighbors` | Discovered UBNT neighbors |
|
||||||
| `GetDevice` | Hardware and capabilities info |
|
|
||||||
|
|
||||||
All methods have corresponding `GetAll*` variants for multi-device operations.
|
All `Get*` methods have corresponding `GetAll*` variants for multi-device operations.
|
||||||
|
|
||||||
### EdgeOS
|
### EdgeOS
|
||||||
|
|
||||||
| Method | Description |
|
| Method | Description |
|
||||||
|--------|-------------|
|
|--------|-------------|
|
||||||
| `GetConfig` | Complete device configuration |
|
| `Login` | Explicit authentication (also happens automatically on 401) |
|
||||||
|
| `GetEdgeOS` | All device data combined (AuthInfo and Config) |
|
||||||
| `GetAuthInfo` | Device authentication and feature information |
|
| `GetAuthInfo` | Device authentication and feature information |
|
||||||
|
| `GetConfig` | Complete device configuration |
|
||||||
| `GetInterfaces` | Interface configuration (ethernet and switch) |
|
| `GetInterfaces` | Interface configuration (ethernet and switch) |
|
||||||
| `GetSystem` | System configuration (hostname, domain) |
|
| `GetSystem` | System configuration (hostname, domain) |
|
||||||
|
|
||||||
All methods have corresponding `GetAll*` variants for multi-device operations.
|
All `Get*` methods have corresponding `GetAll*` variants for multi-device operations.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
48
cmd/cmd/client.go
Normal file
48
cmd/cmd/client.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
||||||
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fetchDevice retrieves device data using the pre-configured clients from context.
|
||||||
|
// It handles both ToughSwitch and EdgeOS device types.
|
||||||
|
func fetchDevice(ctx context.Context, clientConf *config.ClientConfig) (any, error) {
|
||||||
|
switch clientConf.Type {
|
||||||
|
case config.TypeToughSwitch:
|
||||||
|
return fetchToughSwitchDevice(ctx, clientConf)
|
||||||
|
case config.TypeEdgeOS:
|
||||||
|
return fetchEdgeOSDevice(ctx, clientConf)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unknown device type: %s", clientConf.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchToughSwitchDevice(ctx context.Context, clientConf *config.ClientConfig) (any, error) {
|
||||||
|
client := util.ToughSwitchClientFromContext(ctx)
|
||||||
|
if client == nil {
|
||||||
|
return nil, fmt.Errorf("toughswitch client not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := client.Login(ctx, clientConf.Host); err != nil {
|
||||||
|
return nil, fmt.Errorf("login failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.GetToughSwitch(ctx, clientConf.Host)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchEdgeOSDevice(ctx context.Context, clientConf *config.ClientConfig) (any, error) {
|
||||||
|
client := util.EdgeOSClientFromContext(ctx)
|
||||||
|
if client == nil {
|
||||||
|
return nil, fmt.Errorf("edgeos client not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := client.Login(ctx, clientConf.Host); err != nil {
|
||||||
|
return nil, fmt.Errorf("login failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.GetEdgeOS(ctx, clientConf.Host)
|
||||||
|
}
|
||||||
@@ -1,14 +1,10 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
|
||||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
||||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/edgeos"
|
|
||||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/toughswitch"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -54,49 +50,10 @@ func runGetDevice(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
logger.Debug().Str("device", deviceName).Str("type", string(clientConf.Type)).Msg("fetching device info")
|
logger.Debug().Str("device", deviceName).Str("type", string(clientConf.Type)).Msg("fetching device info")
|
||||||
|
|
||||||
var result any
|
result, err := fetchDevice(ctx, clientConf)
|
||||||
var err error
|
|
||||||
|
|
||||||
switch clientConf.Type {
|
|
||||||
case config.TypeToughSwitch:
|
|
||||||
result, err = getToughSwitchDevice(ctx, clientConf)
|
|
||||||
case config.TypeEdgeOS:
|
|
||||||
result, err = getEdgeOSDevice(ctx, clientConf)
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("unknown device type: %s", clientConf.Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get device info: %w", err)
|
return fmt.Errorf("failed to get device info: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.YAMLOutput(os.Stdout, result, pretty, colorize)
|
return util.YAMLOutput(os.Stdout, result, pretty, colorize)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getToughSwitchDevice(ctx context.Context, clientConf *config.ClientConfig) (any, error) {
|
|
||||||
cfg := toughswitch.Config{
|
|
||||||
Host: clientConf.Host,
|
|
||||||
Scheme: clientConf.Scheme,
|
|
||||||
Insecure: clientConf.Insecure,
|
|
||||||
Username: clientConf.User,
|
|
||||||
Password: clientConf.Pass,
|
|
||||||
Timeout: clientConf.Timeout,
|
|
||||||
}
|
|
||||||
|
|
||||||
client := toughswitch.MustNew(ctx, []toughswitch.Config{cfg})
|
|
||||||
return client.GetDevice(ctx, clientConf.Host)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getEdgeOSDevice(ctx context.Context, clientConf *config.ClientConfig) (any, error) {
|
|
||||||
cfg := edgeos.Config{
|
|
||||||
Host: clientConf.Host,
|
|
||||||
Scheme: clientConf.Scheme,
|
|
||||||
Insecure: clientConf.Insecure,
|
|
||||||
Username: clientConf.User,
|
|
||||||
Password: clientConf.Pass,
|
|
||||||
Timeout: clientConf.Timeout,
|
|
||||||
}
|
|
||||||
|
|
||||||
client := edgeos.MustNew(ctx, []edgeos.Config{cfg})
|
|
||||||
return client.GetConfig(ctx, clientConf.Host)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
||||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
||||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/edgeos"
|
|
||||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/toughswitch"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -63,18 +60,7 @@ func runGetDevices(cmd *cobra.Command, args []string) error {
|
|||||||
Host: cc.Host,
|
Host: cc.Host,
|
||||||
}
|
}
|
||||||
|
|
||||||
var data any
|
data, err := fetchDevice(ctx, &cc)
|
||||||
var err error
|
|
||||||
|
|
||||||
switch cc.Type {
|
|
||||||
case config.TypeToughSwitch:
|
|
||||||
data, err = fetchToughSwitch(ctx, &cc)
|
|
||||||
case config.TypeEdgeOS:
|
|
||||||
data, err = fetchEdgeOS(ctx, &cc)
|
|
||||||
default:
|
|
||||||
err = fmt.Errorf("unknown device type: %s", cc.Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Error = err.Error()
|
result.Error = err.Error()
|
||||||
result.Failed = true
|
result.Failed = true
|
||||||
@@ -93,31 +79,3 @@ func runGetDevices(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
return util.YAMLOutput(os.Stdout, results, pretty, colorize)
|
return util.YAMLOutput(os.Stdout, results, pretty, colorize)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchToughSwitch(ctx context.Context, clientConf *config.ClientConfig) (any, error) {
|
|
||||||
cfg := toughswitch.Config{
|
|
||||||
Host: clientConf.Host,
|
|
||||||
Scheme: clientConf.Scheme,
|
|
||||||
Insecure: clientConf.Insecure,
|
|
||||||
Username: clientConf.User,
|
|
||||||
Password: clientConf.Pass,
|
|
||||||
Timeout: clientConf.Timeout,
|
|
||||||
}
|
|
||||||
|
|
||||||
client := toughswitch.MustNew(ctx, []toughswitch.Config{cfg})
|
|
||||||
return client.GetDevice(ctx, clientConf.Host)
|
|
||||||
}
|
|
||||||
|
|
||||||
func fetchEdgeOS(ctx context.Context, clientConf *config.ClientConfig) (any, error) {
|
|
||||||
cfg := edgeos.Config{
|
|
||||||
Host: clientConf.Host,
|
|
||||||
Scheme: clientConf.Scheme,
|
|
||||||
Insecure: clientConf.Insecure,
|
|
||||||
Username: clientConf.User,
|
|
||||||
Password: clientConf.Pass,
|
|
||||||
Timeout: clientConf.Timeout,
|
|
||||||
}
|
|
||||||
|
|
||||||
client := edgeos.MustNew(ctx, []edgeos.Config{cfg})
|
|
||||||
return client.GetConfig(ctx, clientConf.Host)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import (
|
|||||||
|
|
||||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
||||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
||||||
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/edgeos"
|
||||||
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/toughswitch"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@@ -20,6 +22,8 @@ var preRunFuncs = []preRunFunc{
|
|||||||
validateConfigFile,
|
validateConfigFile,
|
||||||
prepareConfig,
|
prepareConfig,
|
||||||
prepareLogger,
|
prepareLogger,
|
||||||
|
setEnvironment,
|
||||||
|
prepareClients,
|
||||||
}
|
}
|
||||||
|
|
||||||
// preRun executes all registered pre-run functions in order.
|
// preRun executes all registered pre-run functions in order.
|
||||||
@@ -32,6 +36,24 @@ func preRun(cmd *cobra.Command, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setEnvironment(cmd *cobra.Command, _ []string) error {
|
||||||
|
conf := util.ConfigFromContext(cmd.Context())
|
||||||
|
if conf == nil {
|
||||||
|
zerolog.Ctx(cmd.Context()).Fatal().Msg("unconfigured, nothing to do")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, device := range conf.Clients {
|
||||||
|
if device.Type == config.TypeToughSwitch {
|
||||||
|
zerolog.Ctx(cmd.Context()).Debug().
|
||||||
|
Msg("setting GODEBUG=x509negativeserial=1 for toughswitch devices")
|
||||||
|
os.Setenv("GODEBUG", "x509negativeserial=1")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func prepareConfig(cmd *cobra.Command, _ []string) error {
|
func prepareConfig(cmd *cobra.Command, _ []string) error {
|
||||||
configFile, _ := cmd.Root().PersistentFlags().GetString(util.FLAG_CONFIG_FILE)
|
configFile, _ := cmd.Root().PersistentFlags().GetString(util.FLAG_CONFIG_FILE)
|
||||||
conf, err := config.LoadConfig(&configFile)
|
conf, err := config.LoadConfig(&configFile)
|
||||||
@@ -84,3 +106,52 @@ func validateConfigFile(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prepareClients creates toughswitch and edgeos clients based on the configuration.
|
||||||
|
// Clients are only created if there are devices of that type in the config.
|
||||||
|
func prepareClients(cmd *cobra.Command, _ []string) error {
|
||||||
|
ctx := cmd.Context()
|
||||||
|
conf := util.ConfigFromContext(ctx)
|
||||||
|
logger := zerolog.Ctx(ctx)
|
||||||
|
|
||||||
|
var tsConfigs []toughswitch.Config
|
||||||
|
var eosConfigs []edgeos.Config
|
||||||
|
|
||||||
|
for _, clientConf := range conf.Clients {
|
||||||
|
switch clientConf.Type {
|
||||||
|
case config.TypeToughSwitch:
|
||||||
|
tsConfigs = append(tsConfigs, toughswitch.Config{
|
||||||
|
Host: clientConf.Host,
|
||||||
|
Scheme: clientConf.Scheme,
|
||||||
|
Insecure: clientConf.Insecure,
|
||||||
|
Username: clientConf.User,
|
||||||
|
Password: clientConf.Pass,
|
||||||
|
Timeout: clientConf.Timeout,
|
||||||
|
})
|
||||||
|
case config.TypeEdgeOS:
|
||||||
|
eosConfigs = append(eosConfigs, edgeos.Config{
|
||||||
|
Host: clientConf.Host,
|
||||||
|
Scheme: clientConf.Scheme,
|
||||||
|
Insecure: clientConf.Insecure,
|
||||||
|
Username: clientConf.User,
|
||||||
|
Password: clientConf.Pass,
|
||||||
|
Timeout: clientConf.Timeout,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tsConfigs) > 0 {
|
||||||
|
logger.Debug().Int("count", len(tsConfigs)).Msg("creating toughswitch client")
|
||||||
|
tsClient := toughswitch.MustNew(ctx, tsConfigs)
|
||||||
|
ctx = util.ContextWithToughSwitchClient(ctx, tsClient)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(eosConfigs) > 0 {
|
||||||
|
logger.Debug().Int("count", len(eosConfigs)).Msg("creating edgeos client")
|
||||||
|
eosClient := edgeos.MustNew(ctx, eosConfigs)
|
||||||
|
ctx = util.ContextWithEdgeOSClient(ctx, eosClient)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.SetContext(ctx)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,22 +4,25 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
||||||
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/edgeos"
|
||||||
|
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/toughswitch"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CmdContextVal uint8
|
type ctxKey uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CTX_CONFIG CmdContextVal = iota
|
ctxConfig ctxKey = iota
|
||||||
CTX_LOGGER
|
ctxToughSwitchClient
|
||||||
|
ctxEdgeOSClient
|
||||||
)
|
)
|
||||||
|
|
||||||
func ContextWithConfig(baseCtx context.Context, config *config.ClientsConfig) context.Context {
|
func ContextWithConfig(baseCtx context.Context, config *config.ClientsConfig) context.Context {
|
||||||
return context.WithValue(baseCtx, CTX_CONFIG, config)
|
return context.WithValue(baseCtx, ctxConfig, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfigFromContext(ctx context.Context) *config.ClientsConfig {
|
func ConfigFromContext(ctx context.Context) *config.ClientsConfig {
|
||||||
val := ctx.Value(CTX_CONFIG)
|
val := ctx.Value(ctxConfig)
|
||||||
conf, ok := val.(*config.ClientsConfig)
|
conf, ok := val.(*config.ClientsConfig)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
@@ -28,16 +31,42 @@ func ConfigFromContext(ctx context.Context) *config.ClientsConfig {
|
|||||||
return conf
|
return conf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ContextWithLogger stores the logger in context using zerolog's built-in method.
|
||||||
func ContextWithLogger(baseCtx context.Context, logger *zerolog.Logger) context.Context {
|
func ContextWithLogger(baseCtx context.Context, logger *zerolog.Logger) context.Context {
|
||||||
return context.WithValue(baseCtx, CTX_LOGGER, logger)
|
return logger.WithContext(baseCtx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoggerFromContext retrieves the logger from context using zerolog's built-in method.
|
||||||
func LoggerFromContext(ctx context.Context) *zerolog.Logger {
|
func LoggerFromContext(ctx context.Context) *zerolog.Logger {
|
||||||
val := ctx.Value(CTX_LOGGER)
|
return zerolog.Ctx(ctx)
|
||||||
logger, ok := val.(*zerolog.Logger)
|
}
|
||||||
|
|
||||||
|
// ContextWithToughSwitchClient stores a toughswitch client in the context.
|
||||||
|
func ContextWithToughSwitchClient(baseCtx context.Context, client *toughswitch.Client) context.Context {
|
||||||
|
return context.WithValue(baseCtx, ctxToughSwitchClient, client)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToughSwitchClientFromContext retrieves the toughswitch client from context.
|
||||||
|
func ToughSwitchClientFromContext(ctx context.Context) *toughswitch.Client {
|
||||||
|
val := ctx.Value(ctxToughSwitchClient)
|
||||||
|
client, ok := val.(*toughswitch.Client)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
return client
|
||||||
return logger
|
}
|
||||||
|
|
||||||
|
// ContextWithEdgeOSClient stores an edgeos client in the context.
|
||||||
|
func ContextWithEdgeOSClient(baseCtx context.Context, client *edgeos.Client) context.Context {
|
||||||
|
return context.WithValue(baseCtx, ctxEdgeOSClient, client)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EdgeOSClientFromContext retrieves the edgeos client from context.
|
||||||
|
func EdgeOSClientFromContext(ctx context.Context) *edgeos.Client {
|
||||||
|
val := ctx.Value(ctxEdgeOSClient)
|
||||||
|
client, ok := val.(*edgeos.Client)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return client
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -203,3 +203,79 @@ func (c *Client) GetAllSystems(ctx context.Context) (map[string]*SystemConfig, e
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
return results, errs
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -119,6 +119,17 @@ func (c *Client) getDeviceByHost(host string) (*deviceClient, error) {
|
|||||||
return d, nil
|
return d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Login authenticates with the device at the given host.
|
||||||
|
// This is called automatically on 401 responses, but can be called explicitly
|
||||||
|
// to pre-authenticate before making requests.
|
||||||
|
func (c *Client) Login(ctx context.Context, host string) error {
|
||||||
|
d, err := c.getDeviceByHost(host)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return d.login(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
func (d *deviceClient) login(ctx context.Context) error {
|
func (d *deviceClient) login(ctx context.Context) error {
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
defer d.mu.Unlock()
|
defer d.mu.Unlock()
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
package edgeos
|
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.
|
// AuthResponse represents the authentication response from login2 endpoint.
|
||||||
type AuthResponse struct {
|
type AuthResponse struct {
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
|
|||||||
@@ -348,3 +348,134 @@ func (c *Client) GetAllNeighbors(ctx context.Context) (map[string][]Neighbor, er
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
return results, errs
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -120,6 +120,17 @@ func (c *Client) getDeviceByHost(host string) (*deviceClient, error) {
|
|||||||
return d, nil
|
return d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Login authenticates with the device at the given host.
|
||||||
|
// This is called automatically on 401 responses, but can be called explicitly
|
||||||
|
// to pre-authenticate before making requests.
|
||||||
|
func (c *Client) Login(ctx context.Context, host string) error {
|
||||||
|
d, err := c.getDeviceByHost(host)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return d.login(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
func (d *deviceClient) login(ctx context.Context) error {
|
func (d *deviceClient) login(ctx context.Context) error {
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
defer d.mu.Unlock()
|
defer d.mu.Unlock()
|
||||||
|
|||||||
@@ -1,5 +1,16 @@
|
|||||||
package toughswitch
|
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.
|
// LoginResponse represents the response from the login endpoint.
|
||||||
type LoginResponse struct {
|
type LoginResponse struct {
|
||||||
StatusCode int `json:"statusCode"`
|
StatusCode int `json:"statusCode"`
|
||||||
|
|||||||
Reference in New Issue
Block a user