Skip to content

Commit

Permalink
Always use XML builders to construct XML
Browse files Browse the repository at this point in the history
This removes the use of ERB to construct XML and instead always uses a
builder.
  • Loading branch information
ekohl committed Jul 15, 2024
1 parent 4ea420c commit 476c630
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 65 deletions.
11 changes: 11 additions & 0 deletions lib/fog/libvirt/models/compute/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def save
def shutdown
service.destroy_network(uuid)
end

def to_xml
builder = Nokogiri::XML::Builder.new do |xml|
xml.network do
xml.name(name)
xml.bridge(:name => bridge_name, :stp => 'on', :delay => '0')
end
end

builder.to_xml
end
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions lib/fog/libvirt/models/compute/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ def persistent?
def volumes
service.list_pool_volumes uuid
end

def to_xml
builder = Nokogiri::XML::Builder.new do |xml|
xml.pool(:type => 'dir') do
xml.name(name)

xml.target do
xml.path(path)
end
end
end

builder.to_xml
end
end
end
end
Expand Down
6 changes: 0 additions & 6 deletions lib/fog/libvirt/models/compute/templates/network.xml.erb

This file was deleted.

34 changes: 0 additions & 34 deletions lib/fog/libvirt/models/compute/templates/volume.xml.erb

This file was deleted.

8 changes: 0 additions & 8 deletions lib/fog/libvirt/models/compute/util/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ def xml_elements(xml, path, attribute=nil)
attribute.nil? ? (xml/path).map : (xml/path).map{|element| element[attribute.to_sym]}
end

def to_xml template_name = nil
# figure out our ERB template filename
erb = template_name || self.class.to_s.split("::").last.downcase
path = File.join(File.dirname(__FILE__), "..", "templates", "#{erb}.xml.erb")
template = File.read(path)
ERB.new(template, nil, '-').result(binding)
end

def randomized_name
"fog-#{(SecureRandom.random_number*10E14).to_i.round}"
end
Expand Down
41 changes: 41 additions & 0 deletions lib/fog/libvirt/models/compute/volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,47 @@ def upload_image(file_path)
service.upload_volume(pool_name, name, file_path)
end

def to_xml

Check warning on line 82 in lib/fog/libvirt/models/compute/volume.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Assignment Branch Condition size for to_xml is too high. [<6, 40, 5> 40.76/38] Raw Output: lib/fog/libvirt/models/compute/volume.rb:82:9: C: Metrics/AbcSize: Assignment Branch Condition size for to_xml is too high. [<6, 40, 5> 40.76/38]

Check warning on line 82 in lib/fog/libvirt/models/compute/volume.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Method has too many lines. [31/24] Raw Output: lib/fog/libvirt/models/compute/volume.rb:82:9: C: Metrics/MethodLength: Method has too many lines. [31/24]
builder = Nokogiri::XML::Builder.new do |xml|
xml.volume do
xml.name(name)

allocation_size, allocation_unit = split_size_unit(allocation)
xml.allocation(allocation_size, :unit => allocation_unit)

capacity_size, capacity_unit = split_size_unit(capacity)
xml.capacity(capacity_size, :unit => capacity_unit)

xml.target do
xml.format(:type => format_type)

xml.permissions do
xml.owner(owner) if owner
xml.group(group) if group
xml.mode('0744')
xml.label('virt_image_t')
end
end

if backing_volume
xml.backingStore do
xml.path(backing_volume.path)
xml.format(:type => backing_volume.format_type)

xml.permissions do
xml.owner(owner) if owner
xml.group(group) if group
xml.mode('0744')
xml.label('virt_image_t')
end
end
end
end
end

builder.to_xml
end

private

def image_suffix
Expand Down
14 changes: 8 additions & 6 deletions tests/libvirt/models/compute/network_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@

tests("to_xml") do
test("default") do
begin
network.to_xml
false
rescue NameError # forward_mode is undefined
true
end
expected = <<~NETWORK
<?xml version="1.0"?>
<network>
<name>default</name>
<bridge name="virbr0" stp="on" delay="0"/>
</network>
NETWORK
network.to_xml == expected
end
end
end
23 changes: 12 additions & 11 deletions tests/libvirt/models/compute/volume_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@
test('to_xml') do
test('default') do
expected = <<~VOLUME
<?xml version="1.0"?>
<volume>
<name>fog_test</name>
<allocation unit="G">1</allocation>
<capacity unit="G">10</capacity>
<target>
<format type="raw"/>
<permissions>
<mode>0744</mode>
<label>virt_image_t</label>
</permissions>
</target>
</volume>
<name>fog_test</name>
<allocation unit="G">1</allocation>
<capacity unit="G">10</capacity>
<target>
<format type="raw"/>
<permissions>
<mode>0744</mode>
<label>virt_image_t</label>
</permissions>
</target>
</volume>
VOLUME
volume.to_xml == expected
end
Expand Down

0 comments on commit 476c630

Please sign in to comment.