class Libvirt::Network

Public Class Methods

load_ref(pointer) click to toggle source

@param pointer [FFI::Pointer]

# File lib/libvirt/network.rb, line 6
def self.load_ref(pointer)
  result = FFI::Network.virNetworkRef(pointer)
  raise Errors::LibError, "Couldn't retrieve network reference" if result.negative?

  new(pointer)
end
new(pointer) click to toggle source

@param pointer [FFI::Pointer]

# File lib/libvirt/network.rb, line 14
def initialize(pointer)
  @ptr = pointer

  free = ->(obj_id) do
    dbg { "Finalize Libvirt::Network object_id=0x#{obj_id.to_s(16)}, pointer=0x#{@ptr.address.to_s(16)}" }
    return unless @ptr

    warn "Couldn't free Libvirt::Network object_id=0x#{obj_id.to_s(16)}, pointer=0x#{@ptr.address.to_s(16)}" if FFI::Network.virNetworkFree(@ptr).negative?
  end
  ObjectSpace.define_finalizer(self, free)
end

Public Instance Methods

active?() click to toggle source

@return [Boolean] @raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 62
def active?
  result = FFI::Network.virNetworkIsActive(@ptr)
  raise Errors::LibError, "Couldn't get network is active" if result.nil?

  result == 1
end
auto_start?() click to toggle source

@return [Boolean] @raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 89
def auto_start?
  value = ::FFI::MemoryPointer.new(:int)
  result = FFI::Network.virNetworkGetAutostart(@ptr, value)
  raise Errors::LibError, "Couldn't get network auto_start" if result.negative?

  value.read_int == 1
end
bridge_name() click to toggle source

@return [String] @raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 80
def bridge_name
  result = FFI::Network.virNetworkGetBridgeName(@ptr)
  raise Errors::LibError, "Couldn't get network bridge_name" if result.nil?

  result
end
destroy() click to toggle source

@raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 137
def destroy
  result = FFI::Network.virNetworkDestroy(@ptr)
  raise Errors::LibError, "Couldn't destroy network" if result.negative?
end
dhcp_leases(mac = nil) click to toggle source

@param mac [String] @return [Array<Libvirt::NetworkDhcpLease>, Array] @raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 118
def dhcp_leases(mac = nil)
  size = dhcp_leases_qty(mac)
  return [] if size.zero?

  dhcp_leases_ptr = ::FFI::MemoryPointer.new(:pointer, size)
  result = FFI::Network.virNetworkGetDHCPLeases(@ptr, mac, dhcp_leases_ptr, 0)
  raise Errors::LibError, "Couldn't retrieve network dhcp leases" if result.negative?

  ptr = dhcp_leases_ptr.read_pointer
  ptr.get_array_of_pointer(0, size).map { |dhcpl_ptr| NetworkDhcpLease.new(dhcpl_ptr) }
end
dhcp_leases_qty(mac = nil) click to toggle source

@param mac [String] @return [Integer] @raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 108
def dhcp_leases_qty(mac = nil)
  result = FFI::Network.virNetworkGetDHCPLeases(@ptr, mac, nil, 0)
  raise Errors::LibError, "Couldn't get network dhcp leases qty" if result.nil?

  result
end
name() click to toggle source

@return [String] @raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 43
def name
  result = FFI::Network.virNetworkGetName(@ptr)
  raise Errors::LibError, "Couldn't get network name" if result.nil?

  result
end
persistent?() click to toggle source

@return [Boolean] @raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 71
def persistent?
  result = FFI::Network.virNetworkIsPersistent(@ptr)
  raise Errors::LibError, "Couldn't get network is persistent" if result.nil?

  result == 1
end
set_auto_start(value) click to toggle source

@param value [Boolean] @raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 99
def set_auto_start(value)
  value = value ? 1 : 0
  result = FFI::Network.virNetworkSetAutostart(@ptr, value)
  raise Errors::LibError, "Couldn't set network auto_start" if result.negative?
end
start() click to toggle source

@raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 131
def start
  result = FFI::Network.virNetworkCreate(@ptr)
  raise Errors::LibError, "Couldn't start network" if result.negative?
end
to_ptr() click to toggle source

@return [FFI::Pointer]

# File lib/libvirt/network.rb, line 27
def to_ptr
  @ptr
end
undefine() click to toggle source

@raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 143
def undefine
  result = FFI::Network.virNetworkUndefine(@ptr)
  raise Errors::LibError, "Couldn't undefine network" if result.negative?
end
update(xml, command, section, flags, parent_index = -1) click to toggle source

@param xml [String] @param command [Integer, Symbol] @param section [Integer, Symbol] @param flags [Integer, Symbol] @param parent_index [Integer] default -1 (means don't care) @raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 154
def update(xml, command, section, flags, parent_index = -1)
  command = Util.parse_flags command, FFI::Network.enum_type(:update_command)
  section = Util.parse_flags section, FFI::Network.enum_type(:update_section)
  flags = Util.parse_flags flags, FFI::Network.enum_type(:update_flags)

  result = FFI::Network.virNetworkUpdate(
      @ptr,
      command,
      section,
      parent_index,
      xml,
      flags
  )
  raise Errors::LibError, "Couldn't update network" if result.negative?
end
uuid() click to toggle source

@return [String] @raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 33
def uuid
  buff = ::FFI::MemoryPointer.new(:char, Util::UUID_STRING_BUFLEN)
  result = FFI::Network.virNetworkGetUUIDString(@ptr, buff)
  raise Errors::LibError, "Couldn't get network uuid" if result.negative?

  buff.read_string
end
xml_desc(options_or_flags = nil) click to toggle source

@param options_or_flags [Array<Symbol>,Hash{Symbol=>Boolean},Integer,Symbol,nil] @raise [Libvirt::Errors::LibError]

# File lib/libvirt/network.rb, line 52
def xml_desc(options_or_flags = nil)
  flags = Util.parse_flags options_or_flags, FFI::Network.enum_type(:xml_flags)
  result = FFI::Network.virNetworkGetXMLDesc(@ptr, flags)
  raise Errors::LibError, "Couldn't get network xml_desc" if result.nil?

  result
end

Private Instance Methods

dbg(&block) click to toggle source
# File lib/libvirt/network.rb, line 172
def dbg(&block)
  Util.log(:debug, 'Libvirt::Network', &block)
end