1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #2646 from evertrue/implement_replaceroute

Implement replaceroute
This commit is contained in:
Wesley Beary 2014-02-07 08:37:37 -06:00
commit 4dc0f8dca6
3 changed files with 148 additions and 2 deletions

View file

@ -140,6 +140,7 @@ module Fog
request :release_address
request :replace_network_acl_association
request :replace_network_acl_entry
request :replace_route
request :register_image
request :request_spot_instances
request :reset_network_interface_attribute

View file

@ -0,0 +1,84 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/basic'
# Replaces a route in a route table within a VPC.
#
# ==== Parameters
# * RouteTableId<~String> - The ID of the route table for the route.
# * options<~Hash>:
# * DestinationCidrBlock<~String> - The CIDR address block used for the destination match. Routing decisions are based on the most specific match.
# * GatewayId<~String> - The ID of an Internet gateway attached to your VPC.
# * InstanceId<~String> - The ID of a NAT instance in your VPC. The operation fails if you specify an instance ID unless exactly one network interface is attached.
# * NetworkInterfaceId<~String> - The ID of a network interface.
#
# === Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of the request
# * 'return'<~Boolean> - Returns true if the request succeeds. Otherwise, returns an error.
#
# {Amazon API Reference}[http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-ReplaceRoute.html]
def replace_route(route_table_id, destination_cidr_block, options = {})
options['DestinationCidrBlock'] ||= destination_cidr_block
request({
'Action' => 'ReplaceRoute',
'RouteTableId' => route_table_id,
:idempotent => true,
:parser => Fog::Parsers::Compute::AWS::Basic.new
}.merge!(options))
end
end
class Mock
def replace_route(route_table_id, destination_cidr_block, options = {})
options['instanceOwnerId'] ||= nil
options['DestinationCidrBlock'] ||= destination_cidr_block
route_table = self.data[:route_tables].find { |routetable| routetable["routeTableId"].eql? route_table_id }
if !route_table.nil? && destination_cidr_block
if !options['gatewayId'].nil? || !options['instanceId'].nil? || !options['networkInterfaceId'].nil?
if !options['gatewayId'].nil? && self.internet_gateways.all('internet-gateway-id'=>options['gatewayId']).first.nil?
raise Fog::Compute::AWS::NotFound.new("The gateway ID '#{options['gatewayId']}' does not exist")
elsif !options['instanceId'].nil? && self.servers.all('instance-id'=>options['instanceId']).first.nil?
raise Fog::Compute::AWS::NotFound.new("The instance ID '#{options['instanceId']}' does not exist")
elsif !options['networkInterfaceId'].nil? && self.network_interfaces.all('networkInterfaceId'=>options['networkInterfaceId']).first.nil?
raise Fog::Compute::AWS::NotFound.new("The networkInterface ID '#{options['networkInterfaceId']}' does not exist")
elsif route_table['routeSet'].find { |route| route['destinationCidrBlock'].eql? destination_cidr_block }.nil?
raise Fog::Compute::AWS::Error, "RouteAlreadyExists => The route identified by #{destination_cidr_block} doesn't exist."
else
response = Excon::Response.new
route_set = route_table['routeSet'].find { |routeset| routeset['destinationCidrBlock'].eql? destination_cidr_block }
route_set.merge!(options)
route_set['state'] = 'pending'
route_set['origin'] = 'ReplaceRoute'
response.status = 200
response.body = {
'requestId'=> Fog::AWS::Mock.request_id,
'return' => true
}
response
end
else
message = 'MissingParameter => '
message << 'The request must contain either a gateway id, a network interface id, or an instance id'
raise Fog::Compute::AWS::Error.new(message)
end
elsif route_table.nil?
raise Fog::Compute::AWS::NotFound.new("The routeTable ID '#{route_table_id}' does not exist")
elsif destination_cidr_block.empty?
raise Fog::Compute::AWS::InvalidParameterValue.new("Value () for parameter destinationCidrBlock is invalid. This is not a valid CIDR block.")
end
end
end
end
end
end

View file

