Allow control of whether a user can specify a volume mount point.

Since some hypervisors don't support this feature, it makes the
field in the attach form extremely misleading.

Change-Id: I875c63d35b22b6b73314e001dbc1fbdce4363e25
This commit is contained in:
Gabriel Hurley 2012-08-02 15:40:04 -07:00
parent e6f0d72fc7
commit 414fc7a31f
4 changed files with 46 additions and 6 deletions

View File

@ -7,6 +7,7 @@
Views for managing Nova volumes. Views for managing Nova volumes.
""" """
from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.forms import ValidationError from django.forms import ValidationError
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -126,6 +127,15 @@ class AttachForm(forms.SelfHandlingForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(AttachForm, self).__init__(*args, **kwargs) super(AttachForm, self).__init__(*args, **kwargs)
# Hide the device field if the hypervisor doesn't support it.
hypervisor_features = getattr(settings,
"OPENSTACK_HYPERVISOR_FEATURES",
{})
can_set_mount_point = hypervisor_features.get("can_set_mount_point",
True)
if not can_set_mount_point:
self.fields['device'].widget = forms.widgets.HiddenInput()
# populate volume_id # populate volume_id
volume = kwargs.get('initial', {}).get("volume", None) volume = kwargs.get('initial', {}).get("volume", None)
if volume: if volume:

View File

@ -19,7 +19,9 @@
# under the License. # under the License.
from django import http from django import http
from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.forms import widgets
from mox import IsA from mox import IsA
from horizon import api from horizon import api
@ -183,20 +185,40 @@ class VolumeViewTests(test.TestCase):
volume = self.volumes.first() volume = self.volumes.first()
servers = self.servers.list() servers = self.servers.list()
api.volume_get(IsA(http.HttpRequest), volume.id) \ api.volume_get(IsA(http.HttpRequest), volume.id).AndReturn(volume)
.AndReturn(volume)
api.nova.server_list(IsA(http.HttpRequest)).AndReturn(servers) api.nova.server_list(IsA(http.HttpRequest)).AndReturn(servers)
self.mox.ReplayAll() self.mox.ReplayAll()
url = reverse('horizon:nova:volumes:attach', url = reverse('horizon:nova:volumes:attach', args=[volume.id])
args=[volume.id])
res = self.client.get(url) res = self.client.get(url)
# Asserting length of 2 accounts for the one instance option, # Asserting length of 2 accounts for the one instance option,
# and the one 'Choose Instance' option. # and the one 'Choose Instance' option.
self.assertEqual(len(res.context['form'].fields['instance']._choices), form = res.context['form']
self.assertEqual(len(form.fields['instance']._choices),
2) 2)
self.assertEqual(res.status_code, 200) self.assertEqual(res.status_code, 200)
self.assertTrue(isinstance(form.fields['device'].widget,
widgets.TextInput))
@test.create_stubs({api: ('volume_get',), api.nova: ('server_list',)})
def test_edit_attachments_cannot_set_mount_point(self):
PREV = settings.OPENSTACK_HYPERVISOR_FEATURES['can_set_mount_point']
settings.OPENSTACK_HYPERVISOR_FEATURES['can_set_mount_point'] = False
volume = self.volumes.first()
servers = self.servers.list()
api.volume_get(IsA(http.HttpRequest), volume.id).AndReturn(volume)
api.nova.server_list(IsA(http.HttpRequest)).AndReturn(servers)
self.mox.ReplayAll()
url = reverse('horizon:nova:volumes:attach', args=[volume.id])
res = self.client.get(url)
# Assert the device field is hidden.
form = res.context['form']
self.assertTrue(isinstance(form.fields['device'].widget,
widgets.HiddenInput))
settings.OPENSTACK_HYPERVISOR_FEATURES['can_set_mount_point'] = PREV
@test.create_stubs({api: ('volume_get',), @test.create_stubs({api: ('volume_get',),
api.nova: ('server_get', 'server_list',)}) api.nova: ('server_get', 'server_list',)})

View File

@ -141,6 +141,10 @@ OPENSTACK_KEYSTONE_BACKEND = {
'can_edit_user': True 'can_edit_user': True
} }
OPENSTACK_HYPERVISOR_FEATURES = {
'can_set_mount_point': True
}
LOGGING = { LOGGING = {
'version': 1, 'version': 1,
'disable_existing_loggers': False, 'disable_existing_loggers': False,

View File

@ -74,6 +74,10 @@ OPENSTACK_KEYSTONE_BACKEND = {
'can_edit_user': True 'can_edit_user': True
} }
OPENSTACK_HYPERVISOR_FEATURES = {
'can_set_mount_point': True
}
# OPENSTACK_ENDPOINT_TYPE specifies the endpoint type to use for the endpoints # OPENSTACK_ENDPOINT_TYPE specifies the endpoint type to use for the endpoints
# in the Keystone service catalog. Use this setting when Horizon is running # in the Keystone service catalog. Use this setting when Horizon is running
# external to the OpenStack environment. The default is 'internalURL'. # external to the OpenStack environment. The default is 'internalURL'.