mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #756 from estonfer/associate_vpc_address
associate EIPs in a vpc
This commit is contained in:
commit
7e28669298
6 changed files with 88 additions and 32 deletions
|
@ -6,9 +6,11 @@ module Fog
|
|||
|
||||
class Address < Fog::Model
|
||||
|
||||
identity :public_ip, :aliases => 'publicIp'
|
||||
identity :public_ip, :aliases => 'publicIp'
|
||||
|
||||
attribute :server_id, :aliases => 'instanceId'
|
||||
attribute :allocation_id, :aliases => 'allocationId'
|
||||
attribute :server_id, :aliases => 'instanceId'
|
||||
attribute :network_interface_id, :aliases => 'networkInterfaceId'
|
||||
attribute :domain
|
||||
|
||||
def initialize(attributes = {})
|
||||
|
@ -51,7 +53,7 @@ module Fog
|
|||
else
|
||||
@server = nil
|
||||
self.server_id = new_server.id
|
||||
connection.associate_address(server_id, public_ip)
|
||||
connection.associate_address(server_id, public_ip, network_interface_id, allocation_id)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
25
lib/fog/aws/parsers/compute/associate_address.rb
Normal file
25
lib/fog/aws/parsers/compute/associate_address.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module Compute
|
||||
module AWS
|
||||
|
||||
class AssociateAddress < Fog::Parsers::Base
|
||||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'requestId', 'associationId'
|
||||
@response[name] = value
|
||||
when 'return'
|
||||
if value == 'true'
|
||||
@response[name] = true
|
||||
else
|
||||
@response[name] = false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -12,7 +12,7 @@ module Fog
|
|||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'instanceId', 'publicIp', 'domain', 'allocationId'
|
||||
when 'instanceId', 'publicIp', 'domain', 'allocationId', 'associationId'
|
||||
@address[name] = value
|
||||
when 'item'
|
||||
@response['addressesSet'] << @address
|
||||
|
|
|
@ -3,28 +3,36 @@ module Fog
|
|||
class AWS
|
||||
class Real
|
||||
|
||||
require 'fog/aws/parsers/compute/basic'
|
||||
require 'fog/aws/parsers/compute/associate_address'
|
||||
|
||||
# Associate an elastic IP address with an instance
|
||||
#
|
||||
# ==== Parameters
|
||||
# * instance_id<~String> - Id of instance to associate address with
|
||||
# * public_ip<~String> - Public ip to assign to instance
|
||||
# * 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)
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Hash>:
|
||||
# * 'requestId'<~String> - Id of request
|
||||
# * 'return'<~Boolean> - success?
|
||||
# * 'associationId'<~String> - association Id for eip to node (vpc only)
|
||||
#
|
||||
# {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-AssociateAddress.html]
|
||||
def associate_address(instance_id, public_ip)
|
||||
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(
|
||||
'Action' => 'AssociateAddress',
|
||||
'InstanceId' => instance_id,
|
||||
'PublicIp' => public_ip,
|
||||
:idempotent => true,
|
||||
:parser => Fog::Parsers::Compute::AWS::Basic.new
|
||||
'Action' => 'AssociateAddress',
|
||||
'AllocationId' => allocation_id,
|
||||
'InstanceId' => instance_id,
|
||||
'NetworkInterfaceId' => network_interface_id,
|
||||
'PublicIp' => public_ip,
|
||||
:idempotent => true,
|
||||
:parser => Fog::Parsers::Compute::AWS::AssociateAddress.new
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -32,12 +40,17 @@ module Fog
|
|||
|
||||
class Mock
|
||||
|
||||
def associate_address(instance_id, public_ip)
|
||||
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 = self.data[:addresses][public_ip]
|
||||
if instance && address
|
||||
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
|
||||
|
@ -49,13 +62,27 @@ module Fog
|
|||
instance['ipAddress'] = public_ip
|
||||
instance['dnsName'] = Fog::AWS::Mock.dns_name_for(public_ip)
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'requestId' => Fog::AWS::Mock.request_id,
|
||||
'return' => true
|
||||
}
|
||||
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 ! 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
|
||||
raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_id}' does not exist")
|
||||
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
|
||||
|
|
|
@ -9,6 +9,7 @@ module Fog
|
|||
#
|
||||
# ==== Parameters
|
||||
# * public_ip<~String> - Public ip to assign to instance
|
||||
# * association_id<~String> - Id associating eip to an network interface
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
|
@ -17,12 +18,13 @@ module Fog
|
|||
# * 'return'<~Boolean> - success?
|
||||
#
|
||||
# {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DisassociateAddress.html]
|
||||
def disassociate_address(public_ip)
|
||||
def disassociate_address(public_ip=nil, association_id=nil)
|
||||
request(
|
||||
'Action' => 'DisassociateAddress',
|
||||
'PublicIp' => public_ip,
|
||||
:idempotent => true,
|
||||
:parser => Fog::Parsers::Compute::AWS::Basic.new
|
||||
'Action' => 'DisassociateAddress',
|
||||
'PublicIp' => public_ip,
|
||||
'AssociationId' => association_id,
|
||||
:idempotent => true,
|
||||
:parser => Fog::Parsers::Compute::AWS::Basic.new
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -2,14 +2,14 @@ Shindo.tests('Fog::Compute[:aws] | address requests', ['aws']) do
|
|||
|
||||
@addresses_format = {
|
||||
'addressesSet' => [{
|
||||
'allocationId' => Fog::Nullable::String,
|
||||
'domain' => String,
|
||||
'instanceId' => Fog::Nullable::String,
|
||||
'publicIp' => String
|
||||
'allocationId' => Fog::Nullable::String,
|
||||
'associationId' => Fog::Nullable::String,
|
||||
'domain' => String,
|
||||
'instanceId' => Fog::Nullable::String,
|
||||
'publicIp' => String
|
||||
}],
|
||||
'requestId' => String
|
||||
}
|
||||
|
||||
@server = Fog::Compute[:aws].servers.create
|
||||
@server.wait_for { ready? }
|
||||
@ip_address = @server.public_ip_address
|
||||
|
@ -35,7 +35,7 @@ Shindo.tests('Fog::Compute[:aws] | address requests', ['aws']) do
|
|||
tests("#associate_addresses('#{@server.identity}', '#{@public_ip}')").formats(AWS::Compute::Formats::BASIC) do
|
||||
Fog::Compute[:aws].associate_address(@server.identity, @public_ip).body
|
||||
end
|
||||
|
||||
|
||||
tests("#dissassociate_address('#{@public_ip}')").formats(AWS::Compute::Formats::BASIC) do
|
||||
Fog::Compute[:aws].disassociate_address(@public_ip).body
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue