forked from mitchellh/libvirt-rb
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Libvirt | ||
module Spec | ||
# A specification of a network interface. This translates directly | ||
# down to XML which can be used to define network interfaces. | ||
class Network | ||
include Util | ||
|
||
attr_accessor :name | ||
attr_accessor :uuid | ||
|
||
# Initializes a network specification. If a valid XML string is | ||
# given, it will attempt to be parsed into the structure. | ||
def initialize(xml=nil) | ||
load!(xml) if xml | ||
end | ||
|
||
# Attempts to load the attributes from an XML specification. | ||
# | ||
# @param [String] xml XML spec to attempt to parse into the structure. | ||
def load!(xml) | ||
root = Nokogiri::XML(xml).root | ||
try(root.xpath("//network")) do |network| | ||
try(network.xpath("name")) { |result| self.name = result.text } | ||
try(network.xpath("uuid")) { |result| self.uuid = result.text } | ||
end | ||
end | ||
|
||
# Returns the XML for this specification. | ||
# | ||
# @return [String] | ||
def to_xml | ||
Nokogiri::XML::Builder.new do |xml| | ||
xml.network do | ||
xml.name name if name | ||
xml.uuid uuid if uuid | ||
end | ||
end.to_xml | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'test_helper' | ||
|
||
Protest.describe("Network spec") do | ||
setup do | ||
@klass = Libvirt::Spec::Network | ||
end | ||
|
||
context "initialization with XML parsing" do | ||
should "parse the name" do | ||
@instance = @klass.new("<network><name>foo</name></network>") | ||
assert_equal "foo", @instance.name | ||
end | ||
|
||
should "parse the UUID" do | ||
@instance = @klass.new("<network><uuid>foo</uuid></network>") | ||
assert_equal "foo", @instance.uuid | ||
end | ||
end | ||
end |