2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/aws'
|
2011-08-31 16:52:53 -04:00
|
|
|
|
2011-06-12 16:07:42 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class AutoScaling < Fog::Service
|
2012-06-18 04:44:51 -04:00
|
|
|
extend Fog::AWS::CredentialFetcher::ServiceMethods
|
2011-06-12 16:07:42 -04:00
|
|
|
|
|
|
|
class IdentifierTaken < Fog::Errors::Error; end
|
|
|
|
class ResourceInUse < Fog::Errors::Error; end
|
|
|
|
class ValidationError < Fog::Errors::Error; end
|
|
|
|
|
|
|
|
requires :aws_access_key_id, :aws_secret_access_key
|
2012-09-11 10:01:53 -04:00
|
|
|
recognizes :host, :path, :port, :scheme, :persistent, :region, :use_iam_profile, :aws_session_token, :aws_credentials_expire_at, :instrumentor, :instrumentor_name
|
2011-06-12 16:07:42 -04:00
|
|
|
|
|
|
|
request_path 'fog/aws/requests/auto_scaling'
|
|
|
|
request :create_auto_scaling_group
|
|
|
|
request :create_launch_configuration
|
2012-09-23 15:53:36 -04:00
|
|
|
request :create_or_update_tags
|
2011-06-12 16:07:42 -04:00
|
|
|
request :delete_auto_scaling_group
|
|
|
|
request :delete_launch_configuration
|
2012-09-23 07:37:51 -04:00
|
|
|
request :delete_notification_configuration
|
2011-06-12 16:07:42 -04:00
|
|
|
request :delete_policy
|
|
|
|
request :delete_scheduled_action
|
2012-09-23 15:53:36 -04:00
|
|
|
request :delete_tags
|
2011-06-12 16:07:42 -04:00
|
|
|
request :describe_adjustment_types
|
|
|
|
request :describe_auto_scaling_groups
|
|
|
|
request :describe_auto_scaling_instances
|
2012-09-22 09:12:02 -04:00
|
|
|
request :describe_auto_scaling_notification_types
|
2011-06-12 16:07:42 -04:00
|
|
|
request :describe_launch_configurations
|
|
|
|
request :describe_metric_collection_types
|
2012-09-23 14:24:16 -04:00
|
|
|
request :describe_notification_configurations
|
2011-06-12 16:07:42 -04:00
|
|
|
request :describe_policies
|
|
|
|
request :describe_scaling_activities
|
|
|
|
request :describe_scaling_process_types
|
|
|
|
request :describe_scheduled_actions
|
2012-09-23 15:53:36 -04:00
|
|
|
request :describe_tags
|
2012-09-22 09:12:02 -04:00
|
|
|
request :describe_termination_policy_types
|
2011-06-12 16:07:42 -04:00
|
|
|
request :disable_metrics_collection
|
|
|
|
request :enable_metrics_collection
|
|
|
|
request :execute_policy
|
2012-09-23 07:37:51 -04:00
|
|
|
request :put_notification_configuration
|
2011-06-12 16:07:42 -04:00
|
|
|
request :put_scaling_policy
|
|
|
|
request :put_scheduled_update_group_action
|
|
|
|
request :resume_processes
|
|
|
|
request :set_desired_capacity
|
|
|
|
request :set_instance_health
|
|
|
|
request :suspend_processes
|
|
|
|
request :terminate_instance_in_auto_scaling_group
|
|
|
|
request :update_auto_scaling_group
|
|
|
|
|
|
|
|
model_path 'fog/aws/models/auto_scaling'
|
|
|
|
model :activity
|
|
|
|
collection :activities
|
|
|
|
model :configuration
|
|
|
|
collection :configurations
|
|
|
|
model :group
|
|
|
|
collection :groups
|
|
|
|
model :instance
|
|
|
|
collection :instances
|
2012-06-15 14:10:04 -04:00
|
|
|
model :policy
|
|
|
|
collection :policies
|
2011-06-12 16:07:42 -04:00
|
|
|
|
|
|
|
class Real
|
2012-06-18 04:44:51 -04:00
|
|
|
include Fog::AWS::CredentialFetcher::ConnectionMethods
|
2012-09-23 14:24:16 -04:00
|
|
|
|
|
|
|
attr_accessor :region
|
|
|
|
|
2011-06-12 16:07:42 -04:00
|
|
|
# Initialize connection to AutoScaling
|
|
|
|
#
|
|
|
|
# ==== Notes
|
|
|
|
# options parameter must include values for :aws_access_key_id and
|
|
|
|
# :aws_secret_access_key in order to create a connection
|
|
|
|
#
|
|
|
|
# ==== Examples
|
|
|
|
# as = AutoScaling.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 {}.
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# * AutoScaling object with connection to AWS.
|
2012-09-23 14:24:16 -04:00
|
|
|
|
2011-06-12 16:07:42 -04:00
|
|
|
def initialize(options={})
|
|
|
|
require 'fog/core/parser'
|
|
|
|
|
2012-06-18 04:44:51 -04:00
|
|
|
@use_iam_profile = options[:use_iam_profile]
|
|
|
|
setup_credentials(options)
|
2011-06-12 16:07:42 -04:00
|
|
|
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2012-09-11 10:01:53 -04:00
|
|
|
|
|
|
|
@instrumentor = options[:instrumentor]
|
|
|
|
@instrumentor_name = options[:instrumentor_name] || 'fog.aws.auto_scaling'
|
2012-09-22 09:12:02 -04:00
|
|
|
|
2011-06-12 16:07:42 -04:00
|
|
|
options[:region] ||= 'us-east-1'
|
2012-09-23 14:24:16 -04:00
|
|
|
@region = options[:region]
|
|
|
|
|
2012-02-02 15:39:53 -05:00
|
|
|
@host = options[:host] || "autoscaling.#{options[:region]}.amazonaws.com"
|
2011-09-12 11:01:48 -04:00
|
|
|
@path = options[:path] || '/'
|
|
|
|
@port = options[:port] || 443
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@scheme = options[:scheme] || 'https'
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options)
|
2011-06-12 16:07:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def request(params)
|
2012-06-18 04:44:51 -04:00
|
|
|
refresh_credentials_if_expired
|
|
|
|
|
2011-06-12 16:07:42 -04:00
|
|
|
idempotent = params.delete(:idempotent)
|
|
|
|
parser = params.delete(:parser)
|
|
|
|
|
|
|
|
body = AWS.signed_params(
|
|
|
|
params,
|
|
|
|
{
|
|
|
|
:aws_access_key_id => @aws_access_key_id,
|
2012-06-18 04:44:51 -04:00
|
|
|
:aws_session_token => @aws_session_token,
|
2011-06-12 16:07:42 -04:00
|
|
|
:hmac => @hmac,
|
|
|
|
:host => @host,
|
|
|
|
:path => @path,
|
|
|
|
:port => @port,
|
2012-04-21 03:09:21 -04:00
|
|
|
:version => '2011-01-01'
|
2011-06-12 16:07:42 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2012-09-11 10:01:53 -04:00
|
|
|
if @instrumentor
|
|
|
|
@instrumentor.instrument("#{@instrumentor_name}.request", params) do
|
|
|
|
_request(body, idempotent, parser)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
_request(body, idempotent, parser)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _request(body, idempotent, parser)
|
2011-06-12 16:07:42 -04:00
|
|
|
begin
|
|
|
|
response = @connection.request({
|
|
|
|
:body => body,
|
|
|
|
:expects => 200,
|
|
|
|
:idempotent => idempotent,
|
|
|
|
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
|
|
|
|
:host => @host,
|
|
|
|
:method => 'POST',
|
|
|
|
:parser => parser
|
|
|
|
})
|
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
if match = error.message.match(/<Code>(.*)<\/Code>.*<Message>(.*)<\/Message>/)
|
|
|
|
case match[1]
|
|
|
|
when 'AlreadyExists'
|
|
|
|
#raise Fog::AWS::AutoScaling::IdentifierTaken.new(match[2])
|
|
|
|
raise Fog::AWS::AutoScaling::IdentifierTaken.slurp(error, match[2])
|
|
|
|
when 'ResourceInUse'
|
|
|
|
raise Fog::AWS::AutoScaling::ResourceInUse.slurp(error, match[2])
|
|
|
|
when 'ValidationError'
|
|
|
|
raise Fog::AWS::AutoScaling::ValidationError.slurp(error, match[2])
|
|
|
|
else
|
2011-09-09 10:50:04 -04:00
|
|
|
raise Fog::Compute::AWS::Error.slurp(error, "#{match[1]} => #{match[2]}")
|
2011-06-12 16:07:42 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2012-06-18 04:44:51 -04:00
|
|
|
def setup_credentials(options)
|
2012-09-23 14:24:16 -04:00
|
|
|
@aws_access_key_id = options[:aws_access_key_id]
|
|
|
|
@aws_secret_access_key = options[:aws_secret_access_key]
|
|
|
|
@aws_session_token = options[:aws_session_token]
|
2012-06-18 04:44:51 -04:00
|
|
|
@aws_credentials_expire_at = options[:aws_credentials_expire_at]
|
|
|
|
|
|
|
|
@hmac = Fog::HMAC.new('sha256', @aws_secret_access_key)
|
|
|
|
end
|
2011-06-12 16:07:42 -04:00
|
|
|
end
|
|
|
|
|
2012-06-18 04:44:51 -04:00
|
|
|
|
2011-06-12 16:07:42 -04:00
|
|
|
class Mock
|
2012-09-23 14:24:16 -04:00
|
|
|
include Fog::AWS::CredentialFetcher::ConnectionMethods
|
|
|
|
|
|
|
|
attr_accessor :region
|
2011-06-12 16:07:42 -04:00
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, region|
|
2012-09-23 14:24:16 -04:00
|
|
|
owner_id = Fog::AWS::Mock.owner_id
|
2011-06-12 16:07:42 -04:00
|
|
|
hash[region] = Hash.new do |region_hash, key|
|
|
|
|
region_hash[key] = {
|
|
|
|
:adjustment_types => [
|
|
|
|
'ChangeInCapacity',
|
|
|
|
'ExactCapacity',
|
|
|
|
'PercentChangeInCapacity'
|
|
|
|
],
|
|
|
|
:auto_scaling_groups => {},
|
2012-06-15 14:10:04 -04:00
|
|
|
:scaling_policies => {},
|
2012-09-22 09:12:02 -04:00
|
|
|
:health_states => [
|
|
|
|
'Healthy',
|
|
|
|
'Unhealthy'
|
|
|
|
],
|
2011-06-12 16:07:42 -04:00
|
|
|
:launch_configurations => {},
|
|
|
|
:metric_collection_types => {
|
2012-09-22 09:12:02 -04:00
|
|
|
:granularities => [
|
|
|
|
'1Minute'
|
|
|
|
],
|
2011-06-12 16:07:42 -04:00
|
|
|
:metrics => [
|
|
|
|
'GroupMinSize',
|
|
|
|
'GroupMaxSize',
|
|
|
|
'GroupDesiredCapacity',
|
|
|
|
'GroupInServiceInstances',
|
|
|
|
'GroupPendingInstances',
|
|
|
|
'GroupTerminatingInstances',
|
|
|
|
'GroupTotalInstances'
|
2012-09-22 09:12:02 -04:00
|
|
|
]
|
2011-06-12 16:07:42 -04:00
|
|
|
},
|
2012-09-23 14:24:16 -04:00
|
|
|
:notification_configurations => {},
|
2012-09-22 09:12:02 -04:00
|
|
|
:notification_types => [
|
|
|
|
'autoscaling:EC2_INSTANCE_LAUNCH',
|
|
|
|
'autoscaling:EC2_INSTANCE_LAUNCH_ERROR',
|
|
|
|
'autoscaling:EC2_INSTANCE_TERMINATE',
|
|
|
|
'autoscaling:EC2_INSTANCE_TERMINATE_ERROR',
|
|
|
|
'autoscaling:TEST_NOTIFICATION'
|
|
|
|
],
|
2012-09-23 14:24:16 -04:00
|
|
|
:owner_id => owner_id,
|
2011-06-12 16:07:42 -04:00
|
|
|
:process_types => [
|
|
|
|
'AZRebalance',
|
2012-09-22 09:12:02 -04:00
|
|
|
'AddToLoadBalancer',
|
2011-06-12 16:07:42 -04:00
|
|
|
'AlarmNotification',
|
|
|
|
'HealthCheck',
|
|
|
|
'Launch',
|
|
|
|
'ReplaceUnhealthy',
|
|
|
|
'ScheduledActions',
|
|
|
|
'Terminate'
|
2012-09-22 09:12:02 -04:00
|
|
|
],
|
|
|
|
:termination_policy_types => [
|
|
|
|
'ClosestToNextInstanceHour',
|
|
|
|
'Default',
|
|
|
|
'NewestInstance',
|
|
|
|
'OldestInstance',
|
|
|
|
'OldestLaunchConfiguration'
|
2011-06-12 16:07:42 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options={})
|
2012-06-18 04:44:51 -04:00
|
|
|
@use_iam_profile = options[:use_iam_profile]
|
|
|
|
setup_credentials(options)
|
2011-06-12 16:07:42 -04:00
|
|
|
@region = options[:region] || 'us-east-1'
|
|
|
|
|
2012-11-13 21:32:07 -05:00
|
|
|
unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-west-1', 'sa-east-1', 'us-east-1', 'us-west-1', 'us-west-2'].include?(@region)
|
2011-06-12 16:07:42 -04:00
|
|
|
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-23 14:24:16 -04:00
|
|
|
def region_data
|
|
|
|
self.class.data[@region]
|
2012-06-18 04:44:51 -04:00
|
|
|
end
|
|
|
|
|
2011-06-12 16:07:42 -04:00
|
|
|
def data
|
2012-09-23 14:24:16 -04:00
|
|
|
self.region_data[@aws_access_key_id]
|
2011-06-12 16:07:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
2012-09-23 14:24:16 -04:00
|
|
|
self.region_data.delete(@aws_access_key_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_credentials(options)
|
|
|
|
@aws_access_key_id = options[:aws_access_key_id]
|
2011-06-12 16:07:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|