Merge "Improve RBAC policies panel"

This commit is contained in:
Zuul 2019-01-14 17:36:14 +00:00 committed by Gerrit Code Review
commit df897988d1
2 changed files with 49 additions and 53 deletions

View File

@ -26,56 +26,54 @@ from openstack_dashboard import api
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
# Predefined provider types. ACTION_OBJECT_TYPE_LIST = [
ACTIONS = [
{ {
'name': 'access_as_shared', 'choice': 'shared_network',
'value': _('Access as Shared') 'label': _("Shared Network"),
'object_type': 'network',
'action': 'access_as_shared',
}, },
{ {
'name': 'access_as_external', 'choice': 'external_network',
'value': _('Access as External') 'label': _("External Network"),
} 'object_type': 'network',
] 'action': 'access_as_external',
},
# Predefined provider object types.
OBJECT_TYPES = [
{ {
'name': 'network', 'choice': 'shared_qos_policy',
'value': _('Network') 'label': _("Shared QoS Policy"),
'object_type': 'qos_policy',
'action': 'access_as_shared',
} }
] ]
QOS_POLICY_TYPE = {
'name': 'qos_policy',
'value': _('QoS Policy')
}
class CreatePolicyForm(forms.SelfHandlingForm): class CreatePolicyForm(forms.SelfHandlingForm):
target_tenant = forms.ThemableChoiceField(label=_("Target Project")) target_tenant = forms.ThemableChoiceField(label=_("Target Project"))
object_type = forms.ThemableChoiceField( action_object_type = forms.ThemableChoiceField(
label=_("Object Type"), label=_("Action and Object Type"),
widget=forms.ThemableSelectWidget( widget=forms.ThemableSelectWidget(
attrs={ attrs={
'class': 'switchable', 'class': 'switchable',
'data-slug': 'object_type' 'data-slug': 'action_object_type'
})) }))
network_id = forms.ThemableChoiceField( network_id = forms.ThemableChoiceField(
label=_("Network"), label=_("Network"),
widget=forms.ThemableSelectWidget(attrs={ widget=forms.ThemableSelectWidget(attrs={
'class': 'switched', 'class': 'switched',
'data-switch-on': 'object_type', 'data-switch-on': 'action_object_type',
'data-action_object_type-shared_network': _('Network'),
'data-action_object_type-external_network': _('Network'),
}), }),
required=False) required=False)
qos_policy_id = forms.ThemableChoiceField( qos_policy_id = forms.ThemableChoiceField(
label=_("QoS Policy"), label=_("QoS Policy"),
widget=forms.ThemableSelectWidget(attrs={ widget=forms.ThemableSelectWidget(attrs={
'class': 'switched', 'class': 'switched',
'data-switch-on': 'object_type', 'data-switch-on': 'action_object_type',
'data-action_object_type-shared_qos_policy': _('QoS Policy'),
}), }),
required=False) required=False)
action = forms.ThemableChoiceField(label=_("Action"))
def __init__(self, request, *args, **kwargs): def __init__(self, request, *args, **kwargs):
super(CreatePolicyForm, self).__init__(request, *args, **kwargs) super(CreatePolicyForm, self).__init__(request, *args, **kwargs)
@ -85,48 +83,47 @@ class CreatePolicyForm(forms.SelfHandlingForm):
for tenant in tenants: for tenant in tenants:
tenant_choices.append((tenant.id, tenant.name)) tenant_choices.append((tenant.id, tenant.name))
self.fields['target_tenant'].choices = tenant_choices self.fields['target_tenant'].choices = tenant_choices
action_choices = [('', _("Select an action"))]
for action in ACTIONS:
action_choices.append((action['name'],
action['value']))
self.fields['action'].choices = action_choices
network_choices = []
networks = api.neutron.network_list(request) networks = api.neutron.network_list(request)
for network in networks: network_choices = [(network.id, network.name)
network_choices.append((network.id, network.name)) for network in networks]
network_choices.insert(0, ('', _("Select a network")))
self.fields['network_id'].choices = network_choices self.fields['network_id'].choices = network_choices
# If enable QoS Policy # If enable QoS Policy
if api.neutron.is_extension_supported(request, extension_alias='qos'): qos_supported = api.neutron.is_extension_supported(
request, extension_alias='qos')
if qos_supported:
qos_policies = api.neutron.policy_list(request) qos_policies = api.neutron.policy_list(request)
qos_choices = [(qos_policy['id'], qos_policy['name']) qos_choices = [(qos_policy['id'], qos_policy['name'])
for qos_policy in qos_policies] for qos_policy in qos_policies]
qos_choices.insert(0, ('', _("Select a QoS policy")))
self.fields['qos_policy_id'].choices = qos_choices self.fields['qos_policy_id'].choices = qos_choices
if QOS_POLICY_TYPE not in OBJECT_TYPES:
OBJECT_TYPES.append(QOS_POLICY_TYPE)
object_type_choices = [('', _("Select an object type"))] action_object_type_choices = [('', _("Select action and object type"))]
for object_type in OBJECT_TYPES: for x in ACTION_OBJECT_TYPE_LIST:
object_type_choices.append((object_type['name'], if x['choice'] == 'shared_qos_policy' and not qos_supported:
object_type['value'])) continue
self.fields['object_type'].choices = object_type_choices action_object_type_choices.append((x['choice'], x['label']))
self.fields['action_object_type'].choices = action_object_type_choices
# Register object types which required def _get_action_and_object_type(self, action_object_type):
self.fields['network_id'].widget.attrs.update( _map = dict((x['choice'], x) for x in ACTION_OBJECT_TYPE_LIST)
{'data-object_type-network': _('Network')}) selected = _map[action_object_type]
self.fields['qos_policy_id'].widget.attrs.update( return (selected['action'], selected['object_type'])
{'data-object_type-qos_policy': _('QoS Policy')})
def handle(self, request, data): def handle(self, request, data):
try: try:
action, object_type = self._get_action_and_object_type(
data['action_object_type'])
params = { params = {
'target_tenant': data['target_tenant'], 'target_tenant': data['target_tenant'],
'action': data['action'], 'action': action,
'object_type': data['object_type'], 'object_type': object_type,
} }
if data['object_type'] == 'network': if object_type == 'network':
params['object_id'] = data['network_id'] params['object_id'] = data['network_id']
elif data['object_type'] == 'qos_policy': elif object_type == 'qos_policy':
params['object_id'] = data['qos_policy_id'] params['object_id'] = data['qos_policy_id']
rbac_policy = api.neutron.rbac_policy_create(request, **params) rbac_policy = api.neutron.rbac_policy_create(request, **params)
@ -147,6 +144,7 @@ class UpdatePolicyForm(forms.SelfHandlingForm):
def __init__(self, request, *args, **kwargs): def __init__(self, request, *args, **kwargs):
super(UpdatePolicyForm, self).__init__(request, *args, **kwargs) super(UpdatePolicyForm, self).__init__(request, *args, **kwargs)
tenant_choices = [('', _("Select a project"))] tenant_choices = [('', _("Select a project"))]
tenant_choices.append(("*", "*"))
tenants, has_more = api.keystone.tenant_list(request) tenants, has_more = api.keystone.tenant_list(request)
for tenant in tenants: for tenant in tenants:
tenant_choices.append((tenant.id, tenant.name)) tenant_choices.append((tenant.id, tenant.name))

