Standardize use of nova.db

We already mostly use:
from nova import db
instead of:
import nova.db.api

So change remaining few cases to use nova.db instead of nova.db.api.
Help make code more uniform and easy to work with.

Change-Id: I6183352c4bbe77b88a5b277788f588b0b48c6476
This commit is contained in:
Joe Gordon
2013-07-01 12:46:23 +02:00
parent d891b16473
commit cf34ee9431
11 changed files with 43 additions and 46 deletions

View File

@@ -146,9 +146,9 @@ class CinderCloudTestCase(test.TestCase):
self.stubs.Set(rpc, 'cast', rpc.call) self.stubs.Set(rpc, 'cast', rpc.call)
# make sure we can map ami-00000001/2 to a uuid in FakeImageService # make sure we can map ami-00000001/2 to a uuid in FakeImageService
db.api.s3_image_create(self.context, db.s3_image_create(self.context,
'cedef40a-ed67-4d10-800e-17455edce175') 'cedef40a-ed67-4d10-800e-17455edce175')
db.api.s3_image_create(self.context, db.s3_image_create(self.context,
'76fa36fc-c930-4bf3-8c8a-ea2a2420deb6') '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6')
def tearDown(self): def tearDown(self):

View File

@@ -168,9 +168,9 @@ class CloudTestCase(test.TestCase):
self.stubs.Set(rpc, 'cast', rpc.call) self.stubs.Set(rpc, 'cast', rpc.call)
# make sure we can map ami-00000001/2 to a uuid in FakeImageService # make sure we can map ami-00000001/2 to a uuid in FakeImageService
db.api.s3_image_create(self.context, db.s3_image_create(self.context,
'cedef40a-ed67-4d10-800e-17455edce175') 'cedef40a-ed67-4d10-800e-17455edce175')
db.api.s3_image_create(self.context, db.s3_image_create(self.context,
'76fa36fc-c930-4bf3-8c8a-ea2a2420deb6') '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6')
def tearDown(self): def tearDown(self):
@@ -2414,7 +2414,7 @@ class CloudTestCase(test.TestCase):
# NOTE(yamahata): create ami-3 ... ami-6 # NOTE(yamahata): create ami-3 ... ami-6
# ami-1 and ami-2 is already created by setUp() # ami-1 and ami-2 is already created by setUp()
for i in range(3, 7): for i in range(3, 7):
db.api.s3_image_create(self.context, 'ami-%d' % i) db.s3_image_create(self.context, 'ami-%d' % i)
self.stubs.Set(fake._FakeImageService, 'show', fake_show) self.stubs.Set(fake._FakeImageService, 'show', fake_show)

View File

@@ -100,9 +100,9 @@ class EC2ValidateTestCase(test.TestCase):
self.stubs.Set(rpc, 'cast', rpc.call) self.stubs.Set(rpc, 'cast', rpc.call)
# make sure we can map ami-00000001/2 to a uuid in FakeImageService # make sure we can map ami-00000001/2 to a uuid in FakeImageService
db.api.s3_image_create(self.context, db.s3_image_create(self.context,
'cedef40a-ed67-4d10-800e-17455edce175') 'cedef40a-ed67-4d10-800e-17455edce175')
db.api.s3_image_create(self.context, db.s3_image_create(self.context,
'76fa36fc-c930-4bf3-8c8a-ea2a2420deb6') '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6')
def tearDown(self): def tearDown(self):

View File

