Merge pull request #1892 from govau/fix1891

Use scheme and auth info if present from URL
This commit is contained in:
stuart nelson 2019-05-16 10:33:19 +02:00 committed by GitHub
commit 588b4dd17b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 7 deletions

View File

@ -25,6 +25,8 @@ import (
"github.com/prometheus/alertmanager/api/v2/client" "github.com/prometheus/alertmanager/api/v2/client"
"github.com/prometheus/alertmanager/cli/config" "github.com/prometheus/alertmanager/cli/config"
"github.com/prometheus/alertmanager/cli/format" "github.com/prometheus/alertmanager/cli/format"
clientruntime "github.com/go-openapi/runtime/client"
) )
var ( var (
@ -63,15 +65,24 @@ const (
// NewAlertmanagerClient initializes an alertmanager client with the given URL // NewAlertmanagerClient initializes an alertmanager client with the given URL
func NewAlertmanagerClient(amURL *url.URL) *client.Alertmanager { func NewAlertmanagerClient(amURL *url.URL) *client.Alertmanager {
transportConfig := client.DefaultTransportConfig() address := defaultAmHost + ":" + defaultAmPort
transportConfig.BasePath = defaultAmApiv2path schemes := []string{"http"}
if amURL.Host == "" { if amURL.Host != "" {
transportConfig.Host = defaultAmHost + ":" + defaultAmPort address = amURL.Host // URL documents host as host or host:port
} else {
transportConfig.Host = amURL.Host
} }
return client.NewHTTPClientWithConfig(strfmt.Default, transportConfig) if amURL.Scheme != "" {
schemes = []string{amURL.Scheme}
}
cr := clientruntime.New(address, defaultAmApiv2path, schemes)
if amURL.User != nil {
password, _ := amURL.User.Password()
cr.DefaultAuthentication = clientruntime.BasicAuth(amURL.User.Username(), password)
}
return client.New(cr, strfmt.Default)
} }
// Execute is the main function for the amtool command // Execute is the main function for the amtool command