1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/aws/requests/compute/associate_address.rb

101 lines
4.6 KiB
Ruby
Raw Normal View History

2010-03-16 18:46:21 -04:00
module Fog
module Compute
class AWS
2010-03-16 18:46:21 -04:00
class Real
2009-08-24 23:59:19 -04:00
2012-02-20 16:16:52 -05:00
require 'fog/aws/parsers/compute/associate_address'
2009-08-24 23:59:19 -04:00
# Associate an elastic IP address with an instance
#
# ==== Parameters
2012-02-20 16:16:52 -05:00
# * instance_id<~String> - Id of instance to associate address with (conditional)
# * public_ip<~String> - Public ip to assign to instance (conditional)
# * network_interface_id<~String> - Id of a nic to associate address with (required in a vpc instance with more than one nic) (conditional)
# * allocation_id<~String> - Allocation Id to associate address with (vpc only) (conditional)
2009-08-24 23:59:19 -04:00
#
# ==== Returns
# * response<~Excon::Response>:
2009-08-24 23:59:19 -04:00
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'return'<~Boolean> - success?
2012-02-20 16:16:52 -05:00
# * 'associationId'<~String> - association Id for eip to node (vpc only)
#
# {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-AssociateAddress.html]
2012-02-20 16:16:52 -05:00
def associate_address(instance_id=nil, public_ip=nil, network_interface_id=nil, allocation_id=nil)
# Cannot specify an allocation ip and a public IP at the same time. If you have an allocation Id presumably you are in a VPC
# so we will null out the public IP
public_ip = allocation_id.nil? ? public_ip : nil
request(
2012-02-20 16:16:52 -05:00
'Action' => 'AssociateAddress',
'AllocationId' => allocation_id,
'InstanceId' => instance_id,
'NetworkInterfaceId' => network_interface_id,
'PublicIp' => public_ip,
:idempotent => true,
:parser => Fog::Parsers::Compute::AWS::AssociateAddress.new
)
2009-08-24 23:59:19 -04:00
end
2009-07-23 01:25:24 -04:00
end
2010-03-16 18:46:21 -04:00
class Mock
2009-08-24 23:59:19 -04:00
2012-02-20 16:16:52 -05:00
def associate_address(instance_id=nil, public_ip=nil, network_interface_id=nil, allocation_id=nil)
public_ip = allocation_id.nil? ? public_ip : nil
2009-11-20 14:08:08 -05:00
response = Excon::Response.new
2009-08-24 23:59:19 -04:00
response.status = 200
2011-05-19 18:35:33 -04:00
instance = self.data[:instances][instance_id]
2012-02-20 16:16:52 -05:00
address = public_ip.nil? ? nil : self.data[:addresses][public_ip]
if ((instance && address) || (instance && !allocation_id.nil?) || (!allocation_id.nil? && !network_interface_id.nil?))
if !allocation_id.nil?
allocation_ip = describe_addresses( 'allocation-id' => "#{allocation_id}").body['addressesSet'].first
if !allocation_ip.nil?
public_ip = allocation_ip['publicIp']
end
2012-02-20 16:16:52 -05:00
end
if !address.nil?
if current_instance = self.data[:instances][address['instanceId']]
current_instance['ipAddress'] = current_instance['originalIpAddress']
end
address['instanceId'] = instance_id
end
# detach other address (if any)
if self.data[:addresses][instance['ipAddress']]
self.data[:addresses][instance['ipAddress']]['instanceId'] = nil
end
if !public_ip.nil?
instance['ipAddress'] = public_ip
instance['dnsName'] = Fog::AWS::Mock.dns_name_for(public_ip)
end
2009-08-24 23:59:19 -04:00
response.status = 200
2012-02-20 16:16:52 -05:00
if !instance_id.nil? && !public_ip.nil?
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true
}
elsif !allocation_id.nil?
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true,
'associationId' => Fog::AWS::Mock.request_id
}
end
response
2012-02-20 16:16:52 -05:00
#elsif ! network_interface_id.nil? && allocation_id.nil?
# raise Fog::Compute::AWS::NotFound.new("You must specify an AllocationId when specifying a NetworkInterfaceID")
#elsif instance.nil? && network_interface_id.nil?
# raise Fog::Compute::AWS::Error.new("You must specify either an InstanceId or a NetworkInterfaceID")
#elsif !instance && !network_interface_id
# raise Fog::Compute::AWS::Error.new(" 2 You must specify either an InstanceId or a NetworkInterfaceID")
elsif !instance
2012-02-20 16:16:52 -05:00
raise Fog::Compute::AWS::NotFound.new("You must specify either an InstanceId or a NetworkInterfaceID")
elsif !address
raise Fog::Compute::AWS::Error.new("AuthFailure => The address '#{public_ip}' does not belong to you.")
2009-08-24 23:59:19 -04:00
end
end
end
2009-07-23 01:25:24 -04:00
end
end
2009-08-24 23:59:19 -04:00
end