@@ -18,7 +18,7 @@
import datetime import datetime
from nova.api.openstack import compute from nova.api.openstack import compute
import nova.db.api from nova import db
from nova.openstack.common import jsonutils from nova.openstack.common import jsonutils
import nova.openstack.common.rpc import nova.openstack.common.rpc
from nova import test from nova import test
@@ -64,21 +64,21 @@ class DiskConfigTestCase(test.TestCase):
if id_ == instance['id']: if id_ == instance['id']:
return instance return instance
self.stubs.Set(nova.db, 'instance_get', fake_instance_get) self.stubs.Set(db, 'instance_get', fake_instance_get)
def fake_instance_get_by_uuid(context, uuid): def fake_instance_get_by_uuid(context, uuid):
for instance in FAKE_INSTANCES: for instance in FAKE_INSTANCES:
if uuid == instance['uuid']: if uuid == instance['uuid']:
return instance return instance
self.stubs.Set(nova.db, 'instance_get_by_uuid', self.stubs.Set(db, 'instance_get_by_uuid',
fake_instance_get_by_uuid) fake_instance_get_by_uuid)
def fake_instance_get_all(context, *args, **kwargs): def fake_instance_get_all(context, *args, **kwargs):
return FAKE_INSTANCES return FAKE_INSTANCES
self.stubs.Set(nova.db, 'instance_get_all', fake_instance_get_all) self.stubs.Set(db, 'instance_get_all', fake_instance_get_all)
self.stubs.Set(nova.db, 'instance_get_all_by_filters', self.stubs.Set(db, 'instance_get_all_by_filters',
fake_instance_get_all) fake_instance_get_all)
def fake_instance_create(context, inst_, session=None): def fake_instance_create(context, inst_, session=None):
@@ -103,27 +103,27 @@ class DiskConfigTestCase(test.TestCase):
def fake_instance_get_for_create(context, id_, *args, **kwargs): def fake_instance_get_for_create(context, id_, *args, **kwargs):
return (inst, inst) return (inst, inst)
self.stubs.Set(nova.db, 'instance_update_and_get_original', self.stubs.Set(db, 'instance_update_and_get_original',
fake_instance_get_for_create) fake_instance_get_for_create)
def fake_instance_get_all_for_create(context, *args, **kwargs): def fake_instance_get_all_for_create(context, *args, **kwargs):
return [inst] return [inst]
self.stubs.Set(nova.db, 'instance_get_all', self.stubs.Set(db, 'instance_get_all',
fake_instance_get_all_for_create) fake_instance_get_all_for_create)
self.stubs.Set(nova.db, 'instance_get_all_by_filters', self.stubs.Set(db, 'instance_get_all_by_filters',
fake_instance_get_all_for_create) fake_instance_get_all_for_create)
def fake_instance_add_security_group(context, instance_id, def fake_instance_add_security_group(context, instance_id,
security_group_id): security_group_id):
pass pass
self.stubs.Set(nova.db, self.stubs.Set(db,
'instance_add_security_group', 'instance_add_security_group',
fake_instance_add_security_group) fake_instance_add_security_group)
return inst return inst
self.stubs.Set(nova.db, 'instance_create', fake_instance_create) self.stubs.Set(db, 'instance_create', fake_instance_create)
self.app = compute.APIRouter(init_only=('servers', 'images')) self.app = compute.APIRouter(init_only=('servers', 'images'))

View File

@@ -16,7 +16,6 @@
# under the License. # under the License.
from nova.api.openstack import compute from nova.api.openstack import compute
import nova.db.api
from nova.openstack.common import jsonutils from nova.openstack.common import jsonutils
import nova.openstack.common.rpc import nova.openstack.common.rpc
from nova import test from nova import test

View File

@@ -589,16 +589,16 @@ class DisabledFlavorsWithRealDBTest(test.TestCase):
self.admin_context = context.get_admin_context() self.admin_context = context.get_admin_context()
self.disabled_type = self._create_disabled_instance_type() self.disabled_type = self._create_disabled_instance_type()
self.inst_types = db.api.instance_type_get_all(self.admin_context) self.inst_types = db.instance_type_get_all(self.admin_context)
def tearDown(self): def tearDown(self):
db.api.instance_type_destroy(self.admin_context, db.instance_type_destroy(self.admin_context,
self.disabled_type['name']) self.disabled_type['name'])
super(DisabledFlavorsWithRealDBTest, self).tearDown() super(DisabledFlavorsWithRealDBTest, self).tearDown()
def _create_disabled_instance_type(self): def _create_disabled_instance_type(self):
inst_types = db.api.instance_type_get_all(self.admin_context) inst_types = db.instance_type_get_all(self.admin_context)
inst_type = inst_types[0] inst_type = inst_types[0]
@@ -608,7 +608,7 @@ class DisabledFlavorsWithRealDBTest(test.TestCase):
[int(flavor['flavorid']) for flavor in inst_types]) + 1) [int(flavor['flavorid']) for flavor in inst_types]) + 1)
inst_type['disabled'] = True inst_type['disabled'] = True
disabled_type = db.api.instance_type_create(self.admin_context, disabled_type = db.instance_type_create(self.admin_context,
inst_type) inst_type)
return disabled_type return disabled_type

View File

