2010-03-16 18:46:21 -04:00
|
|
|
module Fog
|
2011-06-16 19:28:54 -04:00
|
|
|
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'
|
2011-02-22 22:05:14 -05:00
|
|
|
|
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
|
2009-11-02 21:48:49 -05:00
|
|
|
# * 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)
|
2011-05-19 12:31:56 -04:00
|
|
|
#
|
|
|
|
# {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
|
2010-03-16 01:15:33 -04:00
|
|
|
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
|
2010-03-16 01:15:33 -04:00
|
|
|
)
|
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']
|
|
|
|
public_ip = allocation_ip['publicIp']
|
|
|
|
end
|
2011-08-16 19:13:56 -04:00
|
|
|
if current_instance = self.data[:instances][address['instanceId']]
|
|
|
|
current_instance['ipAddress'] = current_instance['originalIpAddress']
|
|
|
|
end
|
2009-08-25 21:43:30 -04:00
|
|
|
address['instanceId'] = instance_id
|
2011-05-27 15:22:27 -04:00
|
|
|
# detach other address (if any)
|
2011-08-16 19:13:56 -04:00
|
|
|
if self.data[:addresses][instance['ipAddress']]
|
|
|
|
self.data[:addresses][instance['ipAddress']]['instanceId'] = nil
|
2011-05-27 15:22:27 -04:00
|
|
|
end
|
2010-05-13 16:59:33 -04:00
|
|
|
instance['ipAddress'] = public_ip
|
|
|
|
instance['dnsName'] = Fog::AWS::Mock.dns_name_for(public_ip)
|
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
|
2010-05-26 01:26:20 -04:00
|
|
|
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")
|
2010-05-26 01:26:20 -04:00
|
|
|
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")
|
2010-05-26 01:26:20 -04:00
|
|
|
elsif !address
|
2011-06-16 19:28:54 -04:00
|
|
|
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
|