Class: VirtualBox::HostNetworkInterface

Inherits:
AbstractModel show all
Defined in:
lib/virtualbox/host_network_interface.rb

Overview

Represents a network interface on the host. There are generally two types of network interfaces wihch exist on the host: bridged and host-only. This class represents both.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractModel

#errors, errors_for_relationship, #existing_record!, #inspect, #lazy_attribute?, #lazy_relationship?, #new_record!, #new_record?, #parent_machine, #populate_attributes, #populate_relationship, #populate_relationships, reload!, #reload!, reload?, reloaded!, #save, #save!, #save_attribute, #save_changed_interface_attributes, #save_interface_attribute, #set_relationship, #validate, #write_attribute

Methods included from AbstractModel::Validatable

#__validates_extract_options, #add_error, #clear_errors, #errors, #errors_on, #full_error_messages, #valid?, #validate, #validates_format_of, #validates_inclusion_of, #validates_numericality_of, #validates_presence_of

Methods included from AbstractModel::Relatable

#destroy_relationship, #destroy_relationships, #has_relationship?, included, #lazy_relationship?, #loaded_relationship?, #populate_relationship, #populate_relationships, #read_relationship, #relationship_class, #relationship_data, #save_relationship, #save_relationships, #set_relationship

Methods included from AbstractModel::VersionMatcher

#assert_version_match, #split_version, #version_match?

Methods included from AbstractModel::Dirty

#changed?, #changes, #clear_dirty!, #ignore_dirty, #method_missing, #set_dirty!

Methods included from AbstractModel::InterfaceAttributes

#load_interface_attribute, #load_interface_attributes, #save_interface_attribute, #save_interface_attributes, #spec_to_proc

Methods included from AbstractModel::Attributable

#attributes, #has_attribute?, included, #lazy_attribute?, #loaded_attribute?, #populate_attributes, #read_attribute, #readonly_attribute?, #write_attribute

Methods included from Logger

included, #logger, #logger_output=

Constructor Details

#initialize(inet) ⇒ HostNetworkInterface

Returns a new instance of HostNetworkInterface



56
57
58
# File 'lib/virtualbox/host_network_interface.rb', line 56

def initialize(inet)
  initialize_attributes(inet)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class VirtualBox::AbstractModel::Dirty

Class Method Details

.create(proxy, interface) ⇒ Object

Creates a host only network interface. This method should not be called directly. Instead, the `create` method on the `Global#host` relationship should be called instead. Example:

VirtualBox::Global.global.host.network_interfaces.create

The above will create a host only network interface, add it to the collection, and will return the instance of the new interface.



48
49
50
51
52
53
# File 'lib/virtualbox/host_network_interface.rb', line 48

def create(proxy, interface)
  inet, progress = interface.create_host_only_network_interface
  progress.wait

  new(inet)
end

.populate_relationship(caller, ihost) ⇒ Array<HostNetworkInterface>

Populates a relationship with another model.

**This method typically won't be used except internally.**

Returns:



29
30
31
32
33
34
35
36
37
# File 'lib/virtualbox/host_network_interface.rb', line 29

def populate_relationship(caller, ihost)
  relation = Proxies::Collection.new(caller, self, ihost)

  ihost.network_interfaces.each do |inet|
    relation << new(inet)
  end

  relation
end

Instance Method Details

#added_to_relationship(proxy) ⇒ Object



67
68
69
70
# File 'lib/virtualbox/host_network_interface.rb', line 67

def added_to_relationship(proxy)
  write_attribute(:parent, proxy.parent)
  write_attribute(:parent_collection, proxy)
end

#attached_vmsObject

Gets the VMs which have an adapter which is attached to this network interface.



91
92
93
94
95
96
97
98
99
# File 'lib/virtualbox/host_network_interface.rb', line 91

def attached_vms
  parent.parent.vms.find_all do |vm|
    result = vm.network_adapters.find do |adapter|
      adapter.enabled? && adapter.host_interface == name
    end

    !result.nil?
  end
end

#destroyObject

Destroy the host only network interface. Warning: If any VMs are currently attached to this network interface, their networks will fail to work after removing this. Therefore, one should be careful to make sure to remove all VMs from this network prior to destroying it.



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/virtualbox/host_network_interface.rb', line 125

def destroy
  return false if interface_type == :bridged

  parent.interface.remove_host_only_network_interface(uuid).wait
  dhcp_server.destroy if dhcp_server(false)

  # Remove from collection
  parent_collection.delete(self, true) if parent_collection

  true
end

#dhcp_server(create_if_not_found = true) ⇒ Object

Gets the DHCP server associated with the network interface. Only host only network interfaces have dhcp servers. If a DHCP server doesn't exist for this network interface, one will be created.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/virtualbox/host_network_interface.rb', line 75

def dhcp_server(create_if_not_found=true)
  return nil if interface_type != :host_only

  # Try to find the dhcp server in the list of DHCP servers.
  dhcp_name = "HostInterfaceNetworking-#{name}"
  result = parent.parent.dhcp_servers.find do |dhcp|
    dhcp.network_name == dhcp_name
  end

  # If no DHCP server is found, create one
  result = parent.parent.dhcp_servers.create(dhcp_name) if result.nil? && create_if_not_found
  result
end

#enable_static(ip, netmask = nil) ⇒ Object

Sets up the static IPV4 configuration for the host only network interface. This allows the caller to set the IPV4 address of the interface as well as the netmask.



104
105
106
107
108
109
# File 'lib/virtualbox/host_network_interface.rb', line 104

def enable_static(ip, netmask=nil)
  netmask ||= network_mask

  interface.enable_static_ip_config(ip, netmask)
  reload
end

#initialize_attributes(inet) ⇒ Object



60
61
62
63
64
65
# File 'lib/virtualbox/host_network_interface.rb', line 60

def initialize_attributes(inet)
  write_attribute(:interface, inet)

  load_interface_attributes(inet)
  existing_record!
end

#reloadObject

Reloads the information regarding this host only network interface.



113
114
115
116
117
118
# File 'lib/virtualbox/host_network_interface.rb', line 113

def reload
  # Find the interface again and reload the data
  inet = parent.interface.find_host_network_interface_by_id(uuid)
  initialize_attributes(inet)
  self
end