From 992a2487115033d43adb83173c7d09535fe057ba Mon Sep 17 00:00:00 2001 From: Michael Hale Date: Wed, 31 Jul 2013 23:34:03 -0400 Subject: [PATCH] [AWS|ELB] add support for OtherPolicies --- .../parsers/elb/describe_load_balancers.rb | 26 ++++++++++++------- .../aws/requests/elb/create_load_balancer.rb | 1 + .../elb/create_load_balancer_policy.rb | 23 ++++++++++------ .../requests/elb/describe_load_balancers.rb | 1 + tests/aws/requests/elb/policy_tests.rb | 13 +++++++--- 5 files changed, 43 insertions(+), 21 deletions(-) diff --git a/lib/fog/aws/parsers/elb/describe_load_balancers.rb b/lib/fog/aws/parsers/elb/describe_load_balancers.rb index ae54c59f2..36485d537 100644 --- a/lib/fog/aws/parsers/elb/describe_load_balancers.rb +++ b/lib/fog/aws/parsers/elb/describe_load_balancers.rb @@ -8,14 +8,14 @@ module Fog def reset reset_load_balancer reset_listener_description - reset_policy + reset_stickiness_policy reset_backend_server_description @results = { 'LoadBalancerDescriptions' => [] } @response = { 'DescribeLoadBalancersResult' => {}, 'ResponseMetadata' => {} } end def reset_load_balancer - @load_balancer = { 'Subnets' => [], 'SecurityGroups' => [], 'ListenerDescriptions' => [], 'Instances' => [], 'AvailabilityZones' => [], 'Policies' => {'AppCookieStickinessPolicies' => [], 'LBCookieStickinessPolicies' => [] }, 'HealthCheck' => {}, 'SourceSecurityGroup' => {}, 'BackendServerDescriptions' => [] } + @load_balancer = { 'Subnets' => [], 'SecurityGroups' => [], 'ListenerDescriptions' => [], 'Instances' => [], 'AvailabilityZones' => [], 'Policies' => {'AppCookieStickinessPolicies' => [], 'LBCookieStickinessPolicies' => [], 'OtherPolicies' => []}, 'HealthCheck' => {}, 'SourceSecurityGroup' => {}, 'BackendServerDescriptions' => [] } end def reset_listener_description @@ -26,8 +26,8 @@ module Fog @backend_server_description = {} end - def reset_policy - @policy = {} + def reset_stickiness_policy + @stickiness_policy = {} end def start_element(name, attrs = []) @@ -53,6 +53,8 @@ module Fog @in_app_cookies = true when 'AppCookieStickinessPolicies' @in_app_cookies = true + when 'OtherPolicies' + @in_other_policies = true when 'BackendServerDescriptions' @in_backend_server_descriptions = true end @@ -73,11 +75,13 @@ module Fog @load_balancer['ListenerDescriptions'] << @listener_description reset_listener_description elsif @in_app_cookies - @load_balancer['Policies']['AppCookieStickinessPolicies'] << @policy - reset_policy + @load_balancer['Policies']['AppCookieStickinessPolicies'] << @stickiness_policy + reset_stickiness_policy elsif @in_lb_cookies - @load_balancer['Policies']['LBCookieStickinessPolicies'] << @policy - reset_policy + @load_balancer['Policies']['LBCookieStickinessPolicies'] << @stickiness_policy + reset_stickiness_policy + elsif @in_other_policies + @load_balancer['Policies']['OtherPolicies'] << value elsif @in_backend_server_descriptions && @in_policy_names @backend_server_description['PolicyNames'] ||= [] @backend_server_description['PolicyNames'] << value @@ -130,6 +134,8 @@ module Fog @in_app_cookies = false when 'LBCookieStickinessPolicies' @in_lb_cookies = false + when 'OtherPolicies' + @in_other_policies = false when 'OwnerAlias', 'GroupName' @load_balancer['SourceSecurityGroup'][name] = value @@ -140,9 +146,9 @@ module Fog @load_balancer['HealthCheck'][name] = value when 'PolicyName', 'CookieName' - @policy[name] = value + @stickiness_policy[name] = value when 'CookieExpirationPeriod' - @policy[name] = value.to_i + @stickiness_policy[name] = value.to_i when 'RequestId' @response['ResponseMetadata'][name] = value diff --git a/lib/fog/aws/requests/elb/create_load_balancer.rb b/lib/fog/aws/requests/elb/create_load_balancer.rb index 8736ad47b..dad4585fc 100644 --- a/lib/fog/aws/requests/elb/create_load_balancer.rb +++ b/lib/fog/aws/requests/elb/create_load_balancer.rb @@ -110,6 +110,7 @@ module Fog 'Policies' => { 'AppCookieStickinessPolicies' => [], 'LBCookieStickinessPolicies' => [], + 'OtherPolicies' => [], 'Proper' => [] }, 'SourceSecurityGroup' => { diff --git a/lib/fog/aws/requests/elb/create_load_balancer_policy.rb b/lib/fog/aws/requests/elb/create_load_balancer_policy.rb index 9259ceac9..87f9741ec 100644 --- a/lib/fog/aws/requests/elb/create_load_balancer_policy.rb +++ b/lib/fog/aws/requests/elb/create_load_balancer_policy.rb @@ -33,12 +33,12 @@ module Fog params.merge!(Fog::AWS.indexed_param('PolicyAttributes.member.%d.AttributeValue', attribute_value)) request({ - 'Action' => 'CreateLoadBalancerPolicy', - 'LoadBalancerName' => lb_name, - 'PolicyName' => name, - 'PolicyTypeName' => type_name, - :parser => Fog::Parsers::AWS::ELB::Empty.new - }.merge!(params)) + 'Action' => 'CreateLoadBalancerPolicy', + 'LoadBalancerName' => lb_name, + 'PolicyName' => name, + 'PolicyTypeName' => type_name, + :parser => Fog::Parsers::AWS::ELB::Empty.new + }.merge!(params)) end end @@ -46,8 +46,8 @@ module Fog class Mock def create_load_balancer_policy(lb_name, name, type_name, attributes = {}) if load_balancer = self.data[:load_balancers][lb_name] - raise Fog::AWS::IAM::DuplicatePolicyName if policy = load_balancer['Policies']['Proper'].find { |p| p['PolicyName'] == name } - raise Fog::AWS::IAM::PolicyTypeNotFound unless policy_type = self.data[:policy_types].find { |pt| pt['PolicyTypeName'] == type_name } + raise Fog::AWS::ELB::DuplicatePolicyName, name if policy = load_balancer['Policies']['Proper'].find { |p| p['PolicyName'] == name } + raise Fog::AWS::ELB::PolicyTypeNotFound, policy_type unless policy_type = self.data[:policy_types].find { |pt| pt['PolicyTypeName'] == type_name } response = Excon::Response.new @@ -55,6 +55,13 @@ module Fog {"AttributeName" => key, "AttributeValue" => value.to_s} end + # Update other policies + if %w[PublicKeyPolicyType ProxyProtocolPolicyType].include?(type_name) + unless load_balancer['Policies']['OtherPolicies'].include?(name) + load_balancer['Policies']['OtherPolicies'] << name + end + end + load_balancer['Policies']['Proper'] << { 'PolicyAttributeDescriptions' => attributes, 'PolicyName' => name, diff --git a/lib/fog/aws/requests/elb/describe_load_balancers.rb b/lib/fog/aws/requests/elb/describe_load_balancers.rb index 06b7ba21c..5bef70695 100644 --- a/lib/fog/aws/requests/elb/describe_load_balancers.rb +++ b/lib/fog/aws/requests/elb/describe_load_balancers.rb @@ -44,6 +44,7 @@ module Fog # * '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 + # * 'OtherPolicies'<~Array> - list of policy names other than the stickiness policies # * 'SourceSecurityGroup'<~Hash>: # * 'GroupName'<~String> - Name of the source security group to use with inbound security group rules # * 'OwnerAlias'<~String> - Owner of the source security group diff --git a/tests/aws/requests/elb/policy_tests.rb b/tests/aws/requests/elb/policy_tests.rb index b882f76ab..d8225c2cf 100644 --- a/tests/aws/requests/elb/policy_tests.rb +++ b/tests/aws/requests/elb/policy_tests.rb @@ -49,12 +49,19 @@ Shindo.tests('AWS::ELB | policy_tests', ['aws', 'elb']) do Fog::AWS[:elb].set_load_balancer_policies_of_listener(@load_balancer_id, port, []).body end + proxy_policy = "EnableProxyProtocol" + Fog::AWS[:elb].create_load_balancer_policy(@load_balancer_id, proxy_policy, 'ProxyProtocolPolicyType', { "ProxyProtocol" => true }) + tests("#set_load_balancer_policies_for_backend_server replaces policies on port").formats(AWS::ELB::Formats::BASIC) do - policy = "EnableProxyProtocol" - Fog::AWS[:elb].create_load_balancer_policy(@load_balancer_id, policy, 'ProxyProtocolPolicyType', { "ProxyProtocol" => true }) - Fog::AWS[:elb].set_load_balancer_policies_for_backend_server(@load_balancer_id, 80, [policy]).body + Fog::AWS[:elb].set_load_balancer_policies_for_backend_server(@load_balancer_id, 80, [proxy_policy]).body end + tests("#describe_load_balancers has other policies") do + Fog::AWS[:elb].set_load_balancer_policies_for_backend_server(@load_balancer_id, 80, [proxy_policy]).body + description = Fog::AWS[:elb].describe_load_balancers("LoadBalancerNames" => [@load_balancer_id]).body["DescribeLoadBalancersResult"]["LoadBalancerDescriptions"].first + returns(true) { description["Policies"]["OtherPolicies"].include?(proxy_policy) } + end + Fog::AWS[:elb].delete_load_balancer(@load_balancer_id) end end