Skip to content

Commit

Permalink
Merge pull request fog#2012 from mikehale/aws-elb-parser-fix
Browse files Browse the repository at this point in the history
Aws elb parser fix
  • Loading branch information
geemus committed Aug 1, 2013
2 parents 66755e9 + 57cb4d7 commit db5d9f0
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 12 deletions.
7 changes: 5 additions & 2 deletions lib/fog/aws/parsers/elb/describe_load_balancers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ def end_element(name)

when 'BackendServerDescriptions'
@in_backend_server_descriptions = false

when 'InstancePort'
if @in_backend_server_descriptions
@backend_server_description[name] = value
@backend_server_description[name] = value.to_i
elsif @in_listeners
@listener_description['Listener'][name] = value.to_i
end

when 'CanonicalHostedZoneName', 'CanonicalHostedZoneNameID', 'LoadBalancerName', 'DNSName', 'Scheme'
Expand All @@ -111,7 +114,7 @@ def end_element(name)
@in_policy_names = false
when 'Protocol', 'SSLCertificateId', 'InstanceProtocol'
@listener_description['Listener'][name] = value
when 'LoadBalancerPort', 'InstancePort'
when 'LoadBalancerPort'
@listener_description['Listener'][name] = value.to_i

when 'Instances'
Expand Down
9 changes: 5 additions & 4 deletions lib/fog/aws/requests/elb/create_load_balancer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ def create_load_balancer(availability_zones, lb_name, listeners = [], options =

certificate_ids = Fog::AWS::IAM::Mock.data[@aws_access_key_id][:server_certificates].map {|n, c| c['Arn'] }

instance_ports = listeners.map{|l| l["InstancePort"] }
backend_server_descriptions = instance_ports.map {|port| {'InstancePort' => port } }

listeners = [*listeners].map do |listener|
if listener['SSLCertificateId'] and !certificate_ids.include? listener['SSLCertificateId']
raise Fog::AWS::IAM::NotFound.new('CertificateNotFound')
Expand All @@ -89,7 +86,11 @@ def create_load_balancer(availability_zones, lb_name, listeners = [], options =

self.data[:load_balancers][lb_name] = {
'AvailabilityZones' => availability_zones,
'BackendServerDescriptions' => backend_server_descriptions,
'BackendServerDescriptions' => [],
# Hack to facilitate not updating the local data structure
# (BackendServerDescriptions) until we do a subsequent
# describe as that is how AWS behaves.
'BackendServerDescriptionsRemote' => [],
'Subnets' => options[:subnet_ids] || [],
'Scheme' => options[:scheme].nil? ? 'internet-facing' : options[:scheme],
'SecurityGroups' => options[:security_groups].nil? ? [] : options[:security_groups],
Expand Down
1 change: 1 addition & 0 deletions lib/fog/aws/requests/elb/describe_load_balancers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def describe_load_balancers(options = {})
'LoadBalancerDescriptions' => load_balancers.map do |lb|
lb['Instances'] = lb['Instances'].map { |i| i['InstanceId'] }
lb['Policies'] = lb['Policies'].reject { |name, policies| name == 'Proper' }
lb['BackendServerDescriptions'] = lb.delete('BackendServerDescriptionsRemote')
lb
end
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ def set_load_balancer_policies_for_backend_server(lb_name, instance_port, policy
end
end

# Update backend policies
description = load_balancer['BackendServerDescriptions'].find{|d| d["InstancePort"] == instance_port } || {}
# Update backend policies:
description = load_balancer['BackendServerDescriptionsRemote'].find{|d| d["InstancePort"] == instance_port } || {}
description["InstancePort"] = instance_port
description["PolicyNames"] = policy_names
load_balancer['BackendServerDescriptionsRemote'].delete_if{|d| d["InstancePort"] == instance_port }
load_balancer['BackendServerDescriptionsRemote'] << description

Excon::Response.new.tap do |response|
response.status = 200
Expand Down
3 changes: 3 additions & 0 deletions lib/fog/xml/sax_parser_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ def request(parser, params)

# Prepare the SAX parser
data_stream = Nokogiri::XML::SAX::PushParser.new(parser)
response_string = ""
params[:response_block] = lambda do |chunk, remaining, total|
response_string << chunk if ENV['DEBUG_RESPONSE']
data_stream << chunk
end

# Make request which read chunks into parser
response = @excon.request(params)
Fog::Logger.debug "\n#{response_string}" if ENV['DEBUG_RESPONSE']

# Cease parsing and override response.body with parsed data
data_stream.finish
Expand Down
5 changes: 2 additions & 3 deletions tests/aws/models/elb/model_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,15 @@

tests('backend server descriptions') do
tests('default') do
returns(1) { elb.backend_server_descriptions.size }
returns(80) { elb.backend_server_descriptions.first.instance_port }
returns(0) { elb.backend_server_descriptions.size }
end

tests('with a backend policy') do
policy = "EnableProxyProtocol"
port = 80
Fog::AWS[:elb].create_load_balancer_policy(elb.id, policy, 'ProxyProtocolPolicyType', { "ProxyProtocol" => true })
Fog::AWS[:elb].set_load_balancer_policies_for_backend_server(elb.id, port, [policy]).body

elb.reload
returns([policy]) { elb.backend_server_descriptions.get(port).policy_names }
end
end
Expand Down
65 changes: 65 additions & 0 deletions tests/aws/parsers/elb/describe_load_balancers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'fog/xml'
require 'fog/aws/parsers/elb/describe_load_balancers'

DESCRIBE_LOAD_BALANCERS_RESULT = <<-EOF
<DescribeLoadBalancersResponse
xmlns="http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/">
<DescribeLoadBalancersResult>
<LoadBalancerDescriptions>
<member>
<SecurityGroups/>
<CreatedTime>2013-08-01T15:47:20.930Z</CreatedTime>
<LoadBalancerName>fog-test-elb</LoadBalancerName>
<HealthCheck>
<Interval>30</Interval>
<Target>TCP:80</Target>
<HealthyThreshold>10</HealthyThreshold>
<Timeout>5</Timeout>
<UnhealthyThreshold>2</UnhealthyThreshold>
</HealthCheck>
<ListenerDescriptions>
<member>
<PolicyNames/>
<Listener>
<Protocol>HTTP</Protocol>
<LoadBalancerPort>80</LoadBalancerPort>
<InstanceProtocol>HTTP</InstanceProtocol>
<InstancePort>80</InstancePort>
</Listener>
</member>
</ListenerDescriptions>
<Instances/>
<Policies>
<AppCookieStickinessPolicies/>
<OtherPolicies/>
<LBCookieStickinessPolicies/>
</Policies>
<AvailabilityZones>
<member>us-east-1a</member>
</AvailabilityZones>
<CanonicalHostedZoneName>fog-test-elb-1965660309.us-east-1.elb.amazonaws.com</CanonicalHostedZoneName>
<CanonicalHostedZoneNameID>Z3DZXE0Q79N41H</CanonicalHostedZoneNameID>
<Scheme>internet-facing</Scheme>
<SourceSecurityGroup>
<OwnerAlias>amazon-elb</OwnerAlias>
<GroupName>amazon-elb-sg</GroupName>
</SourceSecurityGroup>
<DNSName>fog-test-elb-1965660309.us-east-1.elb.amazonaws.com</DNSName>
<BackendServerDescriptions/>
<Subnets/>
</member>
</LoadBalancerDescriptions>
</DescribeLoadBalancersResult>
<ResponseMetadata>
<RequestId>a6ea2117-fac1-11e2-abd3-1740ab4ef14e</RequestId>
</ResponseMetadata>
</DescribeLoadBalancersResponse>
EOF

Shindo.tests('AWS::ELB | parsers | describe_load_balancers', ['aws', 'elb', 'parser']) do
tests('parses the xml').formats(AWS::ELB::Formats::DESCRIBE_LOAD_BALANCERS) do
parser = Nokogiri::XML::SAX::Parser.new(Fog::Parsers::AWS::ELB::DescribeLoadBalancers.new)
parser.parse(DESCRIBE_LOAD_BALANCERS_RESULT)
parser.document.response
end
end
3 changes: 2 additions & 1 deletion tests/aws/requests/elb/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Formats

LOAD_BALANCER = {
"AvailabilityZones" => Array,
"BackendServerDescriptions" => Array,
"CanonicalHostedZoneName" => String,
"CanonicalHostedZoneNameID" => String,
"CreatedTime" => Time,
Expand All @@ -25,7 +26,7 @@ module Formats
}
}],
"LoadBalancerName" => String,
"Policies" => {"LBCookieStickinessPolicies" => Array, "AppCookieStickinessPolicies" => Array},
"Policies" => {"LBCookieStickinessPolicies" => Array, "AppCookieStickinessPolicies" => Array, "OtherPolicies" => Array},
"Scheme" => String,
"SecurityGroups" => [Fog::Nullable::String],
"SourceSecurityGroup" => {"GroupName" => String, "OwnerAlias" => String},
Expand Down
4 changes: 4 additions & 0 deletions tests/aws/requests/elb/load_balancer_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
@key_name = 'fog-test'

tests('success') do
if (Fog::AWS[:iam].get_server_certificate(@key_name) rescue nil)
Fog::AWS[:iam].delete_server_certificate(@key_name)
end

@certificate = Fog::AWS[:iam].upload_server_certificate(AWS::IAM::SERVER_CERT_PUBLIC_KEY, AWS::IAM::SERVER_CERT_PRIVATE_KEY, @key_name).body['Certificate']

tests("#create_load_balancer").formats(AWS::ELB::Formats::CREATE_LOAD_BALANCER) do
Expand Down

0 comments on commit db5d9f0

Please sign in to comment.