Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #85 - Adding support for a regex tag blacklist #86

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/chef/handler/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def prepare_report_for_datadog
.with_application_key(config[:application_key])
.with_tag_prefix(config[:tag_prefix])
.with_retries(config[:tags_submission_retries])
.with_tag_blacklist(config[:tags_blacklist_regex])

# Build the chef event information
@event =
Expand Down
14 changes: 13 additions & 1 deletion lib/chef/handler/datadog_chef_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize
@tag_prefix = 'tag:'
@retries = 0
@combined_host_tags = nil
@regex_black_list = nil
end

# set the dogapi client handle
Expand Down Expand Up @@ -85,6 +86,11 @@ def with_retries(retries)
self
end

def with_tag_blacklist(tags_blacklist_regex)
@regex_black_list = Regexp.new(tags_blacklist_regex, Regexp::IGNORECASE) unless tags_blacklist_regex.nil? || tags_blacklist_regex.empty?
self
end

# send updated chef run generated tags to Datadog
def send_update_to_datadog
tags = combined_host_tags
Expand Down Expand Up @@ -135,6 +141,12 @@ def node_env

def node_tags
return [] unless @node.tags
@node.tags.map { |tag| "#{@tag_prefix}#{tag}" }
output = @node.tags.map { |tag| "#{@tag_prefix}#{tag}" }

# No blacklist, return all results
return output if @regex_black_list.nil?

# The blacklist is set, so return the items which are not filtered by it.
output.select { |t| !@regex_black_list.match(t) }
end
end # end class DatadogChefTags
33 changes: 33 additions & 0 deletions spec/datadog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,39 @@
)).to have_been_made.times(1)
end
end

describe 'when tag blacklist is specified' do
it 'does not include the tag(s) specified' do
@node.normal.tags = ['allowed_tag', 'not_allowed_tag']
@handler.config[:tags_blacklist_regex] = 'not_allowed.*'
@handler.run_report_unsafe(@run_status)

expect(a_request(:put, HOST_TAG_ENDPOINT + @node.name).with(
:query => { 'api_key' => @handler.config[:api_key],
'application_key' => @handler.config[:application_key],
'source' => 'chef' },
:body => hash_including(:tags => [
'env:hostile', 'role:highlander', 'tag:allowed_tag'
]),
)).to have_been_made.times(1)
end
end

describe 'when tag blacklist is unspecified' do
it 'should include all of the tag(s) specified' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: shouldn't this test be simply named should include all of the tag(s) ? (since no tags are being blacklisted here)

@node.normal.tags = ['allowed_tag', 'not_allowed_tag']
@handler.run_report_unsafe(@run_status)

expect(a_request(:put, HOST_TAG_ENDPOINT + @node.name).with(
:query => { 'api_key' => @handler.config[:api_key],
'application_key' => @handler.config[:application_key],
'source' => 'chef' },
:body => hash_including(:tags => [
'env:hostile', 'role:highlander', 'tag:allowed_tag', 'tag:not_allowed_tag'
]),
)).to have_been_made.times(1)
end
end
end

context 'tags submission retries' do
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading