class DeltaCloud::StatefulObject

Attributes

state[R]

Public Class Methods

new(opts={}, &block) click to toggle source
# File lib/base_object.rb, line 268
def initialize(opts={}, &block)
  super(opts)
  @state = opts[:initial_state] || ''
  add_default_states!
end

Public Instance Methods

action_method_handler(m, args=[]) click to toggle source
Alias for: method_handler
action_objects() click to toggle source
# File lib/base_object.rb, line 349
def action_objects
  @objects.select { |o| o[:type] == :action_link }
end
action_trigger(action) click to toggle source
# File lib/base_object.rb, line 297
def action_trigger(action)
  # Refresh object state after action unless the object was destroyed
  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!() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/base_object.rb, line 345
def evaluate_state(method_state, current_state)
  method_state.eql?(current_state)
end
method_handler(m, args=[]) click to toggle source
# 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
Also aliased as: action_method_handler
run_command(instance_url, args) click to toggle source

private

# 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!() click to toggle source
# 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