Class: VirtualBox::NetworkAdapter

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

Overview

Represents a single NIC (Network Interface Card) of a virtual machine.

# Create a Network Adapter

There is no need to have the ability to create a network adapter, since when creating a VM from scratch, all eight network adapter slots are created, but set to ‘attachment_type` `nil`. Simply modify the adapter you’re interested in and save.

# Editing a Network Adapter

Network adapters can be modified directly in their relationship to other virtual machines. When VM#save is called, it will also save any changes to its relationships. Additionally, you may call #save on the relationship itself.

vm = VirtualBox::VM.find("foo")
vm.network_adapters[0].macaddress = @new_mac_address
vm.save

# Destroying a Network Adapter

Network adapters can not actually be “destroyed” but can be removed by setting the ‘attachment_type` to `nil` and saving.

# Attributes

Properties of the model are exposed using standard ruby instance methods which are generated on the fly. Because of this, they are not listed below as available instance methods.

These attributes can be accessed and modified via standard ruby-style ‘instance.attribute` and `instance.attribute=` methods. The attributes are listed below. If you aren’t sure what this means or you can’t understand why the below is listed, please read AbstractModel::Attributable.

attribute :slot, :readonly => true
attribute :enabled, :boolean => true
attribute :attachment_type
attribute :adapter_type
attribute :mac_address
attribute :cable_connected, :boolean => true
attribute :nat_network
attribute :internal_network
attribute :host_interface
attribute :interface, :readonly => true, :property => false

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractModel

#destroy, #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_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(caller, inetwork) ⇒ NetworkAdapter

Returns a new instance of NetworkAdapter.



94
95
96
97
98
# File 'lib/virtualbox/network_adapter.rb', line 94

def initialize(caller, inetwork)
  super()

  initialize_attributes(caller, inetwork)
end

Dynamic Method Handling

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

Class Method Details

.populate_relationship(caller, imachine) ⇒ Array<Nic>

Populates the nic relationship for anything which is related to it.

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

Returns:

  • (Array<Nic>)


70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/virtualbox/network_adapter.rb', line 70

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

  # Get the count of network adapters
  count = imachine.parent.system_properties.network_adapter_count

  count.times do |i|
    relation << new(caller, imachine.get_network_adapter(i))
  end

  relation
end

.save_relationship(caller, items) ⇒ Object

Saves the relationship. This simply calls #save on every member of the relationship.

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



87
88
89
90
91
# File 'lib/virtualbox/network_adapter.rb', line 87

def save_relationship(caller, items)
  items.each do |item|
    item.save
  end
end

Instance Method Details

#host_interface_objectObject

Gets the host interface object associated with the class if it exists.



126
127
128
129
130
# File 'lib/virtualbox/network_adapter.rb', line 126

def host_interface_object
  VirtualBox::Global.global.host.network_interfaces.find do |ni|
    ni.name == host_interface
  end
end

#initialize_attributes(parent, inetwork) ⇒ Object

Initializes the attributes of an existing shared folder.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/virtualbox/network_adapter.rb', line 101

def initialize_attributes(parent, inetwork)
  # Set the parent and interface
  write_attribute(:parent, parent)
  write_attribute(:interface, inetwork)

  # Load the interface attributes
  load_interface_attributes(inetwork)

  # Clear dirtiness, since this should only be called initially and
  # therefore shouldn't affect dirtiness
  clear_dirty!

  # But this is an existing record
  existing_record!
end

#load_relationship(name) ⇒ Object



117
118
119
120
121
122
# File 'lib/virtualbox/network_adapter.rb', line 117

def load_relationship(name)
  # Lazy load the NAT driver. This is only supported by VirtualBox
  # 3.2 and higher. This restriction is checked when the
  # relationship attribute is accessed.
  populate_relationship(:nat_driver, interface.nat_driver)
end

#modify_adapterObject

Opens a session, yields the adapter and then saves the machine at the end



159
160
161
162
163
164
# File 'lib/virtualbox/network_adapter.rb', line 159

def modify_adapter
  parent_machine.with_open_session do |session|
    machine = session.machine
    yield machine.get_network_adapter(slot)
  end
end

#saveObject

Save a network adapter.



133
134
135
136
137
138
139
# File 'lib/virtualbox/network_adapter.rb', line 133

def save
  modify_adapter do |adapter|
    save_attachment_type(adapter)
    save_changed_interface_attributes(adapter)
    save_relationships
  end
end

#save_attachment_type(adapter) ⇒ Object

Saves the attachment type. This should never be called directly. Instead, #save should be called, which will call this method if appropriate.



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/virtualbox/network_adapter.rb', line 143

def save_attachment_type(adapter)
  return unless attachment_type_changed?

  mapping = {
    :nat => :attach_to_nat,
    :bridged => :attach_to_bridged_interface,
    :internal => :attach_to_internal_network,
    :host_only => :attach_to_host_only_interface
  }

  adapter.send(mapping[attachment_type])
  clear_dirty!(:attachment_type)
end