Refactor to not use embedded structs for config.
This commit is contained in:
parent
a4c675dd50
commit
4d5208f121
|
@ -8,42 +8,42 @@ import (
|
|||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
)
|
||||
|
||||
type AwsCredentials struct {
|
||||
Region string `toml:"region"` // AWS Region
|
||||
AccessKey string `toml:"access_key"` // Explicit AWS Access Key ID
|
||||
SecretKey string `toml:"secret_key"` // Explicit AWS Secret Access Key
|
||||
RoleArn string `toml:"role_arn"` // Role ARN to assume
|
||||
Profile string `toml:"profile"` // the shared profile to use
|
||||
SharedCredentialFile string `toml:"shared_credential_file"` // location of shared credential file
|
||||
Token string `toml:"token"` // STS session token
|
||||
type CredentialConfig struct {
|
||||
Region string
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
RoleARN string
|
||||
Profile string
|
||||
Filename string
|
||||
Token string
|
||||
}
|
||||
|
||||
func (c *AwsCredentials) Credentials() client.ConfigProvider {
|
||||
if c.RoleArn != "" {
|
||||
func (c *CredentialConfig) Credentials() client.ConfigProvider {
|
||||
if c.RoleARN != "" {
|
||||
return c.assumeCredentials()
|
||||
} else {
|
||||
return c.rootCredentials()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *AwsCredentials) rootCredentials() client.ConfigProvider {
|
||||
func (c *CredentialConfig) rootCredentials() client.ConfigProvider {
|
||||
config := &aws.Config{
|
||||
Region: aws.String(c.Region),
|
||||
}
|
||||
if c.AccessKey != "" || c.SecretKey != "" {
|
||||
config.Credentials = credentials.NewStaticCredentials(c.AccessKey, c.SecretKey, c.Token)
|
||||
} else if c.Profile != "" || c.SharedCredentialFile != "" {
|
||||
config.Credentials = credentials.NewSharedCredentials(c.SharedCredentialFile, c.Profile)
|
||||
} else if c.Profile != "" || c.Filename != "" {
|
||||
config.Credentials = credentials.NewSharedCredentials(c.Filename, c.Profile)
|
||||
}
|
||||
|
||||
return session.New(config)
|
||||
}
|
||||
|
||||
func (c *AwsCredentials) assumeCredentials() client.ConfigProvider {
|
||||
func (c *CredentialConfig) assumeCredentials() client.ConfigProvider {
|
||||
rootCredentials := c.rootCredentials()
|
||||
config := &aws.Config{
|
||||
Region: aws.String(c.Region),
|
||||
}
|
||||
config.Credentials = stscreds.NewCredentials(rootCredentials, c.RoleArn)
|
||||
config.Credentials = stscreds.NewCredentials(rootCredentials, c.RoleARN)
|
||||
return session.New(config)
|
||||
}
|
||||
|
|
|
@ -11,13 +11,20 @@ import (
|
|||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/internal"
|
||||
influxaws "github.com/influxdata/telegraf/internal/config/aws"
|
||||
internalaws "github.com/influxdata/telegraf/internal/config/aws"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
type (
|
||||
CloudWatch struct {
|
||||
influxaws.AwsCredentials
|
||||
Region string `toml:"region"`
|
||||
AccessKey string `toml:"access_key"`
|
||||
SecretKey string `toml:"secret_key"`
|
||||
RoleARN string `toml:"role_arn"`
|
||||
Profile string `toml:"profile"`
|
||||
Filename string `toml:"shared_credential_file"`
|
||||
Token string `toml:"token"`
|
||||
|
||||
Period internal.Duration `toml:"period"`
|
||||
Delay internal.Duration `toml:"delay"`
|
||||
Namespace string `toml:"namespace"`
|
||||
|
@ -64,6 +71,10 @@ func (c *CloudWatch) SampleConfig() string {
|
|||
## 6) EC2 Instance Profile
|
||||
#access_key = ""
|
||||
#secret_key = ""
|
||||
#token = ""
|
||||
#role_arn = ""
|
||||
#profile = ""
|
||||
#shared_credential_file = ""
|
||||
|
||||
## Requested CloudWatch aggregation Period (required - must be a multiple of 60s)
|
||||
period = '1m'
|
||||
|
@ -188,7 +199,16 @@ func init() {
|
|||
* Initialize CloudWatch client
|
||||
*/
|
||||
func (c *CloudWatch) initializeCloudWatch() error {
|
||||
configProvider := c.Credentials()
|
||||
credentialConfig := &internalaws.CredentialConfig{
|
||||
Region: c.Region,
|
||||
AccessKey: c.AccessKey,
|
||||
SecretKey: c.SecretKey,
|
||||
RoleARN: c.RoleARN,
|
||||
Profile: c.Profile,
|
||||
Filename: c.Filename,
|
||||
Token: c.Token,
|
||||
}
|
||||
configProvider := credentialConfig.Credentials()
|
||||
|
||||
c.client = cloudwatch.New(configProvider)
|
||||
return nil
|
||||
|
@ -332,13 +352,12 @@ func (c *MetricCache) IsValid() bool {
|
|||
}
|
||||
|
||||
func hasWilcard(dimensions []*Dimension) bool {
|
||||
wildcard := false
|
||||
for _, d := range dimensions {
|
||||
if d.Value == "" || d.Value == "*" {
|
||||
wildcard = true
|
||||
return true
|
||||
}
|
||||
}
|
||||
return wildcard
|
||||
return false
|
||||
}
|
||||
|
||||
func isSelected(metric *cloudwatch.Metric, dimensions []*Dimension) bool {
|
||||
|
|
|
@ -11,12 +11,19 @@ import (
|
|||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
influxaws "github.com/influxdata/telegraf/internal/config/aws"
|
||||
internalaws "github.com/influxdata/telegraf/internal/config/aws"
|
||||
"github.com/influxdata/telegraf/plugins/outputs"
|
||||
)
|
||||
|
||||
type CloudWatch struct {
|
||||
influxaws.AwsCredentials
|
||||
Region string `toml:"region"`
|
||||
AccessKey string `toml:"access_key"`
|
||||
SecretKey string `toml:"secret_key"`
|
||||
RoleARN string `toml:"role_arn"`
|
||||
Profile string `toml:"profile"`
|
||||
Filename string `toml:"shared_credential_file"`
|
||||
Token string `toml:"token"`
|
||||
|
||||
Namespace string `toml:"namespace"` // CloudWatch Metrics Namespace
|
||||
svc *cloudwatch.CloudWatch
|
||||
}
|
||||
|
@ -35,6 +42,10 @@ var sampleConfig = `
|
|||
## 6) EC2 Instance Profile
|
||||
#access_key = ""
|
||||
#secret_key = ""
|
||||
#token = ""
|
||||
#role_arn = ""
|
||||
#profile = ""
|
||||
#shared_credential_file = ""
|
||||
|
||||
## Namespace for the CloudWatch MetricDatums
|
||||
namespace = 'InfluxData/Telegraf'
|
||||
|
@ -49,7 +60,16 @@ func (c *CloudWatch) Description() string {
|
|||
}
|
||||
|
||||
func (c *CloudWatch) Connect() error {
|
||||
configProvider := c.Credentials()
|
||||
credentialConfig := &internalaws.CredentialConfig{
|
||||
Region: c.Region,
|
||||
AccessKey: c.AccessKey,
|
||||
SecretKey: c.SecretKey,
|
||||
RoleARN: c.RoleARN,
|
||||
Profile: c.Profile,
|
||||
Filename: c.Filename,
|
||||
Token: c.Token,
|
||||
}
|
||||
configProvider := credentialConfig.Credentials()
|
||||
|
||||
svc := cloudwatch.New(configProvider)
|
||||
|
||||
|
|
|
@ -11,12 +11,19 @@ import (
|
|||
"github.com/aws/aws-sdk-go/service/kinesis"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
influxaws "github.com/influxdata/telegraf/internal/config/aws"
|
||||
internalaws "github.com/influxdata/telegraf/internal/config/aws"
|
||||
"github.com/influxdata/telegraf/plugins/outputs"
|
||||
)
|
||||
|
||||
type KinesisOutput struct {
|
||||
influxaws.AwsCredentials
|
||||
Region string `toml:"region"`
|
||||
AccessKey string `toml:"access_key"`
|
||||
SecretKey string `toml:"secret_key"`
|
||||
RoleARN string `toml:"role_arn"`
|
||||
Profile string `toml:"profile"`
|
||||
Filename string `toml:"shared_credential_file"`
|
||||
Token string `toml:"token"`
|
||||
|
||||
StreamName string `toml:"streamname"`
|
||||
PartitionKey string `toml:"partitionkey"`
|
||||
Format string `toml:"format"`
|
||||
|
@ -38,6 +45,10 @@ var sampleConfig = `
|
|||
## 6) EC2 Instance Profile
|
||||
#access_key = ""
|
||||
#secret_key = ""
|
||||
#token = ""
|
||||
#role_arn = ""
|
||||
#profile = ""
|
||||
#shared_credential_file = ""
|
||||
|
||||
## Kinesis StreamName must exist prior to starting telegraf.
|
||||
streamname = "StreamName"
|
||||
|
@ -75,7 +86,16 @@ func (k *KinesisOutput) Connect() error {
|
|||
log.Printf("kinesis: Establishing a connection to Kinesis in %+v", k.Region)
|
||||
}
|
||||
|
||||
configProvider := k.Credentials()
|
||||
credentialConfig := &internalaws.CredentialConfig{
|
||||
Region: k.Region,
|
||||
AccessKey: k.AccessKey,
|
||||
SecretKey: k.SecretKey,
|
||||
RoleARN: k.RoleARN,
|
||||
Profile: k.Profile,
|
||||
Filename: k.Filename,
|
||||
Token: k.Token,
|
||||
}
|
||||
configProvider := credentialConfig.Credentials()
|
||||
svc := kinesis.New(configProvider)
|
||||
|
||||
KinesisParams := &kinesis.ListStreamsInput{
|
||||
|
|
Loading…
Reference in New Issue