Class: Miasma::Models::Compute::Aws

Inherits:
Miasma::Models::Compute show all
Includes:
Contrib::AwsApiCore::ApiCommon, Contrib::AwsApiCore::RequestUtils
Defined in:
lib/miasma/contrib/aws/compute.rb

Overview

Compute interface for AWS

Constant Summary collapse

API_SERVICE =

Service name of the API

'ec2'
API_VERSION =

Supported version of the EC2 API

'2014-06-15'
SERVER_STATE_MAP =

Returns map state to valid internal values.

Returns:

  • (Smash)

    map state to valid internal values

Smash.new(
  'running' => :running,
  'pending' => :pending,
  'shutting-down' => :pending,
  'terminated' => :terminated,
  'stopping' => :pending,
  'stopped' => :stopped
)

Instance Method Summary collapse

Methods included from Contrib::AwsApiCore::RequestUtils

#all_result_pages

Methods included from Contrib::AwsApiCore::ApiCommon

#api_for, #connect, #connection, #endpoint, included, #make_request, #update_request, #uri_escape

Methods inherited from Miasma::Models::Compute

#server_change_state, #server_filter, #servers

Methods inherited from Types::Api

#api_for, #connect, #connection, #endpoint, #format_response, #initialize, #make_request, #provider, #request

Methods included from Utils::Memoization

#_memo, #clear_memoizations!, #memoize, #unmemoize

Methods included from Utils::Lazy

included

Constructor Details

This class inherits a constructor from Miasma::Types::Api

Instance Method Details

#server_allObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/miasma/contrib/aws/compute.rb', line 87

def server_all
  results = all_result_pages(nil, :body, 'DescribeInstancesResponse', 'reservationSet', 'item') do |options|
    request(:path => '/', :params => options.merge('Action' => 'DescribeInstances'))
  end
  results.map do |srv|
    srv = srv[:instancesSet][:item]
    Server.new(
      self,
      :id => srv[:instanceId],
      :name => srv.fetch(:tagSet, :item, []).map{|tag| tag[:value] if tag.is_a?(Hash) && tag[:key] == 'Name'}.compact.first,
      :image_id => srv[:imageId],
      :flavor_id => srv[:instanceType],
      :state => SERVER_STATE_MAP.fetch(srv.get(:instanceState, :name), :pending),
      :addresses_private => [Server::Address.new(:version => 4, :address => srv[:privateIpAddress])],
      :addresses_public => [Server::Address.new(:version => 4, :address => srv[:ipAddress])],
      :status => srv.get(:instanceState, :name),
      :key_name => srv[:keyName]
    ).valid_state
  end
end

#server_destroy(server) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/miasma/contrib/aws/compute.rb', line 52

def server_destroy(server)
  if(server.persisted?)
    result = request(
      :path => '/',
      :params => {
        'Action' => 'TerminateInstances',
        'InstanceId.1' => server.id
      }
    )
  else
    raise "this doesn't even exist"
  end
end

#server_reload(server) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/miasma/contrib/aws/compute.rb', line 29

def server_reload(server)
  result = request(
    :path => '/',
    :params => {
      'Action' => 'DescribeInstances',
      'InstanceId.1' => server.id
    }
  )
  srv = result.get(:body, 'DescribeInstancesResponse', 'reservationSet', 'item', 'instancesSet', 'item')
  server.load_data(
    :id => srv[:instanceId],
    :name => srv.fetch(:tagSet, :item, []).map{|tag| tag[:value] if tag.is_a?(Hash) && tag[:key] == 'Name'}.compact.first,
    :image_id => srv[:imageId],
    :flavor_id => srv[:instanceType],
    :state => SERVER_STATE_MAP.fetch(srv.get(:instanceState, :name), :pending),
    :addresses_private => [Server::Address.new(:version => 4, :address => srv[:privateIpAddress])],
    :addresses_public => [Server::Address.new(:version => 4, :address => srv[:ipAddress])],
    :status => srv.get(:instanceState, :name),
    :key_name => srv[:keyName]
  )
  server.valid_state
end

#server_save(server) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/miasma/contrib/aws/compute.rb', line 66

def server_save(server)
  unless(server.persisted?)
    server.load_data(server.attributes)
    result = request(
      :path => '/',
      :params => {
        'Action' => 'RunInstances',
        'ImageId' => server.image_id,
        'InstanceType' => server.flavor_id,
        'KeyName' => server.key_name,
        'MinCount' => 1,
        'MaxCount' => 1
      }
    )
    server.id = result.get(:body, 'RunInstancesResponse', 'instancesSet', 'item', 'instanceId')
  else
    raise 'WAT DO I DO!?'
  end
end