Beispiel #1
0
 def test_networks_pagination(self):
     networks_per_page = 1
     networkA = dict(name="fake_A", tenant_id="fake", network_plugin="BASE")
     networkB = dict(name="fake_B", tenant_id="fake", network_plugin="BASE")
     db_api.network_create(self.context, **networkA)
     db_api.network_create(self.context, **networkB)
     res = network_api.get_networks(self.context, networks_per_page, ['id'],
                                    None, False)
     self.assertEqual(len(res), networks_per_page)
     res = network_api.get_networks(self.context)
     self.assertNotEqual(len(res), networks_per_page)
Beispiel #2
0
 def test_networks_pagination(self):
     networks_per_page = 1
     networkA = dict(name="fake_A", tenant_id="fake", network_plugin="BASE")
     networkB = dict(name="fake_B", tenant_id="fake", network_plugin="BASE")
     db_api.network_create(self.context, **networkA)
     db_api.network_create(self.context, **networkB)
     res = network_api.get_networks(self.context, networks_per_page,
                                    ['id'], None, False)
     self.assertEqual(len(res), networks_per_page)
     res = network_api.get_networks(self.context)
     self.assertNotEqual(len(res), networks_per_page)
Beispiel #3
0
 def test_port_list_device_owner_found_returns_only_those(self):
     # create a network
     network = dict(name="public", tenant_id="fake", network_plugin="BASE")
     net_mod = db_api.network_create(self.context, **network)
     # create ports
     port1 = dict(network_id=net_mod["id"], backend_key="1", device_id="1",
                  device_owner="Doge")
     port2 = dict(network_id=net_mod["id"], backend_key="1", device_id="1",
                  device_owner=port1["device_owner"])
     port3 = dict(network_id=net_mod["id"], backend_key="1", device_id="1",
                  device_owner="network:dhcp")
     port_mod1 = db_api.port_create(self.context, **port1)
     port_mod2 = db_api.port_create(self.context, **port2)
     port_mod3 = db_api.port_create(self.context, **port3)
     res = db_api.port_find(self.context, scope=db_api.ALL,
                            device_owner=port3["device_owner"])
     self.assertTrue(len(res) == 1)
     self.assertTrue(res[0]["device_owner"] == port3["device_owner"])
     res = db_api.port_find(self.context, scope=db_api.ALL,
                            device_owner=port1["device_owner"])
     self.assertTrue(len(res) == 2)
     self.assertTrue(res[0]["device_owner"] == res[1]["device_owner"] ==
                     port1["device_owner"])
     db_api.network_delete(self.context, net_mod)
     db_api.port_delete(self.context, port_mod1)
     db_api.port_delete(self.context, port_mod2)
     db_api.port_delete(self.context, port_mod3)
Beispiel #4
0
 def test_port_list_device_owner_found_returns_only_those(self):
     # create a network
     network = dict(name="public", tenant_id="fake", network_plugin="BASE")
     net_mod = db_api.network_create(self.context, **network)
     # create ports
     port1 = dict(network_id=net_mod["id"],
                  backend_key="1",
                  device_id="1",
                  device_owner="Doge")
     port2 = dict(network_id=net_mod["id"],
                  backend_key="1",
                  device_id="1",
                  device_owner=port1["device_owner"])
     port3 = dict(network_id=net_mod["id"],
                  backend_key="1",
                  device_id="1",
                  device_owner="network:dhcp")
     port_mod1 = db_api.port_create(self.context, **port1)
     port_mod2 = db_api.port_create(self.context, **port2)
     port_mod3 = db_api.port_create(self.context, **port3)
     res = db_api.port_find(self.context,
                            scope=db_api.ALL,
                            device_owner=port3["device_owner"])
     self.assertTrue(len(res) == 1)
     self.assertTrue(res[0]["device_owner"] == port3["device_owner"])
     res = db_api.port_find(self.context,
                            scope=db_api.ALL,
                            device_owner=port1["device_owner"])
     self.assertTrue(len(res) == 2)
     self.assertTrue(res[0]["device_owner"] == res[1]["device_owner"] ==
                     port1["device_owner"])
     db_api.network_delete(self.context, net_mod)
     db_api.port_delete(self.context, port_mod1)
     db_api.port_delete(self.context, port_mod2)
     db_api.port_delete(self.context, port_mod3)
Beispiel #5
0
    def _stubs(self, network, subnet, address, lock=False):
        self.ipam = quark.ipam.QuarkIpamANY()
        with self.context.session.begin():
            next_ip = subnet.pop("next_auto_assign_ip", 0)
            net_mod = db_api.network_create(self.context, **network)
            subnet["network"] = net_mod
            sub_mod = db_api.subnet_create(self.context, **subnet)

            address["network_id"] = net_mod["id"]
            address["subnet_id"] = sub_mod["id"]
            ip = db_api.ip_address_create(self.context, **address)
            address.pop("address")
            ip = db_api.ip_address_update(self.context, ip, **address)

            # NOTE(asadoughi): update after cidr constructor has been invoked
            db_api.subnet_update(self.context,
                                 sub_mod,
                                 next_auto_assign_ip=next_ip)

        if lock:
            db_api.lock_holder_create(self.context,
                                      ip,
                                      name="testlock",
                                      type="ip_address")
        yield net_mod
