diff --git a/openstack_dashboard/dashboards/admin/info/tabs.py b/openstack_dashboard/dashboards/admin/info/tabs.py index fcffdd2e7..5c3184035 100644 --- a/openstack_dashboard/dashboards/admin/info/tabs.py +++ b/openstack_dashboard/dashboards/admin/info/tabs.py @@ -23,8 +23,6 @@ from openstack_dashboard.api import keystone from openstack_dashboard.usage import quotas from .tables import QuotasTable, ServicesTable from openstack_dashboard.api.base import is_service_enabled -from openstack_dashboard.dashboards.admin.projects.workflows import \ - CINDER_QUOTA_FIELDS class DefaultQuotasTab(tabs.TableTab): @@ -35,11 +33,8 @@ class DefaultQuotasTab(tabs.TableTab): def get_quotas_data(self): request = self.tab_group.request - disabled_quotas = [] - if not is_service_enabled(self.request, 'volume'): - disabled_quotas.extend(CINDER_QUOTA_FIELDS) try: - quota_set = quotas.get_default_quota_data(request, disabled_quotas) + quota_set = quotas.get_default_quota_data(request) data = quota_set.items except: data = [] diff --git a/openstack_dashboard/dashboards/admin/projects/tests.py b/openstack_dashboard/dashboards/admin/projects/tests.py index c3cad2996..fd5a45347 100644 --- a/openstack_dashboard/dashboards/admin/projects/tests.py +++ b/openstack_dashboard/dashboards/admin/projects/tests.py @@ -26,9 +26,7 @@ from horizon import exceptions from openstack_dashboard import api from openstack_dashboard.test import helpers as test from openstack_dashboard.usage import quotas -from .workflows import CreateProject, UpdateProject, NOVA_QUOTA_FIELDS, \ - CINDER_QUOTA_FIELDS -from .views import QUOTA_FIELDS +from .workflows import CreateProject, UpdateProject INDEX_URL = reverse('horizon:admin:projects:index') @@ -60,7 +58,7 @@ class CreateProjectWorkflowTests(test.BaseAdminViewTests): def _get_quota_info(self, quota): quota_data = {} - for field in QUOTA_FIELDS: + for field in quotas.QUOTA_FIELDS: quota_data[field] = int(quota.get(field).limit) return quota_data @@ -153,12 +151,12 @@ class CreateProjectWorkflowTests(test.BaseAdminViewTests): role_id=role.id) nova_updated_quota = dict([(key, quota_data[key]) for key in - NOVA_QUOTA_FIELDS]) + quotas.NOVA_QUOTA_FIELDS]) api.nova.tenant_quota_update(IsA(http.HttpRequest), project.id, **nova_updated_quota) cinder_updated_quota = dict([(key, quota_data[key]) for key in - CINDER_QUOTA_FIELDS]) + quotas.CINDER_QUOTA_FIELDS]) api.cinder.tenant_quota_update(IsA(http.HttpRequest), project.id, **cinder_updated_quota) @@ -283,7 +281,7 @@ class CreateProjectWorkflowTests(test.BaseAdminViewTests): role_id=role.id) nova_updated_quota = dict([(key, quota_data[key]) for key in - NOVA_QUOTA_FIELDS]) + quotas.NOVA_QUOTA_FIELDS]) api.nova.tenant_quota_update(IsA(http.HttpRequest), project.id, **nova_updated_quota) \ @@ -348,13 +346,13 @@ class CreateProjectWorkflowTests(test.BaseAdminViewTests): break nova_updated_quota = dict([(key, quota_data[key]) for key in - NOVA_QUOTA_FIELDS]) + quotas.NOVA_QUOTA_FIELDS]) api.nova.tenant_quota_update(IsA(http.HttpRequest), project.id, **nova_updated_quota) cinder_updated_quota = dict([(key, quota_data[key]) for key in - CINDER_QUOTA_FIELDS]) + quotas.CINDER_QUOTA_FIELDS]) api.cinder.tenant_quota_update(IsA(http.HttpRequest), project.id, **cinder_updated_quota) @@ -405,7 +403,7 @@ class CreateProjectWorkflowTests(test.BaseAdminViewTests): class UpdateProjectWorkflowTests(test.BaseAdminViewTests): def _get_quota_info(self, quota): quota_data = {} - for field in QUOTA_FIELDS: + for field in quotas.QUOTA_FIELDS: quota_data[field] = int(quota.get(field).limit) return quota_data @@ -561,13 +559,13 @@ class UpdateProjectWorkflowTests(test.BaseAdminViewTests): role_id='1') nova_updated_quota = dict([(key, updated_quota[key]) for key in - NOVA_QUOTA_FIELDS]) + quotas.NOVA_QUOTA_FIELDS]) api.nova.tenant_quota_update(IsA(http.HttpRequest), project.id, **nova_updated_quota) cinder_updated_quota = dict([(key, updated_quota[key]) for key in - CINDER_QUOTA_FIELDS]) + quotas.CINDER_QUOTA_FIELDS]) api.cinder.tenant_quota_update(IsA(http.HttpRequest), project.id, **cinder_updated_quota) @@ -763,7 +761,7 @@ class UpdateProjectWorkflowTests(test.BaseAdminViewTests): role_id='2') nova_updated_quota = dict([(key, updated_quota[key]) for key in - NOVA_QUOTA_FIELDS]) + quotas.NOVA_QUOTA_FIELDS]) api.nova.tenant_quota_update(IsA(http.HttpRequest), project.id, **nova_updated_quota) \ diff --git a/openstack_dashboard/dashboards/admin/projects/views.py b/openstack_dashboard/dashboards/admin/projects/views.py index 85614e29e..e87fbf52c 100644 --- a/openstack_dashboard/dashboards/admin/projects/views.py +++ b/openstack_dashboard/dashboards/admin/projects/views.py @@ -33,7 +33,7 @@ from openstack_dashboard.usage import quotas from openstack_dashboard.dashboards.admin.users.views import CreateView from .forms import CreateUser from .tables import TenantsTable, TenantUsersTable, AddUsersTable -from .workflows import CreateProject, UpdateProject, QUOTA_FIELDS +from .workflows import CreateProject, UpdateProject LOG = logging.getLogger(__name__) @@ -136,7 +136,7 @@ class CreateProjectView(workflows.WorkflowView): # get initial quota defaults try: quota_defaults = quotas.get_default_quota_data(self.request) - for field in QUOTA_FIELDS: + for field in quotas.QUOTA_FIELDS: initial[field] = quota_defaults.get(field).limit except: @@ -166,7 +166,7 @@ class UpdateProjectView(workflows.WorkflowView): # get initial project quota quota_data = quotas.get_tenant_quota_data(self.request, tenant_id=project_id) - for field in QUOTA_FIELDS: + for field in quotas.QUOTA_FIELDS: initial[field] = quota_data.get(field).limit except: exceptions.handle(self.request, diff --git a/openstack_dashboard/dashboards/admin/projects/workflows.py b/openstack_dashboard/dashboards/admin/projects/workflows.py index d9534d946..2b0986a8e 100644 --- a/openstack_dashboard/dashboards/admin/projects/workflows.py +++ b/openstack_dashboard/dashboards/admin/projects/workflows.py @@ -31,25 +31,14 @@ from horizon import messages from openstack_dashboard import api from openstack_dashboard.api import cinder, nova from openstack_dashboard.api.base import is_service_enabled +from openstack_dashboard.usage.quotas import (NOVA_QUOTA_FIELDS, + CINDER_QUOTA_FIELDS, + QUOTA_FIELDS, + get_disabled_quotas) INDEX_URL = "horizon:admin:projects:index" ADD_USER_URL = "horizon:admin:projects:create_user" -NOVA_QUOTA_FIELDS = ("metadata_items", - "cores", - "instances", - "injected_files", - "injected_file_content_bytes", - "ram", - "floating_ips", - "security_groups", - "security_group_rules",) - -CINDER_QUOTA_FIELDS = ("volumes", - "gigabytes",) - -QUOTA_FIELDS = NOVA_QUOTA_FIELDS + CINDER_QUOTA_FIELDS - class UpdateProjectQuotaAction(workflows.Action): ifcb_label = _("Injected File Content Bytes") @@ -70,6 +59,16 @@ class UpdateProjectQuotaAction(workflows.Action): security_group_rules = forms.IntegerField(min_value=-1, label=_("Security Group Rules")) + def __init__(self, request, *args, **kwargs): + super(UpdateProjectQuotaAction, self).__init__(request, + *args, + **kwargs) + disabled_quotas = get_disabled_quotas(request) + for field in disabled_quotas: + if field in self.fields: + self.fields[field].required = False + self.fields[field].widget = forms.HiddenInput() + class Meta: name = _("Quota") slug = 'update_quotas' diff --git a/openstack_dashboard/usage/quotas.py b/openstack_dashboard/usage/quotas.py index a517b1181..5f382b2d9 100644 --- a/openstack_dashboard/usage/quotas.py +++ b/openstack_dashboard/usage/quotas.py @@ -6,8 +6,21 @@ from horizon.utils.memoized import memoized from openstack_dashboard.api import nova, cinder, network from openstack_dashboard.api.base import is_service_enabled, QuotaSet -from openstack_dashboard.dashboards.admin.projects.workflows import \ - CINDER_QUOTA_FIELDS + +NOVA_QUOTA_FIELDS = ("metadata_items", + "cores", + "instances", + "injected_files", + "injected_file_content_bytes", + "ram", + "floating_ips", + "security_groups", + "security_group_rules",) + +CINDER_QUOTA_FIELDS = ("volumes", + "gigabytes",) + +QUOTA_FIELDS = NOVA_QUOTA_FIELDS + CINDER_QUOTA_FIELDS class QuotaUsage(dict): @@ -54,12 +67,15 @@ class QuotaUsage(dict): self.usages[name]['available'] = available -def _get_quota_data(request, method_name, disabled_quotas=[], tenant_id=None): +def _get_quota_data(request, method_name, disabled_quotas=None, + tenant_id=None): quotasets = [] if not tenant_id: tenant_id = request.user.tenant_id quotasets.append(getattr(nova, method_name)(request, tenant_id)) qs = QuotaSet() + if disabled_quotas is None: + disabled_quotas = get_disabled_quotas(request) if 'volumes' not in disabled_quotas: quotasets.append(getattr(cinder, method_name)(request, tenant_id)) for quota in itertools.chain(*quotasets): @@ -68,29 +84,35 @@ def _get_quota_data(request, method_name, disabled_quotas=[], tenant_id=None): return qs -def get_default_quota_data(request, disabled_quotas=[], tenant_id=None): +def get_default_quota_data(request, disabled_quotas=None, tenant_id=None): return _get_quota_data(request, "default_quota_get", - disabled_quotas, - tenant_id) + disabled_quotas=disabled_quotas, + tenant_id=tenant_id) -def get_tenant_quota_data(request, disabled_quotas=[], tenant_id=None): +def get_tenant_quota_data(request, disabled_quotas=None, tenant_id=None): return _get_quota_data(request, "tenant_quota_get", - disabled_quotas, - tenant_id) + disabled_quotas=disabled_quotas, + tenant_id=tenant_id) + + +def get_disabled_quotas(request): + disabled_quotas = [] + if not is_service_enabled(request, 'volume'): + disabled_quotas.extend(CINDER_QUOTA_FIELDS) + return disabled_quotas @memoized def tenant_quota_usages(request): # Get our quotas and construct our usage object. - disabled_quotas = [] - if not is_service_enabled(request, 'volume'): - disabled_quotas.extend(CINDER_QUOTA_FIELDS) + disabled_quotas = get_disabled_quotas(request) usages = QuotaUsage() - for quota in get_tenant_quota_data(request, disabled_quotas): + for quota in get_tenant_quota_data(request, + disabled_quotas=disabled_quotas): usages.add_quota(quota) # Get our usages.