@@ -689,18 +689,17 @@ class DisabledFlavorsWithRealDBTest(test.TestCase):
self.admin_context = context.get_admin_context() self.admin_context = context.get_admin_context()
self.disabled_type = self._create_disabled_instance_type() self.disabled_type = self._create_disabled_instance_type()
self.inst_types = db.api.instance_type_get_all( self.inst_types = db.instance_type_get_all(
self.admin_context) self.admin_context)
def tearDown(self): def tearDown(self):
db.api.instance_type_destroy( db.instance_type_destroy(
self.admin_context, self.disabled_type['name']) self.admin_context, self.disabled_type['name'])
super(DisabledFlavorsWithRealDBTest, self).tearDown() super(DisabledFlavorsWithRealDBTest, self).tearDown()
def _create_disabled_instance_type(self): def _create_disabled_instance_type(self):
inst_types = db.api.instance_type_get_all( inst_types = db.instance_type_get_all(self.admin_context)
self.admin_context)
inst_type = inst_types[0] inst_type = inst_types[0]
@@ -710,7 +709,7 @@ class DisabledFlavorsWithRealDBTest(test.TestCase):
[int(flavor['flavorid']) for flavor in inst_types]) + 1) [int(flavor['flavorid']) for flavor in inst_types]) + 1)
inst_type['disabled'] = True inst_type['disabled'] = True
disabled_type = db.api.instance_type_create( disabled_type = db.instance_type_create(
self.admin_context, inst_type) self.admin_context, inst_type)
return disabled_type return disabled_type

View File

@@ -4301,7 +4301,7 @@ class VirtualInterfaceTestCase(test.TestCase, ModelsObjectComparatorMixin):
class NetworkTestCase(test.TestCase, ModelsObjectComparatorMixin): class NetworkTestCase(test.TestCase, ModelsObjectComparatorMixin):
"""Tests for db/api/network_* methods.""" """Tests for db.api.network_* methods."""
def setUp(self): def setUp(self):
super(NetworkTestCase, self).setUp() super(NetworkTestCase, self).setUp()

View File

@@ -25,7 +25,7 @@ import fixtures
from nova.api.ec2 import ec2utils from nova.api.ec2 import ec2utils
from nova import context from nova import context
import nova.db.api from nova import db
from nova import exception from nova import exception
from nova.image import s3 from nova.image import s3
from nova import test from nova import test
@@ -89,12 +89,12 @@ class TestS3ImageService(test.TestCase):
self.useFixture(fixtures.FakeLogger('boto')) self.useFixture(fixtures.FakeLogger('boto'))
# set up 3 fixtures to test shows, should have id '1', '2', and '3' # set up 3 fixtures to test shows, should have id '1', '2', and '3'
nova.db.api.s3_image_create(self.context, db.s3_image_create(self.context,
'155d900f-4e14-4e4c-a73d-069cbf4541e6') '155d900f-4e14-4e4c-a73d-069cbf4541e6')
nova.db.api.s3_image_create(self.context, db.s3_image_create(self.context,
'a2459075-d96c-40d5-893e-577ff92e721c') 'a2459075-d96c-40d5-893e-577ff92e721c')
nova.db.api.s3_image_create(self.context, db.s3_image_create(self.context,
'76fa36fc-c930-4bf3-8c8a-ea2a2420deb6') '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6')
fake.stub_out_image_service(self.stubs) fake.stub_out_image_service(self.stubs)
self.image_service = s3.S3ImageService() self.image_service = s3.S3ImageService()
@@ -146,8 +146,7 @@ class TestS3ImageService(test.TestCase):
# and test that S3ImageService does the correct mapping for # and test that S3ImageService does the correct mapping for
# us. We can't put fake bad or pending states in the real fake # us. We can't put fake bad or pending states in the real fake
# image service as it causes other tests to fail # image service as it causes other tests to fail
self.stubs.Set(nova.tests.image.fake._FakeImageService, 'show', self.stubs.Set(fake._FakeImageService, 'show', my_fake_show)
my_fake_show)
ret_image = self.image_service.show(self.context, '1') ret_image = self.image_service.show(self.context, '1')
self.assertEqual(ret_image['properties']['image_state'], 'pending') self.assertEqual(ret_image['properties']['image_state'], 'pending')
ret_image = self.image_service.show(self.context, '2') ret_image = self.image_service.show(self.context, '2')

View File

