From f3635dcd420708342f7361aad6d069db3dd2ea1b Mon Sep 17 00:00:00 2001 From: Dylan Egan Date: Thu, 7 Jul 2011 14:53:10 -0700 Subject: [PATCH] deregister_instances_from_load_balancer, describe_instance_health, disable_availability_for_load_balancer, enable_availability_zones_for_load_balancer, register_instances_with_load_balancer and updates to others to get ELB model_tests working. --- .../aws/requests/elb/create_load_balancer.rb | 23 ++++++++---- ...deregister_instances_from_load_balancer.rb | 26 +++++++++++--- .../requests/elb/describe_instance_health.rb | 35 +++++++++++++++++++ .../requests/elb/describe_load_balancers.rb | 4 +-- ...le_availability_zones_for_load_balancer.rb | 23 +++++++++--- ...le_availability_zones_for_load_balancer.rb | 21 +++++++++-- .../register_instances_with_load_balancer.rb | 27 +++++++++++--- tests/aws/requests/elb/model_tests.rb | 5 +-- 8 files changed, 134 insertions(+), 30 deletions(-) diff --git a/lib/fog/aws/requests/elb/create_load_balancer.rb b/lib/fog/aws/requests/elb/create_load_balancer.rb index 843112cee..978ddb738 100644 --- a/lib/fog/aws/requests/elb/create_load_balancer.rb +++ b/lib/fog/aws/requests/elb/create_load_balancer.rb @@ -55,6 +55,8 @@ module Fog response = Excon::Response.new response.status = 200 + raise Fog::AWS::ELB::IdentifierTaken if self.data[:load_balancers].has_key? lb_name + dns_name = Fog::AWS::ELB::Mock.dns_name(lb_name, @region) self.data[:load_balancers][lb_name] = { 'AvailabilityZones' => availability_zones, @@ -63,14 +65,23 @@ module Fog 'CreatedTime' => Time.now, 'DNSName' => dns_name, 'HealthCheck' => { - 'HealthyThreshold' => 3, - 'Interval' => 60, - 'Target' => 'TCP:5000', - 'Timeout' => 60, - 'UnhealthyThreshold' => 2 + 'HealthyThreshold' => 10, + 'Timeout' => 5, + 'UnhealthyThreshold' => 2, + 'Interval' => 30, + 'Target' => 'TCP:80' }, 'Instances' => [], - 'ListenerDescriptions' => [], + 'ListenerDescriptions' => [ + { + 'Listener' => { + 'InstancePort' => 80, + 'Protocol' => 'HTTP', + 'LoadBalancerPort' => 80 + }, + 'PolicyNames' => [] + } + ], 'LoadBalancerName' => lb_name, 'Policies' => { 'LBCookieStickinessPolicies' => [], diff --git a/lib/fog/aws/requests/elb/deregister_instances_from_load_balancer.rb b/lib/fog/aws/requests/elb/deregister_instances_from_load_balancer.rb index 89293d278..ff786cb8e 100644 --- a/lib/fog/aws/requests/elb/deregister_instances_from_load_balancer.rb +++ b/lib/fog/aws/requests/elb/deregister_instances_from_load_balancer.rb @@ -33,15 +33,31 @@ module Fog end class Mock - def deregister_instances_from_load_balancer(instance_ids, lb_name) - Fog::Mock.not_implemented + raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name] + instance_ids = [*instance_ids] + instance_ids.each do |instance| + raise Fog::AWS::ELB::InvalidInstance unless Compute[:aws].servers.get(instance) + end + + response = Excon::Response.new + response.status = 200 + + load_balancer['Instances'].delete_if { |i| instance_ids.include? i['InstanceId'] } + + response.body = { + 'ResponseMetadata' => { + 'RequestId' => Fog::AWS::Mock.request_id + }, + 'DeregisterInstancesFromLoadBalancerResult' => { + 'Instances' => load_balancer['Instances'].dup + } + } + + response end - alias :deregister_instances :deregister_instances_from_load_balancer - end - end end end diff --git a/lib/fog/aws/requests/elb/describe_instance_health.rb b/lib/fog/aws/requests/elb/describe_instance_health.rb index 21f2aaaee..db6ec6667 100644 --- a/lib/fog/aws/requests/elb/describe_instance_health.rb +++ b/lib/fog/aws/requests/elb/describe_instance_health.rb @@ -32,6 +32,41 @@ module Fog end end + + class Mock + def describe_instance_health(lb_name, instance_ids = []) + raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name] + + instance_ids = [*instance_ids] + instance_ids = load_balancer['Instances'].collect { |i| i['InstanceId'] } unless instance_ids.any? + data = instance_ids.map do |id| + unless instance = Compute[:aws].servers.get(id) + raise Fog::AWS::ELB::InvalidInstance + end + + { + 'Description' => "", + 'InstanceId' => instance.id, + 'ReasonCode' => "", + 'State' => 'OutOfService' + } + end + + response = Excon::Response.new + response.status = 200 + + response.body = { + 'ResponseMetadata' => { + 'RequestId' => Fog::AWS::Mock.request_id + }, + 'DescribeInstanceHealthResult' => { + 'InstanceStates' => data + } + } + + response + end + end end end end diff --git a/lib/fog/aws/requests/elb/describe_load_balancers.rb b/lib/fog/aws/requests/elb/describe_load_balancers.rb index 83f9828ae..fb4f6b377 100644 --- a/lib/fog/aws/requests/elb/describe_load_balancers.rb +++ b/lib/fog/aws/requests/elb/describe_load_balancers.rb @@ -57,9 +57,9 @@ module Fog lb_names = [*lb_names] load_balancers = if lb_names.any? lb_names.map do |lb_name| - lb = self.data[:load_balancers].find { |name, data| data if name == lb_name } + lb = self.data[:load_balancers].find { |name, data| name == lb_name } raise Fog::AWS::ELB::NotFound unless lb - lb + lb[1] end.compact else self.data[:load_balancers].values diff --git a/lib/fog/aws/requests/elb/disable_availability_zones_for_load_balancer.rb b/lib/fog/aws/requests/elb/disable_availability_zones_for_load_balancer.rb index 2d9ef0059..d3d09376e 100644 --- a/lib/fog/aws/requests/elb/disable_availability_zones_for_load_balancer.rb +++ b/lib/fog/aws/requests/elb/disable_availability_zones_for_load_balancer.rb @@ -17,7 +17,7 @@ module Fog # * 'ResponseMetadata'<~Hash>: # * 'RequestId'<~String> - Id of request # * 'DisableAvailabilityZonesForLoadBalancerResult'<~Hash>: - # * 'AvailabilityZones'<~Array> - array of strings describing instances currently enabled + # * 'AvailabilityZones'<~Array> - A list of updated Availability Zones for the LoadBalancer. def disable_availability_zones_for_load_balancer(availability_zones, lb_name) params = Fog::AWS.indexed_param('AvailabilityZones.member', [*availability_zones]) request({ @@ -32,15 +32,28 @@ module Fog end class Mock - def disable_availability_zones_for_load_balancer(availability_zones, lb_name) - Fog::Mock.not_implemented + raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name] + + response = Excon::Response.new + response.status = 200 + + load_balancer['AvailabilityZones'].delete_if { |az| availability_zones.include? az } + + response.body = { + 'ResponseMetadata' => { + 'RequestId' => Fog::AWS::Mock.request_id + }, + 'DisableAvailabilityZonesForLoadBalancerResult' => { + 'AvailabilityZones' => load_balancer['AvailabilityZones'] + } + } + + response end alias :disable_zones :disable_availability_zones_for_load_balancer - end - end end end diff --git a/lib/fog/aws/requests/elb/enable_availability_zones_for_load_balancer.rb b/lib/fog/aws/requests/elb/enable_availability_zones_for_load_balancer.rb index 0faf24bb2..056f6a07e 100644 --- a/lib/fog/aws/requests/elb/enable_availability_zones_for_load_balancer.rb +++ b/lib/fog/aws/requests/elb/enable_availability_zones_for_load_balancer.rb @@ -34,13 +34,28 @@ module Fog class Mock def enable_availability_zones_for_load_balancer(availability_zones, lb_name) - Fog::Mock.not_implemented + raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name] + + response = Excon::Response.new + response.status = 200 + + load_balancer['AvailabilityZones'] << availability_zones + load_balancer['AvailabilityZones'].flatten!.uniq! + + response.body = { + 'ResponseMetadata' => { + 'RequestId' => Fog::AWS::Mock.request_id + }, + 'EnableAvailabilityZonesForLoadBalancerResult' => { + 'AvailabilityZones' => load_balancer['AvailabilityZones'] + } + } + + response end alias :enable_zones :enable_availability_zones_for_load_balancer - end - end end end diff --git a/lib/fog/aws/requests/elb/register_instances_with_load_balancer.rb b/lib/fog/aws/requests/elb/register_instances_with_load_balancer.rb index 58286b59e..85c9f9519 100644 --- a/lib/fog/aws/requests/elb/register_instances_with_load_balancer.rb +++ b/lib/fog/aws/requests/elb/register_instances_with_load_balancer.rb @@ -33,15 +33,32 @@ module Fog end class Mock - def register_instances_with_load_balancer(instance_ids, lb_name) - Fog::Mock.not_implemented + raise Fog::AWS::ELB::NotFound unless load_balancer = self.data[:load_balancers][lb_name] + instance_ids = [*instance_ids] + instances = instance_ids.map do |instance| + raise Fog::AWS::ELB::InvalidInstance unless Compute[:aws].servers.get(instance) + {'InstanceId' => instance} + end + + response = Excon::Response.new + response.status = 200 + + load_balancer['Instances'] = instances.dup + + response.body = { + 'ResponseMetadata' => { + 'RequestId' => Fog::AWS::Mock.request_id + }, + 'RegisterInstancesWithLoadBalancerResult' => { + 'Instances' => instances + } + } + + response end - alias :register_instances :register_instances_with_load_balancer - end - end end end diff --git a/tests/aws/requests/elb/model_tests.rb b/tests/aws/requests/elb/model_tests.rb index f53ef1e3b..2b56c6029 100644 --- a/tests/aws/requests/elb/model_tests.rb +++ b/tests/aws/requests/elb/model_tests.rb @@ -2,8 +2,6 @@ Shindo.tests('AWS::ELB | models', ['aws', 'elb']) do tests('success') do - pending if Fog.mocking? - tests('load_balancers') do tests('getting a missing elb') do returns(nil) { AWS[:elb].load_balancers.get('no-such-elb') } @@ -44,7 +42,7 @@ Shindo.tests('AWS::ELB | models', ['aws', 'elb']) do raises(Fog::AWS::ELB::InvalidInstance) { elb.deregister_instances('i-00000000') } end - server = Compute[:aws].servers.create + server = Fog::Compute[:aws].servers.create tests('register instance') do begin elb.register_instances(server.id) @@ -183,5 +181,4 @@ Shindo.tests('AWS::ELB | models', ['aws', 'elb']) do elb.destroy end end - end