Пример #1
0
def attach_vpn_gateway(context, vpc_id, vpn_gateway_id):
    vpn_gateway = ec2utils.get_db_item(context, vpn_gateway_id)
    vpc = ec2utils.get_db_item(context, vpc_id)
    if vpn_gateway['vpc_id'] and vpn_gateway['vpc_id'] != vpc['id']:
        raise exception.VpnGatewayAttachmentLimitExceeded()
    attached_vgw = ec2utils.get_attached_gateway(context, vpc['id'], 'vgw')
    if attached_vgw and attached_vgw['id'] != vpn_gateway['id']:
        raise exception.InvalidVpcState(vpc_id=vpc['id'],
                                        vgw_id=attached_vgw['id'])

    subnets = [subnet for subnet in db_api.get_items(context, 'subnet')
               if subnet['vpc_id'] == vpc['id']]
    if not vpn_gateway['vpc_id']:
        external_network_id = None
        if not ec2utils.get_attached_gateway(context, vpc['id'], 'igw'):
            external_network_id = ec2utils.get_os_public_network(context)['id']
        neutron = clients.neutron(context)

        with common.OnCrashCleaner() as cleaner:
            _attach_vpn_gateway_item(context, vpn_gateway, vpc['id'])
            cleaner.addCleanup(_detach_vpn_gateway_item, context, vpn_gateway)

            if external_network_id:
                neutron.add_gateway_router(vpc['os_id'],
                                           {'network_id': external_network_id})
                cleaner.addCleanup(neutron.remove_gateway_router, vpc['os_id'])

            for subnet in subnets:
                _create_subnet_vpnservice(context, neutron, cleaner,
                                          subnet, vpc)
            vpn_connection_api._reset_vpn_connections(
                context, neutron, cleaner, vpn_gateway, subnets=subnets)

    return {'attachment': _format_attachment(vpn_gateway)}
Пример #2
0
def attach_vpn_gateway(context, vpc_id, vpn_gateway_id):
    vpn_gateway = ec2utils.get_db_item(context, vpn_gateway_id)
    vpc = ec2utils.get_db_item(context, vpc_id)
    if vpn_gateway["vpc_id"] and vpn_gateway["vpc_id"] != vpc["id"]:
        raise exception.VpnGatewayAttachmentLimitExceeded()
    attached_vgw = ec2utils.get_attached_gateway(context, vpc["id"], "vgw")
    if attached_vgw and attached_vgw["id"] != vpn_gateway["id"]:
        raise exception.InvalidVpcState(vpc_id=vpc["id"], vgw_id=attached_vgw["id"])

    subnets = [subnet for subnet in db_api.get_items(context, "subnet") if subnet["vpc_id"] == vpc["id"]]
    if not vpn_gateway["vpc_id"]:
        external_network_id = None
        if not ec2utils.get_attached_gateway(context, vpc["id"], "igw"):
            external_network_id = ec2utils.get_os_public_network(context)["id"]
        neutron = clients.neutron(context)

        with common.OnCrashCleaner() as cleaner:
            _attach_vpn_gateway_item(context, vpn_gateway, vpc["id"])
            cleaner.addCleanup(_detach_vpn_gateway_item, context, vpn_gateway)

            if external_network_id:
                neutron.add_gateway_router(vpc["os_id"], {"network_id": external_network_id})
                cleaner.addCleanup(neutron.remove_gateway_router, vpc["os_id"])

            for subnet in subnets:
                _create_subnet_vpnservice(context, neutron, cleaner, subnet, vpc)
            vpn_connection_api._reset_vpn_connections(context, neutron, cleaner, vpn_gateway, subnets=subnets)

    return {"attachment": _format_attachment(vpn_gateway)}
Пример #3
0
def attach_internet_gateway(context, internet_gateway_id, vpc_id):
    igw = ec2utils.get_db_item(context, internet_gateway_id)
    if igw.get('vpc_id'):
        msg_params = {'igw_id': igw['id'],
                      'vpc_id': igw['vpc_id']}
        msg = _('resource %(igw_id)s is already attached to '
                'network %(vpc_id)s') % msg_params
        raise exception.ResourceAlreadyAssociated(msg)
    vpc = ec2utils.get_db_item(context, vpc_id)
    if ec2utils.get_attached_gateway(context, vpc['id'], 'igw'):
        msg = _('Network %(vpc_id)s already has an internet gateway '
                'attached') % {'vpc_id': vpc['id']}
        raise exception.InvalidParameterValue(msg)

    external_network_id = None
    if not ec2utils.get_attached_gateway(context, vpc['id'], 'vgw'):
        external_network_id = ec2utils.get_os_public_network(context)['id']
    neutron = clients.neutron(context)

    # TODO(ft): set attaching state into db
    with common.OnCrashCleaner() as cleaner:
        _attach_internet_gateway_item(context, igw, vpc['id'])
        cleaner.addCleanup(_detach_internet_gateway_item, context, igw)
        if external_network_id:
            neutron.add_gateway_router(vpc['os_id'],
                                       {'network_id': external_network_id})
    return True
