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

Merge pull request #2077 from rbclark/awsroutes-topull

[aws|compute] add route table support for VPC's
This commit is contained in:
Eric Stonfer 2013-08-27 22:19:15 -07:00
commit 33f5250146
17 changed files with 1154 additions and 1 deletions

View file

@ -26,6 +26,8 @@ module Fog
collection :key_pairs
model :network_interface
collection :network_interfaces
model :route_table
collection :route_tables
model :security_group
collection :security_groups
model :server
@ -48,6 +50,7 @@ module Fog
request :associate_address
request :associate_dhcp_options
request :attach_network_interface
request :associate_route_table
request :attach_internet_gateway
request :attach_volume
request :authorize_security_group_ingress
@ -58,6 +61,8 @@ module Fog
request :create_key_pair
request :create_network_interface
request :create_placement_group
request :create_route
request :create_route_table
request :create_security_group
request :create_snapshot
request :create_spot_datafeed_subscription
@ -73,6 +78,8 @@ module Fog
request :delete_network_interface
request :delete_security_group
request :delete_placement_group
request :delete_route
request :delete_route_table
request :delete_snapshot
request :delete_spot_datafeed_subscription
request :delete_subnet
@ -92,6 +99,7 @@ module Fog
request :describe_key_pairs
request :describe_network_interface_attribute
request :describe_network_interfaces
request :describe_route_tables
request :describe_placement_groups
request :describe_regions
request :describe_reserved_instances_offerings
@ -109,6 +117,7 @@ module Fog
request :detach_internet_gateway
request :detach_volume
request :disassociate_address
request :disassociate_route_table
request :get_console_output
request :get_password_data
request :import_key_pair
@ -215,6 +224,7 @@ module Fog
:vpcs => [],
:dhcp_options => [],
:internet_gateways => [],
:route_tables => [],
:account_attributes => [
{
"values" => ["5"],

View file

@ -9,6 +9,7 @@ module Fog
identity :public_ip, :aliases => 'publicIp'
attribute :allocation_id, :aliases => 'allocationId'
attribute :association_id, :aliases => 'associationId'
attribute :server_id, :aliases => 'instanceId'
attribute :network_interface_id, :aliases => 'networkInterfaceId'
attribute :domain

View file

@ -0,0 +1,69 @@
require 'fog/core/model'
module Fog
module Compute
class AWS
class RouteTable < Fog::Model
identity :id, :aliases => 'routeTableId'
attribute :vpc_id, :aliases => 'vpcId'
attribute :routes, :aliases => 'routeSet'
attribute :associations, :aliases => 'associationSet'
attribute :tags, :aliases => 'tagSet'
def initialize(attributes={})
super
end
# Remove an existing route table
#
# route_tables.destroy
#
# ==== Returns
#
# True or false depending on the result
#
def destroy
requires :id
service.delete_route_table (id)
true
end
# Create a route table
#
# >> routetable = connection.route_tables.new
# >> routetable.save
#
# == Returns:
#
# True or an exception depending on the result. Keep in mind that this *creates* a new route table.
#
def save
requires :vpc_id
data = service.create_route_table(vpc_id).body['routeTable'].first
new_attributes = data.reject {|key,value| key == 'requestId'}
merge_attributes(new_attributes)
true
end
private
def associationSet=(new_association_set)
merge_attributes(new_association_set.first || {})
end
def routeSet=(new_route_set)
merge_attributes(new_route_set || {})
end
end
end
end
end

View file

@ -0,0 +1,92 @@
require 'fog/core/collection'
require 'fog/aws/models/compute/route_table'
module Fog
module Compute
class AWS
class RouteTables < Fog::Collection
attribute :filters
model Fog::Compute::AWS::RouteTable
# Creates a new route table
#
# AWS.route_tables.new
#
# ==== Returns
#
# Returns the details of the new route table
#
#>> AWS.route_tables.new
# <Fog::Compute::AWS::RouteTable
# id=nil,
# vpc_id=nil,
# routes=nil,
# associations=nil,
# tags=nil
# >
#
def initialize(attributes)
self.filters ||= {}
super
end
# Returns an array of all route tables that have been created
#
# AWS.route_tables.all
#
# ==== Returns
#
# Returns an array of all route tables
#
#>> AWS.route_tables.all
# <Fog::Compute::AWS::RouteTables
# filters={}
# [
# <Fog::Compute::AWS::RouteTable
# id="rtb-41e8552f",
# TODO
# >
# ]
# >
#
def all(filters = filters)
unless filters.is_a?(Hash)
Fog::Logger.warning("all with #{filters.class} param is deprecated, use all('route-table-id' => []) instead [light_black](#{caller.first})[/]")
filters = {'route-table-id' => [*filters]}
end
self.filters = filters
data = service.describe_route_tables(filters).body
load(data['routeTableSet'])
end
# Used to retrieve a route table
# route_table_id is required to get the associated route table information.
#
# You can run the following command to get the details:
# AWS.route_tables.get("rtb-41e8552f")
#
# ==== Returns
#
#>> AWS.route_tables.get("rtb-41e8552f")
# <Fog::Compute::AWS::RouteTable
# id="rtb-41e8552f",
# TODO
# >
#
def get(route_table_id)
if route_table_id
self.class.new(:service => service).all('route-table-id' => route_table_id).first
end
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Parsers
module Compute
module AWS
class AssociateRouteTable < Fog::Parsers::Base
def end_element(name)
case name
when 'requestId', 'associationId'
@response[name] = value
end
end
end
end
end
end
end

View file

@ -0,0 +1,75 @@
module Fog
module Parsers
module Compute
module AWS
class CreateRouteTable < Fog::Parsers::Base
def reset
@in_route_set = false
@in_association_set = false
@route = {}
@association = {}
@route_table = { 'routeSet' => [], 'tagSet' => {}, 'associationSet' => [] }
@response = { 'routeTable' => [] }
@tag = {}
end
def start_element(name, attrs = [])
super
case name
when 'tagSet'
@in_tag_set = true
when 'routeSet'
@in_route_set = true
when 'associationSet'
@in_association_set = true
end
end
def end_element(name)
if @in_tag_set
case name
when 'item'
@route_table['tagSet'][@tag['key']] = @tag['value']
@tag = {}
when 'tagSet'
@in_tag_set = false
end
elsif @in_route_set
case name
when 'routeSet'
@in_route_set = false
when 'destinationCidrBlock', 'gatewayId', 'state'
@route[name] = value
when 'item'
@route_table['routeSet'] << @route
@route = {}
end
elsif @in_association_set
case name
when 'routeTableAssociationId', 'routeTableId', 'main'
@association[name] = value
when 'associationSet'
@route_table['associationSet'] << @association
@in_association_set = false
end
else
case name
when 'routeTableId', 'vpcId'
@route_table[name] = value
when 'routeTable'
@response['routeTable'] << @route_table
@route_table = { 'routeSet' => {}, 'tagSet' => {}, 'associationSet' => {} }
when 'requestId'
@response[name] = value
end
end
end
end
end
end
end
end

View file

@ -0,0 +1,85 @@
module Fog
module Parsers
module Compute
module AWS
class DescribeRouteTables < Fog::Parsers::Base
def reset
@association = { 'routeTableAssociationId' => nil, 'routeTableId' => nil, 'subnetId' => nil, 'main' => false }
@in_association_set = false
@in_route_set = false
@route = { 'destinationCidrBlock' => nil, 'gatewayId' => nil, 'instanceId' => nil, 'instanceOwnerId' => nil, 'networkInterfaceId' => nil, 'state' => nil, 'origin' => nil }
@response = { 'routeTableSet' => [] }
@tag = {}
@route_table = { 'associationSet' => [], 'tagSet' => {}, 'routeSet' => [] }
end
def start_element(name, attrs = [])
super
case name
when 'associationSet'
@in_association_set = true
when 'tagSet'
@in_tag_set = true
when 'routeSet'
@in_route_set = true
end
end
def end_element(name)
if @in_association_set
case name
when 'associationSet'
@in_association_set = false
when 'routeTableAssociationId', 'routeTableId', 'subnetId'
@association[name] = value
when 'main'
if value == 'true'
@association[name] = true
else
@association[name] = false
end
when 'item'
@route_table['associationSet'] << @association
@association = { 'routeTableAssociationId' => nil, 'routeTableId' => nil, 'subnetId' => nil, 'main' => false }
end
elsif @in_tag_set
case name
when 'key', 'value'
@tag[name] = value
when 'item'
@route_table['tagSet'][@tag['key']] = @tag['value']
@tag = {}
when 'tagSet'
@in_tag_set = false
end
elsif @in_route_set
case name
when 'destinationCidrBlock', 'gatewayId', 'instanceId', 'instanceOwnerId', 'networkInterfaceId', 'state', 'origin'
@route[name] = value
when 'item'
@route_table['routeSet'] << @route
@route = { 'destinationCidrBlock' => nil, 'gatewayId' => nil, 'instanceId' => nil, 'instanceOwnerId' => nil, 'networkInterfaceId' => nil, 'state' => nil, 'origin' => nil }
when 'routeSet'
@in_route_set = false
end
else
case name
when 'routeTableId', 'vpcId'
@route_table[name] = value
when 'item'
@response['routeTableSet'] << @route_table
@route_table = { 'associationSet' => [], 'tagSet' => {}, 'routeSet' => [] }
when 'requestId'
@response[name] = value
end
end
end
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Parsers
module Compute
module AWS
class DisassociateRouteTable < Fog::Parsers::Base
def end_element(name)
case name
when 'requestId', 'return'
@response[name] = value
end
end
end
end
end
end
end

View file

@ -0,0 +1,75 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/associate_route_table'
# Associates a subnet with a route table.
#
# ==== Parameters
# * RouteTableId<~String> - The ID of the route table
# * SubnetId<~String> - The ID of the subnet
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - The ID of the request
# * 'associationId'<~String> - The route table association ID (needed to disassociate the route table)
#
# {Amazon API Reference}[http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-AssociateRouteTable.html]
def associate_route_table(routeTableId, subnetId)
request(
'Action' => 'AssociateRouteTable',
'RouteTableId' => routeTableId,
'SubnetId' => subnetId,
:parser => Fog::Parsers::Compute::AWS::AssociateRouteTable.new
)
end
end
class Mock
def associate_route_table(routeTableId, subnetId)
routetable = self.data[:route_tables].find { |routetable| routetable["routeTableId"].eql? routeTableId }
subnet = self.data[:subnets].find { |subnet| subnet["subnetId"].eql? subnetId }
if !routetable.nil? && !subnet.nil?
response = Excon::Response.new
response.status = 200
association = add_route_association(routeTableId, subnetId)
routetable["associationSet"].push(association)
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'associationId' => association['routeTableAssociationId']
}
response
elsif routetable.nil?
raise Fog::Compute::AWS::NotFound.new("The routeTable ID '#{routeTableId}' does not exist")
else
raise Fog::Compute::AWS::NotFound.new("The subnet ID '#{subnetId}' does not exist")
end
end
private
def add_route_association(routeTableId, subnetId, main=nil)
response = {
"routeTableAssociationId" => "rtbassoc-#{Fog::Mock.random_hex(8)}",
"routeTableId" => routeTableId,
"subnetId" => nil,
"main" => false
}
if main
response['main'] = true
else
response['subnetId'] = subnetId
end
response
end
end
end
end
end

View file

@ -0,0 +1,89 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/basic'
# Creates a route in a route table within a VPC.
#
# ==== Parameters
# * RouteTableId<~String> - The ID of the route table for the route.
# * 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> - he 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-CreateRoute.html]
def create_route(route_table_id, destination_cidr_block, internet_gateway_id=nil, instance_id=nil, network_interface_id=nil)
request_vars = {
'Action' => 'CreateRoute',
'RouteTableId' => route_table_id,
'DestinationCidrBlock' => destination_cidr_block,
:parser => Fog::Parsers::Compute::AWS::Basic.new
}
if internet_gateway_id
request_vars['GatewayId'] = internet_gateway_id
elsif instance_id
request_vars['InstanceId'] = instance_id
elsif network_interface_id
request_vars['NetworkInterfaceId'] = network_interface_id
end
request(request_vars)
end
end
class Mock
def create_route(route_table_id, destination_cidr_block, internet_gateway_id=nil, instance_id=nil, network_interface_id=nil)
instance_owner_id = nil
route_table = self.data[:route_tables].find { |routetable| routetable["routeTableId"].eql? route_table_id }
if !route_table.nil? && destination_cidr_block
if !internet_gateway_id.nil? || !instance_id.nil? || !network_interface_id.nil?
if !internet_gateway_id.nil? && self.internet_gateways.all('internet-gateway-id'=>internet_gateway_id).first.nil?
raise Fog::Compute::AWS::NotFound.new("The gateway ID '#{internet_gateway_id}' does not exist")
elsif !instance_id.nil? && self.servers.all('instance-id'=>instance_id).first.nil?
raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_id}' does not exist")
elsif !network_interface_id.nil? && self.network_interfaces.all('networkInterfaceId'=>network_interface_id).first.nil?
raise Fog::Compute::AWS::NotFound.new("The networkInterface ID '#{network_interface_id}' 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} already exists."
else
response = Excon::Response.new
route_table['routeSet'].push({
"destinationCidrBlock" => destination_cidr_block,
"gatewayId" => internet_gateway_id,
"instanceId"=>instance_id,
"instanceOwnerId"=>instance_owner_id,
"networkInterfaceId"=>network_interface_id,
"state" => "pending",
"origin" => "CreateRoute"
})
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

@ -0,0 +1,70 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/create_route_table'
# Creates a route table for the specified VPC.
#
# ==== Parameters
# * VpcId<~String> - The ID of the VPC.
#
# === Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of the request
# * 'routeTable'<~Array> - Information about the newly created route table
# * 'routeTableId'<~String>
# * 'vpcId'<~String>
# * 'routeSet'<~Array>
# * 'item'<~Array>
# * 'destinationCidrBlock'<~String> - The CIDR address block used for the destination match.
# * 'gatewayId'<~String> - The ID of an Internet gateway attached to your VPC.
# * 'state'<~String> - The state of the route. ['blackhole', 'available']
#
# {Amazon API Reference}[http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-CreateRouteTable.html]
def create_route_table(vpc_id)
request({
'Action' => 'CreateRouteTable',
'VpcId' => vpc_id,
:parser => Fog::Parsers::Compute::AWS::CreateRouteTable.new
})
end
end
class Mock
def create_route_table(vpc_id)
response = Excon::Response.new
vpc = self.data[:vpcs].find { |vpc| vpc["vpcId"].eql? vpc_id }
unless vpc.nil?
response.status = 200
route_table = {
'routeTableId' => "rtb-#{Fog::Mock.random_hex(8)}",
'vpcId' => vpc["vpcId"],
'routeSet' => [{
"destinationCidrBlock" => vpc["cidrBlock"],
"gatewayId" => "local",
"instanceId"=>nil,
"instanceOwnerId"=>nil,
"networkInterfaceId"=>nil,
"state" => "pending",
"origin" => "CreateRouteTable"
}],
'associationSet' => [],
'tagSet' => {}
}
self.data[:route_tables].push(route_table)
response.body = {
'requestId'=> Fog::AWS::Mock.request_id,
'routeTable' => [route_table]
}
response
else
raise Fog::Compute::AWS::NotFound.new("The vpc ID '#{vpc_id}' does not exist")
end
end
end
end
end
end

View file

@ -43,14 +43,29 @@ module Fog
Excon::Response.new.tap do |response|
if cidrBlock
response.status = 200
vpc_id = Fog::AWS::Mock.request_id
self.data[:vpcs].push({
'vpcId' => Fog::AWS::Mock.request_id,
'vpcId' => vpc_id,
'state' => 'pending',
'cidrBlock' => cidrBlock,
'dhcpOptionsId' => Fog::AWS::Mock.request_id,
'tagSet' => {}
})
#Creates a default route for the subnet
default_route = self.route_tables.new(:vpc_id => vpc_id)
default_route.save
# You are not able to push a main route in the normal AWS, so we are re-implementing some of the
# associate_route_table here in order to accomplish this.
route_table = self.data[:route_tables].find { |routetable| routetable["routeTableId"].eql? default_route.id }
# This pushes a main route to the associationSet
# add_route_association(routeTableId, subnetId, main=false) is declared in assocate_route_table.rb
assoc = add_route_association(default_route.id, nil, true)
route_table["associationSet"].push(assoc)
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'vpcSet' => self.data[:vpcs]

View file

@ -0,0 +1,60 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/basic'
# Deletes the specified route from the specified route table.
#
# ==== Parameters
# * RouteTableId<~String> - The ID of the route table.
# * DestinationCidrBlock<~String> - The CIDR range for the route. The value you specify must match the CIDR for the route exactly.
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - The 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-DeleteRoute.html]
def delete_route(route_table_id, destination_cidr_block)
request(
'Action' => 'DeleteRoute',
'RouteTableId' => route_table_id,
'DestinationCidrBlock' => destination_cidr_block,
:parser => Fog::Parsers::Compute::AWS::Basic.new
)
end
end
class Mock
def delete_route(route_table_id, destination_cidr_block)
route_table = self.data[:route_tables].find { |routetable| routetable["routeTableId"].eql? route_table_id }
unless route_table.nil?
route = route_table['routeSet'].find { |route| route["destinationCidrBlock"].eql? destination_cidr_block }
if !route.nil? && route['gatewayId'] != "local"
route_table['routeSet'].delete(route)
response = Excon::Response.new
response.status = 200
response.body = {
'requestId'=> Fog::AWS::Mock.request_id,
'return' => true
}
response
elsif route['gatewayId'] == "local"
# Cannot delete the default route
raise Fog::Compute::AWS::Error, "InvalidParameterValue => cannot remove local route #{destination_cidr_block} in route table #{route_table_id}"
else
raise Fog::Compute::AWS::NotFound.new("no route with destination-cidr-block #{destination_cidr_block} in route table #{route_table_id}")
end
else
raise Fog::Compute::AWS::NotFound.new("no route with destination-cidr-block #{destination_cidr_block} in route table #{route_table_id}")
end
end
end
end
end
end

