some api changes

This commit is contained in:
ston1th 2021-10-19 21:24:14 +02:00
commit 70fd3f71e5
11 changed files with 118 additions and 76 deletions

View file

@ -23,6 +23,7 @@ type Client struct {
endpoint string
username string
password string
auth bool
httpClient *http.Client
debugWriter io.Writer
insecure bool
@ -62,6 +63,7 @@ func WithCredentials(username, password string) ClientOption {
return func(client *Client) {
client.username = username
client.password = password
client.auth = true
}
}
@ -77,6 +79,7 @@ var (
type Config struct {
Endpoint string `json:"endpoint"`
NoAuth bool `json:"no_auth"`
User string `json:"user"`
Password string `json:"password"`
Insecure bool `json:"insecure"`
@ -87,6 +90,13 @@ func ClientOptionsFromConfig(cfg Config) (options []ClientOption, err error) {
err = ErrMissingEndpoint
return
}
options = []ClientOption{
WithEndpoint(cfg.Endpoint),
WithInsecureClient(cfg.Insecure),
}
if cfg.NoAuth {
return
}
if cfg.User == "" {
err = ErrMissingUser
return
@ -95,11 +105,7 @@ func ClientOptionsFromConfig(cfg Config) (options []ClientOption, err error) {
err = ErrMissingPassword
return
}
options = []ClientOption{
WithEndpoint(cfg.Endpoint),
WithCredentials(cfg.User, cfg.Password),
WithInsecureClient(cfg.Insecure),
}
options = append(options, WithCredentials(cfg.User, cfg.Password))
return
}
@ -109,6 +115,17 @@ func ClientOptionsFromEnv() (options []ClientOption, err error) {
err = ErrMissingEndpointEnv
return
}
insecure, _ := strconv.ParseBool(os.Getenv("HAPROXY_LB_INSECURE"))
options = []ClientOption{
WithEndpoint(endpoint),
WithInsecureClient(insecure),
}
noAuth, _ := strconv.ParseBool(os.Getenv("HAPROXY_LB_NO_AUTH"))
if noAuth {
return
}
user := os.Getenv("HAPROXY_LB_USER")
if user == "" {
err = ErrMissingUserEnv
@ -121,13 +138,7 @@ func ClientOptionsFromEnv() (options []ClientOption, err error) {
return
}
insecure, _ := strconv.ParseBool(os.Getenv("HAPROXY_LB_INSECURE"))
options = []ClientOption{
WithEndpoint(endpoint),
WithCredentials(user, pass),
WithInsecureClient(insecure),
}
options = append(options, WithCredentials(user, pass))
return
}
@ -163,7 +174,9 @@ func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Re
if err != nil {
return nil, err
}
req.SetBasicAuth(c.username, c.password)
if c.auth {
req.SetBasicAuth(c.username, c.password)
}
req = req.WithContext(ctx)
return req, nil
}