Skip to content

Commit

Permalink
Tenant quota API cleanup and tests. Fixed bug 913020.
Browse files Browse the repository at this point in the history
Change-Id: I6c3cbd0d806a94467c12c85e3f1335c30a475236
  • Loading branch information
gabrielhurley committed Jan 12, 2012
1 parent a61734e commit 2cfdbcf
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
8 changes: 8 additions & 0 deletions horizon/horizon/api/keystone.py
Expand Up @@ -142,6 +142,14 @@ def tenant_update(request, tenant_id, tenant_name, description, enabled):
enabled))


def tenant_quota_get(request, tenant_id):
return admin_api(request).quota_sets.get(tenant_id)


def tenant_quota_update(request, tenant_id, **kwargs):
admin_api(request).quota_sets.update(tenant_id, **kwargs)


def tenant_list_for_token(request, token, endpoint_type=None):
c = keystoneclient(request,
token_id=token,
Expand Down
3 changes: 2 additions & 1 deletion horizon/horizon/dashboards/syspanel/tenants/forms.py
Expand Up @@ -149,7 +149,8 @@ class UpdateQuotas(forms.SelfHandlingForm):

def handle(self, request, data):
try:
api.admin_api(request).quota_sets.update(data['tenant_id'],
api.keystone.tenant_quota_update(request,
data['tenant_id'],
metadata_items=data['metadata_items'],
injected_file_content_bytes=data['injected_file_content_bytes'],
volumes=data['volumes'],
Expand Down
97 changes: 97 additions & 0 deletions horizon/horizon/dashboards/syspanel/tenants/tests.py
@@ -0,0 +1,97 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 Nebula, 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.

from django import http
from django.core.urlresolvers import reverse
from mox import IgnoreArg, IsA
from openstackx.api import exceptions as api_exceptions

from horizon import api
from horizon import test


INDEX_URL = reverse('horizon:syspanel:tenants:index')


class FakeQuotaSet(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)


class TenantsViewTests(test.BaseAdminViewTests):
def setUp(self):
super(TenantsViewTests, self).setUp()

self.tenant = api.keystone.Tenant(None)
self.tenant.id = self.TEST_TENANT
self.tenant.name = self.TEST_TENANT_NAME
self.tenant.enabled = True

self.quota_data = dict(metadata_items='1',
injected_file_content_bytes='1',
volumes='1',
gigabytes='1',
ram=1,
floating_ips='1',
instances='1',
injected_files='1',
cores='1')
self.quota = FakeQuotaSet(id=self.TEST_TENANT, **self.quota_data)

self.tenants = [self.tenant,]

def test_index(self):
self.mox.StubOutWithMock(api, 'tenant_list')
api.tenant_list(IsA(http.HttpRequest)).AndReturn(self.tenants)

self.mox.ReplayAll()

res = self.client.get(INDEX_URL)

self.assertTemplateUsed(res, 'syspanel/tenants/index.html')
self.assertItemsEqual(res.context['table'].data, self.tenants)

def test_modify_quota(self):
self.mox.StubOutWithMock(api.keystone, 'tenant_get')
self.mox.StubOutWithMock(api.keystone, 'tenant_quota_get')
self.mox.StubOutWithMock(api.keystone, 'tenant_quota_update')

api.keystone.tenant_get(IgnoreArg(), self.TEST_TENANT) \
.AndReturn(self.tenant)
api.keystone.tenant_quota_get(IgnoreArg(), self.TEST_TENANT) \
.AndReturn(self.quota)
api.keystone.tenant_quota_update(IgnoreArg(),
self.TEST_TENANT,
**self.quota_data)

self.mox.ReplayAll()

url = reverse('horizon:syspanel:tenants:quotas',
args=(self.TEST_TENANT,))
data = {"method": "UpdateQuotas",
"tenant_id": self.TEST_TENANT,
"metadata_items": '1',
"injected_files": '1',
"injected_file_content_bytes": '1',
"cores": '1',
"instances": '1',
"volumes": '1',
"gigabytes": '1',
"ram": 1,
"floating_ips": '1'}
res = self.client.post(url, data)
self.assertRedirectsNoFollow(res, INDEX_URL)
8 changes: 4 additions & 4 deletions horizon/horizon/dashboards/syspanel/tenants/views.py
Expand Up @@ -115,12 +115,12 @@ class QuotasView(forms.ModalFormView):
template_name = 'syspanel/tenants/quotas.html'
context_object_name = 'tenant'

def get_object(self, tenant_id):
return api.tenant_get(self.request, tenant_id)
def get_object(self, *args, **kwargs):
return api.keystone.tenant_get(self.request, kwargs["tenant_id"])

def get_initial(self):
admin_api = api.admin_api(self.request)
quotas = admin_api.quota_sets.get(self.kwargs['tenant_id'])
quotas = api.keystone.tenant_quota_get(self.request,
self.kwargs['tenant_id'])
return {
'tenant_id': quotas.id,
'metadata_items': quotas.metadata_items,
Expand Down

0 comments on commit 2cfdbcf

Please sign in to comment.