Skip to content

Commit

Permalink
Refactor to not use embedded structs for config.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnrengelman committed May 19, 2016
1 parent 685d61e commit 1280d3a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 27 deletions.
30 changes: 15 additions & 15 deletions internal/config/aws/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
31 changes: 25 additions & 6 deletions plugins/inputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 23 additions & 3 deletions plugins/outputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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'
Expand All @@ -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)

Expand Down
26 changes: 23 additions & 3 deletions plugins/outputs/kinesis/kinesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 1280d3a

Please sign in to comment.