1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Adding initial Elastic Load Balancer support

This commit is contained in:
Alex Malinovich 2010-05-02 15:13:35 +08:00 committed by Wesley Beary
parent 7ac536e873
commit 7649e5c493
5 changed files with 264 additions and 0 deletions

View file

@ -1,4 +1,5 @@
require 'fog/aws/ec2.rb'
require 'fog/aws/elb.rb'
require 'fog/aws/s3'
require 'fog/aws/simpledb'

View file

@ -15,6 +15,8 @@ module AWS
hash[key] = case key
when :ec2
Fog::AWS::EC2.new(credentials)
when :elb
Fog::AWS::ELB.new(credentials)
when :simpledb
Fog::AWS::SimpleDB.new(credentials)
when :s3

105
lib/fog/aws/elb.rb Normal file
View file

@ -0,0 +1,105 @@
module Fog
module AWS
module ELB
def self.new(options={})
unless @required
require 'fog/aws/requests/elb/describe_load_balancers'
require 'fog/aws/parsers/elb/describe_load_balancers'
@required = true
end
unless options[:aws_access_key_id]
raise ArgumentError.new('aws_access_key_id is required to access elb')
end
unless options[:aws_secret_access_key]
raise ArgumentError.new('aws_secret_access_key is required to access elb')
end
Fog::AWS::ELB::Real.new(options)
end
class Real
# Initialize connection to ELB
#
# ==== Notes
# options parameter must include values for :aws_access_key_id and
# :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 {}.
# * region<~String> - optional region to use, in ['eu-west-1', 'us-east-1', 'us-west-1'i, 'ap-southeast-1']
#
# ==== Returns
# * ELB object with connection to AWS.
def initialize(options={})
@aws_access_key_id = options[:aws_access_key_id]
@aws_secret_access_key = options[:aws_secret_access_key]
@hmac = HMAC::SHA256.new(@aws_secret_access_key)
@host = options[:host] || case options[:region]
when 'eu-west-1'
'elasticloadbalancing.eu-west-1.amazonaws.com'
when 'us-east-1'
'elasticloadbalancing.us-east-1.amazonaws.com'
when 'us-west-1'
'elasticloadbalancing.us-west-1.amazonaws.com'
when 'ap-southeast-1'
'elasticloadbalancing.ap-southeast-1.amazonaws.com'
else
'elasticloadbalancing.amazonaws.com'
end
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
end
private
def request(params)
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
idempotent = params.delete(:idempotent)
parser = params.delete(:parser)
params.merge!({
'AWSAccessKeyId' => @aws_access_key_id,
'SignatureMethod' => 'HmacSHA256',
'SignatureVersion' => '2',
'Timestamp' => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
'Version' => '2009-11-25'
})
body = ''
for key in params.keys.sort
unless (value = params[key]).nil?
body << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')}&"
end
end
string_to_sign = "POST\n#{@host}\n/\n" << body.chop
hmac = @hmac.update(string_to_sign)
body << "Signature=#{CGI.escape(Base64.encode64(hmac.digest).chomp!).gsub(/\+/, '%20')}"
response = @connection.request({
:body => body,
:expects => 200,
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
:idempotent => idempotent,
:host => @host,
:method => 'POST',
:parser => parser
})
response
end
end
end
end
end

View file

@ -0,0 +1,105 @@
module Fog
module Parsers
module AWS
module ELB
class DescribeLoadBalancers < Fog::Parsers::Base
def reset
@load_balancer = { 'ListenerDescriptions' => [], 'Instances' => [], 'AvailabilityZones' => [], 'Policies' => {'AppCookieStickinessPolicies' => [], 'LBCookieStickinessPolicies' => [] }, 'HealthCheck' => {} }
@listener = { }
@listener_descriptions = []
@listener_description = { 'PolicyNames' => [], 'Listener' => {} }
@results = { 'LoadBalancerDescriptions' => [] }
@response = { 'DescribeLoadBalancersResult' => {}, 'ResponseMetadata' => {} }
end
def start_element(name, attrs = [])
case name
when 'ListenerDescriptions'
@in_listeners = true
when 'Instances'
@in_instances = true
when 'AvailabilityZones'
@in_availability_zones = true
when 'PolicyNames'
@in_policy_names = true
when 'Policies'
@in_policies = true
when 'LBCookieStickinessPolicies'
@in_lb_cookies = true
when 'AppCookieStickinessPolicies'
@in_app_cookies = true
end
@value = ''
end
def end_element(name)
case name
when 'member'
if @in_policy_names
@listener_description['PolicyNames'] << @value
elsif @in_availability_zones
@load_balancer['AvailabilityZones'] << @value
elsif @in_listeners
@listener_description['Listener'] = @listener
@listener_descriptions << @listener_description
@load_balancer['ListenerDescriptions'] = @listener_descriptions
@listener_description = { 'PolicyNames' => [], 'Listener' => {} }
elsif @in_app_cookies
@load_balancer['Policies']['AppCookieStickinessPolicies'] << @value
elsif @in_lb_cookies
@load_balancer['Policies']['LBCookieStickinessPolicies'] << @value
elsif !@in_instances && !@in_policies
@results['LoadBalancerDescriptions'] << @load_balancer
@load_balancer = { 'ListenerDescriptions' => [], 'Instances' => [], 'AvailabilityZones' => [], 'Policies' => {'AppCookieStickinessPolicies' => [], 'LBCookieStickinessPolicies' => [] }, 'HealthCheck' => {} }
end
when 'LoadBalancerName', 'DNSName'
@load_balancer[name] = @value
when 'CreatedTime'
@load_balancer[name] = Time.parse(@value)
when 'ListenerDescriptions'
@in_listeners = false
when 'PolicyNames'
@in_policy_names = false
when 'Protocol'
@listener[name] = @value
when 'LoadBalancerPort', 'InstancePort'
@listener[name] = @value.to_i
when 'Instances'
@in_instances = false
when 'InstanceId'
@load_balancer['Instances'] << @value
when 'AvailabilityZones'
@in_availability_zones = false
when 'Policies'
@in_policies = false
when 'AppCookieStickinessPolicies'
@in_app_cookies = false
when 'LBCookieStickinessPolicies'
@in_lb_cookies = false
when 'Interval', 'HealthyThreshold', 'Timeout', 'UnhealthyThreshold'
@load_balancer['HealthCheck'][name] = @value.to_i
when 'Target'
@load_balancer['HealthCheck'][name] = @value
when 'RequestId'
@response['ResponseMetadata'][name] = @value
when 'DescribeLoadBalancersResponse'
@response['DescribeLoadBalancersResult'] = @results
end
end
end
end
end
end
end

View file

@ -0,0 +1,51 @@
module Fog
module AWS
module ELB
class Real
# Describe all or specified load balancers
#
# ==== Parameters
# * lb_name<~Array> - List of load balancer names to describe, defaults to all
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'ResponseMetadata'<~Hash>:
# * 'RequestId'<~String> - Id of request
# * 'DescribeLoadBalancersResult'<~Hash>:
# * 'LoadBalancerDescriptions'<~Array>
# * 'LoadBalancerName'<~String> - name of load balancer
# * 'DNSName'<~String> - external DNS name of load balancer
# * 'CreatedTime'<~Time> - time load balancer was created
# * 'ListenerDescriptions'<~Array>
# * 'PolicyNames'<~Array> - list of policies enabled
# * 'Listener'<~Hash>:
# * 'InstancePort'<~Integer> - port on instance that requests are sent to
# * 'Protocol'<~String> - transport protocol used for routing in [TCP, HTTP]
# * 'LoadBalancerPort'<~Integer> - port that load balancer listens on for requests
# * 'HealthCheck'<~Hash>:
# * 'HealthyThreshold'<~Integer> - number of consecutive health probe successes required before moving the instance to the Healthy state
# * 'Timeout'<~Integer> - number of seconds after which no response means a failed health probe
# * 'Interval'<~Integer> - interval (in seconds) between health checks of an individual instance
# * 'UnhealthyThreshold'<~Integer> - number of consecutive health probe failures that move the instance to the unhealthy state
# * 'Target'<~String> - string describing protocol type, port and URL to check
# * 'Policies'<~Hash>:
# * 'LBCookieStickinessPolicies'<~Array> - list of Load Balancer Generated Cookie Stickiness policies for the LoadBalancer
# * 'AppCookieStickinessPolicies'<~Array> - list of Application Generated Cookie Stickiness policies for the LoadBalancer
# * 'AvailabilityZones'<~Array> - list of availability zones covered by this load balancer
# * 'Instances'<~Array> - list of instances that the load balancer balances between
def describe_load_balancers(lb_name = [])
lb_name = [lb_name] if lb_name.instance_of?(String)
lb_name.unshift(nil)
params = AWS.indexed_param('LoadBalancerNames.member', lb_name)
request({
'Action' => 'DescribeLoadBalancers',
:parser => Fog::Parsers::AWS::ELB::DescribeLoadBalancers.new
}.merge!(params))
end
end
end
end
end