Пример #4
0
def attach_internet_gateway(context, internet_gateway_id, vpc_id):
    igw = ec2utils.get_db_item(context, internet_gateway_id)
    if igw.get('vpc_id'):
        msg_params = {'igw_id': igw['id'], 'vpc_id': igw['vpc_id']}
        msg = _('resource %(igw_id)s is already attached to '
                'network %(vpc_id)s') % msg_params
        raise exception.ResourceAlreadyAssociated(msg)
    vpc = ec2utils.get_db_item(context, vpc_id)
    if ec2utils.get_attached_gateway(context, vpc['id'], 'igw'):
        msg = _('Network %(vpc_id)s already has an internet gateway '
                'attached') % {
                    'vpc_id': vpc['id']
                }
        raise exception.InvalidParameterValue(msg)

    external_network_id = None
    if not ec2utils.get_attached_gateway(context, vpc['id'], 'vgw'):
        external_network_id = ec2utils.get_os_public_network(context)['id']
    neutron = clients.neutron(context)

    # TODO(ft): set attaching state into db
    with common.OnCrashCleaner() as cleaner:
        _attach_internet_gateway_item(context, igw, vpc['id'])
        cleaner.addCleanup(_detach_internet_gateway_item, context, igw)
        if external_network_id:
            neutron.add_gateway_router(vpc['os_id'],
                                       {'network_id': external_network_id})
    return True
def attach_internet_gateway(context, internet_gateway_id, vpc_id):
    igw = ec2utils.get_db_item(context, internet_gateway_id)
    if igw.get('vpc_id'):
        msg_params = {'igw_id': igw['id'], 'vpc_id': igw['vpc_id']}
        msg = _("resource %(igw_id)s is already attached to "
                "network %(vpc_id)s") % msg_params
        raise exception.ResourceAlreadyAssociated(msg)
    vpc = ec2utils.get_db_item(context, vpc_id)
    # TODO(ft): move search by vpc_id to DB api
    for gw in db_api.get_items(context, 'igw'):
        if gw.get('vpc_id') == vpc['id']:
            msg = _("Network %(vpc_id)s already has an internet gateway "
                    "attached") % {
                        'vpc_id': vpc['id']
                    }
            raise exception.InvalidParameterValue(msg)

    os_public_network = ec2utils.get_os_public_network(context)
    neutron = clients.neutron(context)

    # TODO(ft): set attaching state into db
    with common.OnCrashCleaner() as cleaner:
        _attach_internet_gateway_item(context, igw, vpc['id'])
        cleaner.addCleanup(_detach_internet_gateway_item, context, igw)
        neutron.add_gateway_router(vpc['os_id'],
                                   {'network_id': os_public_network['id']})
    return True
Пример #6
0
    def test_get_os_public_network(self, neutron):
        neutron = neutron.return_value
        context = base.create_context()
        conf = self.useFixture(config_fixture.Config())

        conf.config(external_network='fake_public_network')
        neutron.list_networks.return_value = {'networks': ['network_object']}
        net = ec2utils.get_os_public_network(context)
        self.assertEqual('network_object', net)
        neutron.list_networks.assert_called_once_with(
            **{
                'router:external': True,
                'name': 'fake_public_network'
            })

        neutron.list_networks.return_value = {'networks': []}
        with fixtures.FakeLogger() as log:
            self.assertRaises(exception.Unsupported,
                              ec2utils.get_os_public_network, context)
        self.assertNotEqual(0, len(log.output))
        self.assertIn('fake_public_network', log.output)

        neutron.list_networks.return_value = {'networks': ['obj1', 'obj2']}
        with fixtures.FakeLogger() as log:
            self.assertRaises(exception.Unsupported,
                              ec2utils.get_os_public_network, context)
        self.assertNotEqual(0, len(log.output))
        self.assertIn('fake_public_network', log.output)

        conf.config(external_network=None)
        with fixtures.FakeLogger() as log:
            self.assertRaises(exception.Unsupported,
                              ec2utils.get_os_public_network, context)
        self.assertNotEqual(0, len(log.output))
        self.assertNotIn('None', log.output)

        neutron.list_networks.return_value = {'networks': []}
        with fixtures.FakeLogger() as log:
            self.assertRaises(exception.Unsupported,
                              ec2utils.get_os_public_network, context)
        self.assertNotEqual(0, len(log.output))
        self.assertNotIn('None', log.output)