Beispiel #6
0
 def test_ports_sorted_by_created_at(self):
     # create a network
     network = dict(name="public", tenant_id="fake", network_plugin="BASE")
     net_mod = db_api.network_create(self.context, **network)
     # create ports
     port1 = dict(network_id=net_mod["id"], backend_key="1", device_id="1")
     port2 = dict(network_id=net_mod["id"], backend_key="1", device_id="1")
     port3 = dict(network_id=net_mod["id"], backend_key="1", device_id="1")
     port_mod1 = db_api.port_create(self.context, **port1)
     port_mod2 = db_api.port_create(self.context, **port2)
     port_mod3 = db_api.port_create(self.context, **port3)
     res = db_api.port_find(context=self.context,
                            limit=None,
                            sorts=['created_at'],
                            marker_obj=None,
                            fields=None,
                            scope=db_api.ALL)
     self.assertTrue(
         res[0]["created_at"] < res[1]["created_at"] < res[2]['created_at'])
     self.assertTrue(res[0]['id'] == port_mod1['id'])
     self.assertTrue(res[1]['id'] == port_mod2['id'])
     self.assertTrue(res[2]['id'] == port_mod3['id'])
     db_api.network_delete(self.context, net_mod)
     db_api.port_delete(self.context, port_mod1)
     db_api.port_delete(self.context, port_mod2)
     db_api.port_delete(self.context, port_mod3)
Beispiel #7
0
 def test_get_subnets_cidr_set(self):
     network = db_api.network_create(self.context)
     db_api.subnet_create(
         self.context,
         network=network, cidr=self.cidr)
     self.context.session.flush()
     ipset = null_routes.get_subnets_cidr_set(self.context, [network.id])
     self.assertEqual(ipset, netaddr.IPSet(netaddr.IPNetwork(self.cidr)))
Beispiel #8
0
 def _stubs(self, network, subnets):
     self.ipam = quark.ipam.QuarkIpamANY()
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         for subnet in subnets:
             subnet["network"] = net_mod
             db_api.subnet_create(self.context, **subnet)
     yield net_mod
Beispiel #9
0
 def _stubs(self, network, subnets):
     self.ipam = quark.ipam.QuarkIpamANY()
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         for subnet in subnets:
             subnet["network"] = net_mod
             db_api.subnet_create(self.context, **subnet)
     yield net_mod
Beispiel #10
0
 def _stubs(self, network, subnet):
     self.ipam = quark.ipam.QuarkIpamANY()
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         subnet["network"] = net_mod
         sub1 = db_api.subnet_create(self.context, **subnet)
         subnet["id"] = 2
         sub2 = db_api.subnet_create(self.context, do_not_use=True, **subnet)
     yield net_mod, sub1, sub2
Beispiel #11
0
 def _stubs(self, network, subnet):
     self.ipam = quark.ipam.QuarkIpamANY()
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         subnet["network"] = net_mod
         sub_mod = db_api.subnet_create(self.context, **subnet)
     yield net_mod
     with self.context.session.begin():
         db_api.subnet_delete(self.context, sub_mod)
         db_api.network_delete(self.context, net_mod)
Beispiel #12
0
 def _stubs(self, network, subnet):
     self.ipam = quark.ipam.QuarkIpamANY()
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         subnet["network"] = net_mod
         sub1 = db_api.subnet_create(self.context, **subnet)
         subnet["id"] = 2
         sub2 = db_api.subnet_create(self.context, do_not_use=True,
                                     **subnet)
     yield net_mod, sub1, sub2
Beispiel #13
0
 def _stubs(self, network, subnet, dealloc=True):
     self.plugin = quark.plugin.Plugin()
     self.ipam = quark.ipam.QuarkIpamANY()
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         subnet["network"] = net_mod
         db_api.subnet_create(self.context, **subnet)
         ip_addr = self.ipam.allocate_ip_address(self.context, net_mod["id"], 0, 0)
         if dealloc:
             self.ipam._deallocate_ip_address(self.context, ip_addr[0])
     yield net_mod
Beispiel #14
0
 def _stubs(self, network, subnet):
     self.ipam = quark.ipam.QuarkIpamANY()
     with self.context.session.begin():
         next_ip = subnet.pop("next_auto_assign_ip", 0)
         net_mod = db_api.network_create(self.context, **network)
         subnet["network"] = net_mod
         sub_mod = db_api.subnet_create(self.context, **subnet)
         # NOTE(asadoughi): update after cidr constructor has been invoked
         db_api.subnet_update(self.context,
                              sub_mod,
                              next_auto_assign_ip=next_ip)
     yield net_mod
