mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-11 14:53:49 +00:00
f3be3573e7
- Sort the discovered labels in alphabetical order at https://docs.victoriametrics.com/sd_configs/#vultr_sd_configs - Rename VultrConfigs to VultrSDConfigs to be consistent with the naming for other SD configs. - Prepare query arg filters for `list instances API` at newAPIConfig() instead of passing them in a separate listParams struct. This simplifies the code a bit. - Return error when bearer token isn't set at vultr_sd_configs, since this token is mandatory according to https://docs.victoriametrics.com/sd_configs/#vultr_sd_configs - Remove unused fields from the parsed response from Vultr list instances API in order to simplify the code a bit. - Remove double logging of errors inside getInstances() function, since these errors must be already logged by the caller. - Simplify tests, so they are easier to maintain. Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6041 Updates https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6068
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package vultr
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
|
)
|
|
|
|
// apiConfig contains config for API server.
|
|
type apiConfig struct {
|
|
c *discoveryutils.Client
|
|
port int
|
|
|
|
listQueryParams string
|
|
}
|
|
|
|
// getAPIConfig get or create API config from configMap.
|
|
func getAPIConfig(sdc *SDConfig, baseDir string) (*apiConfig, error) {
|
|
v, err := configMap.Get(sdc, func() (interface{}, error) { return newAPIConfig(sdc, baseDir) })
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return v.(*apiConfig), nil
|
|
}
|
|
|
|
// newAPIConfig create API Config.
|
|
func newAPIConfig(sdc *SDConfig, baseDir string) (*apiConfig, error) {
|
|
port := sdc.Port
|
|
if port == 0 {
|
|
port = 80
|
|
}
|
|
|
|
// See: https://www.vultr.com/api/
|
|
apiServer := "https://api.vultr.com"
|
|
|
|
if sdc.HTTPClientConfig.BearerToken == nil {
|
|
return nil, fmt.Errorf("missing `bearer_token` option")
|
|
}
|
|
|
|
ac, err := sdc.HTTPClientConfig.NewConfig(baseDir)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot parse auth config: %w", err)
|
|
}
|
|
proxyAC, err := sdc.ProxyClientConfig.NewConfig(baseDir)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot parse proxy auth config: %w", err)
|
|
}
|
|
|
|
c, err := discoveryutils.NewClient(apiServer, ac, sdc.ProxyURL, proxyAC, &sdc.HTTPClientConfig)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot create client for %q: %w", apiServer, err)
|
|
}
|
|
|
|
// Prepare additional query params for list instance API.
|
|
// See https://www.vultr.com/api/#tag/instances/operation/list-instances
|
|
var qp url.Values
|
|
if sdc.Label != "" {
|
|
qp.Set("label", sdc.Label)
|
|
}
|
|
if sdc.MainIP != "" {
|
|
qp.Set("main_ip", sdc.MainIP)
|
|
}
|
|
if sdc.Region != "" {
|
|
qp.Set("region", sdc.Region)
|
|
}
|
|
if sdc.FirewallGroupID != "" {
|
|
qp.Set("firewall_group_id", sdc.FirewallGroupID)
|
|
}
|
|
if sdc.Hostname != "" {
|
|
qp.Set("hostname", sdc.Hostname)
|
|
}
|
|
|
|
cfg := &apiConfig{
|
|
c: c,
|
|
port: port,
|
|
|
|
listQueryParams: qp.Encode(),
|
|
}
|
|
return cfg, nil
|
|
}
|