@@ -2139,7 +2139,7 @@ class FloatingIPTestCase(test.TestCase):
self.network.associate_floating_ip(ctxt, 'fl_ip', 'fix_ip', True) self.network.associate_floating_ip(ctxt, 'fl_ip', 'fix_ip', True)
def test_double_deallocation(self): def test_double_deallocation(self):
instance_ref = db.api.instance_create(self.context, instance_ref = db.instance_create(self.context,
{"project_id": self.project_id}) {"project_id": self.project_id})
# Run it twice to make it fault if it does not handle # Run it twice to make it fault if it does not handle
# instances without fixed networks # instances without fixed networks
@@ -2152,14 +2152,14 @@ class FloatingIPTestCase(test.TestCase):
def test_deallocation_deleted_instance(self): def test_deallocation_deleted_instance(self):
self.stubs.Set(self.network, '_teardown_network_on_host', self.stubs.Set(self.network, '_teardown_network_on_host',
lambda *args, **kwargs: None) lambda *args, **kwargs: None)
instance = db.api.instance_create(self.context, { instance = db.instance_create(self.context, {
'project_id': self.project_id, 'deleted': True}) 'project_id': self.project_id, 'deleted': True})
network = db.api.network_create_safe(self.context.elevated(), { network = db.network_create_safe(self.context.elevated(), {
'project_id': self.project_id}) 'project_id': self.project_id})
fixed = db.fixed_ip_create(self.context, {'allocated': True, fixed = db.fixed_ip_create(self.context, {'allocated': True,
'instance_uuid': instance['uuid'], 'address': '10.1.1.1', 'instance_uuid': instance['uuid'], 'address': '10.1.1.1',
'network_id': network['id']}) 'network_id': network['id']})
db.api.floating_ip_create(self.context, { db.floating_ip_create(self.context, {
'address': '10.10.10.10', 'instance_uuid': instance['uuid'], 'address': '10.10.10.10', 'instance_uuid': instance['uuid'],
'fixed_ip_id': fixed['id'], 'fixed_ip_id': fixed['id'],
'project_id': self.project_id}) 'project_id': self.project_id})
@@ -2169,17 +2169,17 @@ class FloatingIPTestCase(test.TestCase):
def test_deallocation_duplicate_floating_ip(self): def test_deallocation_duplicate_floating_ip(self):
self.stubs.Set(self.network, '_teardown_network_on_host', self.stubs.Set(self.network, '_teardown_network_on_host',
lambda *args, **kwargs: None) lambda *args, **kwargs: None)
instance = db.api.instance_create(self.context, { instance = db.instance_create(self.context, {
'project_id': self.project_id}) 'project_id': self.project_id})
network = db.api.network_create_safe(self.context.elevated(), { network = db.network_create_safe(self.context.elevated(), {
'project_id': self.project_id}) 'project_id': self.project_id})
fixed = db.fixed_ip_create(self.context, {'allocated': True, fixed = db.fixed_ip_create(self.context, {'allocated': True,
'instance_uuid': instance['uuid'], 'address': '10.1.1.1', 'instance_uuid': instance['uuid'], 'address': '10.1.1.1',
'network_id': network['id']}) 'network_id': network['id']})
db.api.floating_ip_create(self.context, { db.floating_ip_create(self.context, {
'address': '10.10.10.10', 'address': '10.10.10.10',
'deleted': True}) 'deleted': True})
db.api.floating_ip_create(self.context, { db.floating_ip_create(self.context, {
'address': '10.10.10.10', 'instance_uuid': instance['uuid'], 'address': '10.10.10.10', 'instance_uuid': instance['uuid'],
'fixed_ip_id': fixed['id'], 'fixed_ip_id': fixed['id'],
'project_id': self.project_id}) 'project_id': self.project_id})

View File

@@ -73,7 +73,7 @@ def find_orphaned_instances(xenapi):
for vm_ref, vm_rec in _get_applicable_vm_recs(xenapi): for vm_ref, vm_rec in _get_applicable_vm_recs(xenapi):
try: try:
uuid = vm_rec['other_config']['nova_uuid'] uuid = vm_rec['other_config']['nova_uuid']
instance = db.api.instance_get_by_uuid(ctxt, uuid) instance = db.instance_get_by_uuid(ctxt, uuid)
except (KeyError, exception.InstanceNotFound): except (KeyError, exception.InstanceNotFound):
# NOTE(jk0): Err on the side of caution here. If we don't know # NOTE(jk0): Err on the side of caution here. If we don't know
# anything about the particular instance, ignore it. # anything about the particular instance, ignore it.