Class: Deltacloud::EC2::ActionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2/query_parser.rb

Constant Summary collapse

MAPPINGS =
{
  :describe_availability_zones => { :method => :realms, :params => { 'ZoneName.1' => :id } },
  :describe_images => { :method => :images, :params => { 'ImageId.1' => :id }},
  :describe_instances => { :method => :instances, :params => {'InstanceId.1' => :id } },
  :describe_key_pairs => { :method => :keys, :params => {} },
  :create_key_pair => { :method => :create_key, :params => { 'KeyName' => :key_name }},
  :delete_key_pair => { :method => :destroy_key, :params => { 'KeyName' => :id }},
  :run_instances => { :method => :create_instance, :params => { 'ImageId' => :image_id, 'InstanceType' => :hwp_id, 'Placement.AvailabilityZone' => :realm_id, 'UserData' => :user_data }},
  :stop_instances => { :method => :stop_instance, :params => { 'InstanceId.1' => :id }},
  :start_instances => { :method => :start_instance, :params => { 'InstanceId.1' => :id }},
  :reboot_instances => { :method => :reboot_instance, :params => { 'InstanceId.1' => :id }},
  :terminate_instances => { :method => :destroy_instance, :params => { 'InstanceId.1' => :id }},
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action) ⇒ ActionHandler

Returns a new instance of ActionHandler.



40
41
42
# File 'lib/ec2/query_parser.rb', line 40

def initialize(action)
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



38
39
40
# File 'lib/ec2/query_parser.rb', line 38

def action
  @action
end

Class Method Details

.mappingsObject



34
35
36
# File 'lib/ec2/query_parser.rb', line 34

def self.mappings
  MAPPINGS
end

Instance Method Details

#deltacloud_methodObject



44
45
46
# File 'lib/ec2/query_parser.rb', line 44

def deltacloud_method
  self.class.mappings[action.action][:method]
end

#deltacloud_method_paramsObject



48
49
50
51
52
53
54
# File 'lib/ec2/query_parser.rb', line 48

def deltacloud_method_params
  parameters = action.parameters.dup
  self.class.mappings[action.action][:params].inject({}) do |result, p|
    result[p.last] = parameters.delete(p.first)
    result.delete_if { |k,v| v.nil? }
  end
end

#instance_action(driver, action, credentials, id) ⇒ Object

Some drivers, like RHEV-M does not return the instance object but just notify client that the action was executed successfully.

If we not received an Instance object, then we need to do additional query.



73
74
75
76
77
78
79
80
# File 'lib/ec2/query_parser.rb', line 73

def instance_action(driver, action, credentials, id)
  instance = driver.send(action, credentials, id)
  if instance.kind_of? Instance
    instance
  else
    driver.instance(credentials, :id => id)
  end
end

#perform!(credentials, driver) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/ec2/query_parser.rb', line 56

def perform!(credentials, driver)
  @result = case deltacloud_method
    when :create_instance then driver.send(deltacloud_method, credentials, deltacloud_method_params.delete(:image_id), deltacloud_method_params)
    when :stop_instance then instance_action(driver, deltacloud_method, credentials, deltacloud_method_params.delete(:id))
    when :start_instance then instance_action(driver, deltacloud_method, credentials, deltacloud_method_params.delete(:id))
    when :destroy_instance then driver.send(deltacloud_method, credentials, deltacloud_method_params.delete(:id))
    when :reboot_instance then driver.send(deltacloud_method, credentials, deltacloud_method_params.delete(:id))
    else driver.send(deltacloud_method, credentials, deltacloud_method_params)
  end
end

#to_xml(context) ⇒ Object



82
83
84
# File 'lib/ec2/query_parser.rb', line 82

def to_xml(context)
  ResultParser.parse(action, @result, context)
end