Merge pull request #1723 from hashicorp/nil-config-client

Use default config and read environment by default while creating client object
This commit is contained in:
Vishal Nayak 2016-08-12 13:09:37 -04:00 committed by GitHub
commit 14c508ea91
2 changed files with 42 additions and 0 deletions

View File

@ -238,6 +238,13 @@ type Client struct {
// automatically added to the client. Otherwise, you must manually call // automatically added to the client. Otherwise, you must manually call
// `SetToken()`. // `SetToken()`.
func NewClient(c *Config) (*Client, error) { func NewClient(c *Config) (*Client, error) {
if c == nil {
c = DefaultConfig()
if err := c.ReadEnvironment(); err != nil {
return nil, fmt.Errorf("error reading environment: %v", err)
}
}
u, err := url.Parse(c.Address) u, err := url.Parse(c.Address)
if err != nil { if err != nil {
return nil, err return nil, err
@ -271,6 +278,18 @@ func NewClient(c *Config) (*Client, error) {
return client, nil return client, nil
} }
// Sets the address of Vault in the client. The format of address should be
// "<Scheme>://<Host>:<Port>". Setting this on a client will override the
// value of VAULT_ADDR environment variable.
func (c *Client) SetAddress(addr string) error {
var err error
if c.addr, err = url.Parse(addr); err != nil {
return fmt.Errorf("failed to set address: %v", err)
}
return nil
}
// SetWrappingLookupFunc sets a lookup function that returns desired wrap TTLs // SetWrappingLookupFunc sets a lookup function that returns desired wrap TTLs
// for a given operation and path // for a given operation and path
func (c *Client) SetWrappingLookupFunc(lookupFunc WrappingLookupFunc) { func (c *Client) SetWrappingLookupFunc(lookupFunc WrappingLookupFunc) {

View File

@ -36,6 +36,29 @@ func TestDefaultConfig_envvar(t *testing.T) {
} }
} }
func TestClientNilConfig(t *testing.T) {
client, err := NewClient(nil)
if err != nil {
t.Fatal(err)
}
if client == nil {
t.Fatal("expected a non-nil client")
}
}
func TestClientSetAddress(t *testing.T) {
client, err := NewClient(nil)
if err != nil {
t.Fatal(err)
}
if err := client.SetAddress("http://172.168.2.1:8300"); err != nil {
t.Fatal(err)
}
if client.addr.Host != "172.168.2.1:8300" {
t.Fatalf("bad: expected: '172.168.2.1:8300' actual: %q", client.addr.Host)
}
}
func TestClientToken(t *testing.T) { func TestClientToken(t *testing.T) {
tokenValue := "foo" tokenValue := "foo"
handler := func(w http.ResponseWriter, req *http.Request) {} handler := func(w http.ResponseWriter, req *http.Request) {}