lib/awsapi: pass filtersQueryString arg to GetEC2APIResponse() function, so the caller could decide whether to use the filters during the AWS API query

The filters shouldn't be passed to DescribeAvailabilityZones API call.
See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1626
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1287

Related commits:
0e09fdb8b0
d289ecded1
This commit is contained in:
Aliaksandr Valialkin 2022-05-05 10:21:56 +03:00
parent 6bb32ab9de
commit d285c2fea7
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
5 changed files with 23 additions and 23 deletions

View file

@ -232,7 +232,7 @@ func getAWSAPIConfig(argIdx int) (*awsapi.Config, error) {
roleARN := awsRoleARN.GetOptionalArg(argIdx)
accessKey := awsAccessKey.GetOptionalArg(argIdx)
secretKey := awsSecretKey.GetOptionalArg(argIdx)
cfg, err := awsapi.NewConfig(region, roleARN, accessKey, secretKey, "")
cfg, err := awsapi.NewConfig(region, roleARN, accessKey, secretKey)
if err != nil {
return nil, err
}

View file

@ -22,8 +22,6 @@ type Config struct {
roleARN string
webTokenPath string
filtersQueryString string
ec2Endpoint string
stsEndpoint string
@ -45,17 +43,13 @@ type credentials struct {
}
// NewConfig returns new AWS Config.
//
// filtersQueryString must contain an optional percent-encoded query string for aws filters.
// See https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstances.html for examples.
func NewConfig(region, roleARN, accessKey, secretKey, filtersQueryString string) (*Config, error) {
func NewConfig(region, roleARN, accessKey, secretKey string) (*Config, error) {
cfg := &Config{
client: http.DefaultClient,
region: region,
roleARN: roleARN,
filtersQueryString: filtersQueryString,
defaultAccessKey: os.Getenv("AWS_ACCESS_KEY_ID"),
defaultSecretKey: os.Getenv("AWS_SECRET_ACCESS_KEY"),
client: http.DefaultClient,
region: region,
roleARN: roleARN,
defaultAccessKey: os.Getenv("AWS_ACCESS_KEY_ID"),
defaultSecretKey: os.Getenv("AWS_SECRET_ACCESS_KEY"),
}
cfg.region = region
if cfg.region == "" {
@ -91,14 +85,18 @@ func NewConfig(region, roleARN, accessKey, secretKey, filtersQueryString string)
}
// GetEC2APIResponse performs EC2 API request with ghe given action.
func (cfg *Config) GetEC2APIResponse(action, nextPageToken string) ([]byte, error) {
//
// filtersQueryString must contain an optional percent-encoded query string for aws filters.
// See https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstances.html for examples.
// See also https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_Filter.html
func (cfg *Config) GetEC2APIResponse(action, filtersQueryString, nextPageToken string) ([]byte, error) {
ac, err := cfg.getFreshAPICredentials()
if err != nil {
return nil, err
}
apiURL := fmt.Sprintf("%s?Action=%s", cfg.ec2Endpoint, url.QueryEscape(action))
if len(cfg.filtersQueryString) > 0 {
apiURL += "&" + cfg.filtersQueryString
if len(filtersQueryString) > 0 {
apiURL += "&" + filtersQueryString
}
if len(nextPageToken) > 0 {
apiURL += fmt.Sprintf("&NextToken=%s", url.QueryEscape(nextPageToken))

View file

@ -11,8 +11,9 @@ import (
)
type apiConfig struct {
awsConfig *awsapi.Config
port int
awsConfig *awsapi.Config
filtersQueryString string
port int
// A map from AZ name to AZ id.
azMap map[string]string
@ -35,13 +36,14 @@ func newAPIConfig(sdc *SDConfig) (*apiConfig, error) {
if sdc.Port != nil {
port = *sdc.Port
}
awsCfg, err := awsapi.NewConfig(sdc.Region, sdc.RoleARN, sdc.AccessKey, sdc.SecretKey.String(), fqs)
awsCfg, err := awsapi.NewConfig(sdc.Region, sdc.RoleARN, sdc.AccessKey, sdc.SecretKey.String())
if err != nil {
return nil, err
}
cfg := &apiConfig{
awsConfig: awsCfg,
port: port,
awsConfig: awsCfg,
filtersQueryString: fqs,
port: port,
}
return cfg, nil
}

View file

@ -29,7 +29,7 @@ func getAZMap(cfg *apiConfig) map[string]string {
func getAvailabilityZones(cfg *apiConfig) ([]AvailabilityZone, error) {
// See https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeAvailabilityZones.html
data, err := cfg.awsConfig.GetEC2APIResponse("DescribeAvailabilityZones", "")
data, err := cfg.awsConfig.GetEC2APIResponse("DescribeAvailabilityZones", "", "")
if err != nil {
return nil, fmt.Errorf("cannot obtain availability zones: %w", err)
}

View file

@ -29,7 +29,7 @@ func getReservations(cfg *apiConfig) ([]Reservation, error) {
var rs []Reservation
pageToken := ""
for {
data, err := cfg.awsConfig.GetEC2APIResponse("DescribeInstances", pageToken)
data, err := cfg.awsConfig.GetEC2APIResponse("DescribeInstances", cfg.filtersQueryString, pageToken)
if err != nil {
return nil, fmt.Errorf("cannot obtain instances: %w", err)
}