View file

@ -0,0 +1,51 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/basic'
# Deletes the specified route table.
#
# ==== Parameters
# * RouteTableId<~String> - The ID of the route table.
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - The ID of 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-DeleteRouteTable.html]
def delete_route_table(route_table_id)
request(
'Action' => 'DeleteRouteTable',
'RouteTableId' => route_table_id,
:parser => Fog::Parsers::Compute::AWS::Basic.new
)
end
end
class Mock
def delete_route_table(route_table_id)
route_table = self.data[:route_tables].find { |routetable| routetable["routeTableId"].eql? route_table_id }
if !route_table.nil? && route_table['associationSet'].empty?
self.data[:route_tables].delete(route_table)
response = Excon::Response.new
response.status = 200
response.body = {
'requestId'=> Fog::AWS::Mock.request_id,
'return' => true
}
response
elsif route_table.nil?
raise Fog::Compute::AWS::NotFound.new("The routeTable ID '#{route_table_id}' does not exist")
elsif !route_table['associationSet'].empty?
raise Fog::Compute::AWS::Error, "DependencyViolation => The routeTable '#{route_table_id}' has dependencies and cannot be deleted."
end
end
end
end
end
end

View file

@ -0,0 +1,87 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/describe_route_tables'
# Describe one or more of your route tables.
#
# ==== Parameters
# * RouteTableId<~String> - One or more route table IDs.
# * filters<~Hash> - List of filters to limit results with
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - The ID of the request.
# * 'routeTableSet'<~Array>:
# * 'routeTableId'<~String> - The route table's ID.
# * 'vpcId'<~String> - The ID of the VPC for the route table.
# * 'routeSet'<~Array>:
# * 'destinationCidrBlock'<~String> - The CIDR address block used for the destination match.
# * 'gatewayId'<~String> - The ID of a gateway attached to your VPC.
# * 'instanceId'<~String> - The ID of a NAT instance in your VPC.
# * 'instanceOwnerId'<~String> - The owner of the instance.
# * 'networkInterfaceId'<~String> - The network interface ID.
# * 'state'<~String> - The state of the route. The blackhole state indicates that the route's target isn't available.
# * 'origin'<~String> - Describes how the route was created.
# * 'associationSet'<~Array>:
# * 'RouteTableAssociationId'<~String> - An identifier representing the association between a route table and a subnet.
# * 'routeTableId'<~String> - The ID of the route table.
# * 'subnetId'<~String> - The ID of the subnet.
# * 'main'<~Boolean> - Indicates whether this is the main route table.
# * 'propagatingVgwSet'<~Array>:
# * 'gatewayID'<~String> - The ID of the virtual private gateway (VGW).
# * 'tagSet'<~Array>:
# * 'key'<~String> - The tag key.
# * 'value'<~String> - The tag value.
#
# {Amazon API Reference}[http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeRouteTables.html]
def describe_route_tables(filters = {})
unless filters.is_a?(Hash)
Fog::Logger.deprecation("describe_route_tables with #{filters.class} param is deprecated, use describe_route_tables('route-table-id' => []) instead [light_black](#{caller.first})[/]")
filters = {'route-table-id' => [*filters]}
end
params = Fog::AWS.indexed_filters(filters)
request({
'Action' => 'DescribeRouteTables',
:parser => Fog::Parsers::Compute::AWS::DescribeRouteTables.new
}.merge!(params))
end
end
class Mock
def describe_route_tables(filters = {})
unless filters.is_a?(Hash)
Fog::Logger.deprecation("describe_route_tables with #{filters.class} param is deprecated, use describe_route_tables('route-table-id' => []) instead [light_black](#{caller.first})[/]")
filters = {'route-table-id' => [*filters]}
end
display_routes = self.data[:route_tables].dup
aliases = {
'route-table-id' => 'routeTableId',
'vpc-id' => 'vpcId'
}
for filter_key, filter_value in filters
filter_attribute = aliases[filter_key]
case filter_attribute
when 'routeTableId', 'vpcId'
display_routes.reject! { |routetable| routetable[filter_attribute] != filter_value }
end
end
Excon::Response.new(
:status => 200,
:body => {
'requestId' => Fog::AWS::Mock.request_id,
'routeTableSet' => display_routes
}
)
end
end
end
end
end

