2020-04-27 16:25:45 +00:00
|
|
|
package ec2
|
|
|
|
|
|
|
|
import (
|
2020-09-21 15:42:53 +00:00
|
|
|
"sync"
|
2020-04-29 14:27:08 +00:00
|
|
|
|
2022-05-04 17:24:19 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/awsapi"
|
2020-04-29 14:27:08 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2020-04-27 16:25:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type apiConfig struct {
|
2022-05-05 07:59:28 +00:00
|
|
|
awsConfig *awsapi.Config
|
|
|
|
filters []awsapi.Filter
|
|
|
|
port int
|
2021-08-03 10:26:28 +00:00
|
|
|
|
|
|
|
// A map from AZ name to AZ id.
|
|
|
|
azMap map[string]string
|
|
|
|
azMapLock sync.Mutex
|
2020-09-21 13:04:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:21:24 +00:00
|
|
|
var configMap = discoveryutils.NewConfigMap()
|
2020-04-27 16:25:45 +00:00
|
|
|
|
2020-05-04 13:21:24 +00:00
|
|
|
func getAPIConfig(sdc *SDConfig) (*apiConfig, error) {
|
2020-09-21 15:42:53 +00:00
|
|
|
v, err := configMap.Get(sdc, func() (interface{}, error) { return newAPIConfig(sdc) })
|
2020-04-27 16:25:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-04 13:21:24 +00:00
|
|
|
return v.(*apiConfig), nil
|
2020-04-27 16:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newAPIConfig(sdc *SDConfig) (*apiConfig, error) {
|
|
|
|
port := 80
|
|
|
|
if sdc.Port != nil {
|
|
|
|
port = *sdc.Port
|
|
|
|
}
|
2022-05-05 07:21:56 +00:00
|
|
|
awsCfg, err := awsapi.NewConfig(sdc.Region, sdc.RoleARN, sdc.AccessKey, sdc.SecretKey.String())
|
2022-05-04 17:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-21 13:04:15 +00:00
|
|
|
}
|
2022-05-04 17:24:19 +00:00
|
|
|
cfg := &apiConfig{
|
2022-05-05 07:59:28 +00:00
|
|
|
awsConfig: awsCfg,
|
|
|
|
filters: sdc.Filters,
|
|
|
|
port: port,
|
2020-09-21 13:04:15 +00:00
|
|
|
}
|
|
|
|
return cfg, nil
|
2020-04-27 16:25:45 +00:00
|
|
|
}
|