Skip to content

Commit

Permalink
Refactor LWRPs into custom resources
Browse files Browse the repository at this point in the history
This originated from a need to fix the deprecation warnings for
use_inline_resources (FC113)[1] and this commit implements just the bare
minimum required to convert to custom resources while keeping all the
tests passing.

Support for chef-client 12 is dropped as that will be EOL this month[2].

I also removed the exec bit from some template and attribute files.

[1] http://www.foodcritic.io/#FC113
[2] https://www.chef.io/eol-chef12-and-chefdk1/
  • Loading branch information
ccrebolder committed Apr 20, 2018
1 parent 6ea1cc2 commit 8d196b7
Show file tree
Hide file tree
Showing 20 changed files with 339 additions and 240 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dhcp Cookbook CHANGELOG

- Convert to custom resources
- Drop support for Chef 12 https://www.chef.io/eol-chef12-and-chefdk1/

## 6.0.0 (2018-03-04)

- Remove matchers. Breaking change. This requires ChefDK 2.0+
Expand Down
Empty file modified attributes/default.rb
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
license 'Apache-2.0'
description 'Installs and configures DHCP'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
chef_version '>= 12.7' if respond_to?(:chef_version)
chef_version '>= 13.0' if respond_to?(:chef_version)
source_url 'https://github.com/sous-chefs/dhcp'
issues_url 'https://github.com/sous-chefs/dhcp/issues'
version '6.0.0'
Expand Down
24 changes: 0 additions & 24 deletions providers/class.rb

This file was deleted.

38 changes: 0 additions & 38 deletions providers/group.rb

This file was deleted.

39 changes: 0 additions & 39 deletions providers/host.rb

This file was deleted.

55 changes: 0 additions & 55 deletions providers/shared_network.rb

This file was deleted.

48 changes: 0 additions & 48 deletions providers/subnet.rb

This file was deleted.

49 changes: 46 additions & 3 deletions resources/class.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
actions :add
# frozen_string_literal: true
#
# Cookbook:: dhcp
# Resource:: class
#
# 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.
#

default_action :add

attribute :match, kind_of: String, required: true
attribute :conf_dir, kind_of: String, default: '/etc/dhcp'
property :match, String, required: true
property :conf_dir, String, default: '/etc/dhcp'

attr_accessor :subclasses

def subclass(value)
@subclasses ||= []
@subclasses << value
end

action_class do
include Dhcp::Helpers
end

action :add do
with_run_context :root do
run_context.include_recipe 'dhcp::_service'

directory "#{new_resource.conf_dir}/classes.d #{new_resource.name}" do
path "#{new_resource.conf_dir}/classes.d"
end

template "#{new_resource.conf_dir}/classes.d/#{new_resource.name}.conf" do
cookbook 'dhcp'
source 'class.conf.erb'
variables name: new_resource.name, match: new_resource.match, subclasses: new_resource.subclasses
owner 'root'
group 'root'
mode '0644'
notifies :restart, "service[#{node['dhcp']['service_name']}]", :delayed
end

write_include 'classes.d', new_resource.name
end
end
68 changes: 62 additions & 6 deletions resources/group.rb
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,64 @@
actions :add, :remove
# frozen_string_literal: true
#
# Cookbook:: dhcp
# Resource:: group
#
# 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.
#

default_action :add

attribute :name, kind_of: String, name_attribute: true
attribute :parameters, kind_of: Array, default: []
attribute :evals, kind_of: Array, default: []
attribute :hosts, kind_of: Hash, default: {}
attribute :conf_dir, kind_of: String, default: '/etc/dhcp'
property :parameters, Array, default: []
property :evals, Array, default: []
property :hosts, Hash, default: {}
property :conf_dir, String, default: '/etc/dhcp'

action_class do
include Dhcp::Helpers
end

action :add do
with_run_context :root do
directory "#{new_resource.conf_dir}/groups.d #{new_resource.name}" do
path "#{new_resource.conf_dir}/groups.d"
end

template "#{new_resource.conf_dir}/groups.d/#{new_resource.name}.conf" do
cookbook 'dhcp'
source 'group.conf.erb'
variables(
name: new_resource.name,
parameters: new_resource.parameters,
evals: new_resource.evals,
hosts: new_resource.hosts
)
owner 'root'
group 'root'
mode '0644'
notifies :restart, "service[#{node['dhcp']['service_name']}]", :delayed
end

write_include 'groups.d', new_resource.name
end
end

action :remove do
with_run_context :root do
file "#{new_resource.conf_dir}/groups.d/#{new_resource.name}.conf" do
action :delete
notifies :restart, "service[#{node['dhcp']['service_name']}]", :delayed
end

write_include 'groups.d', new_resource.name
end
end
Loading

0 comments on commit 8d196b7

Please sign in to comment.