Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 3 additions & 30 deletions receiver/ciscoosreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,21 @@ import (
"fmt"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/xconfmap"
"go.opentelemetry.io/collector/scraper/scraperhelper"
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/ciscoosreceiver/internal/connection"
)

// Config defines configuration for Cisco OS receiver.
type Config struct {
scraperhelper.ControllerConfig `mapstructure:",squash"`
Device DeviceConfig `mapstructure:"device"`
Device connection.DeviceConfig `mapstructure:"device"`
Scrapers map[component.Type]component.Config `mapstructure:"-"`
}

// DeviceConfig represents configuration for a single Cisco device using semantic conventions
type DeviceConfig struct {
Device DeviceInfo `mapstructure:"device"`
Auth AuthConfig `mapstructure:"auth"`
}

// DeviceInfo follows semantic conventions for device identification
type DeviceInfo struct {
// DO NOT USE unkeyed struct initialization
_ struct{} `mapstructure:"-"`

Host HostInfo `mapstructure:"host"`
}

// HostInfo contains host-specific information
type HostInfo struct {
Name string `mapstructure:"name"`
IP string `mapstructure:"ip"`
Port int `mapstructure:"port"`
}

// AuthConfig represents authentication configuration
type AuthConfig struct {
Username string `mapstructure:"username"`
Password configopaque.String `mapstructure:"password"`
KeyFile string `mapstructure:"key_file"`
}

var (
_ xconfmap.Validator = (*Config)(nil)
_ confmap.Unmarshaler = (*Config)(nil)
Expand Down
52 changes: 27 additions & 25 deletions receiver/ciscoosreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/scraper/scraperhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/ciscoosreceiver/internal/connection"
)