View File

@ -63,8 +63,7 @@ class RBACPolicyTests(test.BaseAdminViewTests):
self.mock_rbac_policy_create.return_value = rbac_policy self.mock_rbac_policy_create.return_value = rbac_policy
form_data = {'target_tenant': rbac_policy.target_tenant, form_data = {'target_tenant': rbac_policy.target_tenant,
'action': 'access_as_external', 'action_object_type': 'external_network',
'object_type': 'network',
'network_id': network.id} 'network_id': network.id}
url = reverse('horizon:admin:rbac_policies:create') url = reverse('horizon:admin:rbac_policies:create')
res = self.client.post(url, form_data) res = self.client.post(url, form_data)
@ -100,8 +99,7 @@ class RBACPolicyTests(test.BaseAdminViewTests):
self.mock_rbac_policy_create.return_value = rbac_policy self.mock_rbac_policy_create.return_value = rbac_policy
form_data = {'target_tenant': rbac_policy.target_tenant, form_data = {'target_tenant': rbac_policy.target_tenant,
'action': 'access_as_shared', 'action_object_type': 'shared_qos_policy',
'object_type': 'qos_policy',
'qos_policy_id': qos_policy.id} 'qos_policy_id': qos_policy.id}
url = reverse('horizon:admin:rbac_policies:create') url = reverse('horizon:admin:rbac_policies:create')
res = self.client.post(url, form_data) res = self.client.post(url, form_data)