Route Tables: Added support for associate/disassociate subnets. (added replace route table association)

This commit is contained in:
dreadpirateshawn 2014-10-14 11:23:42 -07:00
parent d6b93af7fd
commit db044df0a9
3 changed files with 86 additions and 1 deletions

View File

@ -1659,6 +1659,14 @@ class RouteTableBackend(object):
return route_table.associations.pop(association_id, None)
raise InvalidAssociationIdError(association_id)
def replace_route_table_association(self, association_id, route_table_id):
route_tables_by_association_id = ec2_backend.get_all_route_tables(filters={'association.route-table-association-id':[association_id]})
if not route_tables_by_association_id:
raise InvalidAssociationIdError(association_id)
previous_route_table = route_tables_by_association_id[0]
subnet_id = previous_route_table.associations.pop(association_id,None)
return self.associate_route_table(route_table_id, subnet_id)
class Route(object):
def __init__(self, route_table, destination_cidr_block, local=False,

View File

@ -83,7 +83,11 @@ class RouteTables(BaseResponse):
return template.render()
def replace_route_table_association(self):
raise NotImplementedError('RouteTables(AmazonVPC).replace_route_table_association is not yet implemented')
route_table_id = self.querystring.get('RouteTableId')[0]
association_id = self.querystring.get('AssociationId')[0]
new_association_id = ec2_backend.replace_route_table_association(association_id, route_table_id)
template = Template(REPLACE_ROUTE_TABLE_ASSOCIATION_RESPONSE)
return template.render(association_id=new_association_id)
CREATE_ROUTE_RESPONSE = """
@ -201,3 +205,10 @@ DISASSOCIATE_ROUTE_TABLE_RESPONSE = """
<return>true</return>
</DisassociateRouteTableResponse>
"""
REPLACE_ROUTE_TABLE_ASSOCIATION_RESPONSE = """
<ReplaceRouteTableAssociationResponse xmlns="http://ec2.amazonaws.com/doc/2014-06-15/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<newAssociationId>{{ association_id }}</newAssociationId>
</ReplaceRouteTableAssociationResponse>
"""

View File

@ -213,6 +213,72 @@ def test_route_table_associations():
cm.exception.request_id.should_not.be.none
@mock_ec2
def test_route_table_replace_route_table_association():
"""
Note: Boto has deprecated replace_route_table_assocation (which returns status)
and now uses replace_route_table_assocation_with_assoc (which returns association ID).
"""
conn = boto.connect_vpc('the_key', 'the_secret')
vpc = conn.create_vpc("10.0.0.0/16")
subnet = conn.create_subnet(vpc.id, "10.0.0.0/18")
route_table1 = conn.create_route_table(vpc.id)
route_table2 = conn.create_route_table(vpc.id)
all_route_tables = conn.get_all_route_tables()
all_route_tables.should.have.length_of(3)
# Refresh
route_table1 = conn.get_all_route_tables(route_table1.id)[0]
route_table1.associations.should.have.length_of(0)
# Associate
association_id1 = conn.associate_route_table(route_table1.id, subnet.id)
# Refresh
route_table1 = conn.get_all_route_tables(route_table1.id)[0]
route_table2 = conn.get_all_route_tables(route_table2.id)[0]
# Validate
route_table1.associations.should.have.length_of(1)
route_table2.associations.should.have.length_of(0)
route_table1.associations[0].id.should.equal(association_id1)
route_table1.associations[0].main.should.equal(False)
route_table1.associations[0].route_table_id.should.equal(route_table1.id)
route_table1.associations[0].subnet_id.should.equal(subnet.id)
# Replace Association
association_id2 = conn.replace_route_table_association_with_assoc(association_id1, route_table2.id)
# Refresh
route_table1 = conn.get_all_route_tables(route_table1.id)[0]
route_table2 = conn.get_all_route_tables(route_table2.id)[0]
# Validate
route_table1.associations.should.have.length_of(0)
route_table2.associations.should.have.length_of(1)
route_table2.associations[0].id.should.equal(association_id2)
route_table2.associations[0].main.should.equal(False)
route_table2.associations[0].route_table_id.should.equal(route_table2.id)
route_table2.associations[0].subnet_id.should.equal(subnet.id)
# Error: Replace association with invalid association ID
with assert_raises(EC2ResponseError) as cm:
conn.replace_route_table_association_with_assoc("rtbassoc-1234abcd", route_table1.id)
cm.exception.code.should.equal('InvalidAssociationID.NotFound')
cm.exception.status.should.equal(400)
cm.exception.request_id.should_not.be.none
# Error: Replace association with invalid route table ID
with assert_raises(EC2ResponseError) as cm:
conn.replace_route_table_association_with_assoc(association_id2, "rtb-1234abcd")
cm.exception.code.should.equal('InvalidRouteTableID.NotFound')
cm.exception.status.should.equal(400)
cm.exception.request_id.should_not.be.none
@mock_ec2
def test_routes_additional():
conn = boto.connect_vpc('the_key', 'the_secret')