Пример #7
0
    def allocate_address(self, context, domain=None):
        if not domain or domain == 'standard':
            return AddressEngineNova().allocate_address(context)
        os_public_network = ec2utils.get_os_public_network(context)
        neutron = clients.neutron(context)

        with common.OnCrashCleaner() as cleaner:
            os_floating_ip = {'floating_network_id': os_public_network['id']}
            try:
                os_floating_ip = neutron.create_floatingip(
                        {'floatingip': os_floating_ip})
            except neutron_exception.OverQuotaClient:
                raise exception.AddressLimitExceeded()
            os_floating_ip = os_floating_ip['floatingip']
            cleaner.addCleanup(neutron.delete_floatingip, os_floating_ip['id'])

            address = {'os_id': os_floating_ip['id'],
                       'public_ip': os_floating_ip['floating_ip_address']}
            address = db_api.add_item(context, 'eipalloc', address)
        return address, os_floating_ip
Пример #8
0
    def test_get_os_public_network(self, neutron):
        neutron = neutron.return_value
        context = base.create_context()
        conf = self.useFixture(config_fixture.Config())

        conf.config(external_network='fake_public_network')
        neutron.list_networks.return_value = {'networks': ['network_object']}
        net = ec2utils.get_os_public_network(context)
        self.assertEqual('network_object', net)
        neutron.list_networks.assert_called_once_with(
            **{'router:external': True, 'name': 'fake_public_network'})

        neutron.list_networks.return_value = {'networks': []}
        with fixtures.FakeLogger() as log:
            self.assertRaises(exception.Unsupported,
                              ec2utils.get_os_public_network, context)
        self.assertNotEqual(0, len(log.output))
        self.assertIn('fake_public_network', log.output)

        neutron.list_networks.return_value = {'networks': ['obj1', 'obj2']}
        with fixtures.FakeLogger() as log:
            self.assertRaises(exception.Unsupported,
                              ec2utils.get_os_public_network, context)
        self.assertNotEqual(0, len(log.output))
        self.assertIn('fake_public_network', log.output)

        conf.config(external_network=None)
        with fixtures.FakeLogger() as log:
            self.assertRaises(exception.Unsupported,
                              ec2utils.get_os_public_network, context)
        self.assertNotEqual(0, len(log.output))
        self.assertNotIn('None', log.output)

        neutron.list_networks.return_value = {'networks': []}
        with fixtures.FakeLogger() as log:
            self.assertRaises(exception.Unsupported,
                              ec2utils.get_os_public_network, context)
        self.assertNotEqual(0, len(log.output))
        self.assertNotIn('None', log.output)
Пример #9
0
def attach_internet_gateway(context, internet_gateway_id, vpc_id):
    igw = ec2utils.get_db_item(context, internet_gateway_id)
    if igw.get("vpc_id"):
        msg_params = {"igw_id": igw["id"], "vpc_id": igw["vpc_id"]}
        msg = _("resource %(igw_id)s is already attached to " "network %(vpc_id)s") % msg_params
        raise exception.ResourceAlreadyAssociated(msg)
    vpc = ec2utils.get_db_item(context, vpc_id)
    if ec2utils.get_attached_gateway(context, vpc["id"], "igw"):
        msg = _("Network %(vpc_id)s already has an internet gateway " "attached") % {"vpc_id": vpc["id"]}
        raise exception.InvalidParameterValue(msg)

    external_network_id = None
    if not ec2utils.get_attached_gateway(context, vpc["id"], "vgw"):
        external_network_id = ec2utils.get_os_public_network(context)["id"]
    neutron = clients.neutron(context)

    # TODO(ft): set attaching state into db
    with common.OnCrashCleaner() as cleaner:
        _attach_internet_gateway_item(context, igw, vpc["id"])
        cleaner.addCleanup(_detach_internet_gateway_item, context, igw)
        if external_network_id:
            neutron.add_gateway_router(vpc["os_id"], {"network_id": external_network_id})
    return True