Allows congress to fetch environments from all tenants

Adds request param all_tenants allowing listing environments from all tenants.
Congress data source needs populate its tables by data from all tenants.
Similar machansim uses nova to allow list servers from all tenants.

Partially implements: blueprint murano-api-all-tenants-search

Change-Id: I842292720a475992a137c1e4715873a059ec605c
This commit is contained in:
Filip Blaha
2015-07-07 17:01:12 +02:00
parent ed6c26c8ca
commit ed4c44d521
4 changed files with 55 additions and 10 deletions

View File

@@ -95,6 +95,14 @@ List environments
| | | Environments |
+----------+----------------------------------+----------------------------------+
*Parameters:*
* `all_tenants` - boolean, indicates whether environments from all tenants are listed.
*True* environments from all tenants are listed. Admin user required.
*False* environments only from current tenant are listed (default like option unspecified).
*Response*

View File

@@ -19,6 +19,7 @@
"statuses_deployments": "rule:default",
"list_environments": "rule:default",
"list_environments_all_tenants": "rule:admin_api",
"show_environment": "rule:default",
"update_environment": "rule:default",
"create_environment": "rule:default",

View File

@@ -40,11 +40,17 @@ VALID_NAME_REGEX = re.compile('^[a-zA-Z]+[\w.-]*$')
class Controller(object):
@request_statistics.stats_count(API_NAME, 'Index')
def index(self, request):
LOG.debug('Environments:List')
policy.check('list_environments', request.context)
all_tenants = request.GET.get('all_tenants', 'false').lower() == 'true'
LOG.debug('Environments:List <all_tenants: {0}>'.format(all_tenants))
if all_tenants:
policy.check('list_environments_all_tenants', request.context)
filters = {}
else:
policy.check('list_environments', request.context)
# Only environments from same tenant as user should be returned
filters = {'tenant_id': request.context.tenant}
# Only environments from same tenant as user should be returned
filters = {'tenant_id': request.context.tenant}
environments = envs.EnvironmentServices.get_environments_by(filters)
environments = [env.to_dict() for env in environments]

View File

@@ -31,6 +31,15 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase):
super(TestEnvironmentApi, self).setUp()
self.controller = environments.Controller()
@staticmethod
def _configure_opts():
opts = [
cfg.StrOpt('config_dir'),
cfg.StrOpt('config_file', default='murano.conf'),
cfg.StrOpt('project', default='murano'),
]
config.CONF.register_opts(opts)
def test_list_empty_environments(self):
"""Check that with no environments an empty list is returned."""
self._set_policy_rules(
@@ -42,14 +51,35 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase):
result = req.get_response(self.api)
self.assertEqual({'environments': []}, json.loads(result.body))
def test_list_all_tenants(self):
"""Check whether all_tenants param is taken into account."""
self._configure_opts()
self._set_policy_rules(
{'list_environments': '@',
'create_environment': '@',
'list_environments_all_tenants': '@'}
)
self.expect_policy_check('create_environment')
body = {'name': 'my_env'}
req = self._post('/environments', json.dumps(body), tenant="other")
req.get_response(self.api)
self._check_listing(False, 'list_environments', 0)
self._check_listing(True, 'list_environments_all_tenants', 1)
def _check_listing(self, all_tenants, expected_check, expected_count):
self.expect_policy_check(expected_check)
req = self._get('/environments', {'all_tenants': all_tenants})
response = req.get_response(self.api)
body = json.loads(response.body)
self.assertEqual(200, response.status_code)
self.assertEqual(expected_count, len(body['environments']))
def test_create_environment(self):
"""Create an environment, test environment.show()."""
opts = [
cfg.StrOpt('config_dir'),
cfg.StrOpt('config_file', default='murano.conf'),
cfg.StrOpt('project', default='murano'),
]
config.CONF.register_opts(opts)
self._configure_opts()
self._set_policy_rules(
{'list_environments': '@',
'create_environment': '@',