diff options
-rw-r--r-- | lib/chef/resource/chef_client_config.rb | 26 | ||||
-rw-r--r-- | lib/chef/resource/support/client.erb | 9 | ||||
-rw-r--r-- | spec/unit/resource/chef_client_config_spec.rb | 18 |
3 files changed, 40 insertions, 13 deletions
diff --git a/lib/chef/resource/chef_client_config.rb b/lib/chef/resource/chef_client_config.rb index 6ba8128f83..89ca1fb326 100644 --- a/lib/chef/resource/chef_client_config.rb +++ b/lib/chef/resource/chef_client_config.rb @@ -70,6 +70,7 @@ class Chef # @todo policy_file or policy_group being set requires the other to be set so enforce that. # @todo all properties for automate report # @todo add all descriptions + # @todo validate handler hash structure # # @param [String, Symbol] prop_val the value from the property @@ -225,7 +226,7 @@ class Chef chef_license: new_resource.chef_license, chef_server_url: new_resource.chef_server_url, event_loggers: new_resource.event_loggers, - exception_handlers: new_resource.exception_handlers, + exception_handlers: format_handler(new_resource.exception_handlers), file_backup_path: new_resource.file_backup_path, file_cache_path: new_resource.file_cache_path, file_staging_uses_destdir: new_resource.file_staging_uses_destdir, @@ -244,10 +245,10 @@ class Chef pid_file: new_resource.pid_file, policy_group: new_resource.policy_group, policy_name: new_resource.policy_name, - report_handlers: new_resource.report_handlers, + report_handlers: format_handler(new_resource.report_handlers), run_path: new_resource.run_path, ssl_verify_mode: new_resource.ssl_verify_mode, - start_handlers: new_resource.start_handlers, + start_handlers: format_handler(new_resource.start_handlers), additional_config: new_resource.additional_config ) mode "0640" @@ -260,6 +261,25 @@ class Chef action :delete end end + + action_class do + # + # Format the handler document in the way we want it presented in the client.rb file + # + # @param [Hash] a handler property + # + # @return [Array] Array of handler data + # + def format_handler(handler_property) + handler_data = [] + + handler_property.each do |handler| + handler_data << "#{handler["class"]}.new(#{handler["arguments"].join(",")})" + end + + handler_data + end + end end end end diff --git a/lib/chef/resource/support/client.erb b/lib/chef/resource/support/client.erb index ed03302dcd..8ef965187c 100644 --- a/lib/chef/resource/support/client.erb +++ b/lib/chef/resource/support/client.erb @@ -24,7 +24,7 @@ <% next if eval('@' + prop).nil? || eval('@' + prop).empty? -%> <%=prop %> = <%= eval('@' + prop).inspect %> <% end -%> -<%# log_location is special due to STDOUT/STDERR from String -> IO Object %> +<%# log_location is special due to STDOUT/STDERR from String -> IO Object -%> <% unless @log_location.nil? %> <% if @log_location.is_a? String && %w(STDOUT STDERR).include?(@log_location) -%> log_location = <%= @log_location %> @@ -32,11 +32,12 @@ log_location = <%= @log_location %> log_location = <%= @log_location.inspect %> <% end -%> <% end -%> +<%# The code below is not DRY on purpose to improve readability -%> <% unless @start_handlers.empty? -%> # Do not crash if a start handler is missing / not installed yet begin <% @start_handlers.each do |handler| -%> - start_handlers << <%= handler["class"] %>.new(<%= handler["arguments"].join(',') %>) + start_handlers << <%= @handler %> <% end -%> rescue NameError => e Chef::Log.error e @@ -46,7 +47,7 @@ log_location = <%= @log_location.inspect %> # Do not crash if a report handler is missing / not installed yet begin <% @report_handlers.each do |handler| -%> - report_handlers << <%= handler["class"] %>.new(<%= handler["arguments"].join(',') %>) + report_handlers << <%= @handler %> <% end -%> rescue NameError => e Chef::Log.error e @@ -56,7 +57,7 @@ log_location = <%= @log_location.inspect %> # Do not crash if an exception handler is missing / not installed yet begin <% @exception_handlers.each do |handler| -%> - exception_handlers << <%= handler["class"] %>.new(<%= handler["arguments"].join(',') %>) + exception_handlers << <%= @handler %> <% end -%> rescue NameError => e Chef::Log.error e diff --git a/spec/unit/resource/chef_client_config_spec.rb b/spec/unit/resource/chef_client_config_spec.rb index fd9741adcc..51754ea03f 100644 --- a/spec/unit/resource/chef_client_config_spec.rb +++ b/spec/unit/resource/chef_client_config_spec.rb @@ -34,7 +34,7 @@ describe Chef::Resource::ChefClientConfig do expect { resource.action :remove }.not_to raise_error end - context "ssl_verify_mode" do + describe "ssl_verify_mode property" do it "coerces String to Symbol" do resource.ssl_verify_mode "verify_peer" expect(resource.ssl_verify_mode).to eql(:verify_peer) @@ -52,7 +52,7 @@ describe Chef::Resource::ChefClientConfig do end end - context "no_proxy" do + describe "no_proxy property" do it "coerces Array into comma separated list" do resource.no_proxy ["something.com", "example.com"] expect(resource.no_proxy).to eql("something.com,example.com") @@ -64,7 +64,7 @@ describe Chef::Resource::ChefClientConfig do end end - context "ohai_disabled_plugins" do + describe "ohai_disabled_plugins property" do it "coerces String values into capitalized symbols" do resource.ohai_disabled_plugins %w{foo Bar} expect(resource.ohai_disabled_plugins).to eql(%i{Foo Bar}) @@ -81,7 +81,7 @@ describe Chef::Resource::ChefClientConfig do end end - context "ohai_optional_plugins" do + describe "ohai_optional_plugins property" do it "coerces String values into capitalized symbols" do resource.ohai_optional_plugins %w{foo Bar} expect(resource.ohai_optional_plugins).to eql(%i{Foo Bar}) @@ -98,7 +98,7 @@ describe Chef::Resource::ChefClientConfig do end end - context "log_level" do + describe "log_level property" do it "accepts auto trace debug info warn fatal" do expect { resource.log_level(:auto) }.not_to raise_error expect { resource.log_level(:trace) }.not_to raise_error @@ -112,7 +112,7 @@ describe Chef::Resource::ChefClientConfig do end end - context "log_location" do + describe "log_location property" do it "accepts a String logfile location" do expect { resource.log_location("/foo/bar/") }.not_to raise_error end @@ -128,4 +128,10 @@ describe Chef::Resource::ChefClientConfig do expect { resource.log_location(:nope) }.to raise_error(Chef::Exceptions::ValidationFailed) end end + + describe "#format_handler" do + it "provides an array of handler object creation code" do + expect(provider.format_handler([{"class" => "Foo", "arguments" => ["'one'", "two", "three"]}])).to eql(["Foo.new('one',two,three)"]) + end + end end |