Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Add Listen Address resolver #186

Merged
merged 3 commits into from
Aug 15, 2018
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
100 changes: 100 additions & 0 deletions config/listenaddress/listenaddress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Package listenaddress provides a configuration struct for resolving
// a listen address from YAML.
package listenaddress

import (
"errors"
"fmt"
"os"
"strconv"
)

// Resolver is a type of port resolver
type Resolver string

const (
// ConfigResolver resolves port using a value provided in config
ConfigResolver Resolver = "config"
// EnvironmentResolver resolves port using an environment variable
// of which the name is provided in config
EnvironmentResolver Resolver = "environment"
)

// Configuration is the configuration for resolving a listen address.
type Configuration struct {
// Hostname is the hostname to use
Hostname string `yaml:"hostname" validate:"nonzero"`

// Port specifies the port to listen on
Port *Port `yaml:"port" validate:"nonzero"`
}

// Port specifies the port to use
type Port struct {
// PortType is the port type for the port
PortType Resolver `yaml:"portType" validate:"nonzero"`

// Value is the config specified port if using config port type.
Value *int `yaml:"value"`

// EnvVarName is the environment specified port if using environment port type.
EnvVarName *string `yaml:"envVarName"`
}

// Resolve returns the resolved listen address given the configuration.
func (c Configuration) Resolve() (string, error) {
if c.Port == nil {
return "", errors.New("missing port config")
}
p := c.Port

var port int
switch p.PortType {
case ConfigResolver:
if p.Value == nil {
err := fmt.Errorf("missing port type using: resolver=%s",
string(p.PortType))
return "", err
}
port = *p.Value
case EnvironmentResolver:
if p.EnvVarName == nil {
err := fmt.Errorf("missing port env var name using: resolver=%s",
string(p.PortType))
return "", err
}
portStr := os.Getenv(*p.EnvVarName)
var err error
port, err = strconv.Atoi(portStr)
if err != nil {
err := fmt.Errorf("invalid port env var value using: resolver=%s, name=%s",
string(p.PortType), *p.EnvVarName)
return "", err
}
default:
return "", fmt.Errorf("unknown port type: resolver=%s",
string(p.PortType))
}

return fmt.Sprintf("%s:%d", c.Hostname, port), nil
}
138 changes: 138 additions & 0 deletions config/listenaddress/listenaddress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Package listenaddress provides a configuration struct for resolving
// a listen address from YAML.
package listenaddress

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
port = 9000
hostname = "0.0.0.0"
)

func TestListenAddressResolver(t *testing.T) {
cfg := Configuration{
Hostname: hostname,
Port: &Port{
PortType: ConfigResolver,
Value: &port,
},
}

value, err := cfg.Resolve()
require.NoError(t, err)

assert.Equal(t, "0.0.0.0:9000", value)
}

func TestConfigResolverErrorWhenMissing(t *testing.T) {
cfg := Configuration{
Hostname: hostname,
}

_, err := cfg.Resolve()
require.Error(t, err)
}

func TestEnvironmentVariableResolver(t *testing.T) {
varName := "PORT_ENV_NAME"
expected := "9000"

require.NoError(t, os.Setenv(varName, expected))

cfg := Configuration{
Hostname: hostname,
Port: &Port{
PortType: EnvironmentResolver,
EnvVarName: &varName,
},
}

value, err := cfg.Resolve()
require.NoError(t, err)

assert.Equal(t, "0.0.0.0:9000", value)
}

func TestInvalidEnvironmentVariableResolver(t *testing.T) {
varName := "PORT_ENV_NAME"
expected := "foo"

require.NoError(t, os.Setenv(varName, expected))

cfg := Configuration{
Hostname: hostname,
Port: &Port{
PortType: EnvironmentResolver,
EnvVarName: &varName,
},
}

_, err := cfg.Resolve()
require.Error(t, err)
}

func TestEnvironmentResolverErrorWhenNameMissing(t *testing.T) {
cfg := Configuration{
Hostname: hostname,
Port: &Port{
PortType: EnvironmentResolver,
},
}

_, err := cfg.Resolve()
require.Error(t, err)
}

func TestEnvironmentResolverErrorWhenValueMissing(t *testing.T) {
varName := "PORT_ENV_NAME_OTHER"

cfg := Configuration{
Hostname: hostname,
Port: &Port{
PortType: EnvironmentResolver,
EnvVarName: &varName,
},
}

_, err := cfg.Resolve()
require.Error(t, err)
}

func TestUnknownResolverError(t *testing.T) {
cfg := Configuration{
Hostname: hostname,
Port: &Port{
PortType: "some-unknown-resolver",
Value: &port,
},
}

_, err := cfg.Resolve()
require.Error(t, err)
}