Re-use uint8ToString

This commit is contained in:
Kyle Havlovitz 2018-05-25 10:28:18 -07:00 committed by Mitchell Hashimoto
parent 61d7e38148
commit ab4a9a94f4
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 16 additions and 19 deletions

View File

@ -37,14 +37,6 @@ func ParseConsulCAConfig(raw map[string]interface{}) (*structs.ConsulCAProviderC
// ParseDurationFunc is a mapstructure hook for decoding a string or // ParseDurationFunc is a mapstructure hook for decoding a string or
// []uint8 into a time.Duration value. // []uint8 into a time.Duration value.
func ParseDurationFunc() mapstructure.DecodeHookFunc { func ParseDurationFunc() mapstructure.DecodeHookFunc {
uint8ToString := func(bs []uint8) string {
b := make([]byte, len(bs))
for i, v := range bs {
b[i] = byte(v)
}
return string(b)
}
return func( return func(
f reflect.Type, f reflect.Type,
t reflect.Type, t reflect.Type,
@ -63,7 +55,7 @@ func ParseDurationFunc() mapstructure.DecodeHookFunc {
} }
return v, nil return v, nil
case f == reflect.SliceOf(reflect.TypeOf(uint8(0))): case f == reflect.SliceOf(reflect.TypeOf(uint8(0))):
s := uint8ToString(data.([]uint8)) s := Uint8ToString(data.([]uint8))
if dur, err := time.ParseDuration(s); err != nil { if dur, err := time.ParseDuration(s); err != nil {
return nil, err return nil, err
} else { } else {
@ -75,3 +67,11 @@ func ParseDurationFunc() mapstructure.DecodeHookFunc {
} }
} }
} }
func Uint8ToString(bs []uint8) string {
b := make([]byte, len(bs))
for i, v := range bs {
b[i] = byte(v)
}
return string(b)
}

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/hashicorp/consul/agent/connect/ca"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
) )
@ -47,8 +48,12 @@ func (s *HTTPServer) ConnectCAConfigurationGet(resp http.ResponseWriter, req *ht
var reply structs.CAConfiguration var reply structs.CAConfiguration
err := s.agent.RPC("ConnectCA.ConfigurationGet", &args, &reply) err := s.agent.RPC("ConnectCA.ConfigurationGet", &args, &reply)
if err != nil {
return nil, err
}
fixupConfig(&reply) fixupConfig(&reply)
return reply, err return reply, nil
} }
// PUT /v1/connect/ca/configuration // PUT /v1/connect/ca/configuration
@ -77,16 +82,8 @@ func fixupConfig(conf *structs.CAConfiguration) {
if conf.Provider == structs.ConsulCAProvider { if conf.Provider == structs.ConsulCAProvider {
if v, ok := conf.Config["RotationPeriod"]; ok { if v, ok := conf.Config["RotationPeriod"]; ok {
if raw, ok := v.([]uint8); ok { if raw, ok := v.([]uint8); ok {
conf.Config["RotationPeriod"] = uint8ToString(raw) conf.Config["RotationPeriod"] = ca.Uint8ToString(raw)
} }
} }
} }
} }
func uint8ToString(bs []uint8) string {
b := make([]byte, len(bs))
for i, v := range bs {
b[i] = byte(v)
}
return string(b)
}