Radomir Dopieralski d7e9c9a0cd Error handling in the API
Currently error handling in Tuskar-UI involves a lot of repeated
code, as the same pattern is used everywhere and the same API calls
tend to be handled in the same way no matter where they appear.

This patch introduces a decorator that can be used to add default
error handling to all API calls that take a request as a parameter.

That default error handling can be disabled or modified using the
special parameters passed to the call, all of which start with
"_error" to avoid conflicts.

Change-Id: Ie441789c471deeb6d64267bf4424f5661ef073af
2014-03-03 11:27:59 +01:00

78 lines
3.0 KiB
Python

# -*- coding: utf8 -*-
#
# 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.
from django.utils.translation import ugettext_lazy as _
from horizon import tables
from openstack_dashboard.dashboards.admin.flavors \
import tables as flavor_tables
from tuskar_ui import api
class CreateNodeProfile(flavor_tables.CreateFlavor):
verbose_name = _("New Node Profile")
url = "horizon:infrastructure:node_profiles:create"
class DeleteNodeProfile(flavor_tables.DeleteFlavor):
def __init__(self, **kwargs):
super(DeleteNodeProfile, self).__init__(**kwargs)
# NOTE(dtantsur): setting class attributes doesn't work
# probably due to metaclass magic in actions
self.data_type_singular = _("Node Profile")
self.data_type_plural = _("Node Profiles")
def allowed(self, request, datum=None):
"""Check that action is allowed on node profile
This is overrided method from horizon.tables.BaseAction.
:param datum: node profile we're operating on
:type datum: tuskar_ui.api.NodeProfile
"""
if datum is not None:
deployed_profiles = api.NodeProfile.list_deployed_ids(
request, _error_default=None)
if deployed_profiles is None or datum.id in deployed_profiles:
return False
return super(DeleteNodeProfile, self).allowed(request, datum)
class NodeProfilesTable(tables.DataTable):
name = tables.Column('name', verbose_name=_('Node'))
arch = tables.Column('cpu_arch', verbose_name=_('Architecture'))
vcpus = tables.Column('vcpus', verbose_name=_('CPUs'))
ram = tables.Column(flavor_tables.get_size,
verbose_name=_('Memory'),
attrs={'data-type': 'size'})
disk = tables.Column(flavor_tables.get_disk_size,
verbose_name=_('Disk'),
attrs={'data-type': 'size'})
# FIXME(dtantsur): would be much better to have names here
kernel_image_id = tables.Column('kernel_image_id',
verbose_name=_('Deploy Kernel Image ID'))
ramdisk_image_id = tables.Column('ramdisk_image_id',
verbose_name=_('Deploy Ramdisk Image ID'))
class Meta:
name = "node_profiles"
verbose_name = _("Node Profiles")
table_actions = (CreateNodeProfile,
DeleteNodeProfile,
flavor_tables.FlavorFilterAction)
row_actions = (DeleteNodeProfile,)