From 4cf0acceef9d1ae5eb9b938b9021e4eaf631edef Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Tue, 13 Oct 2020 11:23:15 -0700 Subject: Move some of the erb logic into the resource This makes it easier to read and easier to test Signed-off-by: Tim Smith --- lib/chef/resource/chef_client_config.rb | 26 +++++++++++++++++++++++--- lib/chef/resource/support/client.erb | 9 +++++---- 2 files changed, 28 insertions(+), 7 deletions(-) (limited to 'lib/chef/resource') 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 -- cgit v1.2.1