Beispiel #15
0
 def _stubs(self, network, subnet):
     self.ipam = quark.ipam.QuarkIpamANY()
     with self.context.session.begin():
         next_ip = subnet.pop("next_auto_assign_ip", 0)
         net_mod = db_api.network_create(self.context, **network)
         subnet["network"] = net_mod
         sub_mod = db_api.subnet_create(self.context, **subnet)
         # NOTE(asadoughi): update after cidr constructor has been invoked
         db_api.subnet_update(self.context,
                              sub_mod,
                              next_auto_assign_ip=next_ip)
     yield net_mod
Beispiel #16
0
    def test_create_locks_address_exists(self):
        network = db_api.network_create(self.context)
        address_model = db_api.ip_address_create(
            self.context,
            address=netaddr.IPAddress("192.168.10.1"),
            network=network)
        self.context.session.flush()

        addresses = netaddr.IPSet(netaddr.IPNetwork(self.sub_cidr))
        null_routes.create_locks(self.context, [network.id], addresses)
        self.context.session.refresh(address_model)
        self.assertIsNotNone(address_model.lock_id)
Beispiel #17
0
    def _fixtures(self, models):

        self.ipam = quark.ipam.QuarkIpamANY()
        net = dict(name="public", tenant_id='fake')
        net_mod = db_api.network_create(self.context, **net)
        with self.context.session.begin():
            for model in models:
                policy_mod = db_api.ip_policy_create(self.context,
                                                     **model['ip_policy'])
                model['subnet']["network"] = net_mod
                model['subnet']["ip_policy"] = policy_mod
                db_api.subnet_create(self.context, **model['subnet'])
        yield net_mod
Beispiel #18
0
    def _fixtures(self, models):

        self.ipam = quark.ipam.QuarkIpamANY()
        net = dict(name="public", tenant_id='fake')
        net_mod = db_api.network_create(self.context, **net)
        with self.context.session.begin():
            for model in models:
                policy_mod = db_api.ip_policy_create(
                    self.context, **model['ip_policy'])
                model['subnet']["network"] = net_mod
                model['subnet']["ip_policy"] = policy_mod
                db_api.subnet_create(self.context, **model['subnet'])
        yield net_mod
Beispiel #19
0
    def test_delete_locks_doesnt_have_lock(self):
        network = db_api.network_create(self.context)
        address_model = db_api.ip_address_create(
            self.context,
            address=netaddr.IPAddress("192.168.10.1"),
            network=network)
        db_api.lock_holder_create(
            self.context, address_model,
            name="not-null-routes", type="ip_address")
        self.context.session.flush()

        null_routes.delete_locks(self.context, [network.id], [])
        self.context.session.refresh(address_model)
        self.assertIsNotNone(address_model.lock_id)
Beispiel #20
0
    def _stubs(self, network, subnet, dealloc=True):
        self.plugin = quark.plugin.Plugin()
        self.ipam = quark.ipam.QuarkIpamANY()
        with self.context.session.begin():
            net_mod = db_api.network_create(self.context, **network)
            subnet["network"] = net_mod
            next_auto = subnet.pop("next_auto_assign_ip", 0)
            sub_mod = db_api.subnet_create(self.context, **subnet)
            db_api.subnet_update(self.context, sub_mod, next_auto_assign_ip=next_auto)

        ip_addr = []
        self.ipam.allocate_ip_address(self.context, ip_addr, net_mod["id"], 0, 0)
        if dealloc:
            self.ipam.deallocate_ip_address(self.context, ip_addr[0])
        yield net_mod
Beispiel #21
0
    def test_create_locks_address_doesnt_exist(self):
        network = db_api.network_create(self.context)
        subnet = db_api.subnet_create(
            self.context,
            network=network,
            cidr=self.cidr,
            ip_version=4)
        self.context.session.flush()

        addresses = netaddr.IPSet(netaddr.IPNetwork(self.sub_cidr))
        null_routes.create_locks(self.context, [network.id], addresses)
        address = db_api.ip_address_find(
            self.context, subnet_id=subnet.id, scope=db_api.ONE)
        self.assertIsNotNone(address)
        self.assertIsNotNone(address.lock_id)
