Skip to content

Commit

Permalink
human readable diff in case of changes; correct sort for portgroups i…
Browse files Browse the repository at this point in the history
…nside XML
  • Loading branch information
banuchka committed Feb 8, 2024
1 parent a9e9437 commit 4c55908
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/puppet/provider/libvirt_network/virsh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,16 @@ def content
formatter = REXML::Formatters::Pretty.new(2)
formatter.compact = true
output = ''.dup
formatter.write(recursive_sort(xml.root), output)

xml = recursive_sort(xml.root)
sorted_portgroups = xml.root.get_elements('//portgroup').sort_by { |obj| obj.attributes['name'] }

REXML::XPath.match(xml, '//portgroup').each(&:remove)
sorted_portgroups.each do |pg|
xml.root.add_element pg
end

formatter.write(xml.root, output)
output
end

Expand Down
16 changes: 16 additions & 0 deletions lib/puppet/type/libvirt_network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@
raise ArgumentError, 'content needs to be a XML string' unless value.is_a?(String)
end

def change_to_s(current, desire)
if @resource[:show_diff]
current_exp = Tempfile.new("current", "/tmp").open
current_exp.write(current.to_s)
current_exp.close
current_path = current_exp.path
desire_exp = Tempfile.new("desire", "/tmp").open
desire_exp.write(desire.to_s)
desire_exp.close
desire_path = desire_exp.path
system "diff -u #{current_path} #{desire_path}"
else
'{md5}' + Digest::MD5.hexdigest(current.to_s) + " to: " + '{md5}' + Digest::MD5.hexdigest(desire.to_s)
end
end

def should_to_s(value)
if @resource[:show_diff]
":\n" + value + "\n"
Expand Down

0 comments on commit 4c55908

Please sign in to comment.