def test_delete_attached_internet_gateway(self):
        igw_ctx = {
            'name': 'igw01'
        }

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

        igw_filters = [{'Name': 'tag:Name', 'Values': ['igw01']}]
        vpc_filters = [{'Name': 'tag:Name', 'Values': ['vpc01']}]

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_igw.InternetGatewayWrapper)

        with mock_ec2():
            event = 'creating-resource-class.ec2.InternetGateway'
            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()

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

            self.assertCountEqual(igw.attachments, [])

            # 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]

            # Test that the internet gateway has been attached
            igw.reload()
            attachments = [{'VpcId': vpc.id, 'State': 'available'}]
            self.assertCountEqual(igw.attachments, attachments)

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

            # Delete the internet gateway
            h = ec2_igw.create_handler(igw_ctx, self.credentials)
            h.delete_resource()

            gateways = list(ec2.internet_gateways.filter(Filters=igw_filters))

            # The gateway was not deleted
            self.assertEqual(len(gateways), 1)
    def test_delete_internet_gateway(self):
        ctx = {
            'name': 'igw01'
        }

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

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_igw.InternetGatewayWrapper)

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

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

            gateways = list(ec2.internet_gateways.filter(Filters=filters))

            self.assertEqual(len(gateways), 1)

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

            # Delete the internet gateway
            h.delete_resource()

            gateways = list(ec2.internet_gateways.filter(Filters=filters))

            self.assertEqual(len(gateways), 0)
Beispiel #3
0
    def test_attach_internet_gateway(self):
        igw_ctx = {
            'name': 'igw01'
        }

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

        igw_filters = [{'Name': 'tag:Name', 'Values': ['igw01']}]
        vpc_filters = [{'Name': 'tag:Name', 'Values': ['vpc01']}]

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_vpc.VpcWrapper)

        with mock_ec2():
            event = 'creating-resource-class.ec2.Vpc'
            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()

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

            self.assertCountEqual(igw.attachments, [])

            # 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]

            # Test that the internet gateway has been attached
            igw.reload()
            attachments = [{'VpcId': vpc.id, 'State': 'available'}]
            self.assertCountEqual(igw.attachments, attachments)
    def test_create_internet_gateway(self):
        ctx = {
            'name': 'igw01',
            'tags': {
                'description': 'Internet gateway for VPC vpc01'
            }
        }

        tags = [
            {
                'Key': 'Name',
                'Value': 'igw01'
            },
            {
                'Key': 'Description',
                'Value': 'Internet gateway for VPC vpc01'
            }
        ]

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

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_igw.InternetGatewayWrapper)

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

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

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

            self.assertEqual(len(gateways), 1)
            self.assertEqual(igw.name, 'igw01')
            self.assertCountEqual(igw.tags, tags)
            self.assertCountEqual(igw.attachments, [])
    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, [])
Beispiel #6
0
    def test_another_internet_gateway_already_attached_2(self):
        # Case 2: an internet gateway is attached to the VPC and there
        # is another internet gateway defined in the configuration. We
        # also leave everything unchanged.
        igw01_ctx = {
            'name': 'igw01'
        }

        igw02_ctx = {
            'name': 'igw02'
        }

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

        vpc01_ctx2 = {
            'name': 'vpc01',
            'cidr_block': '10.0.10.0/24',
            'internet_gateway': 'igw02'
        }

        igw01_filters = [{'Name': 'tag:Name', 'Values': ['igw01']}]
        igw02_filters = [{'Name': 'tag:Name', 'Values': ['igw02']}]
        vpc01_filters = [{'Name': 'tag:Name', 'Values': ['vpc01']}]

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_vpc.VpcWrapper)

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

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

            gateways = list(ec2.internet_gateways.filter(Filters=igw01_filters))
            igw01 = gateways[0]

            self.assertCountEqual(igw01.attachments, [])

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

            vpcs = list(ec2.vpcs.filter(Filters=vpc01_filters))
            vpc01 = vpcs[0]

            attachments = [{'VpcId': vpc01.id, 'State': 'available'}]

            # Test that the internet gateway has been attached
            igw01.reload()
            self.assertCountEqual(igw01.attachments, attachments)

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

            gateways = list(ec2.internet_gateways.filter(Filters=igw02_filters))
            igw02 = gateways[0]

            self.assertCountEqual(igw02.attachments, [])

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

            # Update the VPC with the new context
            h = ec2_vpc.create_handler(vpc01_ctx2, self.credentials)
            h.update_resource()

            # Test that the first internet gateway is still attached to the VPC
            igw01.reload()
            self.assertCountEqual(igw01.attachments, attachments)

            # Test that the second internet gateway is not attached
            igw02.reload()
            self.assertCountEqual(igw02.attachments, [])
    def test_update_internet_gateway(self):
        ctx1 = {
            'name': 'igw01',
            'tags': {
                'description': 'Internet gateway for VPC vpc01',
                'stack': 'Test',
                'owner': 'Team A'
            }
        }

        ctx2 = {
            'name': 'igw01',
            'tags': {
                'description': 'Internet gateway for VPC vpc01',
                'stack': 'Production'
            }
        }

        tags1 = [
            {
                'Key': 'Name',
                'Value': 'igw01'
            },
            {
                'Key': 'Description',
                'Value': 'Internet gateway for VPC vpc01'
            },
            {
                'Key': 'Stack',
                'Value': 'Test'
            },
            {
                'Key': 'Owner',
                'Value': 'Team A'
            }
        ]

        tags2 = [
            {
                'Key': 'Name',
                'Value': 'igw01'
            },
            {
                'Key': 'Description',
                'Value': 'Internet gateway for VPC vpc01'
            },
            {
                'Key': 'Stack',
                'Value': 'Production'
            }
        ]

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

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_igw.InternetGatewayWrapper)

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

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

            gateways = list(ec2.internet_gateways.filter(Filters=filters))
            igw = gateways[0]
            igw_id = igw.id

            self.assertEqual(len(gateways), 1)
            self.assertCountEqual(igw.tags, tags1)

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

            # Update the internet gateway
            h = ec2_igw.create_handler(ctx2, self.credentials)
            h.update_resource()

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

            self.assertEqual(len(gateways), 1)
            self.assertEqual(igw.id, igw_id)
            self.assertCountEqual(igw.tags, tags2)