Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use XML builders to construct XML #135

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further testing told me save isn't even implemented now so I'm not too worried about this.

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.

6 changes: 0 additions & 6 deletions lib/fog/libvirt/models/compute/templates/pool.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 @@
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
12 changes: 12 additions & 0 deletions tests/libvirt/models/compute/network_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@
test('be a kind of Fog::Libvirt::Compute::Network') { network.kind_of? Fog::Libvirt::Compute::Network }
end

tests("to_xml") do
test("default") do
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
20 changes: 20 additions & 0 deletions tests/libvirt/models/compute/volume_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,24 @@
test('succeed') { volume.xml == new_vol.xml }
end

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>
VOLUME
volume.to_xml == expected
end
end
end
Loading