Skip to content

Commit 476c630

Browse files
committed
Always use XML builders to construct XML
This removes the use of ERB to construct XML and instead always uses a builder.
1 parent 4ea420c commit 476c630

File tree

8 files changed

+86
-65
lines changed

8 files changed

+86
-65
lines changed

lib/fog/libvirt/models/compute/network.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ def save
2727
def shutdown
2828
service.destroy_network(uuid)
2929
end
30+
31+
def to_xml
32+
builder = Nokogiri::XML::Builder.new do |xml|
33+
xml.network do
34+
xml.name(name)
35+
xml.bridge(:name => bridge_name, :stp => 'on', :delay => '0')
36+
end
37+
end
38+
39+
builder.to_xml
40+
end
3041
end
3142
end
3243
end

lib/fog/libvirt/models/compute/pool.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ def persistent?
7878
def volumes
7979
service.list_pool_volumes uuid
8080
end
81+
82+
def to_xml
83+
builder = Nokogiri::XML::Builder.new do |xml|
84+
xml.pool(:type => 'dir') do
85+
xml.name(name)
86+
87+
xml.target do
88+
xml.path(path)
89+
end
90+
end
91+
end
92+
93+
builder.to_xml
94+
end
8195
end
8296
end
8397
end

lib/fog/libvirt/models/compute/templates/network.xml.erb

Lines changed: 0 additions & 6 deletions
This file was deleted.

lib/fog/libvirt/models/compute/templates/volume.xml.erb

Lines changed: 0 additions & 34 deletions
This file was deleted.

lib/fog/libvirt/models/compute/util/util.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ def xml_elements(xml, path, attribute=nil)
1616
attribute.nil? ? (xml/path).map : (xml/path).map{|element| element[attribute.to_sym]}
1717
end
1818

19-
def to_xml template_name = nil
20-
# figure out our ERB template filename
21-
erb = template_name || self.class.to_s.split("::").last.downcase
22-
path = File.join(File.dirname(__FILE__), "..", "templates", "#{erb}.xml.erb")
23-
template = File.read(path)
24-
ERB.new(template, nil, '-').result(binding)
25-
end
26-
2719
def randomized_name
2820
"fog-#{(SecureRandom.random_number*10E14).to_i.round}"
2921
end

lib/fog/libvirt/models/compute/volume.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,47 @@ def upload_image(file_path)
7979
service.upload_volume(pool_name, name, file_path)
8080
end
8181

82+
def to_xml
83+
builder = Nokogiri::XML::Builder.new do |xml|
84+
xml.volume do
85+
xml.name(name)
86+
87+
allocation_size, allocation_unit = split_size_unit(allocation)
88+
xml.allocation(allocation_size, :unit => allocation_unit)
89+
90+
capacity_size, capacity_unit = split_size_unit(capacity)
91+
xml.capacity(capacity_size, :unit => capacity_unit)
92+
93+
xml.target do
94+
xml.format(:type => format_type)
95+
96+
xml.permissions do
97+
xml.owner(owner) if owner
98+
xml.group(group) if group
99+
xml.mode('0744')
100+
xml.label('virt_image_t')
101+
end
102+
end
103+
104+
if backing_volume
105+
xml.backingStore do
106+
xml.path(backing_volume.path)
107+
xml.format(:type => backing_volume.format_type)
108+
109+
xml.permissions do
110+
xml.owner(owner) if owner
111+
xml.group(group) if group
112+
xml.mode('0744')
113+
xml.label('virt_image_t')
114+
end
115+
end
116+
end
117+
end
118+
end
119+
120+
builder.to_xml
121+
end
122+
82123
private
83124

84125
def image_suffix

tests/libvirt/models/compute/network_tests.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030

3131
tests("to_xml") do
3232
test("default") do
33-
begin
34-
network.to_xml
35-
false
36-
rescue NameError # forward_mode is undefined
37-
true
38-
end
33+
expected = <<~NETWORK
34+
<?xml version="1.0"?>
35+
<network>
36+
<name>default</name>
37+
<bridge name="virbr0" stp="on" delay="0"/>
38+
</network>
39+
NETWORK
40+
network.to_xml == expected
3941
end
4042
end
4143
end

tests/libvirt/models/compute/volume_tests.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,19 @@
3838
test('to_xml') do
3939
test('default') do
4040
expected = <<~VOLUME
41+
<?xml version="1.0"?>
4142
<volume>
42-
<name>fog_test</name>
43-
<allocation unit="G">1</allocation>
44-
<capacity unit="G">10</capacity>
45-
<target>
46-
<format type="raw"/>
47-
<permissions>
48-
<mode>0744</mode>
49-
<label>virt_image_t</label>
50-
</permissions>
51-
</target>
52-
</volume>
43+
<name>fog_test</name>
44+
<allocation unit="G">1</allocation>
45+
<capacity unit="G">10</capacity>
46+
<target>
47+
<format type="raw"/>
48+
<permissions>
49+
<mode>0744</mode>
50+
<label>virt_image_t</label>
51+
</permissions>
52+
</target>
53+
</volume>
5354
VOLUME
5455
volume.to_xml == expected
5556
end

0 commit comments

Comments
 (0)