Class: DeltaCloud::StatefulObject
Instance Attribute Summary collapse
Attributes inherited from BaseObject
#base_name, #client, #id, #objects, #url
Instance Method Summary
collapse
#action_urls, #actions, #add_action_link!, #base_method_handler, #method_missing, #original_method_missing
Methods inherited from BaseObject
#add_addresses!, #add_authentication!, #add_blob!, #add_collection!, #add_hwp_property!, #add_link!, #add_provider!, #add_text!, #method_missing
Constructor Details
#initialize(opts = {}, &block) ⇒ StatefulObject
Returns a new instance of StatefulObject.
268
269
270
271
272
|
# File 'lib/base_object.rb', line 268
def initialize(opts={}, &block)
super(opts)
@state = opts[:initial_state] || ''
add_default_states!
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class DeltaCloud::ActionObject
Instance Attribute Details
#state ⇒ Object
Returns the value of attribute state.
266
267
268
|
# File 'lib/base_object.rb', line 266
def state
@state
end
|
Instance Method Details
#action_method_handler ⇒ Object
313
|
# File 'lib/base_object.rb', line 313
alias :action_method_handler :method_handler
|
#action_objects ⇒ Object
349
350
351
|
# File 'lib/base_object.rb', line 349
def action_objects
@objects.select { |o| o[:type] == :action_link }
end
|
#action_trigger(action) ⇒ Object
297
298
299
300
301
302
303
|
# File 'lib/base_object.rb', line 297
def action_trigger(action)
return if action.to_s == "destroy"
@new_state_object = @client.send(self.base_name, self.id)
@state = @new_state_object.state
self.update_actions!
end
|
#add_default_states! ⇒ Object
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# File 'lib/base_object.rb', line 274
def add_default_states!
@objects << {
:method_name => 'stopped?',
:type => :state,
:state => 'STOPPED'
}
@objects << {
:method_name => 'running?',
:type => :state,
:state => 'RUNNING'
}
@objects << {
:method_name => 'pending?',
:type => :state,
:state => 'PENDING'
}
@objects << {
:method_name => 'shutting_down?',
:type => :state,
:state => 'SHUTTING_DOWN'
}
end
|
#add_run_action!(id, link) ⇒ Object
305
306
307
308
309
310
311
|
# File 'lib/base_object.rb', line 305
def add_run_action!(id, link)
@objects << {
:method_name => 'run',
:type => :run,
:url => link,
}
end
|
#evaluate_state(method_state, current_state) ⇒ Object
345
346
347
|
# File 'lib/base_object.rb', line 345
def evaluate_state(method_state, current_state)
method_state.eql?(current_state)
end
|
#method_handler(m, args = []) ⇒ Object
315
316
317
318
319
320
321
322
323
324
325
|
# File 'lib/base_object.rb', line 315
def method_handler(m, args=[])
begin
action_method_handler(m, args)
rescue NoHandlerForMethod
case m[:type]
when :state then evaluate_state(m[:state], @state)
when :run then run_command(m[:url][:href], args)
else raise NoHandlerForMethod
end
end
end
|
#run_command(instance_url, args) ⇒ Object
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
# File 'lib/base_object.rb', line 329
def run_command(instance_url, args)
credentials = args[1]
params = {
:cmd => args[0],
:private_key => credentials[:pem] ? File.read(credentials[:pem]) : nil,
}
params.merge!({
:username => credentials[:username],
:password => credentials[:password]
}) if credentials[:username] and credentials[:password]
@client.request(:post, instance_url, {}, params) do |response|
output = Nokogiri::XML(response)
(output/'/instance/output').first.text
end
end
|
#update_actions! ⇒ Object
353
354
355
356
357
|
# File 'lib/base_object.rb', line 353
def update_actions!
new_actions = @new_state_object.action_objects
@objects.reject! { |o| o[:type] == :action_link }
@objects = (@objects + new_actions)
end
|