Move 'all_tenants' options to context
In before, bay and x509keypair supported list resources from all tenants. It is desirable to generalize this capability while reducing duplicated codes. Therefore, moving 'all_tenants' options to context. Change-Id: Icfe31a6f2ac2e21fa7f377e244764f10892d25c7 Partially-Implements: blueprint autoscale-bay
This commit is contained in:
parent
aeb401e5a5
commit
fa5bcee4ab
|
@ -22,7 +22,7 @@ class RequestContext(context.RequestContext):
|
|||
project_name=None, project_id=None, is_admin=False,
|
||||
is_public_api=False, read_only=False, show_deleted=False,
|
||||
request_id=None, trust_id=None, auth_token_info=None,
|
||||
**kwargs):
|
||||
all_tenants=False, **kwargs):
|
||||
"""Stores several additional request parameters:
|
||||
|
||||
:param domain_id: The ID of the domain.
|
||||
|
@ -41,6 +41,7 @@ class RequestContext(context.RequestContext):
|
|||
self.auth_url = auth_url
|
||||
self.auth_token_info = auth_token_info
|
||||
self.trust_id = trust_id
|
||||
self.all_tenants = all_tenants
|
||||
|
||||
super(RequestContext, self).__init__(auth_token=auth_token,
|
||||
user=user_name,
|
||||
|
@ -66,7 +67,8 @@ class RequestContext(context.RequestContext):
|
|||
'show_deleted': self.show_deleted,
|
||||
'request_id': self.request_id,
|
||||
'trust_id': self.trust_id,
|
||||
'auth_token_info': self.auth_token_info})
|
||||
'auth_token_info': self.auth_token_info,
|
||||
'all_tenants': self.all_tenants})
|
||||
return value
|
||||
|
||||
@classmethod
|
||||
|
@ -78,7 +80,7 @@ def make_context(*args, **kwargs):
|
|||
return RequestContext(*args, **kwargs)
|
||||
|
||||
|
||||
def make_admin_context(show_deleted=False):
|
||||
def make_admin_context(show_deleted=False, all_tenants=False):
|
||||
"""Create an administrator context.
|
||||
|
||||
:param show_deleted: if True, will show deleted items when query db
|
||||
|
@ -86,7 +88,8 @@ def make_admin_context(show_deleted=False):
|
|||
context = RequestContext(user_id=None,
|
||||
project=None,
|
||||
is_admin=True,
|
||||
show_deleted=show_deleted)
|
||||
show_deleted=show_deleted,
|
||||
all_tenants=all_tenants)
|
||||
return context
|
||||
|
||||
|
||||
|
|
|
@ -111,13 +111,8 @@ class Connection(api.Connection):
|
|||
def __init__(self):
|
||||
pass
|
||||
|
||||
def _add_tenant_filters(self, context, query, opts=None):
|
||||
if opts is None:
|
||||
opts = {}
|
||||
|
||||
all_tenants = opts.get('get_all_tenants', False)
|
||||
|
||||
if context.is_admin and all_tenants:
|
||||
def _add_tenant_filters(self, context, query):
|
||||
if context.is_admin and context.all_tenants:
|
||||
return query
|
||||
|
||||
if context.project_id:
|
||||
|
@ -155,11 +150,9 @@ class Connection(api.Connection):
|
|||
return query
|
||||
|
||||
def get_bay_list(self, context, filters=None, limit=None, marker=None,
|
||||
sort_key=None, sort_dir=None, opts=None):
|
||||
if opts is None:
|
||||
opts = {}
|
||||
sort_key=None, sort_dir=None):
|
||||
query = model_query(models.Bay)
|
||||
query = self._add_tenant_filters(context, query, opts=opts)
|
||||
query = self._add_tenant_filters(context, query)
|
||||
query = self._add_bays_filters(query, filters)
|
||||
return _paginate_query(models.Bay, limit, marker,
|
||||
sort_key, sort_dir, query)
|
||||
|
@ -1028,12 +1021,9 @@ class Connection(api.Connection):
|
|||
return query
|
||||
|
||||
def get_x509keypair_list(self, context, filters=None, limit=None,
|
||||
marker=None, sort_key=None, sort_dir=None,
|
||||
opts=None):
|
||||
if opts is None:
|
||||
opts = {}
|
||||
marker=None, sort_key=None, sort_dir=None):
|
||||
query = model_query(models.X509KeyPair)
|
||||
query = self._add_tenant_filters(context, query, opts=opts)
|
||||
query = self._add_tenant_filters(context, query)
|
||||
query = self._add_x509keypairs_filters(query, filters)
|
||||
return _paginate_query(models.X509KeyPair, limit, marker,
|
||||
sort_key, sort_dir, query)
|
||||
|
|
|
@ -151,31 +151,6 @@ class Bay(base.MagnumPersistentObject, base.MagnumObject,
|
|||
filters=filters)
|
||||
return Bay._from_db_object_list(db_bays, cls, context)
|
||||
|
||||
@base.remotable_classmethod
|
||||
def list_all(cls, context, limit=None, marker=None,
|
||||
sort_key=None, sort_dir=None, filters=None):
|
||||
"""Return all tenants of Bay objects.
|
||||
|
||||
:param context: Security context, should be an admin context.
|
||||
:param limit: maximum number of resources to return in a single result.
|
||||
:param marker: pagination marker for large data sets.
|
||||
:param sort_key: column to sort results by.
|
||||
:param sort_dir: direction to sort. "asc" or "desc".
|
||||
:param filters: filter dict, can includes 'baymodel_id', 'name',
|
||||
'node_count', 'stack_id', 'api_address',
|
||||
'node_addresses', 'project_id', 'user_id',
|
||||
'status'(should be a status list), 'master_count'.
|
||||
:returns: a list of :class:`Bay` object.
|
||||
|
||||
"""
|
||||
db_bays = cls.dbapi.get_bay_list(context, limit=limit,
|
||||
marker=marker,
|
||||
sort_key=sort_key,
|
||||
sort_dir=sort_dir,
|
||||
filters=filters,
|
||||
opts={'get_all_tenants': True})
|
||||
return Bay._from_db_object_list(db_bays, cls, context)
|
||||
|
||||
@base.remotable
|
||||
def create(self, context=None):
|
||||
"""Create a Bay record in the DB.
|
||||
|
|
|
@ -132,32 +132,6 @@ class X509KeyPair(base.MagnumPersistentObject, base.MagnumObject,
|
|||
filters=filters)
|
||||
return X509KeyPair._from_db_object_list(db_x509keypairs, cls, context)
|
||||
|
||||
@base.remotable_classmethod
|
||||
def list_all(cls, context, limit=None, marker=None,
|
||||
sort_key=None, sort_dir=None, filters=None):
|
||||
"""Return all tenants of X509KeyPair objects.
|
||||
|
||||
:param context: Security context, should be an admin context.
|
||||
:param limit: maximum number of resources to return in a single result.
|
||||
:param marker: pagination marker for large data sets.
|
||||
:param sort_key: column to sort results by.
|
||||
:param sort_dir: direction to sort. "asc" or "desc".
|
||||
:param filters: filter dict, can include 'x509keypairmodel_id', 'name',
|
||||
'node_count', 'stack_id', 'api_address',
|
||||
'node_addresses', 'project_id', 'user_id',
|
||||
'status'(should be a status list).
|
||||
:returns: a list of :class:`X509KeyPair` object.
|
||||
|
||||
"""
|
||||
db_x509keypairs = cls.dbapi.get_x509keypair_list(
|
||||
context, limit=limit,
|
||||
marker=marker,
|
||||
sort_key=sort_key,
|
||||
sort_dir=sort_dir,
|
||||
filters=filters,
|
||||
opts={'get_all_tenants': True})
|
||||
return X509KeyPair._from_db_object_list(db_x509keypairs, cls, context)
|
||||
|
||||
@base.remotable
|
||||
def create(self, context=None):
|
||||
"""Create a X509KeyPair record in the DB.
|
||||
|
|
|
@ -28,13 +28,14 @@ from magnum.i18n import _LW
|
|||
from magnum import objects
|
||||
from magnum.objects.bay import Status as bay_status
|
||||
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
def set_context(func):
|
||||
@functools.wraps(func)
|
||||
def handler(self, ctx):
|
||||
ctx = context.make_admin_context()
|
||||
ctx = context.make_admin_context(all_tenants=True)
|
||||
context.set_ctx(ctx)
|
||||
func(self, ctx)
|
||||
context.set_ctx(None)
|
||||
|
@ -46,7 +47,6 @@ class MagnumPeriodicTasks(periodic_task.PeriodicTasks):
|
|||
|
||||
Any periodic task job need to be added into this class
|
||||
'''
|
||||
|
||||
@periodic_task.periodic_task(run_immediately=True)
|
||||
@set_context
|
||||
def sync_bay_status(self, ctx):
|
||||
|
@ -57,7 +57,7 @@ class MagnumPeriodicTasks(periodic_task.PeriodicTasks):
|
|||
bay_status.UPDATE_IN_PROGRESS,
|
||||
bay_status.DELETE_IN_PROGRESS]
|
||||
filters = {'status': status}
|
||||
bays = objects.Bay.list_all(ctx, filters=filters)
|
||||
bays = objects.Bay.list(ctx, filters=filters)
|
||||
if not bays:
|
||||
return
|
||||
sid_to_bay_mapping = {bay.stack_id: bay for bay in bays}
|
||||
|
|
|
@ -158,8 +158,8 @@ class DbBayTestCase(base.DbTestCase):
|
|||
project_id=magnum_utils.generate_uuid(),
|
||||
user_id=magnum_utils.generate_uuid())
|
||||
uuids.append(six.text_type(bay['uuid']))
|
||||
ctx = context.make_admin_context()
|
||||
res = self.dbapi.get_bay_list(ctx, opts={'get_all_tenants': True})
|
||||
ctx = context.make_admin_context(all_tenants=True)
|
||||
res = self.dbapi.get_bay_list(ctx)
|
||||
res_uuids = [r.uuid for r in res]
|
||||
self.assertEqual(sorted(uuids), sorted(res_uuids))
|
||||
|
||||
|
|
|
@ -119,9 +119,8 @@ class DbX509KeyPairTestCase(base.DbTestCase):
|
|||
project_id=magnum_utils.generate_uuid(),
|
||||
user_id=magnum_utils.generate_uuid())
|
||||
uuids.append(six.text_type(x509keypair['uuid']))
|
||||
ctx = context.make_admin_context()
|
||||
res = self.dbapi.get_x509keypair_list(
|
||||
ctx, opts={'get_all_tenants': True})
|
||||
ctx = context.make_admin_context(all_tenants=True)
|
||||
res = self.dbapi.get_x509keypair_list(ctx)
|
||||
res_uuids = [r.uuid for r in res]
|
||||
self.assertEqual(sorted(uuids), sorted(res_uuids))
|
||||
|
||||
|
|
|
@ -74,10 +74,11 @@ class TestBayObject(base.DbTestCase):
|
|||
with mock.patch.object(self.dbapi, 'get_bay_list',
|
||||
autospec=True) as mock_get_list:
|
||||
mock_get_list.return_value = [self.fake_bay]
|
||||
bays = objects.Bay.list_all(self.context)
|
||||
self.context.all_tenants = True
|
||||
bays = objects.Bay.list(self.context)
|
||||
mock_get_list.assert_called_once_with(
|
||||
self.context, limit=None, opts={'get_all_tenants': True},
|
||||
marker=None, filters=None, sort_dir=None, sort_key=None)
|
||||
self.context, limit=None, marker=None, filters=None,
|
||||
sort_dir=None, sort_key=None)
|
||||
self.assertEqual(mock_get_list.call_count, 1)
|
||||
self.assertThat(bays, HasLength(1))
|
||||
self.assertIsInstance(bays[0], objects.Bay)
|
||||
|
|
|
@ -75,10 +75,11 @@ class TestX509KeyPairObject(base.DbTestCase):
|
|||
with mock.patch.object(self.dbapi, 'get_x509keypair_list',
|
||||
autospec=True) as mock_get_list:
|
||||
mock_get_list.return_value = [self.fake_x509keypair]
|
||||
x509keypairs = objects.X509KeyPair.list_all(self.context)
|
||||
self.context.all_tenants = True
|
||||
x509keypairs = objects.X509KeyPair.list(self.context)
|
||||
mock_get_list.assert_called_once_with(
|
||||
self.context, limit=None, opts={'get_all_tenants': True},
|
||||
marker=None, filters=None, sort_dir=None, sort_key=None)
|
||||
self.context, limit=None, marker=None, filters=None,
|
||||
sort_dir=None, sort_key=None)
|
||||
self.assertEqual(mock_get_list.call_count, 1)
|
||||
self.assertThat(x509keypairs, HasLength(1))
|
||||
self.assertIsInstance(x509keypairs[0], objects.X509KeyPair)
|
||||
|
|
|
@ -60,7 +60,7 @@ class PeriodicTestCase(base.TestCase):
|
|||
self.bay2 = objects.Bay(ctx, **bay2)
|
||||
self.bay3 = objects.Bay(ctx, **bay3)
|
||||
|
||||
@mock.patch.object(objects.Bay, 'list_all')
|
||||
@mock.patch.object(objects.Bay, 'list')
|
||||
@mock.patch('magnum.common.clients.OpenStackClients')
|
||||
@mock.patch.object(dbapi.Connection, 'destroy_bay')
|
||||
@mock.patch.object(dbapi.Connection, 'update_bay')
|
||||
|
@ -84,7 +84,7 @@ class PeriodicTestCase(base.TestCase):
|
|||
mock_db_destroy.assert_called_once_with(self.bay2.uuid)
|
||||
self.assertEqual(self.bay3.status, bay_status.UPDATE_COMPLETE)
|
||||
|
||||
@mock.patch.object(objects.Bay, 'list_all')
|
||||
@mock.patch.object(objects.Bay, 'list')
|
||||
@mock.patch('magnum.common.clients.OpenStackClients')
|
||||
def test_sync_bay_status_not_changes(self, mock_oscc, mock_bay_list):
|
||||
mock_heat_client = mock.MagicMock()
|
||||
|
@ -104,7 +104,7 @@ class PeriodicTestCase(base.TestCase):
|
|||
self.assertEqual(self.bay2.status, bay_status.DELETE_IN_PROGRESS)
|
||||
self.assertEqual(self.bay3.status, bay_status.UPDATE_IN_PROGRESS)
|
||||
|
||||
@mock.patch.object(objects.Bay, 'list_all')
|
||||
@mock.patch.object(objects.Bay, 'list')
|
||||
@mock.patch('magnum.common.clients.OpenStackClients')
|
||||
@mock.patch.object(dbapi.Connection, 'destroy_bay')
|
||||
@mock.patch.object(dbapi.Connection, 'update_bay')
|
||||
|
|
Loading…
Reference in New Issue