Merge branch 'public-single-node' into pmm-6401-read-prometheus-data-files

This commit is contained in:
Aliaksandr Valialkin 2022-05-05 10:33:08 +03:00
commit 620b0d11b7
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) roleARN := awsRoleARN.GetOptionalArg(argIdx)
accessKey := awsAccessKey.GetOptionalArg(argIdx) accessKey := awsAccessKey.GetOptionalArg(argIdx)
secretKey := awsSecretKey.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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -22,8 +22,6 @@ type Config struct {
roleARN string roleARN string
webTokenPath string webTokenPath string
filtersQueryString string
ec2Endpoint string ec2Endpoint string
stsEndpoint string stsEndpoint string
@ -45,17 +43,13 @@ type credentials struct {
} }
// NewConfig returns new AWS Config. // NewConfig returns new AWS Config.
// func NewConfig(region, roleARN, accessKey, secretKey string) (*Config, 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.
func NewConfig(region, roleARN, accessKey, secretKey, filtersQueryString string) (*Config, error) {
cfg := &Config{ cfg := &Config{
client: http.DefaultClient, client: http.DefaultClient,
region: region, region: region,
roleARN: roleARN, roleARN: roleARN,
filtersQueryString: filtersQueryString, defaultAccessKey: os.Getenv("AWS_ACCESS_KEY_ID"),
defaultAccessKey: os.Getenv("AWS_ACCESS_KEY_ID"), defaultSecretKey: os.Getenv("AWS_SECRET_ACCESS_KEY"),
defaultSecretKey: os.Getenv("AWS_SECRET_ACCESS_KEY"),
} }
cfg.region = region cfg.region = region
if cfg.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. // 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() ac, err := cfg.getFreshAPICredentials()
if err != nil { if err != nil {
return nil, err return nil, err
} }
apiURL := fmt.Sprintf("%s?Action=%s", cfg.ec2Endpoint, url.QueryEscape(action)) apiURL := fmt.Sprintf("%s?Action=%s", cfg.ec2Endpoint, url.QueryEscape(action))
if len(cfg.filtersQueryString) > 0 { if len(filtersQueryString) > 0 {
apiURL += "&" + cfg.filtersQueryString apiURL += "&" + filtersQueryString
} }
if len(nextPageToken) > 0 { if len(nextPageToken) > 0 {
apiURL += fmt.Sprintf("&NextToken=%s", url.QueryEscape(nextPageToken)) apiURL += fmt.Sprintf("&NextToken=%s", url.QueryEscape(nextPageToken))

View file

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

View file

@ -29,7 +29,7 @@ func getAZMap(cfg *apiConfig) map[string]string {
func getAvailabilityZones(cfg *apiConfig) ([]AvailabilityZone, error) { func getAvailabilityZones(cfg *apiConfig) ([]AvailabilityZone, error) {
// See https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeAvailabilityZones.html // 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 { if err != nil {
return nil, fmt.Errorf("cannot obtain availability zones: %w", err) 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 var rs []Reservation
pageToken := "" pageToken := ""
for { for {
data, err := cfg.awsConfig.GetEC2APIResponse("DescribeInstances", pageToken) data, err := cfg.awsConfig.GetEC2APIResponse("DescribeInstances", cfg.filtersQueryString, pageToken)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot obtain instances: %w", err) return nil, fmt.Errorf("cannot obtain instances: %w", err)
} }