示例#1
0
    def test_delete_route_table_with_association(self):
        vpc_ctx = {
            'name': 'vpc01',
            'cidr_block': '10.0.10.0/24'
        }

        subnet_ctx = {
            'name': 'subnet01a',
            'cidr_block': '10.0.10.0/25',
            'zone': 'us-west-2a',
            'vpc': 'vpc01'
        }

        rt_ctx = {
            'name': 'rt01',
            'vpc': 'vpc01',
            'subnets': [
                'subnet01a'
            ]
        }

        filters = [{'Name': 'tag:Name', 'Values': ['rt01']}]

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_rt.RouteTableWrapper)

        with mock_ec2():
            event = 'creating-resource-class.ec2.RouteTable'
            session = Session(**self.credentials)
            session.events.register(event, _add_wrapper)
            ec2 = session.resource('ec2')

            # Create the VPC
            h = ec2_vpc.create_handler(vpc_ctx, self.credentials)
            h.create_resource()

            # Create the subnet
            h = ec2_subnet.create_handler(subnet_ctx, self.credentials)
            h.create_resource()

            # Create the route table
            h = ec2_rt.create_handler(rt_ctx, self.credentials)
            h.create_resource()

            route_tables = list(ec2.route_tables.filter(Filters=filters))

            self.assertEqual(len(route_tables), 1)

            # We clear the resource cache to simulate a new
            # program execution with the 'delete' option
            base.BaseHandler._cache.clear()

            # Delete the route table
            h.delete_resource()

            route_tables = list(ec2.route_tables.filter(Filters=filters))

            self.assertEqual(len(route_tables), 0)
示例#2
0
    def test_create_route_table(self):
        vpc_ctx = {
            'name': 'vpc01',
            'cidr_block': '10.0.10.0/24'
        }

        rt_ctx = {
            'name': 'rt01',
            'vpc': 'vpc01',
            'tags': {
                'description': 'Replace the default route table for VPC vpc01'
            }
        }

        tags = [
            {
                'Key': 'Name',
                'Value': 'rt01'
            },
            {
                'Key': 'Description',
                'Value': 'Replace the default route table for VPC vpc01'
            }
        ]

        vpc_filters = [{'Name': 'tag:Name', 'Values': ['vpc01']}]
        rt_filters = [{'Name': 'tag:Name', 'Values': ['rt01']}]

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_rt.RouteTableWrapper)

        with mock_ec2():
            event = 'creating-resource-class.ec2.RouteTable'
            session = Session(**self.credentials)
            session.events.register(event, _add_wrapper)
            ec2 = session.resource('ec2')

            # Create the VPC
            h = ec2_vpc.create_handler(vpc_ctx, self.credentials)
            h.create_resource()

            vpcs = list(ec2.vpcs.filter(Filters=vpc_filters))
            vpc = vpcs[0]

            # Create the route table
            h = ec2_rt.create_handler(rt_ctx, self.credentials)
            h.create_resource()

            route_tables = list(ec2.route_tables.filter(Filters=rt_filters))
            rt = route_tables[0]

            self.assertEqual(len(route_tables), 1)
            self.assertEqual(rt.name, 'rt01')
            self.assertEqual(rt.vpc_id, vpc.id)
            self.assertCountEqual(rt.tags, tags)
示例#3
0
    def test_update_route_table(self):
        vpc_ctx = {
            'name': 'vpc01',
            'cidr_block': '10.0.10.0/24'
        }

        rt_ctx1 = {
            'name': 'rt01',
            'vpc': 'vpc01',
            'tags': {
                'description': 'Replace the default route table for VPC vpc01',
                'stack': 'Test',
                'owner': 'Team A'
            }
        }

        rt_ctx2 = {
            'name': 'rt01',
            'vpc': 'vpc01',
            'tags': {
                'description': 'Replace the default route table for VPC vpc01',
                'stack': 'Production'
            }
        }

        tags1 = [
            {
                'Key': 'Name',
                'Value': 'rt01'
            },
            {
                'Key': 'Description',
                'Value': 'Replace the default route table for VPC vpc01'
            },
            {
                'Key': 'Stack',
                'Value': 'Test'
            },
            {
                'Key': 'Owner',
                'Value': 'Team A'
            }
        ]

        tags2 = [
            {
                'Key': 'Name',
                'Value': 'rt01'
            },
            {
                'Key': 'Description',
                'Value': 'Replace the default route table for VPC vpc01'
            },
            {
                'Key': 'Stack',
                'Value': 'Production'
            }
        ]

        rt_filters = [{'Name': 'tag:Name', 'Values': ['rt01']}]

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_rt.RouteTableWrapper)

        with mock_ec2():
            event = 'creating-resource-class.ec2.RouteTable'
            session = Session(**self.credentials)
            session.events.register(event, _add_wrapper)
            ec2 = session.resource('ec2')

            # Create the VPC
            h = ec2_vpc.create_handler(vpc_ctx, self.credentials)
            h.create_resource()

            # Create the route table
            h = ec2_rt.create_handler(rt_ctx1, self.credentials)
            h.create_resource()

            route_tables = list(ec2.route_tables.filter(Filters=rt_filters))
            rt = route_tables[0]
            rt_id = rt.id

            self.assertEqual(len(route_tables), 1)
            self.assertCountEqual(rt.tags, tags1)

            # Update the route table
            h = ec2_rt.create_handler(rt_ctx2, self.credentials)
            h.update_resource()

            route_tables = list(ec2.route_tables.filter(Filters=rt_filters))
            rt = route_tables[0]

            self.assertEqual(len(route_tables), 1)
            self.assertEqual(rt.id, rt_id)
            self.assertCountEqual(rt.tags, tags2)
