config.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package vcenterreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver"
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/url"
  8. "go.opentelemetry.io/collector/config/configopaque"
  9. "go.opentelemetry.io/collector/config/configtls"
  10. "go.opentelemetry.io/collector/receiver/scraperhelper"
  11. "go.uber.org/multierr"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver/internal/metadata"
  13. )
  14. // Config is the configuration of the receiver
  15. type Config struct {
  16. scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
  17. configtls.TLSClientSetting `mapstructure:"tls,omitempty"`
  18. metadata.MetricsBuilderConfig `mapstructure:",squash"`
  19. Endpoint string `mapstructure:"endpoint"`
  20. Username string `mapstructure:"username"`
  21. Password configopaque.String `mapstructure:"password"`
  22. }
  23. // Validate checks to see if the supplied config will work for the receiver
  24. func (c *Config) Validate() error {
  25. if c.Endpoint == "" {
  26. return errors.New("no endpoint was provided")
  27. }
  28. var err error
  29. res, err := url.Parse(c.Endpoint)
  30. if err != nil {
  31. err = multierr.Append(err, fmt.Errorf("unable to parse url %s: %w", c.Endpoint, err))
  32. return err
  33. }
  34. if res.Scheme != "http" && res.Scheme != "https" {
  35. err = multierr.Append(err, errors.New("url scheme must be http or https"))
  36. }
  37. if c.Username == "" {
  38. err = multierr.Append(err, errors.New("username not provided and is required"))
  39. }
  40. if c.Password == "" {
  41. err = multierr.Append(err, errors.New("password not provided and is required"))
  42. }
  43. if _, tlsErr := c.LoadTLSConfig(); err != nil {
  44. err = multierr.Append(err, fmt.Errorf("error loading tls configuration: %w", tlsErr))
  45. }
  46. return err
  47. }
  48. // SDKUrl returns the url for the vCenter SDK
  49. func (c *Config) SDKUrl() (*url.URL, error) {
  50. res, err := url.Parse(c.Endpoint)
  51. if err != nil {
  52. return res, err
  53. }
  54. res.Path = "/sdk"
  55. return res, nil
  56. }