2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/aws'
|
2011-08-31 16:52:53 -04:00
|
|
|
|
2010-05-02 03:13:35 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
2010-09-03 04:11:45 -04:00
|
|
|
class ELB < Fog::Service
|
2012-06-18 04:44:51 -04:00
|
|
|
extend Fog::AWS::CredentialFetcher::ServiceMethods
|
2010-05-02 03:13:35 -04:00
|
|
|
|
2012-01-23 20:23:23 -05:00
|
|
|
class DuplicatePolicyName < Fog::Errors::Error; end
|
|
|
|
class IdentifierTaken < Fog::Errors::Error; end
|
|
|
|
class InvalidInstance < Fog::Errors::Error; end
|
|
|
|
class InvalidConfigurationRequest < Fog::Errors::Error; end
|
|
|
|
class PolicyNotFound < Fog::Errors::Error; end
|
|
|
|
class PolicyTypeNotFound < Fog::Errors::Error; end
|
|
|
|
class Throttled < Fog::Errors::Error; end
|
|
|
|
class TooManyPolicies < Fog::Errors::Error; end
|
|
|
|
class ValidationError < Fog::Errors::Error; end
|
2011-03-08 17:41:49 -05:00
|
|
|
|
2010-12-16 18:31:24 -05:00
|
|
|
requires :aws_access_key_id, :aws_secret_access_key
|
2012-07-27 08:07:14 -04:00
|
|
|
recognizes :region, :host, :path, :port, :scheme, :persistent, :use_iam_profile, :aws_session_token, :aws_credentials_expire_at, :instrumentor, :instrumentor_name
|
2010-05-02 03:13:35 -04:00
|
|
|
|
2010-06-12 18:31:17 -04:00
|
|
|
request_path 'fog/aws/requests/elb'
|
2011-04-09 16:54:24 -04:00
|
|
|
request :configure_health_check
|
2011-04-28 13:46:28 -04:00
|
|
|
request :create_app_cookie_stickiness_policy
|
|
|
|
request :create_lb_cookie_stickiness_policy
|
2010-09-02 19:01:19 -04:00
|
|
|
request :create_load_balancer
|
2011-04-28 12:05:56 -04:00
|
|
|
request :create_load_balancer_listeners
|
2011-12-19 18:43:45 -05:00
|
|
|
request :create_load_balancer_policy
|
2010-09-02 19:01:19 -04:00
|
|
|
request :delete_load_balancer
|
2011-04-28 12:05:56 -04:00
|
|
|
request :delete_load_balancer_listeners
|
2011-04-28 13:51:42 -04:00
|
|
|
request :delete_load_balancer_policy
|
2010-09-02 19:01:19 -04:00
|
|
|
request :deregister_instances_from_load_balancer
|
|
|
|
request :describe_instance_health
|
|
|
|
request :describe_load_balancers
|
2011-12-19 18:43:45 -05:00
|
|
|
request :describe_load_balancer_policies
|
2011-12-19 16:20:53 -05:00
|
|
|
request :describe_load_balancer_policy_types
|
2010-09-02 19:01:19 -04:00
|
|
|
request :disable_availability_zones_for_load_balancer
|
|
|
|
request :enable_availability_zones_for_load_balancer
|
|
|
|
request :register_instances_with_load_balancer
|
2011-07-11 12:01:07 -04:00
|
|
|
request :set_load_balancer_listener_ssl_certificate
|
2011-04-28 14:31:18 -04:00
|
|
|
request :set_load_balancer_policies_of_listener
|
2012-02-26 04:17:08 -05:00
|
|
|
request :attach_load_balancer_to_subnets
|
|
|
|
request :detach_load_balancer_from_subnets
|
|
|
|
request :apply_security_groups_to_load_balancer
|
2010-05-02 03:13:35 -04:00
|
|
|
|
2011-05-11 15:27:27 -04:00
|
|
|
model_path 'fog/aws/models/elb'
|
2011-03-08 17:41:49 -05:00
|
|
|
model :load_balancer
|
|
|
|
collection :load_balancers
|
2011-05-02 18:17:19 -04:00
|
|
|
model :policy
|
|
|
|
collection :policies
|
2011-05-04 18:29:07 -04:00
|
|
|
model :listener
|
|
|
|
collection :listeners
|
2011-03-08 17:41:49 -05:00
|
|
|
|
2010-06-12 18:31:17 -04:00
|
|
|
class Mock
|
|
|
|
|
2011-12-19 18:43:45 -05:00
|
|
|
require 'fog/aws/elb/policy_types'
|
|
|
|
|
2011-07-06 19:11:14 -04:00
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, region|
|
|
|
|
owner_id = Fog::AWS::Mock.owner_id
|
|
|
|
hash[region] = Hash.new do |region_hash, key|
|
|
|
|
region_hash[key] = {
|
|
|
|
:owner_id => owner_id,
|
2011-12-19 16:20:53 -05:00
|
|
|
:load_balancers => {},
|
2011-12-19 18:43:45 -05:00
|
|
|
:policy_types => Fog::AWS::ELB::Mock::POLICY_TYPES
|
2011-07-06 19:11:14 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.dns_name(name, region)
|
|
|
|
"#{name}-#{Fog::Mock.random_hex(8)}.#{region}.elb.amazonaws.com"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
2010-06-12 18:31:17 -04:00
|
|
|
def initialize(options={})
|
2012-06-18 04:44:51 -04:00
|
|
|
@use_iam_profile = options[:use_iam_profile]
|
|
|
|
setup_credentials(options)
|
2011-07-06 19:11:14 -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', 'us-east-1', 'us-west-1', 'us-west-2'].include?(@region)
|
2011-07-06 19:11:14 -04:00
|
|
|
raise ArgumentError, "Unknown region: #{@region.inspect}"
|
|
|
|
end
|
2010-05-02 03:13:35 -04:00
|
|
|
end
|
2010-06-12 18:31:17 -04:00
|
|
|
|
2012-06-18 04:44:51 -04:00
|
|
|
def setup_credentials(options)
|
|
|
|
@aws_access_key_id = options[:aws_access_key_id]
|
|
|
|
end
|
|
|
|
|
2011-07-06 19:11:14 -04:00
|
|
|
def data
|
|
|
|
self.class.data[@region][@aws_access_key_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data[@region].delete(@aws_access_key_id)
|
|
|
|
end
|
2010-05-02 03:13:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
2012-06-18 04:44:51 -04:00
|
|
|
include Fog::AWS::CredentialFetcher::ConnectionMethods
|
2010-05-02 03:13:35 -04:00
|
|
|
# Initialize connection to ELB
|
|
|
|
#
|
|
|
|
# ==== Notes
|
2010-05-02 19:21:35 -04:00
|
|
|
# options parameter must include values for :aws_access_key_id and
|
2010-05-02 03:13:35 -04:00
|
|
|
# :aws_secret_access_key in order to create a connection
|
|
|
|
#
|
|
|
|
# ==== Examples
|
|
|
|
# elb = ELB.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 {}.
|
2012-02-02 16:39:46 -05:00
|
|
|
# * region<~String> - optional region to use. For instance, 'eu-west-1', 'us-east-1', etc.
|
2010-05-02 03:13:35 -04:00
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# * ELB object with connection to AWS.
|
|
|
|
def initialize(options={})
|
2011-02-16 20:25:50 -05:00
|
|
|
require 'fog/core/parser'
|
|
|
|
|
2012-06-18 04:44:51 -04:00
|
|
|
@use_iam_profile = options[:use_iam_profile]
|
|
|
|
setup_credentials(options)
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2012-07-27 08:07:14 -04:00
|
|
|
@instrumentor = options[:instrumentor]
|
|
|
|
@instrumentor_name = options[:instrumentor_name] || 'fog.aws.elb'
|
|
|
|
|
2010-08-24 18:27:50 -04:00
|
|
|
options[:region] ||= 'us-east-1'
|
2012-02-02 16:39:46 -05:00
|
|
|
@host = options[:host] || "elasticloadbalancing.#{options[:region]}.amazonaws.com"
|
2011-09-12 11:01:48 -04:00
|
|
|
@path = options[:path] || '/'
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@port = options[:port] || 443
|
|
|
|
@scheme = options[:scheme] || 'https'
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options)
|
2010-06-19 21:56:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
2010-05-02 03:13:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-06-18 04:44:51 -04:00
|
|
|
def setup_credentials(options={})
|
|
|
|
@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]
|
|
|
|
@aws_credentials_expire_at = options[:aws_credentials_expire_at]
|
|
|
|
|
|
|
|
@hmac = Fog::HMAC.new('sha256', @aws_secret_access_key)
|
|
|
|
end
|
|
|
|
|
2010-05-02 03:13:35 -04:00
|
|
|
def request(params)
|
2012-06-18 04:44:51 -04:00
|
|
|
refresh_credentials_if_expired
|
|
|
|
|
2010-05-02 03:13:35 -04:00
|
|
|
idempotent = params.delete(:idempotent)
|
|
|
|
parser = params.delete(:parser)
|
|
|
|
|
2011-06-20 16:49:37 -04:00
|
|
|
body = Fog::AWS.signed_params(
|
2010-05-02 16:25:39 -04:00
|
|
|
params,
|
|
|
|
{
|
|
|
|
:aws_access_key_id => @aws_access_key_id,
|
[AWS] make beanstalk, cdn, cloudformation, cloudwatch, elasticache, elb, storage, rds, ses, sns, route53 temporary credential friendly
2012-06-20 18:16:34 -04:00
|
|
|
:aws_session_token => @aws_session_token,
|
2010-05-02 16:25:39 -04:00
|
|
|
:hmac => @hmac,
|
|
|
|
:host => @host,
|
2010-09-29 20:53:32 -04:00
|
|
|
:path => @path,
|
2010-12-23 16:54:02 -05:00
|
|
|
:port => @port,
|
2012-06-12 19:18:20 -04:00
|
|
|
:version => '2012-06-01'
|
2011-03-08 17:41:49 -05:00
|
|
|
}
|
2010-05-02 16:25:39 -04:00
|
|
|
)
|
2011-07-19 13:12:49 -04:00
|
|
|
|
2012-07-27 08:07:14 -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)
|
2012-05-14 15:26:28 -04:00
|
|
|
@connection.request({
|
2011-07-19 13:12:49 -04:00
|
|
|
:body => body,
|
|
|
|
:expects => 200,
|
|
|
|
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
|
|
|
|
:idempotent => idempotent,
|
|
|
|
:host => @host,
|
|
|
|
:method => 'POST',
|
|
|
|
:parser => parser
|
|
|
|
})
|
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
if match = error.message.match(/<Code>(.*)<\/Code>(?:.*<Message>(.*)<\/Message>)?/m)
|
|
|
|
case match[1]
|
|
|
|
when 'CertificateNotFound'
|
|
|
|
raise Fog::AWS::IAM::NotFound.slurp(error, match[2])
|
|
|
|
when 'DuplicateLoadBalancerName'
|
2011-07-19 13:14:04 -04:00
|
|
|
raise Fog::AWS::ELB::IdentifierTaken.slurp(error, match[2])
|
2011-12-19 16:20:53 -05:00
|
|
|
when 'DuplicatePolicyName'
|
|
|
|
raise Fog::AWS::ELB::DuplicatePolicyName.slurp(error, match[2])
|
2011-07-19 13:12:49 -04:00
|
|
|
when 'InvalidInstance'
|
2011-07-19 13:14:04 -04:00
|
|
|
raise Fog::AWS::ELB::InvalidInstance.slurp(error, match[2])
|
2012-01-23 20:23:23 -05:00
|
|
|
when 'InvalidConfigurationRequest'
|
|
|
|
# when do they fucking use this shit?
|
|
|
|
raise Fog::AWS::ELB::InvalidConfigurationRequest.slurp(error, match[2])
|
2011-12-19 16:20:53 -05:00
|
|
|
when 'LoadBalancerNotFound'
|
|
|
|
raise Fog::AWS::ELB::NotFound.slurp(error, match[2])
|
2011-12-20 12:32:30 -05:00
|
|
|
when 'PolicyNotFound'
|
|
|
|
raise Fog::AWS::ELB::PolicyNotFound.slurp(error, match[2])
|
2011-12-19 16:20:53 -05:00
|
|
|
when 'PolicyTypeNotFound'
|
|
|
|
raise Fog::AWS::ELB::PolicyTypeNotFound.slurp(error, match[2])
|
2011-08-26 18:59:27 -04:00
|
|
|
when 'Throttling'
|
|
|
|
raise Fog::AWS::ELB::Throttled.slurp(error, match[2])
|
2011-12-19 16:20:53 -05:00
|
|
|
when 'TooManyPolicies'
|
|
|
|
raise Fog::AWS::ELB::TooManyPolicies.slurp(error, match[2])
|
2012-01-23 20:23:23 -05:00
|
|
|
when 'ValidationError'
|
|
|
|
raise Fog::AWS::ELB::ValidationError.slurp(error, match[2])
|
2011-03-08 17:41:49 -05:00
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
2011-07-19 13:12:49 -04:00
|
|
|
else
|
|
|
|
raise
|
2011-03-08 17:41:49 -05:00
|
|
|
end
|
2010-05-02 03:13:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|