Add private flavor filter for access

Change-Id: I4cb561f1b4cfc9ee1744f28e3682270bffb10f97
This commit is contained in:
Zhenguo Niu 2017-04-28 17:39:33 +08:00
parent 4644110ec8
commit 5ae6a6972d
2 changed files with 19 additions and 3 deletions

View File

@ -24,9 +24,11 @@ from oslo_log import log as logging
from oslo_utils import strutils
from oslo_utils import timeutils
from oslo_utils import uuidutils
from sqlalchemy import or_
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm import joinedload
from sqlalchemy.sql.expression import desc
from sqlalchemy.sql import true
from mogan.common import exception
from mogan.common.i18n import _
@ -131,6 +133,14 @@ class Connection(api.Connection):
def flavor_get(self, context, flavor_uuid):
query = model_query(context, models.Flavors).filter_by(
uuid=flavor_uuid).options(joinedload('extra_specs'))
if not context.is_admin:
the_filter = [models.Flavors.is_public == true()]
the_filter.extend([
models.Flavors.projects.has(project_id=context.project_id)
])
query = query.filter(or_(*the_filter))
try:
return _dict_with_extra_specs(query.one())
except NoResultFound:
@ -151,8 +161,15 @@ class Connection(api.Connection):
return ref
def flavor_get_all(self, context):
results = model_query(context, models.Flavors)
return [_dict_with_extra_specs(i) for i in results]
query = model_query(context, models.Flavors)
if not context.is_admin:
the_filter = [models.Flavors.is_public == true()]
the_filter.extend([
models.Flavors.projects.has(project_id=context.project_id)
])
query = query.filter(or_(*the_filter))
return [_dict_with_extra_specs(i) for i in query.all()]
def flavor_destroy(self, context, flavor_uuid):
with _session_for_write():

View File

@ -27,7 +27,6 @@ class DbFlavorTestCase(base.DbTestCase):
def setUp(self):
super(DbFlavorTestCase, self).setUp()
self.context = {}
self.flavor = utils.create_test_flavor()
def test_create_flavor(self):