2010-09-08 17:40:02 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class Compute < Fog::Service
|
|
|
|
|
2010-12-16 18:31:24 -05:00
|
|
|
requires :aws_access_key_id, :aws_secret_access_key
|
|
|
|
recognizes :endpoint, :region, :host, :path, :port, :scheme, :persistent
|
2011-01-07 21:14:39 -05:00
|
|
|
recognizes :provider # remove post deprecation
|
2010-09-08 17:40:02 -04:00
|
|
|
|
2011-01-07 19:52:09 -05:00
|
|
|
model_path 'fog/compute/models/aws'
|
2010-09-08 17:40:02 -04:00
|
|
|
model :address
|
|
|
|
collection :addresses
|
|
|
|
model :flavor
|
|
|
|
collection :flavors
|
|
|
|
model :image
|
|
|
|
collection :images
|
|
|
|
model :key_pair
|
|
|
|
collection :key_pairs
|
|
|
|
model :security_group
|
|
|
|
collection :security_groups
|
|
|
|
model :server
|
|
|
|
collection :servers
|
|
|
|
model :snapshot
|
|
|
|
collection :snapshots
|
2010-10-09 21:39:48 -04:00
|
|
|
model :tag
|
|
|
|
collection :tags
|
2010-09-08 17:40:02 -04:00
|
|
|
model :volume
|
|
|
|
collection :volumes
|
|
|
|
|
2011-01-07 19:52:09 -05:00
|
|
|
request_path 'fog/compute/requests/aws'
|
2010-09-08 17:40:02 -04:00
|
|
|
request :allocate_address
|
|
|
|
request :associate_address
|
|
|
|
request :attach_volume
|
|
|
|
request :authorize_security_group_ingress
|
2010-10-12 14:00:59 -04:00
|
|
|
request :create_image
|
2010-09-08 17:40:02 -04:00
|
|
|
request :create_key_pair
|
|
|
|
request :create_security_group
|
|
|
|
request :create_snapshot
|
2010-10-12 14:00:59 -04:00
|
|
|
request :create_tags
|
2010-09-08 17:40:02 -04:00
|
|
|
request :create_volume
|
|
|
|
request :delete_key_pair
|
|
|
|
request :delete_security_group
|
|
|
|
request :delete_snapshot
|
2010-10-12 14:00:59 -04:00
|
|
|
request :delete_tags
|
2010-09-08 17:40:02 -04:00
|
|
|
request :delete_volume
|
|
|
|
request :deregister_image
|
|
|
|
request :describe_addresses
|
|
|
|
request :describe_availability_zones
|
|
|
|
request :describe_images
|
|
|
|
request :describe_instances
|
|
|
|
request :describe_reserved_instances
|
|
|
|
request :describe_key_pairs
|
|
|
|
request :describe_regions
|
2011-01-26 16:35:22 -05:00
|
|
|
request :describe_reserved_instances_offerings
|
2010-09-08 17:40:02 -04:00
|
|
|
request :describe_security_groups
|
|
|
|
request :describe_snapshots
|
2010-10-09 21:39:48 -04:00
|
|
|
request :describe_tags
|
2010-09-08 17:40:02 -04:00
|
|
|
request :describe_volumes
|
|
|
|
request :detach_volume
|
|
|
|
request :disassociate_address
|
|
|
|
request :get_console_output
|
2011-05-09 18:28:54 -04:00
|
|
|
request :get_password_data
|
2010-09-22 15:06:25 -04:00
|
|
|
request :import_key_pair
|
2010-09-08 17:40:02 -04:00
|
|
|
request :modify_image_attributes
|
|
|
|
request :modify_snapshot_attribute
|
|
|
|
request :reboot_instances
|
|
|
|
request :release_address
|
|
|
|
request :register_image
|
|
|
|
request :revoke_security_group_ingress
|
|
|
|
request :run_instances
|
|
|
|
request :terminate_instances
|
|
|
|
request :start_instances
|
|
|
|
request :stop_instances
|
2011-03-11 16:06:44 -05:00
|
|
|
request :monitor_instances
|
|
|
|
request :unmonitor_instances
|
2010-09-08 17:40:02 -04:00
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
def self.data
|
2010-09-24 16:50:44 -04:00
|
|
|
@data ||= Hash.new do |hash, region|
|
2010-09-08 17:40:02 -04:00
|
|
|
owner_id = Fog::AWS::Mock.owner_id
|
2010-10-13 16:20:18 -04:00
|
|
|
hash[region] = Hash.new do |region_hash, key|
|
|
|
|
region_hash[key] = {
|
2010-09-24 16:50:44 -04:00
|
|
|
:deleted_at => {},
|
|
|
|
:addresses => {},
|
2010-10-14 20:06:50 -04:00
|
|
|
:images => {},
|
2010-09-24 16:50:44 -04:00
|
|
|
:instances => {},
|
|
|
|
:key_pairs => {},
|
|
|
|
:limits => { :addresses => 5 },
|
|
|
|
:owner_id => owner_id,
|
|
|
|
:security_groups => {
|
|
|
|
'default' => {
|
|
|
|
'groupDescription' => 'default group',
|
|
|
|
'groupName' => 'default',
|
|
|
|
'ipPermissions' => [
|
|
|
|
{
|
|
|
|
'groups' => [{'groupName' => 'default', 'userId' => owner_id}],
|
|
|
|
'fromPort' => -1,
|
|
|
|
'toPort' => -1,
|
|
|
|
'ipProtocol' => 'icmp',
|
|
|
|
'ipRanges' => []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'groups' => [{'groupName' => 'default', 'userId' => owner_id}],
|
|
|
|
'fromPort' => 0,
|
|
|
|
'toPort' => 65535,
|
|
|
|
'ipProtocol' => 'tcp',
|
|
|
|
'ipRanges' => []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'groups' => [{'groupName' => 'default', 'userId' => owner_id}],
|
|
|
|
'fromPort' => 0,
|
|
|
|
'toPort' => 65535,
|
|
|
|
'ipProtocol' => 'udp',
|
|
|
|
'ipRanges' => []
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'ownerId' => owner_id
|
|
|
|
}
|
|
|
|
},
|
|
|
|
:snapshots => {},
|
2010-10-14 17:55:08 -04:00
|
|
|
:volumes => {},
|
2010-10-14 20:06:50 -04:00
|
|
|
:tags => {}
|
2010-09-24 16:50:44 -04:00
|
|
|
}
|
|
|
|
end
|
2010-09-08 17:40:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-17 14:02:28 -04:00
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
2010-09-08 17:40:02 -04:00
|
|
|
def initialize(options={})
|
2011-01-07 21:14:39 -05:00
|
|
|
unless options.delete(:provider)
|
|
|
|
location = caller.first
|
|
|
|
warning = "[yellow][WARN] Fog::AWS::Compute.new is deprecated, use Fog::Compute.new(:provider => 'AWS') instead[/]"
|
|
|
|
warning << " [light_black](" << location << ")[/] "
|
|
|
|
Formatador.display_line(warning)
|
|
|
|
end
|
|
|
|
|
2011-02-16 20:25:50 -05:00
|
|
|
require 'fog/compute/parsers/aws/basic'
|
|
|
|
|
2010-09-08 17:40:02 -04:00
|
|
|
@aws_access_key_id = options[:aws_access_key_id]
|
2011-05-17 13:32:44 -04:00
|
|
|
|
2010-09-24 16:50:44 -04:00
|
|
|
@region = options[:region] || 'us-east-1'
|
2011-03-11 16:06:44 -05:00
|
|
|
|
2011-05-18 14:38:18 -04:00
|
|
|
unless ['ap-northeast-1', 'ap-southeast-1', 'eu-west-1', 'us-east-1', 'us-west-1'].include?(@region)
|
|
|
|
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
|
|
|
end
|
|
|
|
|
2011-03-21 13:54:07 -04:00
|
|
|
@data = self.class.data[@region][@aws_access_key_id]
|
2011-03-11 16:06:44 -05:00
|
|
|
@owner_id = @data[:owner_id]
|
2011-03-10 14:16:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data[@region].delete(@aws_access_key_id)
|
|
|
|
@data = self.class.data[@region][@aws_access_key_id]
|
2010-09-08 17:40:02 -04:00
|
|
|
end
|
|
|
|
|
2011-03-23 13:33:27 -04:00
|
|
|
def apply_tag_filters(resources, filters)
|
2011-03-28 17:00:25 -04:00
|
|
|
# tag-key: match resources tagged with this key (any value)
|
|
|
|
if filters.has_key?('tag-key')
|
|
|
|
value = filters.delete('tag-key')
|
|
|
|
resources = resources.select{|r| r['tagSet'].has_key?(value)}
|
|
|
|
end
|
|
|
|
|
|
|
|
# tag-value: match resources tagged with this value (any key)
|
|
|
|
if filters.has_key?('tag-value')
|
|
|
|
value = filters.delete('tag-value')
|
|
|
|
resources = resources.select{|r| r['tagSet'].values.include?(value)}
|
|
|
|
end
|
|
|
|
|
|
|
|
# tag:key: match resources taged with a key-value pair. Value may be an array, which is OR'd.
|
2011-03-23 13:33:27 -04:00
|
|
|
tag_filters = {}
|
|
|
|
filters.keys.each do |key|
|
|
|
|
tag_filters[key.gsub('tag:', '')] = filters.delete(key) if /^tag:/ =~ key
|
|
|
|
end
|
|
|
|
for tag_key, tag_value in tag_filters
|
2011-03-28 17:00:25 -04:00
|
|
|
resources = resources.select{|r| tag_value.include?(r['tagSet'][tag_key])}
|
2011-03-23 13:33:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
resources
|
|
|
|
end
|
2010-09-08 17:40:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
# Initialize connection to EC2
|
|
|
|
#
|
|
|
|
# ==== Notes
|
|
|
|
# options parameter must include values for :aws_access_key_id and
|
|
|
|
# :aws_secret_access_key in order to create a connection
|
|
|
|
#
|
|
|
|
# ==== Examples
|
|
|
|
# sdb = SimpleDB.new(
|
|
|
|
# :aws_access_key_id => your_aws_access_key_id,
|
|
|
|
# :aws_secret_access_key => your_aws_secret_access_key
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * options<~Hash> - config arguments for connection. Defaults to {}.
|
2011-04-26 20:47:44 -04:00
|
|
|
# * region<~String> - optional region to use, in
|
|
|
|
# ['eu-west-1', 'us-east-1', 'us-west-1', 'ap-northeast-1', 'ap-southeast-1']
|
2010-09-08 17:40:02 -04:00
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# * EC2 object with connection to aws.
|
|
|
|
def initialize(options={})
|
2011-01-07 21:14:39 -05:00
|
|
|
unless options.delete(:provider)
|
|
|
|
location = caller.first
|
|
|
|
warning = "[yellow][WARN] Fog::AWS::Compute.new is deprecated, use Fog::Compute.new(:provider => 'AWS') instead[/]"
|
|
|
|
warning << " [light_black](" << location << ")[/] "
|
|
|
|
Formatador.display_line(warning)
|
|
|
|
end
|
|
|
|
|
2011-02-16 20:25:50 -05:00
|
|
|
require 'fog/core/parser'
|
|
|
|
|
2010-09-08 17:40:02 -04:00
|
|
|
@aws_access_key_id = options[:aws_access_key_id]
|
|
|
|
@aws_secret_access_key = options[:aws_secret_access_key]
|
|
|
|
@hmac = Fog::HMAC.new('sha256', @aws_secret_access_key)
|
2011-04-14 19:31:58 -04:00
|
|
|
@region = options[:region] ||= 'us-east-1'
|
|
|
|
|
2010-09-29 20:53:32 -04:00
|
|
|
if @endpoint = options[:endpoint]
|
|
|
|
endpoint = URI.parse(@endpoint)
|
|
|
|
@host = endpoint.host
|
|
|
|
@path = endpoint.path
|
|
|
|
@port = endpoint.port
|
|
|
|
@scheme = endpoint.scheme
|
|
|
|
else
|
|
|
|
@host = options[:host] || case options[:region]
|
2011-03-02 13:57:52 -05:00
|
|
|
when 'ap-northeast-1'
|
|
|
|
'ec2.ap-northeast-1.amazonaws.com'
|
2010-09-08 17:40:02 -04:00
|
|
|
when 'ap-southeast-1'
|
|
|
|
'ec2.ap-southeast-1.amazonaws.com'
|
|
|
|
when 'eu-west-1'
|
|
|
|
'ec2.eu-west-1.amazonaws.com'
|
|
|
|
when 'us-east-1'
|
|
|
|
'ec2.us-east-1.amazonaws.com'
|
|
|
|
when 'us-west-1'
|
|
|
|
'ec2.us-west-1.amazonaws.com'
|
|
|
|
else
|
|
|
|
raise ArgumentError, "Unknown region: #{options[:region].inspect}"
|
|
|
|
end
|
2010-09-29 20:53:32 -04:00
|
|
|
@path = options[:path] || '/'
|
|
|
|
@port = options[:port] || 443
|
|
|
|
@scheme = options[:scheme] || 'https'
|
|
|
|
end
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", options[:persistent])
|
2010-09-08 17:40:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2010-11-09 20:57:34 -05:00
|
|
|
|
2010-09-08 17:40:02 -04:00
|
|
|
def request(params)
|
|
|
|
idempotent = params.delete(:idempotent)
|
|
|
|
parser = params.delete(:parser)
|
|
|
|
|
|
|
|
body = AWS.signed_params(
|
|
|
|
params,
|
|
|
|
{
|
|
|
|
:aws_access_key_id => @aws_access_key_id,
|
|
|
|
:hmac => @hmac,
|
|
|
|
:host => @host,
|
2010-09-29 20:53:32 -04:00
|
|
|
:path => @path,
|
2010-12-23 16:54:02 -05:00
|
|
|
:port => @port,
|
2010-09-22 15:06:25 -04:00
|
|
|
:version => '2010-08-31'
|
2010-09-08 17:40:02 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
begin
|
|
|
|
response = @connection.request({
|
|
|
|
:body => body,
|
|
|
|
:expects => 200,
|
|
|
|
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
|
|
|
|
:idempotent => idempotent,
|
|
|
|
:host => @host,
|
|
|
|
:method => 'POST',
|
|
|
|
:parser => parser
|
|
|
|
})
|
2010-11-29 23:07:47 -05:00
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
2010-09-08 17:40:02 -04:00
|
|
|
if match = error.message.match(/<Code>(.*)<\/Code><Message>(.*)<\/Message>/)
|
|
|
|
raise case match[1].split('.').last
|
|
|
|
when 'NotFound'
|
2010-09-09 20:50:38 -04:00
|
|
|
Fog::AWS::Compute::NotFound.slurp(error, match[2])
|
2010-09-08 17:40:02 -04:00
|
|
|
else
|
2010-09-09 20:50:38 -04:00
|
|
|
Fog::AWS::Compute::Error.slurp(error, "#{match[1]} => #{match[2]}")
|
2010-09-08 17:40:02 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
raise error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|