Beispiel #22
0
def create_network(context, network):
    """Create a network.

    Create a network which represents an L2 network segment which
    can have a set of subnets and ports associated with it.
    : param context: neutron api request context
    : param network: dictionary describing the network, with keys
        as listed in the RESOURCE_ATTRIBUTE_MAP object in
        neutron/api/v2/attributes.py.  All keys will be populated.
    """
    LOG.info("create_network for tenant %s" % context.tenant_id)

    # Generate a uuid that we're going to hand to the backend and db
    net_uuid = uuidutils.generate_uuid()

    #TODO(mdietz) this will be the first component registry hook, but
    #             lets make it work first
    pnet_type, phys_net, seg_id = _adapt_provider_nets(context, network)
    net_attrs = network["network"]

    # NOTE(mdietz) I think ideally we would create the providernet
    # elsewhere as a separate driver step that could be
    # kept in a plugin and completely removed if desired. We could
    # have a pre-callback/observer on the netdriver create_network
    # that gathers any additional parameters from the network dict
    net_driver.create_network(context, net_attrs["name"], network_id=net_uuid,
                              phys_type=pnet_type, phys_net=phys_net,
                              segment_id=seg_id)

    subs = net_attrs.pop("subnets", [])

    net_attrs["id"] = net_uuid
    net_attrs["tenant_id"] = context.tenant_id
    new_net = db_api.network_create(context, **net_attrs)

    new_subnets = []
    for sub in subs:
        sub["subnet"]["network_id"] = new_net["id"]
        sub["subnet"]["tenant_id"] = context.tenant_id
        s = db_api.subnet_create(context, **sub["subnet"])
        new_subnets.append(s)
    new_net["subnets"] = new_subnets

    if not security_groups.get_security_groups(
            context,
            filters={"id": security_groups.DEFAULT_SG_UUID}):
        security_groups._create_default_security_group(context)
    return v._make_network_dict(new_net)
Beispiel #23
0
    def setUp(self):
        super(BaseFloatingIPTest, self).setUp()
        self.public_net_id = "00000000-0000-0000-0000-000000000000"
        net_stat = '{"%s": {}}' % self.public_net_id
        cfg.CONF.set_override('default_net_strategy', net_stat, group='QUARK')
        old_strat = db_api.STRATEGY

        def reset_strategy():
            db_api.STRATEGY = old_strat

        db_api.STRATEGY = network_strategy.JSONStrategy()
        self.addCleanup(reset_strategy)
        admin_ctx = context.get_admin_context()
        self._setup_mock_requests()
        self.plugin = quark.plugin.Plugin()
        mac = {'mac_address_range': dict(cidr="AA:BB:CC")}
        macrng_api.create_mac_address_range(admin_ctx, mac)
        with admin_ctx.session.begin():
            tenant = 'rackspace'
            floating_net = dict(name='publicnet', tenant_id=tenant,
                                id=self.public_net_id)
            self.floating_network = db_api.network_create(
                self.context, **floating_net)
            self.pub_net_cidr = "10.1.1.0/24"
            floating_subnet = dict(id=self.public_net_id,
                                   cidr=self.pub_net_cidr,
                                   ip_policy=None, tenant_id=tenant,
                                   segment_id='floating_ip',
                                   network_id=self.floating_network.id)
            self.floating_subnet = db_api.subnet_create(
                self.context, **floating_subnet)
        user_net = dict(name='user_network', tenant_id='fake')
        self.user_network = self.plugin.create_network(
            self.context, {'network': user_net})
        user_subnet = dict(cidr="192.168.1.0/24",
                           ip_policy=None, tenant_id="fake",
                           network_id=self.user_network['id'])
        self.user_subnet = self.plugin.create_subnet(
            self.context, {'subnet': user_subnet})
        user_port1 = dict(name='user_port1',
                          network_id=self.user_network['id'])
        self.user_port1 = self.plugin.create_port(
            self.context, {'port': user_port1})
        user_port2 = dict(name='user_port2',
                          network_id=self.user_network['id'])
        self.user_port2 = self.plugin.create_port(
            self.context, {'port': user_port2})
Beispiel #24
0
 def test_ports_sorted_by_created_at(self):
     # create a network
     network = dict(name="public", tenant_id="fake", network_plugin="BASE")
     net_mod = db_api.network_create(self.context, **network)
     # create ports
     port1 = dict(network_id=net_mod["id"], backend_key="1", device_id="1")
     port2 = dict(network_id=net_mod["id"], backend_key="1", device_id="1")
     port3 = dict(network_id=net_mod["id"], backend_key="1", device_id="1")
     port_mod1 = db_api.port_create(self.context, **port1)
     port_mod2 = db_api.port_create(self.context, **port2)
     port_mod3 = db_api.port_create(self.context, **port3)
     res = db_api.port_find(self.context, scope=db_api.ALL)
     self.assertTrue(res[0]["created_at"] < res[1]["created_at"] < res[2]["created_at"])
     db_api.network_delete(self.context, net_mod)
     db_api.port_delete(self.context, port_mod1)
     db_api.port_delete(self.context, port_mod2)
     db_api.port_delete(self.context, port_mod3)
