Class: Libvirt::Spec::Device::Interface

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/libvirt/spec/device/interface.rb

Overview

Represents a network interface device which is attached to a domain.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#raise_if_unparseables, #try

Constructor Details

#initialize(xml = nil) ⇒ Interface

Initializes a new network interface device. If an XML string is given, it will be used to attempt to initialize the attributes.



16
17
18
# File 'lib/libvirt/spec/device/interface.rb', line 16

def initialize(xml=nil)
  load!(xml) if xml
end

Instance Attribute Details

#mac_addressObject

Returns the value of attribute mac_address.



10
11
12
# File 'lib/libvirt/spec/device/interface.rb', line 10

def mac_address
  @mac_address
end

#model_typeObject

Returns the value of attribute model_type.



11
12
13
# File 'lib/libvirt/spec/device/interface.rb', line 11

def model_type
  @model_type
end

#source_networkObject

Returns the value of attribute source_network.



12
13
14
# File 'lib/libvirt/spec/device/interface.rb', line 12

def source_network
  @source_network
end

#typeObject

Returns the value of attribute type.



9
10
11
# File 'lib/libvirt/spec/device/interface.rb', line 9

def type
  @type
end

Instance Method Details

#load!(xml) ⇒ Object

Attempts to initialize object attributes based on XML attributes.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/libvirt/spec/device/interface.rb', line 21

def load!(xml)
  xml = Nokogiri::XML(xml).root if !xml.is_a?(Nokogiri::XML::Element)
  try(xml.xpath("//interface")) do |interface|
    self.type = interface["type"].to_sym if interface["type"]
    try(interface.xpath("mac")) { |result| self.mac_address = result["address"] }
    try(interface.xpath("model")) { |result| self.model_type = result["type"] }
    try(interface.xpath("source")) do |result|
      self.source_network = result["network"]
    end

    raise_if_unparseables(interface.xpath("*"))
  end
end

#to_xml(xml = Nokogiri::XML::Builder.new) ⇒ Object

Returns the XML representation of this device



36
37
38
39
40
41
42
43
# File 'lib/libvirt/spec/device/interface.rb', line 36

def to_xml(xml=Nokogiri::XML::Builder.new)
  xml.interface(:type => type) do |i|
    i.mac(:address => mac_address) if mac_address
    i.model(:type => model_type) if model_type
  end

  xml.to_xml
end