Skip to content
This repository has been archived by the owner on Apr 7, 2018. It is now read-only.

Commit

Permalink
Convert to a custom_resource with type property not supports
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 committed Jun 19, 2017
1 parent 75bf993 commit 4c42e1f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 96 deletions.
1 change: 1 addition & 0 deletions .kitchen.yml
Expand Up @@ -3,6 +3,7 @@ driver:

provisioner:
name: chef_zero
deprecations_as_errors: true

platforms:
- name: centos-6.9
Expand Down
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -14,7 +14,7 @@ Provides a resource for installing and automatically loading [Chef report and ex

### Chef

- Chef 12.1+
- Chef 12.7+

### Cookbooks

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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 }
)
```

Expand Down
1 change: 1 addition & 0 deletions libraries/helpers.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion metadata.rb
Expand Up @@ -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
Expand Down
72 changes: 0 additions & 72 deletions providers/default.rb

This file was deleted.

66 changes: 48 additions & 18 deletions resources/default.rb
Expand Up @@ -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
12 changes: 11 additions & 1 deletion 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'
Expand All @@ -11,4 +20,5 @@

chef_handler 'MyCorp::MyLibraryHandler' do
action :enable
supports exception: true
end

0 comments on commit 4c42e1f

Please sign in to comment.