View file

@ -0,0 +1,57 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/disassociate_route_table'
# Disassociates a subnet from a route table.
#
# ==== Parameters
# * AssociationId<~String> - The association ID representing the current association between the route table and subnet.
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - The 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-DisassociateRouteTable.html]
def disassociate_route_table(association_id)
request(
'Action' => 'DisassociateRouteTable',
'AssociationId' => association_id,
:parser => Fog::Parsers::Compute::AWS::Basic.new
)
end
end
class Mock
def disassociate_route_table(association_id)
assoc_array = nil
routetable = self.data[:route_tables].find { |routetable|
assoc_array = routetable["associationSet"].find { |association|
association['routeTableAssociationId'].eql? association_id
}
}
if !assoc_array.nil? && assoc_array['main'] == false
routetable['associationSet'].delete(assoc_array)
response = Excon::Response.new
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true
}
response
elsif assoc_array.nil?
raise Fog::Compute::AWS::NotFound.new("The association ID '#{association_id}' does not exist")
elsif assoc_array['main'] == true
raise Fog::Compute::AWS::Error, "InvalidParameterValue => cannot disassociate the main route table association #{association_id}"
end
end
end
end
end
end

View file

@ -0,0 +1,277 @@
Shindo.tests('Fog::Compute[:aws] | route table requests', ['aws']) do
@route_table_format = {
'routeTable' => [{
'routeSet' => [{
'destinationCidrBlock' => String,
'gatewayId' => String,
'state' => String,
}],
'tagSet' => Hash,
'associationSet' => Array,
'routeTableId' => String,
'vpcId' => String,
}],
'requestId' => String
}
@route_tables_format = {
'routeTableSet' => [{
'associationSet' => [{
'routeTableAssociationId' => Fog::Nullable::String,
'routeTableId' => String,
'subnetId' => Fog::Nullable::String,
'main' => Fog::Boolean
}],
'tagSet' => Hash,
'routeSet' => [{
'destinationCidrBlock' => String,
'gatewayId' => Fog::Nullable::String,
'instanceId' => Fog::Nullable::String,
'instanceOwnerId' => Fog::Nullable::String,
'networkInterfaceId' => Fog::Nullable::String,
'state' => String,
'origin' => String
}],
'routeTableId' => String,
'vpcId' => String,
}],
'requestId' => String
}
vpc = Fog::Compute[:aws].vpcs.create('cidr_block' => '10.0.10.0/24')
if !Fog.mocking?
vpc.wait_for { state.eql? "available" }
end
@subnet_id = Fog::Compute[:aws].create_subnet(vpc.id, '10.0.10.0/24').body['subnetSet'].first['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']
@network_interface_id = @network_interface['networkInterface']['networkInterfaceId']
key_name = 'fog-test-key'
key = Fog::Compute[:aws].key_pairs.create(:name => key_name)
@cidr_block = '10.0.10.0/24'
@destination_cidr_block = '10.0.10.0/23'
@ami = 'ami-79c0ae10' # ubuntu 12.04 daily build 20120728
tests('success') do
# Test create_route_table
#
tests("#create_route_table('#{vpc.id}')").formats(@route_table_format) do
data = Fog::Compute[:aws].create_route_table(vpc.id).body
@route_table_id = data['routeTable'].first['routeTableId']
data
end
# Test associate_route_table
#
tests("#associate_route_table('#{@route_table_id}', '#{@subnet_id}')").formats({'requestId'=>String, 'associationId'=>String}) do
data = Fog::Compute[:aws].associate_route_table(@route_table_id, @subnet_id).body
@association_id = data['associationId']
data
end
# Tests create_route
# - using internet gateway
# - using instance id
# - using network interface
#
Fog::Compute[:aws].attach_internet_gateway(@internet_gateway_id, vpc.id).body
tests("#create_route('#{@route_table_id}', '#{@destination_cidr_block}', '#{@internet_gateway_id}', 'nil')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].create_route(@route_table_id, @destination_cidr_block, @internet_gateway_id, nil).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("#create_route('#{@route_table_id}', '10.0.10.0/22', 'nil', '#{instance.id}')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].create_route(@route_table_id, '10.0.10.0/22', nil, instance.id).body
end
tests("#create_route('#{@route_table_id}', '10.0.10.0/21', 'nil', 'nil', '#{@network_interface_id}')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].create_route(@route_table_id, '10.0.10.0/21', nil, nil, @network_interface_id).body
end
# Tests describe_route_tables
# - no parameters
# - filter: vpc-id => vpc_id
# - filter: vpc-id => ['all']
#
tests('#describe_route_tables').formats(@route_tables_format) do
Fog::Compute[:aws].describe_route_tables.body
end
tests("#describe_route_tables('vpc-id' => #{vpc.id})").formats(@route_tables_format) do
Fog::Compute[:aws].describe_route_tables('vpc-id' => vpc.id).body
end
tests("#describe_route_tables('vpc-id' => ['all'])").formats(@route_tables_format) do
Fog::Compute[:aws].describe_route_tables('vpc-id' => ['all']).body
end
# Test delete_route(route_table_id, cidr_block)
#
tests("#delete_route('#{@route_table_id}', '10.0.10.0/21')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].delete_route(@route_table_id, '10.0.10.0/21').body
end
tests("#delete_route('#{@route_table_id}', '10.0.10.0/22')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].delete_route(@route_table_id, '10.0.10.0/22').body
end
Fog::Compute[:aws].servers.all('instance-id'=>instance.id).first.destroy
if !Fog.mocking?
instance.wait_for { state.eql? "terminated" }
end
tests("#delete_route('#{@route_table_id}', '#{@destination_cidr_block}')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].delete_route(@route_table_id, @destination_cidr_block).body
end
# Test disassociate_route_table(association_id)
#
tests("#disassociate_route_table('#{@association_id}')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].disassociate_route_table(@association_id).body
end
# Test delete_route_table(route_table_id)
#
tests("#delete_route_table('#{@route_table_id}')").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].delete_route_table(@route_table_id).body
end
end
tests('failure') do
@route_table_id = Fog::Compute[:aws].create_route_table(vpc.id).body['routeTable'].first['routeTableId']
@association_id = Fog::Compute[:aws].associate_route_table(@route_table_id, @subnet_id).body['associationId']
Fog::Compute[:aws].create_route(@route_table_id, @destination_cidr_block, @internet_gateway_id, nil)
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 create_route_table
# - no parameters
# - passing a nonexisting vpc
#
tests('#create_route_table').raises(ArgumentError) do
Fog::Compute[:aws].create_route_table
end
tests("#create_route_table('vpc-00000000')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].create_route_table('vpc-00000000')
end
# Tests associate_route_table
# - no parameters
# - passing a nonexisiting route table
# - passing a nonexisiting subnet
#
tests('#associate_route_table').raises(ArgumentError) do
Fog::Compute[:aws].associate_route_table
end
tests("#associate_route_table('rtb-00000000', '#{@subnet_id}')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].associate_route_table('rtb-00000000', @subnet_id)
end
tests("#associate_route_table('#{@route_table_id}', 'subnet-00000000')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].associate_route_table(@route_table_id, 'subnet-00000000')
end
# Tests create_route
# - no parameters
# - passing a nonexisiting route table and an exisiting internet gateway
# - passing a nonexisiting internet gateway
# - 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 the same destination cidr block as another
# - attempting to add a route at a less specific destination cidr block
#
tests('#create_route').raises(ArgumentError) do
Fog::Compute[:aws].create_route
end
tests("#create_route('rtb-00000000', '#{@destination_cidr_block}', '#{@internet_gateway_id}')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].create_route('rtb-00000000', @destination_cidr_block, @internet_gateway_id)
end
tests("#create_route('#{@route_table_id}', '#{@destination_cidr_block}', 'igw-00000000')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].create_route(@route_table_id, @destination_cidr_block, 'igw-00000000')
end
tests("#create_route('rtb-00000000', '#{@destination_cidr_block}', 'nil', '#{instance.id}')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].create_route('rtb-00000000', @destination_cidr_block, instance.id)
end
tests("#create_route('#{@route_table_id}', '#{@destination_cidr_block}', 'nil', 'i-00000000')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].create_route(@route_table_id, @destination_cidr_block, nil, 'i-00000000')
end
tests("#create_route('#{@route_table_id}', '#{@destinationCidrBlock}', 'nil', 'nil', 'eni-00000000')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].create_route(@route_table_id, @destination_cidr_block, nil, nil, 'eni-00000000')
end
tests("#create_route('#rtb-00000000', '#{@destination_cidr_block}', 'nil, 'nil', '#{@network_interface_id}')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].create_route('rtb-00000000', @destination_cidr_block, nil, nil, @network_interface_id)
end
tests("#create_route same destination_cidr_block").raises(Fog::Compute::AWS::Error) do
Fog::Compute[:aws].create_route(@route_table_id, @destination_cidr_block, @internet_gateway_id)
Fog::Compute[:aws].create_route(@route_table_id, @destination_cidr_block, nil, nil, @network_interface_id).body
end
if !Fog.mocking?
tests("#create_route less specific destination_cidr_block").raises(Fog::Compute::AWS::Error) do
Fog::Compute[:aws].create_route(@route_table_id, '10.0.10.0/25', @internet_gateway_id)
Fog::Compute[:aws].delete_route(@route_table_id, @destination_cidr_block).body
end
end
# Test describe_route_tables
# - passing a nonexisiting vpc
#
tests("#describe_route_tables('vpc-id' => 'vpc-00000000").formats({'routeTableSet'=>Array, 'requestId'=>String}) do
Fog::Compute[:aws].describe_route_tables('vpc-id' => 'vpc-00000000').body
end
# Tests delete_route
# - no parameters
# - passing a nonexisiting route table
#
tests('#delete_route').raises(ArgumentError) do
Fog::Compute[:aws].delete_route
end
tests("#delete_route('rtb-00000000', '#{@destination_cidr_block}')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].delete_route('rtb-00000000', @destination_cidr_block)
end
# Tests disassociate_route_table
# - no parameters
# - passing a nonexisiting route table association id
#
tests('#disassociate_route_table').raises(ArgumentError) do
Fog::Compute[:aws].disassociate_route_table
end
tests("#disassociate_route_table('rtbassoc-00000000')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].disassociate_route_table('rtbassoc-00000000')
end
# Tests delete_route_table
# - no parameters
# - passing a nonexisiting route table
#
tests('#delete_route_table').raises(ArgumentError) do
Fog::Compute[:aws].delete_route_table
end
tests("#delete_route_table('rtb-00000000')").raises(Fog::Compute::AWS::NotFound) do
Fog::Compute[:aws].delete_route_table('rtb-00000000')
end
# Dependency Tests
# - route is depending on route_table, so route_table cannot be deleted
#
tests("#delete_route_table('#{@route_table_id}')").raises(Fog::Compute::AWS::Error) do
Fog::Compute[:aws].delete_route_table(@route_table_id)
end
Fog::Compute[:aws].servers.all('instance-id'=>instance.id).first.destroy
if !Fog.mocking?
instance.wait_for { state.eql? "terminated" }
end
Fog::Compute[:aws].delete_route(@route_table_id, @destination_cidr_block)
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)
Fog::Compute[:aws].delete_subnet(@subnet_id)
vpc.destroy
key.destroy
end