Class: Deltacloud::DSL::Instance
- Inherits:
-
Object
- Object
- Deltacloud::DSL::Instance
- Includes:
- EM::Deferrable
- Defined in:
- lib/instance.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Instance Method Summary collapse
-
#create(definition) ⇒ Object
Make call to Deltacloud to create this instance object Please note that this call will put the instance into PENDING state in most cases (except Mock).
-
#from_instance!(instance) ⇒ Object
Convert attributes from DeltaCloud::Instance object to Instance.
-
#initialize(name, &block) ⇒ Instance
constructor
A new instance of Instance.
-
#instance_action(action) ⇒ Object
Helper for instance actions.
-
#method_missing(name, *args) ⇒ Object
Define various helper methods:.
-
#params ⇒ Object
Convert instance param hash to OpenStruct object for easy retrieval.
-
#profile(name = nil, &block) ⇒ Object
Assing InstanceProfile to the instance.
-
#update! ⇒ Object
This method will try to connect to Deltacloud API and refresh all attributes.
Constructor Details
#initialize(name, &block) ⇒ Instance
Returns a new instance of Instance.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/instance.rb', line 11 def initialize(name, &block) @name = name @parameters = { :state => :new } instance_eval(&block) if block_given? warn "WARNING: Realm is not defined for instance '#{name}'. (Default driver realm will be used)." if params.realm_id.nil? raise "Image is not defined for instance '#{@name}'" if params.image_id.nil? raise "Profile is not defined for instance '#{@name}'" if params.profile.nil? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Define various helper methods:
instance_ -> retrieve instance attribute (instance_state,
instance_public_addresses, etc...)
is_? -> true if instance is in given state is_not_? -> same as above just negated
Whatever else method is called on this object with some parameters, this method will automatically be transformed to instance variable.
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/instance.rb', line 153 def method_missing(name, *args) if name.to_s =~ /^instance_([\w_]+)$/ @parameters[$1.intern] elsif name.to_s =~ /^is_(\w+)\?$/ state_is?($1.intern) elsif name.to_s =~ /^is_not_(\w+)\?$/ !state_is?($1.intern) elsif !args.empty? set_param(name.to_sym, args) else super end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/instance.rb', line 8 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
9 10 11 |
# File 'lib/instance.rb', line 9 def parameters @parameters end |
Instance Method Details
#create(definition) ⇒ Object
Make call to Deltacloud to create this instance object Please note that this call will put the instance into PENDING state in most cases (except Mock).
Use .wait_for_running! to pool Deltacloud until instance is not RUNNING
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/instance.rb', line 29 def create(definition) @client = Deltacloud::Client(@definition = definition) instance_opts = { :name => self.name } instance_opts.merge!(profile.to_instance_params) instance_opts.merge!({ :realm_id => instance_realm_id, }) from_instance!(@client.create_instance(instance_image_id, instance_opts)) self end |
#from_instance!(instance) ⇒ Object
Convert attributes from DeltaCloud::Instance object to Instance
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/instance.rb', line 106 def from_instance!(instance) attrs = { :instance_id => instance.id, :state => instance.state.downcase.intern, :public_addresses => instance.public_addresses, :private_addresses => instance.private_addresses } attrs.merge!(:owner_id => instance.owner_id) @parameters.merge!(attrs) self end |
#instance_action(action) ⇒ Object
Helper for instance actions
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/instance.rb', line 91 def instance_action(action) must_have_definition! @client.instance(params.instance_id).send(:"#{action}!") instance = @client.instance(params.instance_id) if instance.nil? @parameters[:state] = :destroyed @parameters.delete(:instance_id) self else from_instance!(instance) end end |
#params ⇒ Object
Convert instance param hash to OpenStruct object for easy retrieval
139 140 141 |
# File 'lib/instance.rb', line 139 def params Deltacloud::DSL::Configuration(@parameters) end |
#profile(name = nil, &block) ⇒ Object
Assing InstanceProfile to the instance. If no profile name is provided, will return the active profile
InstanceProfile DSL example:
instance ‘test-instance’ do
profile 'm1-small' do |p|
p.memory = 500
end
end
129 130 131 132 133 134 135 |
# File 'lib/instance.rb', line 129 def profile(name=nil, &block) if name @parameters[:profile] = Deltacloud::DSL::InstanceProfile(name, &block) else instance_profile end end |
#update! ⇒ Object
This method will try to connect to Deltacloud API and refresh all attributes
72 73 74 75 |
# File 'lib/instance.rb', line 72 def update! raise 'Instance must be created first in order to call update' if @definition.nil? from_instance!(@client.instance(params.instance_id)) end |