Skip to content

Commit

Permalink
Allow for a hash or string when configuring tags (#296)
Browse files Browse the repository at this point in the history
* Extract datadog tags into a template var
* Support strings and hashes
* Update docs for tag attribute
* Add test coverage for tags (string, hash, empty hash value)

Closes #186
martinisoft authored and miketheman committed Apr 21, 2016
1 parent aa40923 commit a0fbc0e
Showing 3 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -37,6 +37,9 @@
default['datadog']['url'] = 'https://app.datadoghq.com'

# Add tags as override attributes in your role
# This can be a string of comma separated tags or a hash in this format:
# default['datadog']['tags'] = { 'datacenter' => 'us-east' }
# Thie above outputs a string: 'datacenter:us-east'
# When using the Datadog Chef Handler, tags are set on the node with preset prefixes:
# `env:node.chef_environment`, `role:node.node.run_list.role`, `tag:somecheftag`
default['datadog']['tags'] = ''
66 changes: 66 additions & 0 deletions spec/dd-agent_spec.rb
Original file line number Diff line number Diff line change
@@ -229,6 +229,72 @@ def set_env_var(name, value)

it_behaves_like 'debianoids'
end

context 'allows a string for tags' do
cached(:chef_run) do
ChefSpec::SoloRunner.new(
platform: 'ubuntu',
version: '12.04'
) do |node|
node.set['datadog'] = {
'api_key' => 'somethingnotnil',
'tags' => 'datacenter:us-foo,database:bar'
}
node.set['languages'] = { 'python' => { 'version' => '2.6.2' } }
end.converge described_recipe
end

it_behaves_like 'common linux resources'

it 'sets tags from the tags attribute' do
expect(chef_run).to render_file('/etc/dd-agent/datadog.conf')
.with_content(/^tags: datacenter:us-foo,database:bar$/)
end
end

context 'allows key/value for tags' do
cached(:chef_run) do
ChefSpec::SoloRunner.new(
platform: 'ubuntu',
version: '12.04'
) do |node|
node.set['datadog'] = {
'api_key' => 'somethingnotnil',
'tags' => { 'datacenter' => 'us-foo', 'database' => 'bar' }
}
node.set['languages'] = { 'python' => { 'version' => '2.6.2' } }
end.converge described_recipe
end

it_behaves_like 'common linux resources'

it 'sets tags from the tags attribute' do
expect(chef_run).to render_file('/etc/dd-agent/datadog.conf')
.with_content(/^tags: datacenter:us-foo,database:bar$/)
end
end

context 'does not use empty tags' do
cached(:chef_run) do
ChefSpec::SoloRunner.new(
platform: 'ubuntu',
version: '12.04'
) do |node|
node.set['datadog'] = {
'api_key' => 'somethingnotnil',
'tags' => { 'datacenter' => 'us-foo', 'database' => '' }
}
node.set['languages'] = { 'python' => { 'version' => '2.6.2' } }
end.converge described_recipe
end

it_behaves_like 'common linux resources'

it 'sets tags from the tags attribute' do
expect(chef_run).to render_file('/etc/dd-agent/datadog.conf')
.with_content(/^tags: datacenter:us-foo$/)
end
end
end

context 'service action' do
4 changes: 4 additions & 0 deletions templates/default/datadog.conf.erb
Original file line number Diff line number Diff line change
@@ -15,7 +15,11 @@ autorestart: <%= node['datadog']['autorestart'] %>
skip_ssl_validation: <%= node['datadog']['web_proxy']['skip_ssl_validation'] %>
<% end -%>

<% if node['datadog']['tags'].respond_to?(:each_pair) -%>
tags: <%= node['datadog']['tags'].reject{ |_k,v| v.empty? }.map{ |k,v| "#{k}:#{v}" }.join(',') %>
<% else -%>
tags: <%= node['datadog']['tags'] %>
<% end -%>
<% if node['datadog']['create_dd_check_tags'] -%>
create_dd_check_tags: <%= node['datadog']['create_dd_check_tags'] %>
<% end -%>

0 comments on commit a0fbc0e

Please sign in to comment.