Skip to content

Commit

Permalink
Merge pull request #75 from banuchka/XML_sort_and_useful_diff
Browse files Browse the repository at this point in the history
Human readable diff in case of changes and correct sort for portgroups
  • Loading branch information
trefzer committed Mar 15, 2024
2 parents a9e9437 + 4c55908 commit 9b79032
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
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
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 9b79032

Please sign in to comment.