func TestConfigValidate(t *testing.T) {
Expand All @@ -27,15 +29,15 @@ func TestConfigValidate(t *testing.T) {
Timeout: 30 * time.Second,
CollectionInterval: 60 * time.Second,
},
Device: DeviceConfig{
Device: DeviceInfo{
Host: HostInfo{
Device: connection.DeviceConfig{
Device: connection.DeviceInfo{
Host: connection.HostInfo{
Name: "test-device",
IP: "192.168.1.1",
Port: 22,
},
},
Auth: AuthConfig{
Auth: connection.AuthConfig{
Username: "admin",
Password: configopaque.String("password"),
},
Expand All @@ -53,15 +55,15 @@ func TestConfigValidate(t *testing.T) {
Timeout: 30 * time.Second,
CollectionInterval: 60 * time.Second,
},
Device: DeviceConfig{
Device: DeviceInfo{
Host: HostInfo{
Device: connection.DeviceConfig{
Device: connection.DeviceInfo{
Host: connection.HostInfo{
Name: "test-device",
IP: "192.168.1.1",
Port: 22,
},
},
Auth: AuthConfig{
Auth: connection.AuthConfig{
Username: "admin",
KeyFile: "/path/to/key",
},
Expand All @@ -79,7 +81,7 @@ func TestConfigValidate(t *testing.T) {
Timeout: 30 * time.Second,
CollectionInterval: 60 * time.Second,
},
Device: DeviceConfig{},
Device: connection.DeviceConfig{},
Scrapers: map[component.Type]component.Config{
component.MustNewType("system"): nil,
},
Expand All @@ -93,15 +95,15 @@ func TestConfigValidate(t *testing.T) {
Timeout: 30 * time.Second,
CollectionInterval: 60 * time.Second,
},
Device: DeviceConfig{
Device: DeviceInfo{
Host: HostInfo{
Device: connection.DeviceConfig{
Device: connection.DeviceInfo{
Host: connection.HostInfo{
Name: "test-device",
IP: "192.168.1.1",
Port: 0,
},
},
Auth: AuthConfig{
Auth: connection.AuthConfig{
Username: "admin",
Password: configopaque.String("password"),
},
Expand All @@ -119,15 +121,15 @@ func TestConfigValidate(t *testing.T) {
Timeout: 30 * time.Second,
CollectionInterval: 60 * time.Second,
},
Device: DeviceConfig{
Device: DeviceInfo{
Host: HostInfo{
Device: connection.DeviceConfig{
Device: connection.DeviceInfo{
Host: connection.HostInfo{
Name: "test-device",
IP: "192.168.1.1",
Port: 22,
},
},
Auth: AuthConfig{
Auth: connection.AuthConfig{
Username: "",
Password: configopaque.String("password"),
},
Expand All @@ -145,15 +147,15 @@ func TestConfigValidate(t *testing.T) {
Timeout: 30 * time.Second,
CollectionInterval: 60 * time.Second,
},
Device: DeviceConfig{
Device: DeviceInfo{
Host: HostInfo{
Device: connection.DeviceConfig{
Device: connection.DeviceInfo{
Host: connection.HostInfo{
Name: "test-device",
IP: "192.168.1.1",
Port: 22,
},
},
Auth: AuthConfig{
Auth: connection.AuthConfig{
Username: "admin",
},
},
Expand All @@ -170,15 +172,15 @@ func TestConfigValidate(t *testing.T) {
Timeout: 30 * time.Second,
CollectionInterval: 60 * time.Second,
},
Device: DeviceConfig{
Device: DeviceInfo{
Host: HostInfo{
Device: connection.DeviceConfig{
Device: connection.DeviceInfo{
Host: connection.HostInfo{
Name: "test-device",
IP: "192.168.1.1",
Port: 22,
},
},
Auth: AuthConfig{
Auth: connection.AuthConfig{
Username: "admin",
Password: configopaque.String("password"),
},
Expand Down
34 changes: 2 additions & 32 deletions receiver/ciscoosreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func createMetricsReceiver(

// Inject device configuration into scraper config
if sysCfg, ok := scraperCfg.(*systemscraper.Config); ok {
sysCfg.Device = convertToSystemScraperDeviceConfig(conf.Device)
sysCfg.Device = conf.Device
}
if intfCfg, ok := scraperCfg.(*interfacesscraper.Config); ok {
intfCfg.Device = convertToInterfacesScraperDeviceConfig(conf.Device)
intfCfg.Device = conf.Device
}

scraperOptions = append(scraperOptions, scraperhelper.AddFactoryWithConfig(factory, scraperCfg))
Expand All @@ -87,36 +87,6 @@ func createMetricsReceiver(
)
}

func convertToSystemScraperDeviceConfig(device DeviceConfig) systemscraper.DeviceConfig {
return systemscraper.DeviceConfig{
Host: systemscraper.HostInfo{
Name: device.Device.Host.Name,
IP: device.Device.Host.IP,
Port: device.Device.Host.Port,
},
Auth: systemscraper.AuthConfig{
Username: device.Auth.Username,
Password: string(device.Auth.Password),
KeyFile: device.Auth.KeyFile,
},
}
}

func convertToInterfacesScraperDeviceConfig(device DeviceConfig) interfacesscraper.DeviceConfig {
return interfacesscraper.DeviceConfig{
Host: interfacesscraper.HostInfo{
Name: device.Device.Host.Name,
IP: device.Device.Host.IP,
Port: device.Device.Host.Port,
},
Auth: interfacesscraper.AuthConfig{
Username: device.Auth.Username,
Password: string(device.Auth.Password),
KeyFile: device.Auth.KeyFile,
},
}
}

type nopMetricsReceiver struct{}

func (*nopMetricsReceiver) Start(_ context.Context, _ component.Host) error { return nil }
Expand Down
25 changes: 13 additions & 12 deletions receiver/ciscoosreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/ciscoosreceiver/internal/connection"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/ciscoosreceiver/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/ciscoosreceiver/internal/scraper/interfacesscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/ciscoosreceiver/internal/scraper/systemscraper"
Expand Down Expand Up @@ -45,15 +46,15 @@ func TestCreateMetricsReceiver(t *testing.T) {

// Add a device and scraper to make config valid
config := cfg.(*Config)
config.Device = DeviceConfig{
Device: DeviceInfo{
Host: HostInfo{
config.Device = connection.DeviceConfig{
Device: connection.DeviceInfo{
Host: connection.HostInfo{
Name: "test-device",
IP: "192.168.1.1",
Port: 22,
},
},
Auth: AuthConfig{
Auth: connection.AuthConfig{
Username: "admin",
Password: configopaque.String("password"),
},
Expand Down Expand Up @@ -85,15 +86,15 @@ func TestCreateMetricsReceiverWithInterfacesScraper(t *testing.T) {

// Add a device and interfaces scraper
config := cfg.(*Config)
config.Device = DeviceConfig{
Device: DeviceInfo{
Host: HostInfo{
config.Device = connection.DeviceConfig{
Device: connection.DeviceInfo{
Host: connection.HostInfo{
Name: "test-device",
IP: "192.168.1.1",
Port: 22,
},
},
Auth: AuthConfig{
Auth: connection.AuthConfig{
Username: "admin",
Password: configopaque.String("password"),
},
Expand All @@ -118,15 +119,15 @@ func TestCreateMetricsReceiverWithBothScrapers(t *testing.T) {

// Add a device and both scrapers
config := cfg.(*Config)
config.Device = DeviceConfig{
Device: DeviceInfo{
Host: HostInfo{
config.Device = connection.DeviceConfig{
Device: connection.DeviceInfo{
Host: connection.HostInfo{
Name: "test-device",
IP: "192.168.1.1",
Port: 22,
},
},
Auth: AuthConfig{
Auth: connection.AuthConfig{
Username: "admin",
Password: configopaque.String("password"),
},
Expand Down
34 changes: 34 additions & 0 deletions receiver/ciscoosreceiver/internal/connection/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package connection // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/ciscoosreceiver/internal/connection"

import "go.opentelemetry.io/collector/config/configopaque"

// DeviceConfig represents configuration for a single Cisco device using semantic conventions
type DeviceConfig struct {
Device DeviceInfo `mapstructure:"device"`
Auth AuthConfig `mapstructure:"auth"`
}

// DeviceInfo follows semantic conventions for device identification
type DeviceInfo struct {
// DO NOT USE unkeyed struct initialization
_ struct{} `mapstructure:"-"`

Host HostInfo `mapstructure:"host"`
}

// HostInfo contains host-specific information
type HostInfo struct {
Name string `mapstructure:"name"`
IP string `mapstructure:"ip"`
Port int `mapstructure:"port"`
}

// AuthConfig represents authentication configuration
type AuthConfig struct {
Username string `mapstructure:"username"`
Password configopaque.String `mapstructure:"password"`
KeyFile string `mapstructure:"key_file"`
}
Loading