Skip to content

Commit

Permalink
fix: Refactor ec2 init for config-api (#9576)
Browse files Browse the repository at this point in the history
(cherry picked from commit dbc4e26)
  • Loading branch information
sspaink authored and reimda committed Aug 18, 2021
1 parent 5e060d4 commit 1e856b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
29 changes: 14 additions & 15 deletions plugins/processors/aws/ec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ func (r *AwsEc2Processor) Init() error {
return errors.New("no tags specified in configuration")
}

for _, tag := range r.ImdsTags {
if len(tag) == 0 || !isImdsTagAllowed(tag) {
return fmt.Errorf("not allowed metadata tag specified in configuration: %s", tag)
}
r.imdsTags[tag] = struct{}{}
}
if len(r.imdsTags) == 0 && len(r.EC2Tags) == 0 {
return errors.New("no allowed metadata tags specified in configuration")
}

return nil
}

func (r *AwsEc2Processor) Start(acc telegraf.Accumulator) error {
ctx := context.Background()
cfg, err := awsconfig.LoadDefaultConfig(ctx)
if err != nil {
Expand Down Expand Up @@ -161,21 +175,6 @@ func (r *AwsEc2Processor) Init() error {
}
}

for _, tag := range r.ImdsTags {
if len(tag) > 0 && isImdsTagAllowed(tag) {
r.imdsTags[tag] = struct{}{}
} else {
return fmt.Errorf("not allowed metadata tag specified in configuration: %s", tag)
}
}
if len(r.imdsTags) == 0 && len(r.EC2Tags) == 0 {
return errors.New("no allowed metadata tags specified in configuration")
}

return nil
}

func (r *AwsEc2Processor) Start(acc telegraf.Accumulator) error {
if r.Ordered {
r.parallel = parallel.NewOrdered(acc, r.asyncAdd, DefaultMaxOrderedQueueSize, r.MaxParallelCalls)
} else {
Expand Down
6 changes: 2 additions & 4 deletions plugins/processors/aws/ec2/ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ func TestBasicStartup(t *testing.T) {
p.Log = &testutil.Logger{}
p.ImdsTags = []string{"accountId", "instanceId"}
acc := &testutil.Accumulator{}
require.NoError(t, p.Start(acc))
require.NoError(t, p.Stop())
require.NoError(t, p.Init())

require.Len(t, acc.GetTelegrafMetrics(), 0)
require.Len(t, acc.Errors, 0)
Expand All @@ -26,8 +25,7 @@ func TestBasicStartupWithEC2Tags(t *testing.T) {
p.ImdsTags = []string{"accountId", "instanceId"}
p.EC2Tags = []string{"Name"}
acc := &testutil.Accumulator{}
require.NoError(t, p.Start(acc))
require.NoError(t, p.Stop())
require.NoError(t, p.Init())

require.Len(t, acc.GetTelegrafMetrics(), 0)
require.Len(t, acc.Errors, 0)
Expand Down

0 comments on commit 1e856b0

Please sign in to comment.