Class: Libvirt::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/libvirt/network.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer) ⇒ Network

Returns a new instance of Network.

Parameters:

  • pointer (FFI::Pointer)


14
15
16
17
18
19
20
21
22
23
24
# 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

Class Method Details

.load_ref(pointer) ⇒ Object

Parameters:

  • pointer (FFI::Pointer)

Raises:



6
7
8
9
10
11
# 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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

Raises:



62
63
64
65
66
67
# 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?Boolean

Returns:

  • (Boolean)

Raises:



89
90
91
92
93
94
95
# 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_nameString

Returns:

  • (String)

Raises:



80
81
82
83
84
85
# 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

#destroyObject



137
138
139
140
# 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) ⇒ Array<Libvirt::NetworkDhcpLease>, Array

Parameters:

  • mac (String) (defaults to: nil)

Returns:

Raises:



118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Integer

Parameters:

  • mac (String) (defaults to: nil)

Returns:

  • (Integer)

Raises:



108
109
110
111
112
113
# 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

#nameString

Returns:

  • (String)

Raises:



43
44
45
46
47
48
# 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?Boolean

Returns:

  • (Boolean)

Raises:



71
72
73
74
75
76
# 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) ⇒ Object

Parameters:

  • value (Boolean)

Raises:



99
100
101
102
103
# 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

#startObject



131
132
133
134
# 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_ptrFFI::Pointer

Returns:

  • (FFI::Pointer)


27
28
29
# File 'lib/libvirt/network.rb', line 27

def to_ptr
  @ptr
end

#undefineObject



143
144
145
146
# 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)) ⇒ Object

Parameters:

  • xml (String)
  • command (Integer, Symbol)
  • section (Integer, Symbol)
  • flags (Integer, Symbol)
  • parent_index (Integer) (defaults to: -1))

    default -1 (means don’t care)

Raises:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 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

#uuidString

Returns:

  • (String)

Raises:



33
34
35
36
37
38
39
# 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) ⇒ Object

Parameters:

  • options_or_flags (Array<Symbol>, Hash{Symbol=>Boolean}, Integer, Symbol, nil) (defaults to: nil)

Raises:



52
53
54
55
56
57
58
# 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