def associate_address(instance_id=nil, public_ip=nil, network_interface_id=nil, allocation_id=nil)
public_ip = allocation_id.nil? ? public_ip : nil
response = Excon::Response.new
response.status = 200
instance = self.data[:instances][instance_id]
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
if current_instance = self.data[:instances][address['instanceId']]
current_instance['ipAddress'] = current_instance['originalIpAddress']
end
address['instanceId'] = instance_id
if self.data[:addresses][instance['ipAddress']]
self.data[:addresses][instance['ipAddress']]['instanceId'] = nil
end
instance['ipAddress'] = public_ip
instance['dnsName'] = Fog::AWS::Mock.dns_name_for(public_ip)
response.status = 200
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
elsif !instance
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.")
end
end