Modularize workflow action in quota defaults
This is a preparation to support quotas for horizon plugins. Part of blueprint horizon-plugin-tab-for-info-and-quotas Change-Id: I11f9bfb6edae205f452e2619b33fa52254dcf37a
This commit is contained in:
parent
08e4cf5049
commit
f61b3fd03d
@ -114,15 +114,19 @@ class UpdateDefaultQuotasTests(test.BaseAdminViewTests):
|
|||||||
return quota_data
|
return quota_data
|
||||||
|
|
||||||
@test.create_mocks({
|
@test.create_mocks({
|
||||||
api.nova: [('default_quota_update', 'nova_default_quota_update')],
|
api.nova: [('default_quota_update', 'nova_default_quota_update'),
|
||||||
api.cinder: [('default_quota_update', 'cinder_default_quota_update')],
|
('default_quota_get', 'nova_default_quota_get')],
|
||||||
quotas: ['get_default_quota_data', 'get_disabled_quotas']})
|
api.cinder: [('default_quota_update', 'cinder_default_quota_update'),
|
||||||
|
('default_quota_get', 'cinder_default_quota_get')],
|
||||||
|
quotas: ['get_disabled_quotas']})
|
||||||
def test_update_default_quotas(self):
|
def test_update_default_quotas(self):
|
||||||
quota = self.quotas.first()
|
quota = self.quotas.first() + self.cinder_quotas.first()
|
||||||
|
|
||||||
self.mock_get_disabled_quotas.return_value = set()
|
self.mock_get_disabled_quotas.return_value = set()
|
||||||
self.mock_get_default_quota_data.return_value = quota
|
self.mock_nova_default_quota_get.return_value = self.quotas.first()
|
||||||
self.mock_nova_default_quota_update.return_value = None
|
self.mock_nova_default_quota_update.return_value = None
|
||||||
|
self.mock_cinder_default_quota_get.return_value = \
|
||||||
|
self.cinder_quotas.first()
|
||||||
self.mock_cinder_default_quota_update.return_value = None
|
self.mock_cinder_default_quota_update.return_value = None
|
||||||
|
|
||||||
# update some fields
|
# update some fields
|
||||||
@ -138,16 +142,18 @@ class UpdateDefaultQuotasTests(test.BaseAdminViewTests):
|
|||||||
|
|
||||||
self.mock_get_disabled_quotas.assert_called_once_with(
|
self.mock_get_disabled_quotas.assert_called_once_with(
|
||||||
test.IsHttpRequest())
|
test.IsHttpRequest())
|
||||||
self.mock_get_default_quota_data.assert_called_once_with(
|
|
||||||
test.IsHttpRequest())
|
|
||||||
|
|
||||||
nova_fields = quotas.NOVA_QUOTA_FIELDS
|
nova_fields = quotas.NOVA_QUOTA_FIELDS
|
||||||
nova_updated_quota = dict((key, updated_quota[key]) for key in
|
nova_updated_quota = dict((key, updated_quota[key]) for key in
|
||||||
nova_fields if key != 'fixed_ips')
|
nova_fields if key != 'fixed_ips')
|
||||||
|
self.mock_nova_default_quota_get.assert_called_once_with(
|
||||||
|
test.IsHttpRequest(), self.request.user.tenant_id)
|
||||||
self.mock_nova_default_quota_update.assert_called_once_with(
|
self.mock_nova_default_quota_update.assert_called_once_with(
|
||||||
test.IsHttpRequest(), **nova_updated_quota)
|
test.IsHttpRequest(), **nova_updated_quota)
|
||||||
|
|
||||||
cinder_updated_quota = dict((key, updated_quota[key]) for key in
|
cinder_updated_quota = dict((key, updated_quota[key]) for key in
|
||||||
quotas.CINDER_QUOTA_FIELDS)
|
quotas.CINDER_QUOTA_FIELDS)
|
||||||
|
self.mock_cinder_default_quota_get.assert_called_once_with(
|
||||||
|
test.IsHttpRequest(), self.request.user.tenant_id)
|
||||||
self.mock_cinder_default_quota_update.assert_called_once_with(
|
self.mock_cinder_default_quota_update.assert_called_once_with(
|
||||||
test.IsHttpRequest(), **cinder_updated_quota)
|
test.IsHttpRequest(), **cinder_updated_quota)
|
||||||
|
@ -34,15 +34,5 @@ class UpdateDefaultQuotasView(workflows.WorkflowView):
|
|||||||
|
|
||||||
def get_initial(self):
|
def get_initial(self):
|
||||||
initial = super(UpdateDefaultQuotasView, self).get_initial()
|
initial = super(UpdateDefaultQuotasView, self).get_initial()
|
||||||
|
|
||||||
# get initial quota defaults
|
|
||||||
try:
|
|
||||||
quota_defaults = quotas.get_default_quota_data(self.request)
|
|
||||||
for field in quotas.QUOTA_FIELDS:
|
|
||||||
initial[field] = quota_defaults.get(field).limit
|
|
||||||
except Exception:
|
|
||||||
error_msg = _('Unable to retrieve default quota values.')
|
|
||||||
self.add_error_to_step(error_msg, 'update_default_quotas')
|
|
||||||
initial['disabled_quotas'] = quotas.get_disabled_quotas(self.request)
|
initial['disabled_quotas'] = quotas.get_disabled_quotas(self.request)
|
||||||
|
|
||||||
return initial
|
return initial
|
||||||
|
@ -20,6 +20,7 @@ from horizon import exceptions
|
|||||||
from horizon import forms
|
from horizon import forms
|
||||||
from horizon import workflows
|
from horizon import workflows
|
||||||
|
|
||||||
|
from openstack_dashboard.api import base
|
||||||
from openstack_dashboard.api import cinder
|
from openstack_dashboard.api import cinder
|
||||||
from openstack_dashboard.api import nova
|
from openstack_dashboard.api import nova
|
||||||
from openstack_dashboard.usage import quotas
|
from openstack_dashboard.usage import quotas
|
||||||
@ -52,6 +53,19 @@ class UpdateDefaultComputeQuotasAction(workflows.Action):
|
|||||||
self.fields[field].required = False
|
self.fields[field].required = False
|
||||||
self.fields[field].widget = forms.HiddenInput()
|
self.fields[field].widget = forms.HiddenInput()
|
||||||
|
|
||||||
|
def handle(self, request, context):
|
||||||
|
nova_data = {
|
||||||
|
key: value for key, value in context.items()
|
||||||
|
if key in quotas.NOVA_QUOTA_FIELDS
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
nova.default_quota_update(request, **nova_data)
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
exceptions.handle(request,
|
||||||
|
_('Unable to update default compute quotas.'))
|
||||||
|
return False
|
||||||
|
|
||||||
class Meta(object):
|
class Meta(object):
|
||||||
name = _("Compute")
|
name = _("Compute")
|
||||||
slug = 'update_default_compute_quotas'
|
slug = 'update_default_compute_quotas'
|
||||||
@ -64,6 +78,20 @@ class UpdateDefaultComputeQuotasStep(workflows.Step):
|
|||||||
contributes = quotas.NOVA_QUOTA_FIELDS
|
contributes = quotas.NOVA_QUOTA_FIELDS
|
||||||
depends_on = ('disabled_quotas',)
|
depends_on = ('disabled_quotas',)
|
||||||
|
|
||||||
|
def prepare_action_context(self, request, context):
|
||||||
|
try:
|
||||||
|
quota_defaults = nova.default_quota_get(request,
|
||||||
|
request.user.tenant_id)
|
||||||
|
for field in quotas.NOVA_QUOTA_FIELDS:
|
||||||
|
context[field] = quota_defaults.get(field).limit
|
||||||
|
except Exception:
|
||||||
|
exceptions.handle(request,
|
||||||
|
_('Unable to retrieve default compute quotas.'))
|
||||||
|
return context
|
||||||
|
|
||||||
|
def allowed(self, request):
|
||||||
|
return base.is_service_enabled(request, 'compute')
|
||||||
|
|
||||||
|
|
||||||
class UpdateDefaultVolumeQuotasAction(workflows.Action):
|
class UpdateDefaultVolumeQuotasAction(workflows.Action):
|
||||||
volumes = forms.IntegerField(min_value=-1, label=_("Volumes"))
|
volumes = forms.IntegerField(min_value=-1, label=_("Volumes"))
|
||||||
@ -81,6 +109,19 @@ class UpdateDefaultVolumeQuotasAction(workflows.Action):
|
|||||||
self.fields[field].required = False
|
self.fields[field].required = False
|
||||||
self.fields[field].widget = forms.HiddenInput()
|
self.fields[field].widget = forms.HiddenInput()
|
||||||
|
|
||||||
|
def handle(self, request, context):
|
||||||
|
cinder_data = {
|
||||||
|
key: value for key, value in context.items()
|
||||||
|
if key in quotas.CINDER_QUOTA_FIELDS
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
cinder.default_quota_update(request, **cinder_data)
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
exceptions.handle(request,
|
||||||
|
_('Unable to update default volume quotas.'))
|
||||||
|
return False
|
||||||
|
|
||||||
class Meta(object):
|
class Meta(object):
|
||||||
name = _("Volume")
|
name = _("Volume")
|
||||||
slug = 'update_default_volume_quotas'
|
slug = 'update_default_volume_quotas'
|
||||||
@ -93,6 +134,20 @@ class UpdateDefaultVolumeQuotasStep(workflows.Step):
|
|||||||
contributes = quotas.CINDER_QUOTA_FIELDS
|
contributes = quotas.CINDER_QUOTA_FIELDS
|
||||||
depends_on = ('disabled_quotas',)
|
depends_on = ('disabled_quotas',)
|
||||||
|
|
||||||
|
def prepare_action_context(self, request, context):
|
||||||
|
try:
|
||||||
|
quota_defaults = cinder.default_quota_get(request,
|
||||||
|
request.user.tenant_id)
|
||||||
|
for field in quotas.CINDER_QUOTA_FIELDS:
|
||||||
|
context[field] = quota_defaults.get(field).limit
|
||||||
|
except Exception:
|
||||||
|
exceptions.handle(request,
|
||||||
|
_('Unable to retrieve default volume quotas.'))
|
||||||
|
return context
|
||||||
|
|
||||||
|
def allowed(self, request):
|
||||||
|
return cinder.is_volume_service_enabled(request)
|
||||||
|
|
||||||
|
|
||||||
class UpdateDefaultQuotas(workflows.Workflow):
|
class UpdateDefaultQuotas(workflows.Workflow):
|
||||||
slug = "update_default_quotas"
|
slug = "update_default_quotas"
|
||||||
@ -103,52 +158,3 @@ class UpdateDefaultQuotas(workflows.Workflow):
|
|||||||
success_url = "horizon:admin:defaults:index"
|
success_url = "horizon:admin:defaults:index"
|
||||||
default_steps = (UpdateDefaultComputeQuotasStep,
|
default_steps = (UpdateDefaultComputeQuotasStep,
|
||||||
UpdateDefaultVolumeQuotasStep)
|
UpdateDefaultVolumeQuotasStep)
|
||||||
|
|
||||||
def handle(self, request, data):
|
|
||||||
# Update the default quotas.
|
|
||||||
nova_data = {
|
|
||||||
key: value for key, value in data.items()
|
|
||||||
if key in quotas.NOVA_QUOTA_FIELDS
|
|
||||||
}
|
|
||||||
is_error_nova = False
|
|
||||||
is_error_cinder = False
|
|
||||||
is_volume_service_enabled = cinder.is_volume_service_enabled(request)
|
|
||||||
|
|
||||||
# Update the default quotas for nova.
|
|
||||||
try:
|
|
||||||
nova.default_quota_update(request, **nova_data)
|
|
||||||
except Exception:
|
|
||||||
is_error_nova = True
|
|
||||||
|
|
||||||
# Update the default quotas for cinder.
|
|
||||||
if is_volume_service_enabled:
|
|
||||||
cinder_data = {
|
|
||||||
key: value for key, value in data.items()
|
|
||||||
if key in quotas.CINDER_QUOTA_FIELDS
|
|
||||||
}
|
|
||||||
try:
|
|
||||||
cinder.default_quota_update(request, **cinder_data)
|
|
||||||
except Exception:
|
|
||||||
is_error_cinder = True
|
|
||||||
else:
|
|
||||||
LOG.debug('Unable to update Cinder default quotas'
|
|
||||||
' because the Cinder volume service is disabled.')
|
|
||||||
|
|
||||||
# Analyze errors (if any) to determine what success and error messages
|
|
||||||
# to display to the user.
|
|
||||||
if is_error_nova and not is_error_cinder:
|
|
||||||
if is_volume_service_enabled:
|
|
||||||
self.success_message = _('Default quotas updated for Cinder.')
|
|
||||||
exceptions.handle(request,
|
|
||||||
_('Unable to update default quotas'
|
|
||||||
' for Nova.'))
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
elif is_error_cinder and not is_error_nova:
|
|
||||||
self.success_message = _('Default quotas updated for Nova.')
|
|
||||||
exceptions.handle(request,
|
|
||||||
_('Unable to update default quotas for Cinder.'))
|
|
||||||
elif is_error_nova and is_error_cinder:
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
Loading…
Reference in New Issue
Block a user