Remove project_id from db.add_item interface

Since there is no part of code which adds an db item with other project,
this backdoor is not needed.

This patch removes this argument and its proxying by util functions.

This reverts commit b51eb4f350.

Change-Id: I70b5d2e7246152ac2b1014f51f89f44862acab44
This commit is contained in:
Feodor Tersin 2015-08-12 19:51:31 +03:00
parent bcf40ac932
commit 2e40c234f8
19 changed files with 39 additions and 70 deletions

View File

@ -302,21 +302,16 @@ def register_auto_create_db_item_extension(kind, extension):
_auto_create_db_item_extensions[kind] = extension
# TODO(Alex): The project_id passing mechanism can be potentially
# reconsidered in future.
def auto_create_db_item(context, kind, os_id, project_id=None,
**extension_kwargs):
def auto_create_db_item(context, kind, os_id, **extension_kwargs):
item = {'os_id': os_id}
extension = _auto_create_db_item_extensions.get(kind)
if extension:
extension(context, item, **extension_kwargs)
return db_api.add_item(context, kind, item, project_id=project_id)
return db_api.add_item(context, kind, item)
# TODO(Alex): The project_id passing mechanism can be potentially
# reconsidered in future.
def get_db_item_by_os_id(context, kind, os_id, items_by_os_id=None,
project_id=None, **extension_kwargs):
**extension_kwargs):
"""Get DB item by OS id (create if it doesn't exist).
Args:
@ -346,8 +341,7 @@ def get_db_item_by_os_id(context, kind, os_id, items_by_os_id=None,
item = next((i for i in db_api.get_items(context, kind)
if i['os_id'] == os_id), None)
if not item:
item = auto_create_db_item(context, kind, os_id, project_id=project_id,
**extension_kwargs)
item = auto_create_db_item(context, kind, os_id, **extension_kwargs)
if items_by_os_id is not None:
items_by_os_id[os_id] = item
return item

View File

@ -79,12 +79,12 @@ IMPL = EC2DBAPI()
LOG = logging.getLogger(__name__)
def add_item(context, kind, data, project_id=None):
return IMPL.add_item(context, kind, data, project_id=project_id)
def add_item(context, kind, data):
return IMPL.add_item(context, kind, data)
def add_item_id(context, kind, os_id, project_id=None):
return IMPL.add_item_id(context, kind, os_id, project_id=project_id)
return IMPL.add_item_id(context, kind, os_id, project_id)
def update_item(context, item):

View File

@ -90,12 +90,10 @@ def _new_id(kind):
@require_context
def add_item(context, kind, data, project_id=None):
if not project_id:
project_id = context.project_id
def add_item(context, kind, data):
item_ref = models.Item()
item_ref.update({
"project_id": project_id,
"project_id": context.project_id,
"id": _new_id(kind),
})
item_ref.update(_pack_item_data(data))
@ -107,14 +105,14 @@ def add_item(context, kind, data, project_id=None):
raise
item_ref = (model_query(context, models.Item).
filter_by(os_id=data["os_id"]).
filter(or_(models.Item.project_id == project_id,
filter(or_(models.Item.project_id == context.project_id,
models.Item.project_id.is_(None))).
filter(models.Item.id.like('%s-%%' % kind)).
one())
item_data = _unpack_item_data(item_ref)
item_data.update(data)
item_ref.update(_pack_item_data(item_data))
item_ref.project_id = project_id
item_ref.project_id = context.project_id
item_ref.save()
return _unpack_item_data(item_ref)

View File

@ -64,8 +64,7 @@ class AddressTestCase(base.ApiTestCase):
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'eipalloc',
tools.purge_dict(fakes.DB_ADDRESS_1,
('id', 'vpc_id')),
project_id=None)
('id', 'vpc_id')))
self.neutron.create_floatingip.assert_called_once_with(
{'floatingip': {
'floating_network_id':

View File

@ -34,8 +34,7 @@ class CustomerGatewayTestCase(base.ApiTestCase):
resp)
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'cgw',
{'ip_address': fakes.IP_CUSTOMER_GATEWAY_ADDRESS_2},
project_id=None)
{'ip_address': fakes.IP_CUSTOMER_GATEWAY_ADDRESS_2})
resp = self.execute('CreateCustomerGateway',
{'IpAddress': fakes.IP_CUSTOMER_GATEWAY_ADDRESS_2,

View File

@ -178,7 +178,7 @@ class EC2UtilsTestCase(testtools.TestCase):
item_id = ec2utils.os_id_to_ec2_id(fake_context, 'fake', fake_os_id)
self.assertEqual(fake_id, item_id)
db_api.add_item_id.assert_called_once_with(
fake_context, 'fake', fake_os_id, project_id=None)
fake_context, 'fake', fake_os_id, None)
# no item in cache, item isn't found
db_api.reset_mock()
@ -189,7 +189,7 @@ class EC2UtilsTestCase(testtools.TestCase):
self.assertIn(fake_os_id, ids_cache)
self.assertEqual(fake_id, ids_cache[fake_os_id])
db_api.add_item_id.assert_called_once_with(
fake_context, 'fake', fake_os_id, project_id=None)
fake_context, 'fake', fake_os_id, None)
# no item in cache, item is found
db_api.reset_mock()

View File

@ -125,8 +125,7 @@ class ImageTestCase(base.ApiTestCase):
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'ami', {'os_id': image_id,
'is_public': False,
'description': 'fake desc'},
project_id=None)
'description': 'fake desc'})
if not no_reboot:
os_instance.stop.assert_called_once_with()
os_instance.get.assert_called_once_with()
@ -205,8 +204,7 @@ class ImageTestCase(base.ApiTestCase):
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'ami', {'os_id': fakes.ID_OS_IMAGE_2,
'is_public': False,
'description': None},
project_id=None)
'description': None})
self.assertEqual(1, self.glance.images.create.call_count)
self.assertEqual((), self.glance.images.create.call_args[0])
self.assertIn('properties', self.glance.images.create.call_args[1])

View File

@ -128,8 +128,7 @@ class InstanceTestCase(base.ApiTestCase):
nics=[{'port-id': fakes.ID_OS_PORT_1}],
key_name=None, userdata=None)
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'i', tools.purge_dict(fakes.DB_INSTANCE_1, ('id',)),
project_id=None)
mock.ANY, 'i', tools.purge_dict(fakes.DB_INSTANCE_1, ('id',)))
(self.network_interface_api.
_attach_network_interface_item.assert_called_once_with(
mock.ANY, fakes.DB_NETWORK_INTERFACE_1,
@ -270,8 +269,7 @@ class InstanceTestCase(base.ApiTestCase):
[0, 1] * 2,
[True, False, True, False])]))
self.db_api.add_item.assert_has_calls([
mock.call(mock.ANY, 'i', tools.purge_dict(db_instance, ['id']),
project_id=None)
mock.call(mock.ANY, 'i', tools.purge_dict(db_instance, ['id']))
for db_instance in self.DB_INSTANCES])
@mock.patch('ec2api.api.instance._parse_block_device_mapping')
@ -330,7 +328,7 @@ class InstanceTestCase(base.ApiTestCase):
'client_token': 'fake_client_token'}
db_instance.update(extra_db_instance)
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'i', db_instance, project_id=None)
mock.ANY, 'i', db_instance)
self.db_api.reset_mock()
parse_block_device_mapping.assert_called_once_with(
mock.ANY,
@ -1720,10 +1718,9 @@ class InstancePrivateTestCase(test_base.BaseTestCase):
fake_context, instance, os_instance, [], {},
None, None, fake_flavors, [])
db_api.add_item_id.assert_has_calls(
[mock.call(mock.ANY, 'ami', os_instance.image['id'],
project_id=None),
mock.call(mock.ANY, 'aki', kernel_id, project_id=None),
mock.call(mock.ANY, 'ari', ramdisk_id, project_id=None)],
[mock.call(mock.ANY, 'ami', os_instance.image['id'], None),
mock.call(mock.ANY, 'aki', kernel_id, None),
mock.call(mock.ANY, 'ari', ramdisk_id, None)],
any_order=True)
@mock.patch('cinderclient.client.Client')

View File

@ -39,7 +39,7 @@ class IgwTestCase(base.ApiTestCase):
igw = resp['internetGateway']
self.assertThat(fakes.EC2_IGW_2, matchers.DictMatches(igw))
self.db_api.add_item.assert_called_with(
mock.ANY, 'igw', {}, project_id=None)
mock.ANY, 'igw', {})
def test_attach_igw(self):
self.configure(external_network=fakes.NAME_OS_PUBLIC_NETWORK)

View File

@ -41,8 +41,7 @@ class NetworkInterfaceTestCase(base.ApiTestCase):
matchers.DictMatches(resp['networkInterface']))
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'eni',
tools.purge_dict(fakes.DB_NETWORK_INTERFACE_1, ('id',)),
project_id=None)
tools.purge_dict(fakes.DB_NETWORK_INTERFACE_1, ('id',)))
if auto_ips:
self.neutron.create_port.assert_called_once_with(
{'port':
@ -129,8 +128,7 @@ class NetworkInterfaceTestCase(base.ApiTestCase):
'device_index',
'instance_id',
'delete_on_termination',
'attach_time')),
project_id=None)
'attach_time')))
self.neutron.update_port.assert_called_once_with(
fakes.ID_OS_PORT_2,
{'port': {'name':

View File

@ -44,8 +44,7 @@ class RouteTableTestCase(base.ApiTestCase):
'rtb',
{'vpc_id': fakes.ID_EC2_VPC_1,
'routes': [{'destination_cidr_block': fakes.CIDR_VPC_1,
'gateway_id': None}]},
project_id=None)
'gateway_id': None}]})
self.db_api.get_item_by_id.assert_called_once_with(
mock.ANY, fakes.ID_EC2_VPC_1)

View File

@ -55,8 +55,7 @@ class SecurityGroupTestCase(base.ApiTestCase):
'groupname', 'Group description')
db_group = tools.purge_dict(fakes.DB_SECURITY_GROUP_2, ('id',))
db_group['vpc_id'] = None
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'sg', db_group, project_id=None)
self.db_api.add_item.assert_called_once_with(mock.ANY, 'sg', db_group)
self.nova.security_groups.reset_mock()
self.db_api.add_item.reset_mock()
@ -70,8 +69,7 @@ class SecurityGroupTestCase(base.ApiTestCase):
self.assertEqual(fakes.ID_EC2_SECURITY_GROUP_2, resp['groupId'])
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'sg',
tools.purge_dict(fakes.DB_SECURITY_GROUP_2, ('id',)),
project_id=None)
tools.purge_dict(fakes.DB_SECURITY_GROUP_2, ('id',)))
self.nova.security_groups.create.assert_called_once_with(
'groupname', 'Group description')
@ -328,8 +326,7 @@ class SecurityGroupTestCase(base.ApiTestCase):
resp = self.execute('DescribeSecurityGroups', {})
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'sg',
tools.purge_dict(fakes.DB_SECURITY_GROUP_1, ('id',)),
project_id=None)
tools.purge_dict(fakes.DB_SECURITY_GROUP_1, ('id',)))
self.nova.security_groups.create.assert_called_once_with(
fakes.ID_EC2_VPC_1, 'Default VPC security group')

View File

@ -113,8 +113,7 @@ class SnapshotTestCase(base.ApiTestCase):
self.assertThat(fakes.EC2_SNAPSHOT_1, matchers.DictMatches(resp))
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'snap',
tools.purge_dict(fakes.DB_SNAPSHOT_1, ('id',)),
project_id=None)
tools.purge_dict(fakes.DB_SNAPSHOT_1, ('id',)))
self.cinder.volume_snapshots.create.assert_called_once_with(
fakes.ID_OS_VOLUME_2, force=True, display_description=None)

View File

@ -44,8 +44,7 @@ class SubnetTestCase(base.ApiTestCase):
resp['subnet']))
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'subnet',
tools.purge_dict(subnet_1, ('id',)),
project_id=None)
tools.purge_dict(subnet_1, ('id',)))
self.neutron.create_network.assert_called_once_with(
{'network': {}})
self.neutron.update_network.assert_called_once_with(

View File

@ -116,8 +116,7 @@ class VolumeTestCase(base.ApiTestCase):
self.assertThat(fakes.EC2_VOLUME_1, matchers.DictMatches(resp))
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'vol',
tools.purge_dict(fakes.DB_VOLUME_1, ('id',)),
project_id=None)
tools.purge_dict(fakes.DB_VOLUME_1, ('id',)))
self.cinder.volumes.create.assert_called_once_with(
None, snapshot_id=None, volume_type=None,
@ -137,8 +136,7 @@ class VolumeTestCase(base.ApiTestCase):
self.assertThat(fakes.EC2_VOLUME_3, matchers.DictMatches(resp))
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'vol',
tools.purge_dict(fakes.DB_VOLUME_3, ('id',)),
project_id=None)
tools.purge_dict(fakes.DB_VOLUME_3, ('id',)))
self.cinder.volumes.create.assert_called_once_with(
None, snapshot_id=fakes.ID_OS_SNAPSHOT_1, volume_type=None,

View File

@ -49,13 +49,11 @@ class VpcTestCase(base.ApiTestCase):
self.db_api.add_item.assert_any_call(
mock.ANY, 'vpc',
tools.purge_dict(fakes.DB_VPC_1,
('id', 'vpc_id', 'route_table_id')),
project_id=None)
('id', 'vpc_id', 'route_table_id')))
self.db_api.add_item.assert_any_call(
mock.ANY, 'rtb',
tools.purge_dict(fakes.DB_ROUTE_TABLE_1,
('id',)),
project_id=None)
('id',)))
self.db_api.update_item.assert_called_once_with(
mock.ANY,
fakes.DB_VPC_1)

View File

@ -68,8 +68,7 @@ class VpnConnectionTestCase(base.ApiTestCase):
'os_ipsec_site_connections': {}})
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'vpn',
tools.purge_dict(new_vpn_connection_1, ('id', 'vpc_id', 'os_id')),
project_id=None)
tools.purge_dict(new_vpn_connection_1, ('id', 'vpc_id', 'os_id')))
self.neutron.update_ikepolicy.assert_called_once_with(
fakes.ID_OS_IKEPOLICY_1,
{'ikepolicy': {'name': fakes.ID_EC2_VPN_CONNECTION_1}})

View File

@ -45,8 +45,7 @@ class VpnGatewayTestCase(base.ApiTestCase):
resp = self.execute('CreateVpnGateway',
{'Type': 'ipsec.1'})
self.assertEqual({'vpnGateway': fakes.EC2_VPN_GATEWAY_2}, resp)
self.db_api.add_item.assert_called_once_with(
mock.ANY, 'vgw', {}, project_id=None)
self.db_api.add_item.assert_called_once_with(mock.ANY, 'vgw', {})
@mock.patch('ec2api.api.vpn_connection._reset_vpn_connections',
wraps=vpn_connection_api._reset_vpn_connections)

View File

@ -49,14 +49,12 @@ def patch_dict(dict1, dict2, trash_iter):
def get_db_api_add_item(item_id_dict):
"""Generate db_api.add_item mock function."""
def db_api_add_item(context, kind, data, project_id=None):
def db_api_add_item(context, kind, data):
if isinstance(item_id_dict, dict):
item_id = item_id_dict[kind]
else:
item_id = item_id_dict
data = update_dict(data, {'id': item_id})
if project_id:
data = update_dict(data, {'project_id': project_id})
data.setdefault('os_id')
data.setdefault('vpc_id')
return data