config.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package discov
  2. import "errors"
  3. var (
  4. // errEmptyEtcdHosts indicates that etcd hosts are empty.
  5. errEmptyEtcdHosts = errors.New("empty etcd hosts")
  6. // errEmptyEtcdKey indicates that etcd key is empty.
  7. errEmptyEtcdKey = errors.New("empty etcd key")
  8. )
  9. // EtcdConf is the config item with the given key on etcd.
  10. type EtcdConf struct {
  11. Hosts []string
  12. Key string
  13. ID int64 `json:",optional"`
  14. User string `json:",optional"`
  15. Pass string `json:",optional"`
  16. CertFile string `json:",optional"`
  17. CertKeyFile string `json:",optional=CertFile"`
  18. CACertFile string `json:",optional=CertFile"`
  19. InsecureSkipVerify bool `json:",optional"`
  20. }
  21. // HasAccount returns if account provided.
  22. func (c EtcdConf) HasAccount() bool {
  23. return len(c.User) > 0 && len(c.Pass) > 0
  24. }
  25. // HasID returns if ID provided.
  26. func (c EtcdConf) HasID() bool {
  27. return c.ID > 0
  28. }
  29. // HasTLS returns if TLS CertFile/CertKeyFile/CACertFile are provided.
  30. func (c EtcdConf) HasTLS() bool {
  31. return len(c.CertFile) > 0 && len(c.CertKeyFile) > 0 && len(c.CACertFile) > 0
  32. }
  33. // Validate validates c.
  34. func (c EtcdConf) Validate() error {
  35. if len(c.Hosts) == 0 {
  36. return errEmptyEtcdHosts
  37. } else if len(c.Key) == 0 {
  38. return errEmptyEtcdKey
  39. } else {
  40. return nil
  41. }
  42. }