From e27fcc64ab769c15cdc4efac5673157c7b447995 Mon Sep 17 00:00:00 2001 From: Brent Montague Date: Mon, 12 Aug 2024 02:58:20 -0400 Subject: [PATCH] Add datadog_monitor option to override the config file name (#903) This allows people to use the integration multiple times and place different config files in the same integration directory. The use case would be many different cookbooks adding a single check they care about without doing messy attribute overrides. --- README.md | 14 ++++++++------ resources/monitor.rb | 5 +++-- spec/dd-agent_spec.rb | 2 ++ test/cookbooks/test/recipes/monitor_add.rb | 5 +++++ test/cookbooks/test/recipes/monitor_remove.rb | 5 +++++ 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 307e0a0d..1f4fb87e 100644 --- a/README.md +++ b/README.md @@ -320,19 +320,21 @@ datadog_monitor 'name' do instances Array # default value: [] logs Array # default value: [] use_integration_template true, false # default value: false + config_name String # default value: 'conf' action Symbol # defaults to :add end ``` #### Properties -| Property | Description | -|----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `'name'` | The name of the Agent integration to configure and enable. | -| `instances` | The fields used to fill values under the `instances` section in the integration configuration file. | -| `init_config` | The fields used to fill values under the the `init_config` section in the integration configuration file. | -| `logs` | The fields used to fill values under the the `logs` section in the integration configuration file. | +| Property | Description | +|----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `'name'` | The name of the Agent integration to configure and enable. | +| `instances` | The fields used to fill values under the `instances` section in the integration configuration file. | +| `init_config` | The fields used to fill values under the the `init_config` section in the integration configuration file. | +| `logs` | The fields used to fill values under the the `logs` section in the integration configuration file. | | `use_integration_template` | Set to `true` (recommended) to use the default template, which writes the values of `instances`, `init_config`, and `logs` in the YAML under their respective keys. This defaults to `false` for backward compatibility, but may default to `true` in a future major version of the cookbook. | +| `config_name` | The filename used when creating an integrations configuration file. Overriding this property allows the creation of multiple configuration files for a single integration. This defaults to `conf`, which creates a configuration file named `conf.yaml`. | #### Example diff --git a/resources/monitor.rb b/resources/monitor.rb index 2c930126..7f4b5d5e 100644 --- a/resources/monitor.rb +++ b/resources/monitor.rb @@ -35,6 +35,7 @@ property :use_integration_template, [TrueClass, FalseClass], required: false, default: false property :is_jmx, [TrueClass, FalseClass], required: false, default: false property :logs, [Array, nil], required: false, default: [] +property :config_name, [String, nil], required: false, default: 'conf' action :add do Chef::Log.debug("Adding monitoring for #{new_resource.name}") @@ -49,7 +50,7 @@ mode '755' end end - yaml_file = ::File.join(yaml_dir, "#{new_resource.name}.d", 'conf.yaml') + yaml_file = ::File.join(yaml_dir, "#{new_resource.name}.d", "#{new_resource.config_name}.yaml") else yaml_file = ::File.join(yaml_dir, "#{new_resource.name}.yaml") end @@ -101,7 +102,7 @@ action :remove do yaml_file = if Chef::Datadog.agent_major_version(node) != 5 - ::File.join(yaml_dir, "#{new_resource.name}.d", 'conf.yaml') + ::File.join(yaml_dir, "#{new_resource.name}.d", "#{new_resource.config_name}.yaml") else ::File.join(yaml_dir, "#{new_resource.name}.yaml") end diff --git a/spec/dd-agent_spec.rb b/spec/dd-agent_spec.rb index 7c41e884..c89b4069 100644 --- a/spec/dd-agent_spec.rb +++ b/spec/dd-agent_spec.rb @@ -1506,6 +1506,7 @@ def set_env_var(name, value) end it 'creates the config file at A7\'s path' do expect(chef_run).to render_file('/etc/datadog-agent/conf.d/potato.d/conf.yaml') + expect(chef_run).to render_file('/etc/datadog-agent/conf.d/potato.d/potato-one.yaml') end end end @@ -1541,6 +1542,7 @@ def set_env_var(name, value) end it 'creates the config file at A7\'s path' do expect(chef_run).to delete_file('/etc/datadog-agent/conf.d/potato.d/conf.yaml') + expect(chef_run).to delete_file('/etc/datadog-agent/conf.d/potato.d/potato-one.yaml') end end end diff --git a/test/cookbooks/test/recipes/monitor_add.rb b/test/cookbooks/test/recipes/monitor_add.rb index 7f6e3d15..2073ff01 100644 --- a/test/cookbooks/test/recipes/monitor_add.rb +++ b/test/cookbooks/test/recipes/monitor_add.rb @@ -15,3 +15,8 @@ datadog_monitor 'potato' do action :add end + +datadog_monitor 'potato' do + action :add + config_name 'potato-one' +end \ No newline at end of file diff --git a/test/cookbooks/test/recipes/monitor_remove.rb b/test/cookbooks/test/recipes/monitor_remove.rb index 8ad5b8d7..ff81253d 100644 --- a/test/cookbooks/test/recipes/monitor_remove.rb +++ b/test/cookbooks/test/recipes/monitor_remove.rb @@ -15,3 +15,8 @@ datadog_monitor 'potato' do action :remove end + +datadog_monitor 'potato' do + action :remove + config_name 'potato-one' +end \ No newline at end of file