Skip to content

Commit

Permalink
Make converge_by conditional for create, append, update actions
Browse files Browse the repository at this point in the history
  • Loading branch information
bdclark committed Jun 24, 2014
1 parent b53d333 commit 85a7c54
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions providers/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ def whyrun_supported?
Chef::Log.debug "#{new_resource} already exists - overwriting."
end

converge_by("Create #{new_resource}") do
hostsfile.add(
ip_address: new_resource.ip_address,
hostname: new_resource.hostname,
aliases: new_resource.aliases,
comment: new_resource.comment,
priority: new_resource.priority,
unique: new_resource.unique,
)
hostsfile.save
hostsfile.add(
ip_address: new_resource.ip_address,
hostname: new_resource.hostname,
aliases: new_resource.aliases,
comment: new_resource.comment,
priority: new_resource.priority,
unique: new_resource.unique,
)

if hostsfile.content_changed?
converge_by("Create #{new_resource}") { hostsfile.save }
else
Chef::Log.info "#{new_resource} content already matches - nothing to do."
end
end

Expand Down Expand Up @@ -71,33 +74,40 @@ def whyrun_supported?
Chef::Log.info "#{new_resource} does not exist - creating instead."
end

converge_by("Append #{new_resource}") do
hostsfile.append(
hostsfile.append(
ip_address: new_resource.ip_address,
hostname: new_resource.hostname,
aliases: new_resource.aliases,
comment: new_resource.comment,
priority: new_resource.priority,
unique: new_resource.unique,
)

if hostsfile.content_changed?
converge_by("Append #{new_resource}") { hostsfile.save }
else
Chef::Log.info "#{new_resource} content already matches - nothing to do."
end
end

# Updates the given hosts file entry. Does nothing if the entry does not
# exist.
action :update do
if hostsfile.contains?(new_resource)

hostsfile.update(
ip_address: new_resource.ip_address,
hostname: new_resource.hostname,
aliases: new_resource.aliases,
comment: new_resource.comment,
priority: new_resource.priority,
unique: new_resource.unique,
)
hostsfile.save
end
end

# Updates the given hosts file entry. Does nothing if the entry does not
# exist.
action :update do
if hostsfile.contains?(new_resource)
converge_by("Update #{new_resource}") do
hostsfile.update(
ip_address: new_resource.ip_address,
hostname: new_resource.hostname,
aliases: new_resource.aliases,
comment: new_resource.comment,
priority: new_resource.priority,
unique: new_resource.unique,
)
hostsfile.save
if hostsfile.content_changed?
converge_by("Update #{new_resource}") { hostsfile.save }
else
Chef::Log.info "#{new_resource} content already matches - nothing to do."
end
else
Chef::Log.info "#{new_resource} does not exist - skipping update."
Expand Down

0 comments on commit 85a7c54

Please sign in to comment.