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)
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)
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)
def _allocate_db_port(port_attrs, backend_port, addresses, mac): port_attrs["network_id"] = net["id"] port_attrs["id"] = port_id port_attrs["security_groups"] = security_groups LOG.info("Including extra plugin attrs: %s" % backend_port) port_attrs.update(backend_port) with context.session.begin(): new_port = db_api.port_create( context, addresses=addresses, mac_address=mac["address"], backend_key=backend_port["uuid"], **port_attrs) return new_port
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)
def test_create_port_sets_vlan_tag(self): self.context.session.add = mock.Mock() port_req = {"id": 1, "network_id": "2", "vlan_id": 1} new_port = db_api.port_create(self.context, **port_req) self.assertEqual(new_port.tags, [tags.VlanTag().serialize(1)])
def create_port(context, port): """Create a port Create a port which is a connection point of a device (e.g., a VM NIC) to attach to a L2 Neutron network. : param context: neutron api request context : param port: dictionary describing the port, with keys as listed in the RESOURCE_ATTRIBUTE_MAP object in neutron/api/v2/attributes.py. All keys will be populated. """ LOG.info("create_port for tenant %s" % context.tenant_id) port_attrs = port["port"] mac_address = utils.pop_param(port_attrs, "mac_address", None) segment_id = utils.pop_param(port_attrs, "segment_id") fixed_ips = utils.pop_param(port_attrs, "fixed_ips") net_id = port_attrs["network_id"] addresses = [] with context.session.begin(): port_id = uuidutils.generate_uuid() net = db_api.network_find(context, id=net_id, segment_id=segment_id, scope=db_api.ONE) if not net: # Maybe it's a tenant network net = db_api.network_find(context, id=net_id, scope=db_api.ONE) if not net: raise exceptions.NetworkNotFound(net_id=net_id) if not STRATEGY.is_parent_network(net_id): quota.QUOTAS.limit_check(context, context.tenant_id, ports_per_network=len(net.get("ports", [])) + 1) ipam_driver = ipam.IPAM_REGISTRY.get_strategy(net["ipam_strategy"]) if fixed_ips: for fixed_ip in fixed_ips: subnet_id = fixed_ip.get("subnet_id") ip_address = fixed_ip.get("ip_address") if not (subnet_id and ip_address): raise exceptions.BadRequest(resource="fixed_ips", msg="subnet_id and ip_address required") addresses.extend( ipam_driver.allocate_ip_address( context, net["id"], port_id, CONF.QUARK.ipam_reuse_after, ip_address=ip_address ) ) else: addresses.extend(ipam_driver.allocate_ip_address(context, net["id"], port_id, CONF.QUARK.ipam_reuse_after)) group_ids, security_groups = v.make_security_group_list(context, port["port"].pop("security_groups", None)) mac = ipam_driver.allocate_mac_address( context, net["id"], port_id, CONF.QUARK.ipam_reuse_after, mac_address=mac_address ) mac_address_string = str(netaddr.EUI(mac["address"], dialect=netaddr.mac_unix)) address_pairs = [ {"mac_address": mac_address_string, "ip_address": address.get("address_readable", "")} for address in addresses ] net_driver = registry.DRIVER_REGISTRY.get_driver(net["network_plugin"]) backend_port = net_driver.create_port( context, net["id"], port_id=port_id, security_groups=group_ids, allowed_pairs=address_pairs ) port_attrs["network_id"] = net["id"] port_attrs["id"] = port_id port_attrs["security_groups"] = security_groups LOG.info("Including extra plugin attrs: %s" % backend_port) port_attrs.update(backend_port) new_port = db_api.port_create( context, addresses=addresses, mac_address=mac["address"], backend_key=backend_port["uuid"], **port_attrs ) # Include any driver specific bits return v._make_port_dict(new_port)
def create_port(context, port): """Create a port Create a port which is a connection point of a device (e.g., a VM NIC) to attach to a L2 Neutron network. : param context: neutron api request context : param port: dictionary describing the port, with keys as listed in the RESOURCE_ATTRIBUTE_MAP object in neutron/api/v2/attributes.py. All keys will be populated. """ LOG.info("create_port for tenant %s" % context.tenant_id) port_attrs = port["port"] mac_address = utils.pop_param(port_attrs, "mac_address", None) segment_id = utils.pop_param(port_attrs, "segment_id") fixed_ips = utils.pop_param(port_attrs, "fixed_ips") net_id = port_attrs["network_id"] addresses = [] port_id = uuidutils.generate_uuid() net = db_api.network_find(context, id=net_id, shared=True, segment_id=segment_id, scope=db_api.ONE) if not net: # Maybe it's a tenant network net = db_api.network_find(context, id=net_id, scope=db_api.ONE) if not net: raise exceptions.NetworkNotFound(net_id=net_id) quota.QUOTAS.limit_check(context, context.tenant_id, ports_per_network=len(net.get('ports', [])) + 1) if fixed_ips: for fixed_ip in fixed_ips: subnet_id = fixed_ip.get("subnet_id") ip_address = fixed_ip.get("ip_address") if not (subnet_id and ip_address): raise exceptions.BadRequest( resource="fixed_ips", msg="subnet_id and ip_address required") addresses.append( ipam_driver.allocate_ip_address(context, net["id"], port_id, CONF.QUARK.ipam_reuse_after, ip_address=ip_address)) else: addresses.append( ipam_driver.allocate_ip_address(context, net["id"], port_id, CONF.QUARK.ipam_reuse_after)) group_ids, security_groups = v.make_security_group_list( context, port["port"].pop("security_groups", None)) mac = ipam_driver.allocate_mac_address(context, net["id"], port_id, CONF.QUARK.ipam_reuse_after, mac_address=mac_address) mac_address_string = str( netaddr.EUI(mac['address'], dialect=netaddr.mac_unix)) address_pairs = [{ 'mac_address': mac_address_string, 'ip_address': address.get('address_readable', '') } for address in addresses] backend_port = net_driver.create_port(context, net["id"], port_id=port_id, security_groups=group_ids, allowed_pairs=address_pairs) port_attrs["network_id"] = net["id"] port_attrs["id"] = port_id port_attrs["security_groups"] = security_groups new_port = db_api.port_create(context, addresses=addresses, mac_address=mac["address"], backend_key=backend_port["uuid"], **port_attrs) return v._make_port_dict(new_port)
def create_port(context, port): """Create a port Create a port which is a connection point of a device (e.g., a VM NIC) to attach to a L2 Neutron network. : param context: neutron api request context : param port: dictionary describing the port, with keys as listed in the RESOURCE_ATTRIBUTE_MAP object in neutron/api/v2/attributes.py. All keys will be populated. """ LOG.info("create_port for tenant %s" % context.tenant_id) port_attrs = port["port"] mac_address = utils.pop_param(port_attrs, "mac_address", None) segment_id = utils.pop_param(port_attrs, "segment_id") fixed_ips = utils.pop_param(port_attrs, "fixed_ips") net_id = port_attrs["network_id"] addresses = [] port_id = uuidutils.generate_uuid() net = db_api.network_find(context, id=net_id, shared=True, segment_id=segment_id, scope=db_api.ONE) if not net: # Maybe it's a tenant network net = db_api.network_find(context, id=net_id, scope=db_api.ONE) if not net: raise exceptions.NetworkNotFound(net_id=net_id) quota.QUOTAS.limit_check( context, context.tenant_id, ports_per_network=len(net.get('ports', [])) + 1) if fixed_ips: for fixed_ip in fixed_ips: subnet_id = fixed_ip.get("subnet_id") ip_address = fixed_ip.get("ip_address") if not (subnet_id and ip_address): raise exceptions.BadRequest( resource="fixed_ips", msg="subnet_id and ip_address required") addresses.append(ipam_driver.allocate_ip_address( context, net["id"], port_id, CONF.QUARK.ipam_reuse_after, ip_address=ip_address)) else: addresses.append(ipam_driver.allocate_ip_address( context, net["id"], port_id, CONF.QUARK.ipam_reuse_after)) group_ids, security_groups = v.make_security_group_list( context, port["port"].pop("security_groups", None)) mac = ipam_driver.allocate_mac_address(context, net["id"], port_id, CONF.QUARK.ipam_reuse_after, mac_address=mac_address) mac_address_string = str(netaddr.EUI(mac['address'], dialect=netaddr.mac_unix)) address_pairs = [{'mac_address': mac_address_string, 'ip_address': address.get('address_readable', '')} for address in addresses] backend_port = net_driver.create_port(context, net["id"], port_id=port_id, security_groups=group_ids, allowed_pairs=address_pairs) port_attrs["network_id"] = net["id"] port_attrs["id"] = port_id port_attrs["security_groups"] = security_groups new_port = db_api.port_create( context, addresses=addresses, mac_address=mac["address"], backend_key=backend_port["uuid"], **port_attrs) return v._make_port_dict(new_port)
def create_port(self, context, port): """Create a port Create a port which is a connection point of a device (e.g., a VM NIC) to attach to a L2 Quantum network. : param context: quantum api request context : param port: dictionary describing the port, with keys as listed in the RESOURCE_ATTRIBUTE_MAP object in quantum/api/v2/attributes.py. All keys will be populated. """ LOG.info("create_port for tenant %s" % context.tenant_id) mac_address = port["port"].pop("mac_address", None) if mac_address and mac_address is attributes.ATTR_NOT_SPECIFIED: mac_address = None segment_id = port["port"].pop("segment_id", None) addresses = [] port_id = uuidutils.generate_uuid() net_id = port["port"]["network_id"] net = db_api.network_find(context, id=net_id, shared=True, segment_id=segment_id, scope=db_api.ONE) if not net: # Maybe it's a tenant network net = db_api.network_find(context, id=net_id, scope=db_api.ONE) if not net: raise exceptions.NetworkNotFound(net_id=net_id) fixed_ips = port["port"].pop("fixed_ips", None) if fixed_ips and fixed_ips is not attributes.ATTR_NOT_SPECIFIED: for fixed_ip in fixed_ips: subnet_id = fixed_ip.get("subnet_id") ip_address = fixed_ip.get("ip_address") if not (subnet_id and ip_address): raise exceptions.BadRequest( resource="fixed_ips", msg="subnet_id and ip_address required") # Note: we don't allow overlapping subnets, thus subnet_id is # ignored. addresses.append(self.ipam_driver.allocate_ip_address( context, net["id"], port_id, self.ipam_reuse_after, ip_address=ip_address)) else: addresses.append(self.ipam_driver.allocate_ip_address( context, net["id"], port_id, self.ipam_reuse_after)) mac = self.ipam_driver.allocate_mac_address(context, net["id"], port_id, self.ipam_reuse_after, mac_address=mac_address) backend_port = self.net_driver.create_port(context, net["id"], port_id=port_id) port["port"]["network_id"] = net["id"] port["port"]["id"] = port_id new_port = db_api.port_create( context, addresses=addresses, mac_address=mac["address"], backend_key=backend_port["uuid"], **port["port"]) return self._make_port_dict(new_port)