summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2019-01-14 17:36:14 +0000
committerGerrit Code Review <review@openstack.org>2019-01-14 17:36:14 +0000
commitdf897988d14108eabe46fcf73bdc37b0c4b3de74 (patch)
tree6d86bf365baeb7b8609e7491da0a620726124147
parent02a0f81f96eaf6a722d392a541b29992401e914c (diff)
parent6902aee3a30d9ca9bbb917af830e5d5a64f6806a (diff)
Merge "Improve RBAC policies panel"
-rw-r--r--openstack_dashboard/dashboards/admin/rbac_policies/forms.py96
-rw-r--r--openstack_dashboard/dashboards/admin/rbac_policies/tests.py6
2 files changed, 49 insertions, 53 deletions
diff --git a/openstack_dashboard/dashboards/admin/rbac_policies/forms.py b/openstack_dashboard/dashboards/admin/rbac_policies/forms.py
index 767557a..18b0398 100644
--- a/openstack_dashboard/dashboards/admin/rbac_policies/forms.py
+++ b/openstack_dashboard/dashboards/admin/rbac_policies/forms.py
@@ -26,56 +26,54 @@ from openstack_dashboard import api
26LOG = logging.getLogger(__name__) 26LOG = logging.getLogger(__name__)
27 27
28 28
29# Predefined provider types. 29ACTION_OBJECT_TYPE_LIST = [
30ACTIONS = [
31 { 30 {
32 'name': 'access_as_shared', 31 'choice': 'shared_network',
33 'value': _('Access as Shared') 32 'label': _("Shared Network"),
33 'object_type': 'network',
34 'action': 'access_as_shared',
34 }, 35 },
35 { 36 {
36 'name': 'access_as_external', 37 'choice': 'external_network',
37 'value': _('Access as External') 38 'label': _("External Network"),
38 } 39 'object_type': 'network',
39] 40 'action': 'access_as_external',
40 41 },
41# Predefined provider object types.
42OBJECT_TYPES = [
43 { 42 {
44 'name': 'network', 43 'choice': 'shared_qos_policy',
45 'value': _('Network') 44 'label': _("Shared QoS Policy"),
45 'object_type': 'qos_policy',
46 'action': 'access_as_shared',
46 } 47 }
47] 48]
48 49
49QOS_POLICY_TYPE = {
50 'name': 'qos_policy',
51 'value': _('QoS Policy')
52}
53
54 50
55class CreatePolicyForm(forms.SelfHandlingForm): 51class CreatePolicyForm(forms.SelfHandlingForm):
56 target_tenant = forms.ThemableChoiceField(label=_("Target Project")) 52 target_tenant = forms.ThemableChoiceField(label=_("Target Project"))
57 object_type = forms.ThemableChoiceField( 53 action_object_type = forms.ThemableChoiceField(
58 label=_("Object Type"), 54 label=_("Action and Object Type"),
59 widget=forms.ThemableSelectWidget( 55 widget=forms.ThemableSelectWidget(
60 attrs={ 56 attrs={
61 'class': 'switchable', 57 'class': 'switchable',
62 'data-slug': 'object_type' 58 'data-slug': 'action_object_type'
63 })) 59 }))
64 network_id = forms.ThemableChoiceField( 60 network_id = forms.ThemableChoiceField(
65 label=_("Network"), 61 label=_("Network"),
66 widget=forms.ThemableSelectWidget(attrs={ 62 widget=forms.ThemableSelectWidget(attrs={
67 'class': 'switched', 63 'class': 'switched',
68 'data-switch-on': 'object_type', 64 'data-switch-on': 'action_object_type',
65 'data-action_object_type-shared_network': _('Network'),
66 'data-action_object_type-external_network': _('Network'),
69 }), 67 }),
70 required=False) 68 required=False)
71 qos_policy_id = forms.ThemableChoiceField( 69 qos_policy_id = forms.ThemableChoiceField(
72 label=_("QoS Policy"), 70 label=_("QoS Policy"),
73 widget=forms.ThemableSelectWidget(attrs={ 71 widget=forms.ThemableSelectWidget(attrs={
74 'class': 'switched', 72 'class': 'switched',
75 'data-switch-on': 'object_type', 73 'data-switch-on': 'action_object_type',
74 'data-action_object_type-shared_qos_policy': _('QoS Policy'),
76 }), 75 }),
77 required=False) 76 required=False)
78 action = forms.ThemableChoiceField(label=_("Action"))
79 77
80 def __init__(self, request, *args, **kwargs): 78 def __init__(self, request, *args, **kwargs):
81 super(CreatePolicyForm, self).__init__(request, *args, **kwargs) 79 super(CreatePolicyForm, self).__init__(request, *args, **kwargs)
@@ -85,48 +83,47 @@ class CreatePolicyForm(forms.SelfHandlingForm):
85 for tenant in tenants: 83 for tenant in tenants:
86 tenant_choices.append((tenant.id, tenant.name)) 84 tenant_choices.append((tenant.id, tenant.name))
87 self.fields['target_tenant'].choices = tenant_choices 85 self.fields['target_tenant'].choices = tenant_choices
88 action_choices = [('', _("Select an action"))] 86
89 for action in ACTIONS:
90 action_choices.append((action['name'],
91 action['value']))
92 self.fields['action'].choices = action_choices
93 network_choices = []
94 networks = api.neutron.network_list(request) 87 networks = api.neutron.network_list(request)
95 for network in networks: 88 network_choices = [(network.id, network.name)
96 network_choices.append((network.id, network.name)) 89 for network in networks]
90 network_choices.insert(0, ('', _("Select a network")))
97 self.fields['network_id'].choices = network_choices 91 self.fields['network_id'].choices = network_choices
98 92
99 # If enable QoS Policy 93 # If enable QoS Policy
100 if api.neutron.is_extension_supported(request, extension_alias='qos'): 94 qos_supported = api.neutron.is_extension_supported(
95 request, extension_alias='qos')
96 if qos_supported:
101 qos_policies = api.neutron.policy_list(request) 97 qos_policies = api.neutron.policy_list(request)
102 qos_choices = [(qos_policy['id'], qos_policy['name']) 98 qos_choices = [(qos_policy['id'], qos_policy['name'])
103 for qos_policy in qos_policies] 99 for qos_policy in qos_policies]
100 qos_choices.insert(0, ('', _("Select a QoS policy")))
104 self.fields['qos_policy_id'].choices = qos_choices 101 self.fields['qos_policy_id'].choices = qos_choices
105 if QOS_POLICY_TYPE not in OBJECT_TYPES:
106 OBJECT_TYPES.append(QOS_POLICY_TYPE)
107 102
108 object_type_choices = [('', _("Select an object type"))] 103 action_object_type_choices = [('', _("Select action and object type"))]
109 for object_type in OBJECT_TYPES: 104 for x in ACTION_OBJECT_TYPE_LIST:
110 object_type_choices.append((object_type['name'], 105 if x['choice'] == 'shared_qos_policy' and not qos_supported:
111 object_type['value'])) 106 continue
112 self.fields['object_type'].choices = object_type_choices 107 action_object_type_choices.append((x['choice'], x['label']))
108 self.fields['action_object_type'].choices = action_object_type_choices
113 109
114 # Register object types which required 110 def _get_action_and_object_type(self, action_object_type):
115 self.fields['network_id'].widget.attrs.update( 111 _map = dict((x['choice'], x) for x in ACTION_OBJECT_TYPE_LIST)
116 {'data-object_type-network': _('Network')}) 112 selected = _map[action_object_type]
117 self.fields['qos_policy_id'].widget.attrs.update( 113 return (selected['action'], selected['object_type'])
118 {'data-object_type-qos_policy': _('QoS Policy')})
119 114
120 def handle(self, request, data): 115 def handle(self, request, data):
121 try: 116 try:
117 action, object_type = self._get_action_and_object_type(
118 data['action_object_type'])
122 params = { 119 params = {
123 'target_tenant': data['target_tenant'], 120 'target_tenant': data['target_tenant'],
124 'action': data['action'], 121 'action': action,
125 'object_type': data['object_type'], 122 'object_type': object_type,
126 } 123 }
127 if data['object_type'] == 'network': 124 if object_type == 'network':
128 params['object_id'] = data['network_id'] 125 params['object_id'] = data['network_id']
129 elif data['object_type'] == 'qos_policy': 126 elif object_type == 'qos_policy':
130 params['object_id'] = data['qos_policy_id'] 127 params['object_id'] = data['qos_policy_id']
131 128
132 rbac_policy = api.neutron.rbac_policy_create(request, **params) 129 rbac_policy = api.neutron.rbac_policy_create(request, **params)
@@ -147,6 +144,7 @@ class UpdatePolicyForm(forms.SelfHandlingForm):
147 def __init__(self, request, *args, **kwargs): 144 def __init__(self, request, *args, **kwargs):
148 super(UpdatePolicyForm, self).__init__(request, *args, **kwargs) 145 super(UpdatePolicyForm, self).__init__(request, *args, **kwargs)
149 tenant_choices = [('', _("Select a project"))] 146 tenant_choices = [('', _("Select a project"))]
147 tenant_choices.append(("*", "*"))
150 tenants, has_more = api.keystone.tenant_list(request) 148 tenants, has_more = api.keystone.tenant_list(request)
151 for tenant in tenants: 149 for tenant in tenants:
152 tenant_choices.append((tenant.id, tenant.name)) 150 tenant_choices.append((tenant.id, tenant.name))
diff --git a/openstack_dashboard/dashboards/admin/rbac_policies/tests.py b/openstack_dashboard/dashboards/admin/rbac_policies/tests.py
index 8e7e71a..8e2d53b 100644
--- a/openstack_dashboard/dashboards/admin/rbac_policies/tests.py
+++ b/openstack_dashboard/dashboards/admin/rbac_policies/tests.py
@@ -63,8 +63,7 @@ class RBACPolicyTests(test.BaseAdminViewTests):
63 self.mock_rbac_policy_create.return_value = rbac_policy 63 self.mock_rbac_policy_create.return_value = rbac_policy
64 64
65 form_data = {'target_tenant': rbac_policy.target_tenant, 65 form_data = {'target_tenant': rbac_policy.target_tenant,
66 'action': 'access_as_external', 66 'action_object_type': 'external_network',
67 'object_type': 'network',
68 'network_id': network.id} 67 'network_id': network.id}
69 url = reverse('horizon:admin:rbac_policies:create') 68 url = reverse('horizon:admin:rbac_policies:create')
70 res = self.client.post(url, form_data) 69 res = self.client.post(url, form_data)
@@ -100,8 +99,7 @@ class RBACPolicyTests(test.BaseAdminViewTests):
100 self.mock_rbac_policy_create.return_value = rbac_policy 99 self.mock_rbac_policy_create.return_value = rbac_policy
101 100
102 form_data = {'target_tenant': rbac_policy.target_tenant, 101 form_data = {'target_tenant': rbac_policy.target_tenant,
103 'action': 'access_as_shared', 102 'action_object_type': 'shared_qos_policy',
104 'object_type': 'qos_policy',
105 'qos_policy_id': qos_policy.id} 103 'qos_policy_id': qos_policy.id}
106 url = reverse('horizon:admin:rbac_policies:create') 104 url = reverse('horizon:admin:rbac_policies:create')
107 res = self.client.post(url, form_data) 105 res = self.client.post(url, form_data)