From 4c42e1f882a870afc6e6bedcd91e1cacaf5ca91b Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Mon, 19 Jun 2017 11:21:29 -0700 Subject: [PATCH] Convert to a custom_resource with type property not supports Signed-off-by: Tim Smith --- .kitchen.yml | 1 + README.md | 8 +-- libraries/helpers.rb | 1 + metadata.rb | 2 +- providers/default.rb | 72 ------------------- resources/default.rb | 66 ++++++++++++----- .../cookbooks/test/recipes/default.rb | 12 +++- 7 files changed, 66 insertions(+), 96 deletions(-) delete mode 100644 providers/default.rb diff --git a/.kitchen.yml b/.kitchen.yml index faae6dc..4703e2c 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -3,6 +3,7 @@ driver: provisioner: name: chef_zero + deprecations_as_errors: true platforms: - name: centos-6.9 diff --git a/README.md b/README.md index 1943442..c0503d0 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Provides a resource for installing and automatically loading [Chef report and ex ### Chef -- Chef 12.1+ +- Chef 12.7+ ### Cookbooks @@ -42,7 +42,7 @@ It is best to declare `chef_handler` resources early on in the compile phase so - `class_name:` name attribute. The name of the handler class (can be module name-spaced). - `source:` full path to the handler file. can also be a gem path if the handler ships as part of a Ruby gem. can also be nil, in which case the file must be loaded as a library. - `arguments:` an array of arguments to pass the handler's class initializer -- `supports:` type of Chef Handler to register as, i.e. :report, :exception or both. default is `:report => true, :exception => true` +- `type:` type of Chef Handler to register as, i.e. :report, :exception or both. default is `:report => true, :exception => true` #### Example @@ -66,7 +66,7 @@ It is best to declare `chef_handler` resources early on in the compile phase so chef_handler 'Chef::Handler::JsonFile' do source 'chef/handler/json_file' arguments path: '/var/chef/reports' - supports exception: true + type exception: true action :enable end @@ -95,7 +95,7 @@ chef_handler provides built in [chefspec](https://github.com/chefspec/chefspec) expect(runner).to enable_chef_handler('Chef::Handler::JsonFile').with( source: 'chef/handler/json_file', arguments: { path: '/var/chef/reports' }, - supports: { exception: true } + type: { exception: true } ) ``` diff --git a/libraries/helpers.rb b/libraries/helpers.rb index 1b714f7..c8ac018 100644 --- a/libraries/helpers.rb +++ b/libraries/helpers.rb @@ -32,6 +32,7 @@ def register_handler(handler_type, handler) # @param class_full_name [String] such as 'Chef::Handler::ErrorReport'. def unregister_handler(handler_type, class_full_name) Chef::Config.send("#{handler_type}_handlers").delete_if do |v| + # avoid a bit of log spam if v.class.name == class_full_name Chef::Log.info("Disabling #{class_full_name} as a #{handler_type} handler.") true diff --git a/metadata.rb b/metadata.rb index 2b60f8c..1774b7f 100644 --- a/metadata.rb +++ b/metadata.rb @@ -11,7 +11,7 @@ source_url 'https://github.com/chef-cookbooks/chef_handler' issues_url 'https://github.com/chef-cookbooks/chef_handler/issues' -chef_version '>= 12.1' if respond_to?(:chef_version) +chef_version '>= 12.7' if respond_to?(:chef_version) %w(ubuntu debian redhat centos fedora).each do |os| supports os diff --git a/providers/default.rb b/providers/default.rb deleted file mode 100644 index 1c55474..0000000 --- a/providers/default.rb +++ /dev/null @@ -1,72 +0,0 @@ -# -# Author:: Seth Chisamore -# Cookbook Name:: chef_handler -# Provider:: default -# -# Copyright:: 2011-2016, Chef Software, Inc -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -include ::ChefHandler::Helpers - -use_inline_resources - -def whyrun_supported? - true -end - -# This action needs to find an rb file that presumably contains the indicated class in it and the -# load that file. It then instantiates that class by name and registers it as a handler. -action :enable do - class_name = new_resource.class_name - new_resource.supports.each do |type, enable| - next unless enable - unregister_handler(type, class_name) - end - - handler = nil - - require new_resource.source unless new_resource.source.nil? - - _, klass = get_class(class_name) - handler = klass.send(:new, *collect_args(new_resource.arguments)) - - new_resource.supports.each do |type, enable| - next unless enable - register_handler(type, handler) - end -end - -action :disable do - new_resource.supports.each_key do |type| - unregister_handler(type, new_resource.class_name) - end -end - -def load_current_resource - @current_resource = Chef::Resource.resource_for_node(new_resource.declared_type, run_context.node).new(new_resource.name) - @current_resource.class_name(new_resource.class_name) - @current_resource.source(new_resource.source) - @current_resource -end - -private - -def collect_args(resource_args = []) - if resource_args.is_a? Array - resource_args - else - [resource_args] - end -end diff --git a/resources/default.rb b/resources/default.rb index dcf9428..e6233a7 100644 --- a/resources/default.rb +++ b/resources/default.rb @@ -18,22 +18,52 @@ # limitations under the License. # -actions :enable, :disable -default_action :enable - -state_attrs :arguments, - :class_name, - :source, - :supports - -attribute :class_name, kind_of: String, name_attribute: true -attribute :source, default: nil, kind_of: String -attribute :arguments, default: [] -attribute :supports, kind_of: Hash, default: { report: true, exception: true } - -# we have to set default for the supports attribute -# in initializer since it is a 'reserved' attribute name -def initialize(*args) - super - @supports = { report: true, exception: true } +property :class_name, String, name_property: true +property :source, String +property :arguments, [Array, String], default: [] +property :type, Hash, default: { report: true, exception: true } + +# supports means a different thing in chef-land so we renamed it but +# wanted to make sure we didn't break the world +alias_method :supports, :type + +# This action needs to find an rb file that presumably contains the indicated class in it and the +# load that file. It then instantiates that class by name and registers it as a handler. +action :enable do + class_name = new_resource.class_name + new_resource.type.each do |type, enable| + next unless enable + + unregister_handler(type, class_name) + end + + handler = nil + + require new_resource.source unless new_resource.source.nil? + + _, klass = get_class(class_name) + handler = klass.send(:new, *collect_args(new_resource.arguments)) + + new_resource.type.each do |type, enable| + next unless enable + register_handler(type, handler) + end +end + +action :disable do + new_resource.type.each_key do |type| + unregister_handler(type, new_resource.class_name) + end +end + +action_class do + include ChefHandler::Helpers + + def collect_args(resource_args = []) + if resource_args.is_a? Array + resource_args + else + [resource_args] + end + end end diff --git a/test/fixtures/cookbooks/test/recipes/default.rb b/test/fixtures/cookbooks/test/recipes/default.rb index 3bc0123..2b8e62c 100644 --- a/test/fixtures/cookbooks/test/recipes/default.rb +++ b/test/fixtures/cookbooks/test/recipes/default.rb @@ -1,4 +1,13 @@ -include_recipe 'chef_handler::default' +directory 'Handlers directory' do + path node['chef_handler']['handler_path'] + group node['root_group'] + unless platform?('windows') + owner 'root' + mode '0755' + end + recursive true + action :create +end cookbook_file "#{node['chef_handler']['handler_path']}/my_handler.rb" do source 'my_handler.rb' @@ -11,4 +20,5 @@ chef_handler 'MyCorp::MyLibraryHandler' do action :enable + supports exception: true end