add name into port and subnet.

f-3-cli-usability-improvments

The name is optional to create network/port/subnet and is up to user to
decide its uniqueness.

Change-Id: I40c834ed66e2da14a414232c86c9fd4f5c1d9154
This commit is contained in:
Yong Sheng Gong 2012-07-26 07:32:59 +08:00
parent 7095639552
commit 2157d21923
5 changed files with 35 additions and 19 deletions

View File

@ -144,7 +144,7 @@ RESOURCE_ATTRIBUTE_MAP = {
'validate': {'type:regex': UUID_PATTERN},
'is_visible': True},
'name': {'allow_post': True, 'allow_put': True,
'is_visible': True},
'default': '', 'is_visible': True},
'subnets': {'allow_post': True, 'allow_put': True,
'default': [],
'is_visible': True},
@ -165,6 +165,8 @@ RESOURCE_ATTRIBUTE_MAP = {
'id': {'allow_post': False, 'allow_put': False,
'validate': {'type:regex': UUID_PATTERN},
'is_visible': True},
'name': {'allow_post': True, 'allow_put': True, 'default': '',
'is_visible': True},
'network_id': {'allow_post': True, 'allow_put': False,
'validate': {'type:regex': UUID_PATTERN},
'is_visible': True},
@ -196,6 +198,8 @@ RESOURCE_ATTRIBUTE_MAP = {
'id': {'allow_post': False, 'allow_put': False,
'validate': {'type:regex': UUID_PATTERN},
'is_visible': True},
'name': {'allow_post': True, 'allow_put': True, 'default': '',
'is_visible': True},
'ip_version': {'allow_post': True, 'allow_put': False,
'convert_to': int,
'validate': {'type:values': [4, 6]},

View File

@ -615,6 +615,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
def _make_subnet_dict(self, subnet, fields=None):
res = {'id': subnet['id'],
'name': subnet['name'],
'tenant_id': subnet['tenant_id'],
'network_id': subnet['network_id'],
'ip_version': subnet['ip_version'],
@ -627,6 +628,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
def _make_port_dict(self, port, fields=None):
res = {"id": port["id"],
'name': port['name'],
"network_id": port["network_id"],
'tenant_id': port['tenant_id'],
"mac_address": port["mac_address"],
@ -694,6 +696,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
self._validate_subnet_cidr(network, s['cidr'])
subnet = models_v2.Subnet(tenant_id=tenant_id,
id=s.get('id') or utils.str_uuid(),
name=s['name'],
network_id=s['network_id'],
ip_version=s['ip_version'],
cidr=s['cidr'],
@ -765,6 +768,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
ips = self._allocate_ips_for_port(context, network, port)
port = models_v2.Port(tenant_id=tenant_id,
name=p['name'],
id=p.get('id') or utils.str_uuid(),
network_id=p['network_id'],
mac_address=p['mac_address'],

View File

@ -88,6 +88,7 @@ class IPAllocation(model_base.BASEV2):
class Port(model_base.BASEV2, HasId, HasTenant):
"""Represents a port on a quantum v2 network."""
name = sa.Column(sa.String(255))
network_id = sa.Column(sa.String(36), sa.ForeignKey("networks.id"),
nullable=False)
fixed_ips = orm.relationship(IPAllocation, backref='ports', lazy="dynamic")
@ -103,6 +104,7 @@ class Subnet(model_base.BASEV2, HasId, HasTenant):
When a subnet is created the first and last entries will be created. These
are used for the IP allocation.
"""
name = sa.Column(sa.String(255))
network_id = sa.Column(sa.String(36), sa.ForeignKey('networks.id'))
ip_version = sa.Column(sa.Integer, nullable=False)
cidr = sa.Column(sa.String(64), nullable=False)

View File

@ -525,8 +525,8 @@ class JSONV2TestCase(APIv2TestBase):
self.assertEqual(res.status_int, exc.HTTPBadRequest.code)
def test_create_missing_attr(self):
data = {'network': {'what': 'who', 'tenant_id': _uuid()}}
res = self.api.post_json(_get_path('networks'), data,
data = {'port': {'what': 'who', 'tenant_id': _uuid()}}
res = self.api.post_json(_get_path('ports'), data,
expect_errors=True)
self.assertEqual(res.status_int, 422)
@ -562,16 +562,16 @@ class JSONV2TestCase(APIv2TestBase):
self.assertEqual(res.status_int, exc.HTTPBadRequest.code)
def test_create_bulk_missing_attr(self):
data = {'networks': [{'what': 'who', 'tenant_id': _uuid()}]}
res = self.api.post_json(_get_path('networks'), data,
data = {'ports': [{'what': 'who', 'tenant_id': _uuid()}]}
res = self.api.post_json(_get_path('ports'), data,
expect_errors=True)
self.assertEqual(res.status_int, 422)
def test_create_bulk_partial_body(self):
data = {'networks': [{'name': 'net1', 'admin_state_up': True,
'tenant_id': _uuid()},
{'tenant_id': _uuid()}]}
res = self.api.post_json(_get_path('networks'), data,
data = {'ports': [{'device_id': 'device_1',
'tenant_id': _uuid()},
{'tenant_id': _uuid()}]}
res = self.api.post_json(_get_path('ports'), data,
expect_errors=True)
self.assertEqual(res.status_int, 422)
@ -579,9 +579,10 @@ class JSONV2TestCase(APIv2TestBase):
net_id = _uuid()
tenant_id = _uuid()
device_id = _uuid()
initial_input = {'port': {'network_id': net_id, 'tenant_id': tenant_id,
'device_id': device_id,
'admin_state_up': True}}
initial_input = {'port': {'name': '', 'network_id': net_id,
'tenant_id': tenant_id,
'device_id': device_id,
'admin_state_up': True}}
full_input = {'port': {'admin_state_up': True,
'mac_address': attributes.ATTR_NOT_SPECIFIED,
'fixed_ips': attributes.ATTR_NOT_SPECIFIED,

View File

@ -151,7 +151,8 @@ class QuantumDbPluginV2TestCase(unittest2.TestCase):
content_type = 'application/' + fmt
data = {'port': {'network_id': net_id,
'tenant_id': self._tenant_id}}
for arg in ('admin_state_up', 'device_id', 'mac_address', 'fixed_ips'):
for arg in ('admin_state_up', 'device_id', 'mac_address',
'name', 'fixed_ips'):
# Arg must be present and not empty
if arg in kwargs and kwargs[arg]:
data['port'][arg] = kwargs[arg]
@ -220,16 +221,18 @@ class QuantumDbPluginV2TestCase(unittest2.TestCase):
self._delete('subnets', subnet['subnet']['id'])
@contextlib.contextmanager
def port(self, subnet=None, fixed_ips=None, fmt='json'):
def port(self, subnet=None, fixed_ips=None, fmt='json', **kwargs):
if not subnet:
with self.subnet() as subnet:
net_id = subnet['subnet']['network_id']
port = self._make_port(fmt, net_id, fixed_ips=fixed_ips)
port = self._make_port(fmt, net_id, fixed_ips=fixed_ips,
**kwargs)
yield port
self._delete('ports', port['port']['id'])
else:
net_id = subnet['subnet']['network_id']
port = self._make_port(fmt, net_id, fixed_ips=fixed_ips)
port = self._make_port(fmt, net_id, fixed_ips=fixed_ips,
**kwargs)
yield port
self._delete('ports', port['port']['id'])
@ -340,13 +343,14 @@ class TestV2HTTPResponse(QuantumDbPluginV2TestCase):
class TestPortsV2(QuantumDbPluginV2TestCase):
def test_create_port_json(self):
keys = [('admin_state_up', True), ('status', 'ACTIVE')]
with self.port() as port:
with self.port(name='myname') as port:
for k, v in keys:
self.assertEquals(port['port'][k], v)
self.assertTrue('mac_address' in port['port'])
ips = port['port']['fixed_ips']
self.assertEquals(len(ips), 1)
self.assertEquals(ips[0]['ip_address'], '10.0.0.2')
self.assertEquals('myname', port['port']['name'])
def test_create_port_bad_tenant(self):
with self.network() as network:
@ -854,8 +858,9 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase):
def test_create_subnet(self):
gateway_ip = '10.0.0.1'
cidr = '10.0.0.0/24'
self._test_create_subnet(gateway_ip=gateway_ip,
cidr=cidr)
subnet = self._test_create_subnet(gateway_ip=gateway_ip,
cidr=cidr)
self.assertTrue('name' in subnet['subnet'])
def test_create_two_subnets(self):
gateway_ips = ['10.0.0.1', '10.0.1.1']