示例#4
0
    def test_delete_route(self):
        igw_ctx = {
            'name': 'igw01'
        }

        vpc_ctx = {
            'name': 'vpc01',
            'cidr_block': '10.0.10.0/24',
            'internet_gateway': 'igw01'
        }

        rt_ctx1 = {
            'name': 'rt01',
            'vpc': 'vpc01',
            'routes': [
                {
                    'destination': '0.0.0.0/0',
                    'target_name': 'igw01',
                    'target_type': 'internet_gateway'
                }
            ]
        }

        rt_ctx2 = {
            'name': 'rt01',
            'vpc': 'vpc01',
            'routes': None
        }

        igw_filters = [{'Name': 'tag:Name', 'Values': ['igw01']}]
        rt_filters = [{'Name': 'tag:Name', 'Values': ['rt01']}]

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_rt.RouteTableWrapper)

        with mock_ec2():
            event = 'creating-resource-class.ec2.RouteTable'
            session = Session(**self.credentials)
            session.events.register(event, _add_wrapper)
            ec2 = session.resource('ec2')

            # Create the internet gateway
            h = ec2_igw.create_handler(igw_ctx, self.credentials)
            h.create_resource()

            internet_gateways = list(ec2.internet_gateways.filter(Filters=igw_filters))
            igw = internet_gateways[0]

            # Create the VPC
            h = ec2_vpc.create_handler(vpc_ctx, self.credentials)
            h.create_resource()

            # Create the route table
            h = ec2_rt.create_handler(rt_ctx1, self.credentials)
            h.create_resource()

            route_tables = list(ec2.route_tables.filter(Filters=rt_filters))
            rt = route_tables[0]

            route = {
                'GatewayId': igw.id,
                'DestinationCidrBlock': '0.0.0.0/0',
                'Origin': 'CreateRoute',
                'State': 'active'
            }

            route_wrapper = ec2_rt.RouteWrapper(route)

            self.assertEqual(len(rt.routes), 2)  # the new route + the default VPC route
            self.assertIn(route_wrapper, rt.custom_routes)

            # We clear the resource cache to simulate a new
            # program execution with the 'update' option
            base.BaseHandler._cache.clear()

            # Update the route table
            h = ec2_rt.create_handler(rt_ctx2, self.credentials)
            h.update_resource()

            route_tables = list(ec2.route_tables.filter(Filters=rt_filters))
            rt = route_tables[0]

            self.assertEqual(len(rt.routes), 1)  # the default VPC route
            self.assertCountEqual(rt.custom_routes, [])
示例#5
0
    def test_change_subnet_association(self):
        vpc_ctx = {
            'name': 'vpc01',
            'cidr_block': '10.0.10.0/24'
        }

        subnet_ctx = {
            'name': 'subnet01a',
            'cidr_block': '10.0.10.0/25',
            'zone': 'us-west-2a',
            'vpc': 'vpc01'
        }

        rt_ctx1 = {
            'name': 'rt01',
            'vpc': 'vpc01',
            'subnets': [
                'subnet01a'
            ]
        }

        rt_ctx2 = {
            'name': 'rt02',
            'vpc': 'vpc01',
            'subnets': [
                'subnet01a'
            ]
        }

        subnet_filters = [{'Name': 'tag:Name', 'Values': ['subnet01a']}]
        rt01_filters = [{'Name': 'tag:Name', 'Values': ['rt01']}]
        rt02_filters = [{'Name': 'tag:Name', 'Values': ['rt02']}]

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_rt.RouteTableWrapper)

        with mock_ec2():
            event = 'creating-resource-class.ec2.RouteTable'
            session = Session(**self.credentials)
            session.events.register(event, _add_wrapper)
            ec2 = session.resource('ec2')

            # Create the VPC
            h = ec2_vpc.create_handler(vpc_ctx, self.credentials)
            h.create_resource()

            # Create the subnet
            h = ec2_subnet.create_handler(subnet_ctx, self.credentials)
            h.create_resource()

            subnets = list(ec2.subnets.filter(Filters=subnet_filters))
            subnet = subnets[0]

            # Create route table rt01
            h = ec2_rt.create_handler(rt_ctx1, self.credentials)
            h.create_resource()

            route_tables = list(ec2.route_tables.filter(Filters=rt01_filters))
            rt01 = route_tables[0]

            filters = [
                {
                    'Name': 'association.subnet-id',
                    'Values': [subnet.id]
                },
                {
                    'Name': 'association.route-table-id',
                    'Values': [rt01.id]
                }
            ]

            associations = list(rt01.associations.filter(Filters=filters))

            self.assertEqual(len(associations), 1)

            # Create route table rt02
            h = ec2_rt.create_handler(rt_ctx2, self.credentials)
            h.create_resource()

            route_tables = list(ec2.route_tables.filter(Filters=rt02_filters))
            rt02 = route_tables[0]

            filters = [
                {
                    'Name': 'association.subnet-id',
                    'Values': [subnet.id]
                },
                {
                    'Name': 'association.route-table-id',
                    'Values': [rt02.id]
                }
            ]

            associations = list(rt02.associations.filter(Filters=filters))

            self.assertEqual(len(associations), 1)