Beispiel #25
0
def create_network(context, network):
    """Create a network.

    Create a network which represents an L2 network segment which
    can have a set of subnets and ports associated with it.
    : param context: neutron api request context
    : param network: dictionary describing the network, with keys
        as listed in the RESOURCE_ATTRIBUTE_MAP object in
        neutron/api/v2/attributes.py.  All keys will be populated.
    """
    LOG.info("create_network for tenant %s" % context.tenant_id)

    # Generate a uuid that we're going to hand to the backend and db
    net_uuid = uuidutils.generate_uuid()

    # TODO(mdietz) this will be the first component registry hook, but
    #             lets make it work first
    pnet_type, phys_net, seg_id = _adapt_provider_nets(context, network)
    net_attrs = network["network"]

    # NOTE(mdietz) I think ideally we would create the providernet
    # elsewhere as a separate driver step that could be
    # kept in a plugin and completely removed if desired. We could
    # have a pre-callback/observer on the netdriver create_network
    # that gathers any additional parameters from the network dict
    net_driver.create_network(
        context, net_attrs["name"], network_id=net_uuid, phys_type=pnet_type, phys_net=phys_net, segment_id=seg_id
    )

    subs = net_attrs.pop("subnets", [])

    net_attrs["id"] = net_uuid
    net_attrs["tenant_id"] = context.tenant_id
    new_net = db_api.network_create(context, **net_attrs)

    new_subnets = []
    for sub in subs:
        sub["subnet"]["network_id"] = new_net["id"]
        sub["subnet"]["tenant_id"] = context.tenant_id
        s = db_api.subnet_create(context, **sub["subnet"])
        new_subnets.append(s)
    new_net["subnets"] = new_subnets

    if not security_groups.get_security_groups(context, filters={"id": security_groups.DEFAULT_SG_UUID}):
        security_groups._create_default_security_group(context)
    return v._make_network_dict(new_net)
Beispiel #26
0
    def _stubs(self, network, subnet, dealloc=True):
        self.plugin = quark.plugin.Plugin()
        self.ipam = quark.ipam.QuarkIpamANY()
        with self.context.session.begin():
            net_mod = db_api.network_create(self.context, **network)
            subnet["network"] = net_mod
            next_auto = subnet.pop("next_auto_assign_ip", 0)
            sub_mod = db_api.subnet_create(self.context, **subnet)
            db_api.subnet_update(self.context,
                                 sub_mod,
                                 next_auto_assign_ip=next_auto)

        ip_addr = []
        self.ipam.allocate_ip_address(self.context, ip_addr, net_mod["id"], 0,
                                      0)
        if dealloc:
            self.ipam.deallocate_ip_address(self.context, ip_addr[0])
        yield net_mod
Beispiel #27
0
    def _fixtures(self, models):

        self.ipam = quark.ipam.QuarkIpamANY()
        net = dict(name="public", tenant_id='fake')
        net_mod = db_api.network_create(self.context, **net)
        with self.context.session.begin():
            for model in models:
                policy_mod = db_api.ip_policy_create(
                    self.context, **model['ip_policy'])
                model['subnet']["network"] = net_mod
                model['subnet']["ip_policy"] = policy_mod
                next_ip = model['subnet'].pop("next_auto_assign_ip", 0)
                sub_mod = db_api.subnet_create(self.context, **model['subnet'])
                # NOTE(amir): update after cidr constructor has been invoked
                db_api.subnet_update(self.context,
                                     sub_mod,
                                     next_auto_assign_ip=next_ip)
        yield net_mod
Beispiel #28
0
    def _fixtures(self, models):

        self.ipam = quark.ipam.QuarkIpamANY()
        net = dict(name="public", tenant_id='fake')
        net_mod = db_api.network_create(self.context, **net)
        with self.context.session.begin():
            for model in models:
                policy_mod = db_api.ip_policy_create(self.context,
                                                     **model['ip_policy'])
                model['subnet']["network"] = net_mod
                model['subnet']["ip_policy"] = policy_mod
                next_ip = model['subnet'].pop("next_auto_assign_ip", 0)
                sub_mod = db_api.subnet_create(self.context, **model['subnet'])
                # NOTE(amir): update after cidr constructor has been invoked
                db_api.subnet_update(self.context,
                                     sub_mod,
                                     next_auto_assign_ip=next_ip)
        yield net_mod
Beispiel #29
0
    def test_create_locks_lock_holder_exists(self):
        network = db_api.network_create(self.context)
        address_model = db_api.ip_address_create(
            self.context,
            address=netaddr.IPAddress("192.168.10.1"),
            network=network)
        db_api.lock_holder_create(
            self.context, address_model,
            name=null_routes.LOCK_NAME, type="ip_address")
        self.context.session.flush()

        addresses = netaddr.IPSet(netaddr.IPNetwork(self.sub_cidr))
        null_routes.create_locks(self.context, [network.id], addresses)

        lock_holders = db_api.lock_holder_find(
            self.context,
            lock_id=address_model.lock_id,
            name=null_routes.LOCK_NAME,
            scope=db_api.ALL)
        self.assertEqual(len(lock_holders), 1)
Beispiel #30
0
    def test_ip_address_port_find_service(self):
        net = db_api.network_create(self.context)
        port = db_api.port_create(self.context, network_id=net["id"],
                                  backend_key="", device_id="")
        ip_address = db_api.ip_address_create(
            self.context, address=netaddr.IPAddress("0.0.0.0"))
        self.context.session.flush()

        ip_address = db_api.port_associate_ip(self.context, [port], ip_address)
        ip_address.set_service_for_port(port, "foobar")
        self.context.session.flush()

        ports = ip_api.get_ports_for_ip_address(
            self.context, ip_address["id"],
            filters={"service": "not-foobar"})
        self.assertEqual(len(ports), 0)

        ports = ip_api.get_ports_for_ip_address(
            self.context, ip_address["id"],
            filters={"service": "foobar"})
        self.assertEqual(len(ports), 1)
