diff --git a/cmd/cmd/client.go b/cmd/cmd/client.go index dd0ec35..1080468 100644 --- a/cmd/cmd/client.go +++ b/cmd/cmd/client.go @@ -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) { diff --git a/pkg/toughswitch/api.go b/pkg/toughswitch/api.go index 2adbabd..5264bb8 100644 --- a/pkg/toughswitch/api.go +++ b/pkg/toughswitch/api.go @@ -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 +} diff --git a/pkg/toughswitch/types.go b/pkg/toughswitch/types.go index 229f14a..5b8904e 100644 --- a/pkg/toughswitch/types.go +++ b/pkg/toughswitch/types.go @@ -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"`