Defer evaluation of api and application keys
Some users may wish to assign the api_key and application_key attributes during
runtime instead of compile time -- particularly, users who want to obtain the
value of the key(s) from an external service. Allow doing so by deferring
evaluation of these attributes.
Closes
MODIFY
recipes/dd-agent.rb
recipes/dd-agent.rb
@@ -51,8 +51,10 @@ end
# To add integration-specific configurations, add 'datadog::config_name' to
# the node's run_list and set the relevant attributes
#
-raise "Add a ['datadog']['api_key'] attribute to configure this node's Datadog Agent." if node['datadog'] && node['datadog']['api_key'].nil?
+
+template agent_config_file do
+ def template_vars
api_keys = [node['datadog']['api_key']]
dd_urls = [node['datadog']['url']]
node['datadog']['extra_endpoints'].each do |_, endpoint|
@@ -64,14 +66,17 @@ node['datadog']['extra_endpoints'].each do |_, endpoint|
node['datadog']['url']
end
end
-
extra_config = {}
node['datadog']['extra_config'].each do |option, value|
next if value.nil?
extra_config[option] = value
end
-
-template agent_config_file do
+ {
+ :api_keys => api_keys,
+ :dd_urls => dd_urls,
+ :extra_config => extra_config
+ }
+ end
if is_windows
owner 'Administrators'
rights :full_control, 'Administrators'
@@ -82,9 +87,11 @@ template agent_config_file do
mode '640'
end
variables(
- :api_keys => api_keys,
- :dd_urls => dd_urls,
- :extra_config => extra_config
+ if respond_to?(:lazy)
+ lazy { template_vars }
+ else
+ template_vars
+ end
)
sensitive true if Chef::Resource.instance_methods(false).include?(:sensitive)
end

MODIFY
recipes/dd-handler.rb
recipes/dd-handler.rb
@@ -46,6 +46,9 @@ unless web_proxy['host'].nil?
ENV['DATADOG_PROXY'] = proxy_url.to_s
end
+# Create the handler to run at the end of the Chef execution
+chef_handler 'Chef::Handler::Datadog' do
+ def handler_config # rubocop:disable Metrics/AbcSize
extra_endpoints = []
node['datadog']['extra_endpoints'].each do |_, endpoint|
next unless endpoint['enabled']
@@ -54,7 +57,7 @@ node['datadog']['extra_endpoints'].each do |_, endpoint|
extra_endpoints << endpoint
end
-handler_config = {
+ config = {
:api_key => node['datadog']['api_key'],
:application_key => node['datadog']['application_key'],
:use_ec2_instance_id => node['datadog']['use_ec2_instance_id'],
@@ -65,13 +68,18 @@ handler_config = {
}
unless node['datadog']['use_ec2_instance_id']
- handler_config[:hostname] = node['datadog']['hostname']
+ config[:hostname] = node['datadog']['hostname']
+ end
+ config
end
-
-# Create the handler to run at the end of the Chef execution
-chef_handler 'Chef::Handler::Datadog' do
source 'chef/handler/datadog'
- arguments [handler_config]
+ arguments(
+ if respond_to?(:lazy)
+ lazy { [handler_config] }
+ else
+ [handler_config]
+ end
+ )
supports :report => true, :exception => true
action :nothing
only_if { node['datadog']['chef_handler_enable'] }

MODIFY
spec/dd-agent_spec.rb
spec/dd-agent_spec.rb
@@ -76,7 +76,6 @@ describe 'datadog::dd-agent' do
include EnvVar
context 'no version set' do
- # This recipe needs to have an api_key, otherwise `raise` is called.
# It also depends on the version of Python present on the platform:
# 2.6 and up => datadog-agent is installed
# below 2.6 => datadog-agent-base is installed

Defer evaluation of api and application keys (#328)

Some users may wish to assign the api_key and application_key attributes during
runtime instead of compile time -- particularly, users who want to obtain the
value of the key(s) from an external service. Allow doing so by deferring
evaluation of these attributes.

Closes #328