@ -2,7 +2,7 @@ Shindo.tests('Fog::Compute[:aws] | route table requests', ['aws']) do
@route_table_format = {
'routeTable' => [{
'routeSet' => [{
'routeSet' => [{
'destinationCidrBlock' => String,
'gatewayId' => String,
'state' => String,
@ -46,6 +46,7 @@ Shindo.tests('Fog::Compute[:aws] | route table requests', ['aws']) do
@subnet_id = Fog::Compute[:aws].create_subnet(vpc.id, '10.0.10.0/24').body['subnet']['subnetId']
@network_interface = Fog::Compute[:aws].create_network_interface(@subnet_id, {"PrivateIpAddress" => "10.0.10.23"}).body
@internet_gateway_id = Fog::Compute[:aws].create_internet_gateway.body['internetGatewaySet'].first['internetGatewayId']
@alt_internet_gateway_id = Fog::Compute[:aws].create_internet_gateway.body['internetGatewaySet'].first['internetGatewayId']
@network_interface_id = @network_interface['networkInterface']['networkInterfaceId']
key_name = 'fog-test-key'
key = Fog::Compute[:aws].key_pairs.create(:name => key_name)
@ -91,6 +92,26 @@ Shindo.tests('Fog::Compute[:aws] | route table requests', ['aws']) do
Fog::Compute[:aws].create_route(@route_table_id, '10.0.10.0/21', nil, nil, @network_interface_id).body
end
# Tests replace_route
# - using internet gateway
# - using instance id
# - using network interface
#
Fog::Compute[:aws].attach_internet_gateway(@alt_internet_gateway_id, vpc.id).body
tests("#replace_route('#{@route_table_id}', '#{@destination_cidr_block}', {'gatewayId' => '#{@alt_internet_gateway_id}'})").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].replace_route(@route_table_id, @destination_cidr_block, {'gatewayId' => @alt_internet_gateway_id}).body
end
instance = Fog::Compute[:aws].servers.create(:image_id => @ami, :flavor_id => 't1.micro', :key_name => 'fog-test-key', :subnet_id => @subnet_id)
instance.wait_for { state.eql? "running" }
tests("#replace_route('#{@route_table_id}', '10.0.10.0/22', {'instanceId' => '#{instance.id}'})").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].replace_route(@route_table_id, '10.0.10.0/22', {'instanceId' => instance.id}).body
end
tests("#replace_route('#{@route_table_id}', '10.0.10.0/21', {'networkInterfaceId' => '#{@network_interface_id}'})").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].replace_route(@route_table_id, '10.0.10.0/21', {'networkInterfaceId' => @network_interface_id}).body
end
# Tests describe_route_tables
# - no parameters
# - filter: vpc-id => vpc_id
@ -212,6 +233,46 @@ Shindo.tests('Fog::Compute[:aws] | route table requests', ['aws']) do
end
end
# Tests replace_route
# - no parameters
# - passing a nonexisiting route table and an exisiting internet gateway
# - passing a nonexisiting route table
# - passing a nonexisting route table and an exisiting instance
# - passing a nonexisiting instance
# - passing a nonexsiting route table and an exisiting network interface
# - passing a nonexisiting network interface
# - attempting to add a route at a less specific destination cidr block
#
tests('#replace_route').raises(ArgumentError) do
Fog::Compute[:aws].replace_route
end
tests("#replace_route('rtb-00000000', '#{@destination_cidr_block}', {'internetGatewayId' => '#{@internet_gateway_id}'})").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].replace_route('rtb-00000000', @destination_cidr_block, {'internetGatewayId' => @internet_gateway_id})
end
tests("#replace_route('rtb-00000000', '#{@destination_cidr_block}')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].replace_route('rtb-00000000', @destination_cidr_block)
end
tests("#replace_route('#{@route_table_id}', '#{@destination_cidr_block}', {'gatewayId' => 'igw-00000000'})").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].replace_route(@route_table_id, @destination_cidr_block, {'gatewayId' => 'igw-00000000'})
end
tests("#replace_route('rtb-00000000', '#{@destination_cidr_block}', {'instanceId' => '#{instance.id}'})").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].replace_route('rtb-00000000', @destination_cidr_block, {'instanceId' => instance.id})
end
tests("#replace_route('#{@route_table_id}', '#{@destination_cidr_block}', {'instanceId' => 'i-00000000'})").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].replace_route(@route_table_id, @destination_cidr_block, {'instanceId' => 'i-00000000'})
end
tests("#replace_route('#{@route_table_id}', '#{@destination_cidr_block}', {'networkInterfaceId' => 'eni-00000000'})").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].replace_route(@route_table_id, @destination_cidr_block, {'networkInterfaceId' => 'eni-00000000'})
end
tests("#replace_route('rtb-00000000', '#{@destination_cidr_block}', {'networkInterfaceId' => '#{@network_interface_id}'})").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].replace_route('rtb-00000000', @destination_cidr_block, {'networkInterfaceId' => @network_interface_id})
end
if !Fog.mocking?
tests("#replace_route less specific destination_cidr_block").raises(Fog::Compute::AWS::Error) do
Fog::Compute[:aws].replace_route(@route_table_id, '10.0.10.0/25', {'gatewayId' => @internet_gateway_id})
end
end
# Test describe_route_tables
# - passing a nonexisiting vpc
#
@ -267,7 +328,7 @@ Shindo.tests('Fog::Compute[:aws] | route table requests', ['aws']) do
Fog::Compute[:aws].disassociate_route_table(@association_id)
Fog::Compute[:aws].delete_route_table(@route_table_id)
end
Fog::Compute[:aws].delete_network_interface(@network_interface_id)
Fog::Compute[:aws].detach_internet_gateway(@internet_gateway_id, vpc.id)
Fog::Compute[:aws].delete_internet_gateway(@internet_gateway_id)