From 7d70746c2309d39f5c0835a7441e435404089742 Mon Sep 17 00:00:00 2001 From: Wesley Beary Date: Fri, 14 Aug 2009 11:28:57 -0700 Subject: [PATCH] finishing up address mocks --- lib/fog/aws/ec2.rb | 2 +- lib/fog/aws/requests/ec2/allocate_address.rb | 5 +- .../aws/requests/ec2/describe_addresses.rb | 76 ++++++++++++++----- lib/fog/aws/requests/ec2/release_address.rb | 6 +- 4 files changed, 62 insertions(+), 27 deletions(-) diff --git a/lib/fog/aws/ec2.rb b/lib/fog/aws/ec2.rb index bf8038e90..bfdd7d849 100644 --- a/lib/fog/aws/ec2.rb +++ b/lib/fog/aws/ec2.rb @@ -175,7 +175,7 @@ module Fog @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}") if Fog.mocking? - @data = { :addresses => [] } + @data = { 'addressesSet' => [] } end end diff --git a/lib/fog/aws/requests/ec2/allocate_address.rb b/lib/fog/aws/requests/ec2/allocate_address.rb index 29ea13e22..6251af658 100644 --- a/lib/fog/aws/requests/ec2/allocate_address.rb +++ b/lib/fog/aws/requests/ec2/allocate_address.rb @@ -31,9 +31,10 @@ else response = Fog::Response.new response.status = 200 data ={ - 'publicIp' => Fog::AWS::Mock.ip_address + 'instanceId' => '', + 'publicIp' => Fog::AWS::Mock.ip_address } - @data[:addresses] << data + @data['addressesSet'] << data response.body = { 'publicIp' => data['publicIp'], 'requestId' => Fog::AWS::Mock.request_id diff --git a/lib/fog/aws/requests/ec2/describe_addresses.rb b/lib/fog/aws/requests/ec2/describe_addresses.rb index 3413d4d0f..6e3af35c5 100644 --- a/lib/fog/aws/requests/ec2/describe_addresses.rb +++ b/lib/fog/aws/requests/ec2/describe_addresses.rb @@ -1,26 +1,60 @@ -module Fog - module AWS - class EC2 +unless Fog.mocking? + + module Fog + module AWS + class EC2 + + # Describe all or specified IP addresses. + # + # ==== Parameters + # * public_ip<~Array> - List of ips to describe, defaults to all + # + # ==== Returns + # * response<~Fog::AWS::Response>: + # * body<~Hash>: + # * 'requestId'<~String> - Id of request + # * 'addressesSet'<~Array>: + # * 'instanceId'<~String> - instance for ip address + # * 'publicIp'<~String> - ip address for instance + def describe_addresses(public_ip = []) + params = indexed_params('PublicIp', public_ip) + request({ + 'Action' => 'DescribeAddresses' + }.merge!(params), Fog::Parsers::AWS::EC2::DescribeAddresses.new) + end - # Describe all or specified IP addresses. - # - # ==== Parameters - # * public_ip<~Array> - List of ips to describe, defaults to all - # - # ==== Returns - # * response<~Fog::AWS::Response>: - # * body<~Hash>: - # * 'requestId'<~String> - Id of request - # * 'addressesSet'<~Array>: - # * 'instanceId'<~String> - instance for ip address - # * 'publicIp'<~String> - ip address for instance - def describe_addresses(public_ip = []) - params = indexed_params('PublicIp', public_ip) - request({ - 'Action' => 'DescribeAddresses' - }.merge!(params), Fog::Parsers::AWS::EC2::DescribeAddresses.new) end - end end + +else + + module Fog + module AWS + class EC2 + + def describe_addresses(public_ip = []) + public_ip = [*public_ip] + response = Fog::Response.new + if public_ip != [] + addresses_set = @data['addressesSet'].select {|address| public_ip.include?(address['publicIp'])} + else + addresses_set = @data['addressesSet'] + end + if public_ip.length == 0 || public_ip.length == addresses_set.length + response.status = 200 + response.body = { + 'requestId' => Fog::AWS::Mock.request_id, + 'addressesSet' => addresses_set + } + else + response.status = 400 + end + response + end + + end + end + end + end diff --git a/lib/fog/aws/requests/ec2/release_address.rb b/lib/fog/aws/requests/ec2/release_address.rb index 47017ad8c..7dcae0e37 100644 --- a/lib/fog/aws/requests/ec2/release_address.rb +++ b/lib/fog/aws/requests/ec2/release_address.rb @@ -30,9 +30,9 @@ else def release_address(public_ip) response = Fog::Response.new - initial_addresses_count = @data[:addresses].length - @data[:addresses].delete_if {|address| address['publicIp'] == public_ip} - if initial_addresses_count > @data[:addresses].length + initial_addresses_count = @data['addressesSet'].length + @data['addressesSet'].delete_if {|address| address['publicIp'] == public_ip} + if initial_addresses_count > @data['addressesSet'].length response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id,