Merge "Make flavors is_public option actually work"

This commit is contained in:
Jenkins
2013-07-05 19:47:43 +00:00
committed by Gerrit Code Review
15 changed files with 97 additions and 19 deletions
@@ -95,7 +95,7 @@ class FlavorAccessController(object):
authorize(context)
try:
flavor = flavors.get_flavor_by_flavor_id(flavor_id)
flavor = flavors.get_flavor_by_flavor_id(flavor_id, ctxt=context)
except exception.FlavorNotFound:
explanation = _("Flavor not found.")
raise webob.exc.HTTPNotFound(explanation=explanation)
@@ -41,7 +41,7 @@ class FlavorManageController(wsgi.Controller):
try:
flavor = flavors.get_flavor_by_flavor_id(
id, read_deleted="no")
id, ctxt=context, read_deleted="no")
except exception.NotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message())
+2 -1
View File
@@ -86,7 +86,8 @@ class Controller(wsgi.Controller):
def show(self, req, id):
"""Return data about the given flavor id."""
try:
flavor = flavors.get_flavor_by_flavor_id(id)
context = req.environ['nova.context']
flavor = flavors.get_flavor_by_flavor_id(id, ctxt=context)
req.cache_db_flavor(flavor)
except exception.NotFound:
raise webob.exc.HTTPNotFound()
+1 -1
View File
@@ -1428,7 +1428,7 @@ class API(base.Base):
#NOTE(bcwaldon): this doesn't really belong in this class
def get_instance_type(self, context, instance_type_id):
"""Get an instance type by instance type id."""
return flavors.get_flavor(instance_type_id)
return flavors.get_flavor(instance_type_id, ctxt=context)
def get(self, context, instance_id):
"""Get a single instance with the given instance_id."""
+1 -1
View File
@@ -206,7 +206,7 @@ def get_flavor_by_flavor_id(flavorid, ctxt=None, read_deleted="yes"):
if ctxt is None:
ctxt = context.get_admin_context(read_deleted=read_deleted)
return db.instance_type_get_by_flavor_id(ctxt, flavorid)
return db.instance_type_get_by_flavor_id(ctxt, flavorid, read_deleted)
def get_flavor_access_by_flavor_id(flavorid, ctxt=None):
+2 -2
View File
@@ -1370,9 +1370,9 @@ def instance_type_get_by_name(context, name):
return IMPL.instance_type_get_by_name(context, name)
def instance_type_get_by_flavor_id(context, id):
def instance_type_get_by_flavor_id(context, id, read_deleted=None):
"""Get instance type by flavor id."""
return IMPL.instance_type_get_by_flavor_id(context, id)
return IMPL.instance_type_get_by_flavor_id(context, id, read_deleted)
def instance_type_destroy(context, name):
+11 -4
View File
@@ -3824,9 +3824,16 @@ def _dict_with_extra_specs(inst_type_query):
def _instance_type_get_query(context, session=None, read_deleted=None):
return model_query(context, models.InstanceTypes, session=session,
query = model_query(context, models.InstanceTypes, session=session,
read_deleted=read_deleted).\
options(joinedload('extra_specs'))
options(joinedload('extra_specs'))
if not context.is_admin:
the_filter = [models.InstanceTypes.is_public == True]
the_filter.extend([
models.InstanceTypes.projects.any(project_id=context.project_id)
])
query = query.filter(or_(*the_filter))
return query
@require_context
@@ -3914,9 +3921,9 @@ def instance_type_get_by_name(context, name):
@require_context
def instance_type_get_by_flavor_id(context, flavor_id):
def instance_type_get_by_flavor_id(context, flavor_id, read_deleted):
"""Returns a dict describing specific flavor_id."""
result = _instance_type_get_query(context).\
result = _instance_type_get_query(context, read_deleted=read_deleted).\
filter_by(flavorid=flavor_id).\
first()
if not result:
@@ -68,7 +68,7 @@ def fake_get_flavor_access_by_flavor_id(flavorid):
return res
def fake_get_flavor_by_flavor_id(flavorid):
def fake_get_flavor_by_flavor_id(flavorid, ctxt=None):
return INSTANCE_TYPES[flavorid]
@@ -39,7 +39,7 @@ FAKE_FLAVORS = {
}
def fake_flavor_get_by_flavor_id(flavorid):
def fake_flavor_get_by_flavor_id(flavorid, ctxt=None):
return FAKE_FLAVORS['flavor %s' % flavorid]
@@ -25,7 +25,7 @@ from nova import test
from nova.tests.api.openstack import fakes
def fake_get_flavor_by_flavor_id(flavorid, read_deleted='yes'):
def fake_get_flavor_by_flavor_id(flavorid, ctxt=None, read_deleted='yes'):
if flavorid == 'failtest':
raise exception.NotFound("Not found sucka!")
elif not str(flavorid) == '1234':
@@ -38,7 +38,7 @@ FAKE_FLAVORS = {
}
def fake_flavor_get_by_flavor_id(flavorid):
def fake_flavor_get_by_flavor_id(flavorid, ctxt=None):
return FAKE_FLAVORS['flavor %s' % flavorid]
@@ -39,7 +39,7 @@ FAKE_FLAVORS = {
#TOD(jogo) dedup these accross nova.api.openstack.contrib.test_flavor*
def fake_flavor_get_by_flavor_id(flavorid):
def fake_flavor_get_by_flavor_id(flavorid, ctxt=None):
return FAKE_FLAVORS['flavor %s' % flavorid]
@@ -23,7 +23,7 @@ from nova import test
from nova.tests.api.openstack import fakes
def fake_get_flavor_by_flavor_id(flavorid):
def fake_get_flavor_by_flavor_id(flavorid, ctxt=None):
return {
'id': flavorid,
'flavorid': str(flavorid),
@@ -50,7 +50,7 @@ FAKE_FLAVORS = {
}
def fake_flavor_get_by_flavor_id(flavorid):
def fake_flavor_get_by_flavor_id(flavorid, ctxt=None):
return FAKE_FLAVORS['flavor %s' % flavorid]
@@ -76,7 +76,7 @@ def empty_flavor_get_all(inactive=False, filters=None):
return {}
def return_flavor_not_found(flavor_id):
def return_flavor_not_found(flavor_id, ctxt=None):
raise exception.InstanceTypeNotFound(instance_type_id=flavor_id)
+70
View File
@@ -2038,6 +2038,7 @@ class BaseInstanceTypeTestCase(test.TestCase, ModelsObjectComparatorMixin):
def setUp(self):
super(BaseInstanceTypeTestCase, self).setUp()
self.ctxt = context.get_admin_context()
self.user_ctxt = context.RequestContext('user', 'user')
def _get_base_values(self):
return {
@@ -2518,6 +2519,24 @@ class InstanceTypeTestCase(BaseInstanceTypeTestCase):
inst_type_by_id = db.instance_type_get(self.ctxt, inst_type['id'])
self._assertEqualObjects(inst_type, inst_type_by_id)
def test_instance_type_get_non_public(self):
inst_type = self._create_inst_type({'name': 'abc', 'flavorid': '123',
'is_public': False})
# Admin can see it
inst_type_by_id = db.instance_type_get(self.ctxt, inst_type['id'])
self._assertEqualObjects(inst_type, inst_type_by_id)
# Regular user can not
self.assertRaises(exception.InstanceTypeNotFound, db.instance_type_get,
self.user_ctxt, inst_type['id'])
# Regular user can see it after being granted access
db.instance_type_access_add(self.ctxt, inst_type['flavorid'],
self.user_ctxt.project_id)
inst_type_by_id = db.instance_type_get(self.user_ctxt, inst_type['id'])
self._assertEqualObjects(inst_type, inst_type_by_id)
def test_instance_type_get_by_name(self):
inst_types = [{'name': 'abc', 'flavorid': '123'},
{'name': 'def', 'flavorid': '456'},
@@ -2534,6 +2553,27 @@ class InstanceTypeTestCase(BaseInstanceTypeTestCase):
self.assertRaises(exception.InstanceTypeNotFoundByName,
db.instance_type_get_by_name, self.ctxt, 'nonexists')
def test_instance_type_get_by_name_non_public(self):
inst_type = self._create_inst_type({'name': 'abc', 'flavorid': '123',
'is_public': False})
# Admin can see it
inst_type_by_name = db.instance_type_get_by_name(self.ctxt,
inst_type['name'])
self._assertEqualObjects(inst_type, inst_type_by_name)
# Regular user can not
self.assertRaises(exception.InstanceTypeNotFoundByName,
db.instance_type_get_by_name, self.user_ctxt,
inst_type['name'])
# Regular user can see it after being granted access
db.instance_type_access_add(self.ctxt, inst_type['flavorid'],
self.user_ctxt.project_id)
inst_type_by_name = db.instance_type_get_by_name(self.user_ctxt,
inst_type['name'])
self._assertEqualObjects(inst_type, inst_type_by_name)
def test_instance_type_get_by_flavor_id(self):
inst_types = [{'name': 'abc', 'flavorid': '123'},
{'name': 'def', 'flavorid': '456'},
@@ -2551,6 +2591,36 @@ class InstanceTypeTestCase(BaseInstanceTypeTestCase):
db.instance_type_get_by_flavor_id,
self.ctxt, 'nonexists')
def test_instance_type_get_by_flavor_id_non_public(self):
inst_type = self._create_inst_type({'name': 'abc', 'flavorid': '123',
'is_public': False})
# Admin can see it
inst_type_by_fid = db.instance_type_get_by_flavor_id(self.ctxt,
inst_type['flavorid'])
self._assertEqualObjects(inst_type, inst_type_by_fid)
# Regular user can not
self.assertRaises(exception.FlavorNotFound,
db.instance_type_get_by_flavor_id, self.user_ctxt,
inst_type['flavorid'])
# Regular user can see it after being granted access
db.instance_type_access_add(self.ctxt, inst_type['flavorid'],
self.user_ctxt.project_id)
inst_type_by_fid = db.instance_type_get_by_flavor_id(self.user_ctxt,
inst_type['flavorid'])
self._assertEqualObjects(inst_type, inst_type_by_fid)
def test_instance_type_get_by_flavor_id_deleted(self):
inst_type = self._create_inst_type({'name': 'abc', 'flavorid': '123'})
db.instance_type_destroy(self.ctxt, 'abc')
inst_type_by_fid = db.instance_type_get_by_flavor_id(self.ctxt,
inst_type['flavorid'], read_deleted='yes')
self.assertEqual(inst_type['id'], inst_type_by_fid['id'])
class InstanceTypeExtraSpecsTestCase(BaseInstanceTypeTestCase):