Class: VirtualBox::StorageController

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

Overview

Represents a single storage controller which can be attached to a virtual machine.

**Currently, storage controllers can not be created from scratch. Therefore, the only way to use this model is through a relationship of a VM object.**

# Attributes and Relationships

Properties of the storage controller 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.

Relationships are also accessed like attributes but can’t be set. Instead, they are typically references to other objects such as an AttachedDevice which in turn have their own attributes which can be modified.

## Attributes

This is copied directly from the class header, but lists all available attributes. If you don’t understand what this means, read AbstractModel::Attributable.

attribute :parent, :readonly => true
attribute :name
attribute :type
attribute :max_ports, :populate_key => :maxportcount
attribute :ports, :populate_key => :portcount

## Relationships

In addition to the basic attributes, a virtual machine is related to other things. The relationships are listed below. If you don’t understand this, read AbstractModel::Relatable.

relationship :devices, AttachedDevice, :dependent => :destroy

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_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, icontroller) ⇒ StorageController

Since storage controllers still can’t be created from scratch, this method shouldn’t be called. Instead, storage controllers can be retrieved through relationships of other models such as VM.



113
114
115
116
117
118
119
120
121
122
# File 'lib/virtualbox/storage_controller.rb', line 113

def initialize(caller, icontroller)
  super()

  populate_attributes({
    :parent => caller,
    :interface => icontroller
  }, :ignore_relationships => true)
  load_interface_attributes(icontroller)
  clear_dirty!
end

Dynamic Method Handling

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

Class Method Details

.destroy_relationship(caller, data, *args) ⇒ Object

Destroys a relationship with another model.

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



94
95
96
# File 'lib/virtualbox/storage_controller.rb', line 94

def destroy_relationship(caller, data, *args)
  data.each { |v| v.destroy(*args) }
end

.populate_array_relationship(caller, imachine) ⇒ Array<StorageController>

Populates a has many relationship for a VM.

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

Returns:



69
70
71
72
73
74
75
76
77
# File 'lib/virtualbox/storage_controller.rb', line 69

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

  imachine.storage_controllers.each do |icontroller|
    relation << new(caller, icontroller)
  end

  relation
end

.populate_attachment_relationship(caller, attachment) ⇒ Array<StorageController>

Populates a single relationship for a MediumAttachment.

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

Returns:



84
85
86
87
88
89
# File 'lib/virtualbox/storage_controller.rb', line 84

def populate_attachment_relationship(caller, attachment)
  # Find the storage controller with the matching name
  attachment.parent.storage_controllers.find do |sc|
    sc.name == attachment.controller_name
  end
end

.populate_relationship(caller, data) ⇒ Array<StorageController>

Populates a relationship with another model.

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

Returns:



56
57
58
59
60
61
62
# File 'lib/virtualbox/storage_controller.rb', line 56

def populate_relationship(caller, data)
  if data.is_a?(COM::Util.versioned_interface(:Machine))
    populate_array_relationship(caller, data)
  elsif data.is_a?(MediumAttachment)
    populate_attachment_relationship(caller, data)
  end
end

.save_relationship(caller, controllers) ⇒ Object

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

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



102
103
104
105
106
# File 'lib/virtualbox/storage_controller.rb', line 102

def save_relationship(caller, controllers)
  controllers.each do |sc|
    sc.save
  end
end

Instance Method Details

#destroy(*args) ⇒ Object

Destroys the storage controller. This first detaches all attachments on this storage controller. Note that this does not delete the media on the attachments, unless specified by the options.



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/virtualbox/storage_controller.rb', line 147

def destroy(*args)
  # First remove all attachments
  medium_attachments.each do |ma|
    ma.destroy(*args)
  end

  # Finally, remove ourselves
  parent.with_open_session do |session|
    machine = session.machine
    machine.remove_storage_controller(name)
  end
end

#medium_attachmentsObject

Retrieves the array of medium attachments related to this storage controller. This is not implemented as a relationship simply because it would have been difficult to do so (circular) and its not really necessary.



127
128
129
130
131
# File 'lib/virtualbox/storage_controller.rb', line 127

def medium_attachments
  parent.medium_attachments.find_all do |ma|
    ma.storage_controller == self
  end
end

#saveObject

Saves the storage controller. This method shouldn’t be called directly. Instead, VM#save should be called, which will save all attached storage controllers as well. This will setup the proper parameter for ‘interface` here.



137
138
139
140
141
142
# File 'lib/virtualbox/storage_controller.rb', line 137

def save
  parent.with_open_session do |session|
    machine = session.machine
    save_changed_interface_attributes(machine.get_storage_controller_by_name(name))
  end
end