Refactor to not use embedded structs for config.

This commit is contained in:
John Engelman 2016-05-06 07:37:51 -05:00
parent a4c675dd50
commit 4d5208f121
4 changed files with 86 additions and 27 deletions

View File

@ -8,42 +8,42 @@ import (
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
) )
type AwsCredentials struct { type CredentialConfig struct {
Region string `toml:"region"` // AWS Region Region string
AccessKey string `toml:"access_key"` // Explicit AWS Access Key ID AccessKey string
SecretKey string `toml:"secret_key"` // Explicit AWS Secret Access Key SecretKey string
RoleArn string `toml:"role_arn"` // Role ARN to assume RoleARN string
Profile string `toml:"profile"` // the shared profile to use Profile string
SharedCredentialFile string `toml:"shared_credential_file"` // location of shared credential file Filename string
Token string `toml:"token"` // STS session token Token string
} }
func (c *AwsCredentials) Credentials() client.ConfigProvider { func (c *CredentialConfig) Credentials() client.ConfigProvider {
if c.RoleArn != "" { if c.RoleARN != "" {
return c.assumeCredentials() return c.assumeCredentials()
} else { } else {
return c.rootCredentials() return c.rootCredentials()
} }
} }
func (c *AwsCredentials) rootCredentials() client.ConfigProvider { func (c *CredentialConfig) rootCredentials() client.ConfigProvider {
config := &aws.Config{ config := &aws.Config{
Region: aws.String(c.Region), Region: aws.String(c.Region),
} }
if c.AccessKey != "" || c.SecretKey != "" { if c.AccessKey != "" || c.SecretKey != "" {
config.Credentials = credentials.NewStaticCredentials(c.AccessKey, c.SecretKey, c.Token) config.Credentials = credentials.NewStaticCredentials(c.AccessKey, c.SecretKey, c.Token)
} else if c.Profile != "" || c.SharedCredentialFile != "" { } else if c.Profile != "" || c.Filename != "" {
config.Credentials = credentials.NewSharedCredentials(c.SharedCredentialFile, c.Profile) config.Credentials = credentials.NewSharedCredentials(c.Filename, c.Profile)
} }
return session.New(config) return session.New(config)
} }
func (c *AwsCredentials) assumeCredentials() client.ConfigProvider { func (c *CredentialConfig) assumeCredentials() client.ConfigProvider {
rootCredentials := c.rootCredentials() rootCredentials := c.rootCredentials()
config := &aws.Config{ config := &aws.Config{
Region: aws.String(c.Region), Region: aws.String(c.Region),
} }
config.Credentials = stscreds.NewCredentials(rootCredentials, c.RoleArn) config.Credentials = stscreds.NewCredentials(rootCredentials, c.RoleARN)
return session.New(config) return session.New(config)
} }

View File

@ -11,13 +11,20 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "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" "github.com/influxdata/telegraf/plugins/inputs"
) )
type ( type (
CloudWatch struct { 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"` Period internal.Duration `toml:"period"`
Delay internal.Duration `toml:"delay"` Delay internal.Duration `toml:"delay"`
Namespace string `toml:"namespace"` Namespace string `toml:"namespace"`
@ -64,6 +71,10 @@ func (c *CloudWatch) SampleConfig() string {
## 6) EC2 Instance Profile ## 6) EC2 Instance Profile
#access_key = "" #access_key = ""
#secret_key = "" #secret_key = ""
#token = ""
#role_arn = ""
#profile = ""
#shared_credential_file = ""
## Requested CloudWatch aggregation Period (required - must be a multiple of 60s) ## Requested CloudWatch aggregation Period (required - must be a multiple of 60s)
period = '1m' period = '1m'
@ -188,7 +199,16 @@ func init() {
* Initialize CloudWatch client * Initialize CloudWatch client
*/ */
func (c *CloudWatch) initializeCloudWatch() error { 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) c.client = cloudwatch.New(configProvider)
return nil return nil
@ -332,13 +352,12 @@ func (c *MetricCache) IsValid() bool {
} }
func hasWilcard(dimensions []*Dimension) bool { func hasWilcard(dimensions []*Dimension) bool {
wildcard := false
for _, d := range dimensions { for _, d := range dimensions {
if d.Value == "" || d.Value == "*" { if d.Value == "" || d.Value == "*" {
wildcard = true return true
} }
} }
return wildcard return false
} }
func isSelected(metric *cloudwatch.Metric, dimensions []*Dimension) bool { func isSelected(metric *cloudwatch.Metric, dimensions []*Dimension) bool {

View File

@ -11,12 +11,19 @@ import (
"github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/influxdata/telegraf" "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" "github.com/influxdata/telegraf/plugins/outputs"
) )
type CloudWatch struct { 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 Namespace string `toml:"namespace"` // CloudWatch Metrics Namespace
svc *cloudwatch.CloudWatch svc *cloudwatch.CloudWatch
} }
@ -35,6 +42,10 @@ var sampleConfig = `
## 6) EC2 Instance Profile ## 6) EC2 Instance Profile
#access_key = "" #access_key = ""
#secret_key = "" #secret_key = ""
#token = ""
#role_arn = ""
#profile = ""
#shared_credential_file = ""
## Namespace for the CloudWatch MetricDatums ## Namespace for the CloudWatch MetricDatums
namespace = 'InfluxData/Telegraf' namespace = 'InfluxData/Telegraf'
@ -49,7 +60,16 @@ func (c *CloudWatch) Description() string {
} }
func (c *CloudWatch) Connect() error { 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) svc := cloudwatch.New(configProvider)

View File

@ -11,12 +11,19 @@ import (
"github.com/aws/aws-sdk-go/service/kinesis" "github.com/aws/aws-sdk-go/service/kinesis"
"github.com/influxdata/telegraf" "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" "github.com/influxdata/telegraf/plugins/outputs"
) )
type KinesisOutput struct { 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"` StreamName string `toml:"streamname"`
PartitionKey string `toml:"partitionkey"` PartitionKey string `toml:"partitionkey"`
Format string `toml:"format"` Format string `toml:"format"`
@ -38,6 +45,10 @@ var sampleConfig = `
## 6) EC2 Instance Profile ## 6) EC2 Instance Profile
#access_key = "" #access_key = ""
#secret_key = "" #secret_key = ""
#token = ""
#role_arn = ""
#profile = ""
#shared_credential_file = ""
## Kinesis StreamName must exist prior to starting telegraf. ## Kinesis StreamName must exist prior to starting telegraf.
streamname = "StreamName" streamname = "StreamName"
@ -75,7 +86,16 @@ func (k *KinesisOutput) Connect() error {
log.Printf("kinesis: Establishing a connection to Kinesis in %+v", k.Region) 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) svc := kinesis.New(configProvider)
KinesisParams := &kinesis.ListStreamsInput{ KinesisParams := &kinesis.ListStreamsInput{