Beispiel #31
0
 def _stubs(self, network, subnets, ipam_strategy):
     self.ipam = ipam_strategy
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         next_ip = []
         sub_mod = []
         for sub in subnets:
             next_ip.append(sub.pop("next_auto_assign_ip", 0))
             sub["network"] = net_mod
             sub_mod.append(db_api.subnet_create(self.context, **sub))
         for sub, ip_next in zip(sub_mod, next_ip):
             # NOTE(asadoughi): update after cidr constructor has been
             # invoked
             db_api.subnet_update(self.context,
                                  sub,
                                  next_auto_assign_ip=ip_next)
     yield net_mod, sub_mod
     with self.context.session.begin():
         for sub in sub_mod:
             db_api.subnet_delete(self.context, sub)
         db_api.network_delete(self.context, net_mod)
Beispiel #32
0
 def _stubs(self, network, subnets, ipam_strategy):
     self.ipam = ipam_strategy
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         next_ip = []
         sub_mod = []
         for sub in subnets:
             next_ip.append(sub.pop("next_auto_assign_ip", 0))
             sub["network"] = net_mod
             sub_mod.append(db_api.subnet_create(self.context, **sub))
         for sub, ip_next in zip(sub_mod, next_ip):
             # NOTE(asadoughi): update after cidr constructor has been
             # invoked
             db_api.subnet_update(self.context,
                                  sub,
                                  next_auto_assign_ip=ip_next)
     yield net_mod, sub_mod
     with self.context.session.begin():
         for sub in sub_mod:
             db_api.subnet_delete(self.context, sub)
         db_api.network_delete(self.context, net_mod)
Beispiel #33
0
    def _stubs(self, network, subnet, address, lock=False):
        self.ipam = quark.ipam.QuarkIpamANY()
        with self.context.session.begin():
            next_ip = subnet.pop("next_auto_assign_ip", 0)
            net_mod = db_api.network_create(self.context, **network)
            subnet["network"] = net_mod
            sub_mod = db_api.subnet_create(self.context, **subnet)

            address["network_id"] = net_mod["id"]
            address["subnet_id"] = sub_mod["id"]
            ip = db_api.ip_address_create(self.context, **address)
            address.pop("address")
            ip = db_api.ip_address_update(self.context, ip, **address)

            # NOTE(asadoughi): update after cidr constructor has been invoked
            db_api.subnet_update(self.context,
                                 sub_mod,
                                 next_auto_assign_ip=next_ip)

        if lock:
            db_api.lock_holder_create(self.context, ip,
                                      name="testlock", type="ip_address")
        yield net_mod
Beispiel #34
0
    def create_network(self, context, network):
        """Create a network.

        Create a network which represents an L2 network segment which
        can have a set of subnets and ports associated with it.
        : param context: quantum api request context
        : param network: dictionary describing the network, with keys
            as listed in the RESOURCE_ATTRIBUTE_MAP object in
            quantum/api/v2/attributes.py.  All keys will be populated.
        """
        LOG.info("create_network for tenant %s" % context.tenant_id)
        # Generate a uuid that we're going to hand to the backend and db
        net_uuid = uuidutils.generate_uuid()

        #NOTE(mdietz): probably want to abstract this out as we're getting
        #              too tied to the implementation here
        self.net_driver.create_network(context,
                                       network["network"]["name"],
                                       network_id=net_uuid)

        subnets = []
        if network["network"].get("subnets"):
            subnets = network["network"].pop("subnets")

        network["network"]["id"] = net_uuid
        network["network"]["tenant_id"] = context.tenant_id
        new_net = db_api.network_create(context, **network["network"])

        new_subnets = []
        for sub in subnets:
            sub["subnet"]["network_id"] = new_net["id"]
            sub["subnet"]["tenant_id"] = context.tenant_id
            s = db_api.subnet_create(context, **sub["subnet"])
            new_subnets.append(s)
        new_net["subnets"] = new_subnets
        return self._make_network_dict(new_net)
Beispiel #35
0
 def _stubs(self, network, subnet, ip_address1, ip_address2, ip_address3):
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         subnet["network"] = net_mod
         sub_mod = db_api.subnet_create(self.context, **subnet)
         # set tenant id to "123"
         ip_address1['network_id'] = net_mod.id
         ip_address1['subnet_id'] = sub_mod.id
         self.context.tenant_id = 123
         ip_address1 = db_api.ip_address_create(self.context, **ip_address1)
         # set tenant_id=456
         ip_address2['network_id'] = net_mod.id
         ip_address2['subnet_id'] = sub_mod.id
         self.context.tenant_id = 456
         ip_address2 = db_api.ip_address_create(self.context, **ip_address2)
         # set tenant id = "123" to test the list of IPs
         ip_address3['network_id'] = net_mod.id
         ip_address3['subnet_id'] = sub_mod.id
         self.context.tenant_id = 123
         ip_address3 = db_api.ip_address_create(self.context, **ip_address3)
     yield net_mod
     with self.context.session.begin():
         db_api.subnet_delete(self.context, sub_mod)
         db_api.network_delete(self.context, net_mod)
