Cleanup consul/config

This commit is contained in:
Frank Schroeder 2017-05-03 21:02:01 +02:00 committed by Frank Schröder
parent 10acfb7682
commit 42c4265901
3 changed files with 21 additions and 21 deletions

View File

@ -84,7 +84,7 @@ type Client struct {
// configuration, potentially returning an error // configuration, potentially returning an error
func NewClient(config *Config) (*Client, error) { func NewClient(config *Config) (*Client, error) {
// Check the protocol version // Check the protocol version
if err := config.CheckVersion(); err != nil { if err := config.CheckProtocolVersion(); err != nil {
return nil, err return nil, err
} }

View File

@ -17,6 +17,7 @@ import (
const ( const (
DefaultDC = "dc1" DefaultDC = "dc1"
DefaultRPCPort = 8300
DefaultLANSerfPort = 8301 DefaultLANSerfPort = 8301
DefaultWANSerfPort = 8302 DefaultWANSerfPort = 8302
@ -31,13 +32,13 @@ const (
) )
var ( var (
DefaultRPCAddr = &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 8300} DefaultRPCAddr = &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: DefaultRPCPort}
)
// ProtocolVersionMap is the mapping of Consul protocol versions // ProtocolVersionMap is the mapping of Consul protocol versions
// to Serf protocol versions. We mask the Serf protocols using // to Serf protocol versions. We mask the Serf protocols using
// our own protocol version. // our own protocol version.
var protocolVersionMap map[uint8]uint8 protocolVersionMap map[uint8]uint8
)
func init() { func init() {
protocolVersionMap = map[uint8]uint8{ protocolVersionMap = map[uint8]uint8{
@ -307,19 +308,18 @@ type Config struct {
AutopilotInterval time.Duration AutopilotInterval time.Duration
} }
// CheckVersion is used to check if the ProtocolVersion is valid // CheckProtocolVersion validates the protocol version.
func (c *Config) CheckVersion() error { func (c *Config) CheckProtocolVersion() error {
if c.ProtocolVersion < ProtocolVersionMin { if c.ProtocolVersion < ProtocolVersionMin {
return fmt.Errorf("Protocol version '%d' too low. Must be in range: [%d, %d]", return fmt.Errorf("Protocol version '%d' too low. Must be in range: [%d, %d]", c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax)
c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax) }
} else if c.ProtocolVersion > ProtocolVersionMax { if c.ProtocolVersion > ProtocolVersionMax {
return fmt.Errorf("Protocol version '%d' too high. Must be in range: [%d, %d]", return fmt.Errorf("Protocol version '%d' too high. Must be in range: [%d, %d]", c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax)
c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax)
} }
return nil return nil
} }
// CheckACL is used to sanity check the ACL configuration // CheckACL validates the ACL configuration.
func (c *Config) CheckACL() error { func (c *Config) CheckACL() error {
switch c.ACLDefaultPolicy { switch c.ACLDefaultPolicy {
case "allow": case "allow":
@ -337,7 +337,7 @@ func (c *Config) CheckACL() error {
return nil return nil
} }
// DefaultConfig is used to return a sane default configuration // DefaultConfig returns a sane default configuration.
func DefaultConfig() *Config { func DefaultConfig() *Config {
hostname, err := os.Hostname() hostname, err := os.Hostname()
if err != nil { if err != nil {
@ -453,9 +453,9 @@ func (c *Config) tlsConfig() *tlsutil.Config {
func (c *Config) GetTokenForAgent() string { func (c *Config) GetTokenForAgent() string {
if c.ACLAgentToken != "" { if c.ACLAgentToken != "" {
return c.ACLAgentToken return c.ACLAgentToken
} else if c.ACLToken != "" {
return c.ACLToken
} else {
return ""
} }
if c.ACLToken != "" {
return c.ACLToken
}
return ""
} }

View File

@ -210,7 +210,7 @@ type endpoints struct {
// configuration, potentially returning an error // configuration, potentially returning an error
func NewServer(config *Config) (*Server, error) { func NewServer(config *Config) (*Server, error) {
// Check the protocol version. // Check the protocol version.
if err := config.CheckVersion(); err != nil { if err := config.CheckProtocolVersion(); err != nil {
return nil, err return nil, err
} }