Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
Signed-off-by: clyang82 <chuyang@redhat.com>
  • Loading branch information
clyang82 committed Jan 6, 2022
1 parent eabd9c4 commit d6a5a05
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
10 changes: 3 additions & 7 deletions cli/config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ package config

import (
"io/ioutil"
"os"

promconfig "github.com/prometheus/common/config"
"gopkg.in/yaml.v2"
)

// LoadHTTPConfig returns HTTPClientConfig for the given http_config file
func LoadHTTPConfig(httpConfigFile string) (*promconfig.HTTPClientConfig, error) {
if _, err := os.Stat(httpConfigFile); err != nil {
return nil, err
}
b, err := ioutil.ReadFile(httpConfigFile)
// LoadHTTPConfigFile returns HTTPClientConfig for the given http_config file
func LoadHTTPConfigFile(filename string) (*promconfig.HTTPClientConfig, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down
13 changes: 4 additions & 9 deletions cli/config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@ import (
"testing"
)

const (
AuthorizationCredentials = "theanswertothegreatquestionoflifetheuniverseandeverythingisfortytwo"
)

func TestInvalidHTTPConfig(t *testing.T) {
_, err := LoadHTTPConfig("testdata/http_config.bad.yml")
_, err := LoadHTTPConfigFile("testdata/http_config.bad.yml")
errMsg := `authorization type cannot be set to "basic", use "basic_auth" instead`
if !strings.Contains(err.Error(), errMsg) {
t.Errorf("Expected error for invalid HTTP client configuration to contain %q but got: %s", errMsg, err)
}
}
func TestValidHTTPConfig(t *testing.T) {

cfg, err := LoadHTTPConfig("testdata/http_config.good.yml")
func TestValidHTTPConfig(t *testing.T) {
cfg, err := LoadHTTPConfigFile("testdata/http_config.good.yml")
if err != nil {
t.Fatalf("Error loading HTTP client config: %v", err)
}
Expand All @@ -43,8 +39,7 @@ func TestValidHTTPConfig(t *testing.T) {
}

func TestValidBasicAuthHTTPConfig(t *testing.T) {

cfg, err := LoadHTTPConfig("testdata/http_config.basic_auth.good.yml")
cfg, err := LoadHTTPConfigFile("testdata/http_config.basic_auth.good.yml")
if err != nil {
t.Fatalf("Error loading HTTP client config: %v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,16 @@ func NewAlertmanagerClient(amURL *url.URL) *client.Alertmanager {
cr.DefaultAuthentication = clientruntime.BasicAuth(amURL.User.Username(), password)
}

var httpConfig *promconfig.HTTPClientConfig
if httpConfigFile != "" {
var err error
httpConfig, err = config.LoadHTTPConfig(httpConfigFile)
httpConfig, err := config.LoadHTTPConfigFile(httpConfigFile)
if err != nil {
kingpin.Fatalf("could not load HTTP config file: %v\n", err)
kingpin.Fatalf("failed to load HTTP config file: %v\n", err)
}

httpclient, err := promconfig.NewClientFromConfig(*httpConfig, "amtool")
if err != nil {
kingpin.Fatalf("could not create a new HTTP client: %v\n", err)
kingpin.Fatalf("failed to create a new HTTP client: %v\n", err)
}
cr = clientruntime.NewWithClient(address, path.Join(amURL.Path, defaultAmApiv2path), schemes, httpclient)
}
Expand Down

0 comments on commit d6a5a05

Please sign in to comment.