VictoriaMetrics/lib/promscrape/discovery/ec2/api.go
Boris Petersen 3df8caca15
Add ability to sign requests for all AWS services ()
This adds the ability to utilize sigv4 signing for all AWS services not
just "aps". When the newly introduced property "service" is not set it
will default to "aps".

Signed-off-by: Boris Petersen <boris.petersen@idealo.de>
2022-05-18 14:58:31 +02:00

48 lines
1.1 KiB
Go

package ec2
import (
"sync"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/awsapi"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
)
type apiConfig struct {
awsConfig *awsapi.Config
instanceFilters []awsapi.Filter
azFilters []awsapi.Filter
port int
// A map from AZ name to AZ id.
azMap map[string]string
azMapLock sync.Mutex
}
var configMap = discoveryutils.NewConfigMap()
func getAPIConfig(sdc *SDConfig) (*apiConfig, error) {
v, err := configMap.Get(sdc, func() (interface{}, error) { return newAPIConfig(sdc) })
if err != nil {
return nil, err
}
return v.(*apiConfig), nil
}
func newAPIConfig(sdc *SDConfig) (*apiConfig, error) {
port := 80
if sdc.Port != nil {
port = *sdc.Port
}
awsCfg, err := awsapi.NewConfig(sdc.Region, sdc.RoleARN, sdc.AccessKey, sdc.SecretKey.String(), "ec2")
if err != nil {
return nil, err
}
cfg := &apiConfig{
awsConfig: awsCfg,
instanceFilters: sdc.InstanceFilters,
azFilters: sdc.AZFilters,
port: port,
}
return cfg, nil
}