Beispiel #36
0
 def _stubs(self, network, subnet, ip_address1, ip_address2, ip_address3):
     with self.context.session.begin():
         net_mod = db_api.network_create(self.context, **network)
         subnet["network"] = net_mod
         sub_mod = db_api.subnet_create(self.context, **subnet)
         # set tenant id to "123"
         ip_address1['network_id'] = net_mod.id
         ip_address1['subnet_id'] = sub_mod.id
         self.context.tenant_id = 123
         ip_address1 = db_api.ip_address_create(self.context, **ip_address1)
         # set tenant_id=456
         ip_address2['network_id'] = net_mod.id
         ip_address2['subnet_id'] = sub_mod.id
         self.context.tenant_id = 456
         ip_address2 = db_api.ip_address_create(self.context, **ip_address2)
         # set tenant id = "123" to test the list of IPs
         ip_address3['network_id'] = net_mod.id
         ip_address3['subnet_id'] = sub_mod.id
         self.context.tenant_id = 123
         ip_address3 = db_api.ip_address_create(self.context, **ip_address3)
     yield net_mod
     with self.context.session.begin():
         db_api.subnet_delete(self.context, sub_mod)
         db_api.network_delete(self.context, net_mod)
Beispiel #37
0
 def insert_network(self):
     tenant_id = "foobar"
     network = {"tenant_id": tenant_id}
     network_db = db_api.network_create(self.context, **network)
     self.context.session.flush()
     return network_db
Beispiel #38
0
def create_network(context, network):
    """Create a network.

    Create a network which represents an L2 network segment which
    can have a set of subnets and ports associated with it.
    : param context: neutron api request context
    : param network: dictionary describing the network, with keys
        as listed in the RESOURCE_ATTRIBUTE_MAP object in
        neutron/api/v2/attributes.py.  All keys will be populated.
    """
    LOG.info("create_network for tenant %s" % context.tenant_id)

    with context.session.begin():
        net_attrs = network["network"]
        subs = net_attrs.pop("subnets", [])
        # Enforce subnet quotas
        if not context.is_admin:
            if len(subs) > 0:
                v4_count, v6_count = 0, 0
                for s in subs:
                    version = netaddr.IPNetwork(s['subnet']['cidr']).version
                    if version == 6:
                        v6_count += 1
                    else:
                        v4_count += 1
                if v4_count > 0:
                    tenant_q_v4 = context.session.query(qdv.Quota).filter_by(
                        tenant_id=context.tenant_id,
                        resource='v4_subnets_per_network').first()
                    if tenant_q_v4 != -1:
                        quota.QUOTAS.limit_check(
                            context,
                            context.tenant_id,
                            v4_subnets_per_network=v4_count)
                if v6_count > 0:
                    tenant_q_v6 = context.session.query(qdv.Quota).filter_by(
                        tenant_id=context.tenant_id,
                        resource='v6_subnets_per_network').first()
                    if tenant_q_v6 != -1:
                        quota.QUOTAS.limit_check(
                            context,
                            context.tenant_id,
                            v6_subnets_per_network=v6_count)
        # Generate a uuid that we're going to hand to the backend and db
        net_uuid = utils.pop_param(net_attrs, "id", None)
        net_type = None
        if net_uuid and context.is_admin:
            net = db_api.network_find(context=context,
                                      limit=None,
                                      sorts=['id'],
                                      marker=None,
                                      page_reverse=False,
                                      id=net_uuid,
                                      scope=db_api.ONE)
            net_type = utils.pop_param(net_attrs, "network_plugin", None)
            if net:
                raise q_exc.NetworkAlreadyExists(id=net_uuid)
        else:
            net_uuid = uuidutils.generate_uuid()

        # TODO(mdietz) this will be the first component registry hook, but
        #             lets make it work first
        pnet_type, phys_net, seg_id = _adapt_provider_nets(context, network)

        ipam_strategy = utils.pop_param(net_attrs, "ipam_strategy", None)
        if not ipam_strategy or not context.is_admin:
            ipam_strategy = CONF.QUARK.default_ipam_strategy

        if not ipam.IPAM_REGISTRY.is_valid_strategy(ipam_strategy):
            raise q_exc.InvalidIpamStrategy(strat=ipam_strategy)
        net_attrs["ipam_strategy"] = ipam_strategy

        # NOTE(mdietz) I think ideally we would create the providernet
        # elsewhere as a separate driver step that could be
        # kept in a plugin and completely removed if desired. We could
        # have a pre-callback/observer on the netdriver create_network
        # that gathers any additional parameters from the network dict

        default_net_type = net_type or CONF.QUARK.default_network_type
        net_driver = registry.DRIVER_REGISTRY.get_driver(default_net_type)
        net_driver.create_network(context,
                                  net_attrs["name"],
                                  network_id=net_uuid,
                                  phys_type=pnet_type,
                                  phys_net=phys_net,
                                  segment_id=seg_id)

        net_attrs["id"] = net_uuid
        net_attrs["tenant_id"] = context.tenant_id
        net_attrs["network_plugin"] = default_net_type
        new_net = db_api.network_create(context, **net_attrs)

        new_subnets = []
        for sub in subs:
            sub["subnet"]["network_id"] = new_net["id"]
            sub["subnet"]["tenant_id"] = context.tenant_id
            s = db_api.subnet_create(context, **sub["subnet"])
            new_subnets.append(s)
        new_net["subnets"] = new_subnets

        # if not security_groups.get_security_groups(
        #        context,
        #        filters={"id": security_groups.DEFAULT_SG_UUID}):
        #    security_groups._create_default_security_group(context)
    return v._make_network_dict(new_net)
