Refactor Packages and AppCatalog pagination
Move function for getting list of Application packages to a separate module - muranodashboard.api.packages, so both Packages and AppCatalog views can reuse it. That `package_list()` function obtains packages generator with a call to `muranoclient.packages.filter()` and paginate the results, adding `has_more_data` flag (as it is done for Glance, see http://tinyurl.com/lkbnsed). Also move muranoclient-specific functions from muranodashboard.environments.api to muranodashboard.api module. Implements: blueprint app-catalog-pagination Change-Id: Ic3a433c8467fda94ac7dd3800c29fc1c2b9f3464
This commit is contained in:
93
muranodashboard/api/__init__.py
Normal file
93
muranodashboard/api/__init__.py
Normal file
@@ -0,0 +1,93 @@
|
||||
# Copyright (c) 2014 Mirantis, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import contextlib
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.messages import api as msg_api
|
||||
from django.utils.encoding import force_unicode
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from horizon import exceptions
|
||||
from openstack_dashboard.api import base
|
||||
|
||||
import muranoclient.client as client
|
||||
from muranoclient.common import exceptions as exc
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _handle_message(request, message):
|
||||
def horizon_message_already_queued(_message):
|
||||
_message = force_unicode(_message)
|
||||
if request.is_ajax():
|
||||
for tag, msg, extra in request.horizon['async_messages']:
|
||||
if _message == msg:
|
||||
return True
|
||||
else:
|
||||
for msg in msg_api.get_messages(request):
|
||||
if msg.message == _message:
|
||||
return True
|
||||
return False
|
||||
|
||||
if horizon_message_already_queued(message):
|
||||
exceptions.handle(request, ignore=True)
|
||||
else:
|
||||
exceptions.handle(request, message=message)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def handled_exceptions(request):
|
||||
"""Handles all murano-api specific exceptions."""
|
||||
try:
|
||||
yield
|
||||
except exc.CommunicationError:
|
||||
msg = _('Unable to communicate to murano-api server.')
|
||||
LOG.exception(msg)
|
||||
_handle_message(request, msg)
|
||||
except exc.Unauthorized:
|
||||
msg = _('Check Keystone configuration of murano-api server.')
|
||||
LOG.exception(msg)
|
||||
_handle_message(request, msg)
|
||||
except exc.Forbidden:
|
||||
msg = _('Operation is forbidden by murano-api server.')
|
||||
LOG.exception(msg)
|
||||
_handle_message(request, msg)
|
||||
|
||||
|
||||
def _get_endpoint(request):
|
||||
#prefer location specified in settings for dev purposes
|
||||
endpoint = getattr(settings, 'MURANO_API_URL', None)
|
||||
|
||||
if not endpoint:
|
||||
try:
|
||||
endpoint = base.url_for(request, 'application_catalog')
|
||||
except exceptions.ServiceCatalogException:
|
||||
endpoint = 'http://localhost:8082'
|
||||
LOG.warning('Murano API location could not be found in Service '
|
||||
'Catalog, using default: {0}'.format(endpoint))
|
||||
return endpoint
|
||||
|
||||
|
||||
def muranoclient(request):
|
||||
endpoint = _get_endpoint(request)
|
||||
insecure = getattr(settings, 'MURANO_API_INSECURE', False)
|
||||
|
||||
token_id = request.user.token.id
|
||||
LOG.debug('Murano::Client <Url: {0}, '
|
||||
'TokenId: {1}>'.format(endpoint, token_id))
|
||||
|
||||
return client.Client(1, endpoint=endpoint, token=token_id,
|
||||
insecure=insecure)
|
||||
49
muranodashboard/api/packages.py
Normal file
49
muranodashboard/api/packages.py
Normal file
@@ -0,0 +1,49 @@
|
||||
# Copyright (c) 2014 Mirantis, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import itertools
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from muranodashboard import api
|
||||
|
||||
|
||||
def package_list(request, marker=None, filters=None, paginate=False,
|
||||
page_size=20):
|
||||
limit = getattr(settings, 'PACKAGES_LIMIT', 100)
|
||||
filters = filters or {}
|
||||
|
||||
if paginate:
|
||||
request_size = page_size + 1
|
||||
else:
|
||||
request_size = limit
|
||||
|
||||
if marker:
|
||||
filters['marker'] = marker
|
||||
|
||||
client = api.muranoclient(request)
|
||||
packages_iter = client.packages.filter(page_size=request_size,
|
||||
limit=limit,
|
||||
**filters)
|
||||
|
||||
has_more_data = False
|
||||
if paginate:
|
||||
packages = list(itertools.islice(packages_iter, request_size))
|
||||
if len(packages) > page_size:
|
||||
packages.pop()
|
||||
has_more_data = True
|
||||
else:
|
||||
packages = list(packages_iter)
|
||||
|
||||
return packages, has_more_data
|
||||
Reference in New Issue
Block a user