Beispiel #39
0
def create_network(context, network):
    """Create a network.

    Create a network which represents an L2 network segment which
    can have a set of subnets and ports associated with it.
    : param context: neutron api request context
    : param network: dictionary describing the network, with keys
        as listed in the RESOURCE_ATTRIBUTE_MAP object in
        neutron/api/v2/attributes.py.  All keys will be populated.
    """
    LOG.info("create_network for tenant %s" % context.tenant_id)

    with context.session.begin():
        net_attrs = network["network"]
        subs = net_attrs.pop("subnets", [])
        # Enforce subnet quotas
        if not context.is_admin:
            if len(subs) > 0:
                v4_count, v6_count = 0, 0
                for s in subs:
                    version = netaddr.IPNetwork(s['subnet']['cidr']).version
                    if version == 6:
                        v6_count += 1
                    else:
                        v4_count += 1
                if v4_count > 0:
                    tenant_q_v4 = context.session.query(qdb.Quota).filter_by(
                        tenant_id=context.tenant_id,
                        resource='v4_subnets_per_network').first()
                    if tenant_q_v4 != -1:
                        quota.QUOTAS.limit_check(
                            context,
                            context.tenant_id,
                            v4_subnets_per_network=v4_count)
                if v6_count > 0:
                    tenant_q_v6 = context.session.query(qdb.Quota).filter_by(
                        tenant_id=context.tenant_id,
                        resource='v6_subnets_per_network').first()
                    if tenant_q_v6 != -1:
                        quota.QUOTAS.limit_check(
                            context,
                            context.tenant_id,
                            v6_subnets_per_network=v6_count)
        # Generate a uuid that we're going to hand to the backend and db
        net_uuid = utils.pop_param(net_attrs, "id", None)
        net_type = None
        if net_uuid and context.is_admin:
            net = db_api.network_find(context, id=net_uuid, scope=db_api.ONE)
            net_type = utils.pop_param(net_attrs, "network_plugin", None)
            if net:
                raise q_exc.NetworkAlreadyExists(id=net_uuid)
        else:
            net_uuid = uuidutils.generate_uuid()

        # TODO(mdietz) this will be the first component registry hook, but
        #             lets make it work first
        pnet_type, phys_net, seg_id = _adapt_provider_nets(context, network)

        ipam_strategy = utils.pop_param(net_attrs, "ipam_strategy", None)
        if not ipam_strategy or not context.is_admin:
            ipam_strategy = CONF.QUARK.default_ipam_strategy

        if not ipam.IPAM_REGISTRY.is_valid_strategy(ipam_strategy):
            raise q_exc.InvalidIpamStrategy(strat=ipam_strategy)
        net_attrs["ipam_strategy"] = ipam_strategy

        # NOTE(mdietz) I think ideally we would create the providernet
        # elsewhere as a separate driver step that could be
        # kept in a plugin and completely removed if desired. We could
        # have a pre-callback/observer on the netdriver create_network
        # that gathers any additional parameters from the network dict

        default_net_type = net_type or CONF.QUARK.default_network_type
        net_driver = registry.DRIVER_REGISTRY.get_driver(default_net_type)
        net_driver.create_network(context, net_attrs["name"],
                                  network_id=net_uuid, phys_type=pnet_type,
                                  phys_net=phys_net, segment_id=seg_id)

        net_attrs["id"] = net_uuid
        net_attrs["tenant_id"] = context.tenant_id
        net_attrs["network_plugin"] = default_net_type
        new_net = db_api.network_create(context, **net_attrs)

        new_subnets = []
        for sub in subs:
            sub["subnet"]["network_id"] = new_net["id"]
            sub["subnet"]["tenant_id"] = context.tenant_id
            s = db_api.subnet_create(context, **sub["subnet"])
            new_subnets.append(s)
        new_net["subnets"] = new_subnets

        # if not security_groups.get_security_groups(
        #        context,
        #        filters={"id": security_groups.DEFAULT_SG_UUID}):
        #    security_groups._create_default_security_group(context)
    return v._make_network_dict(new_net)