Browse Source

Merge "Improve RBAC policies panel"

tags/15.0.0.0b2
Zuul 5 months ago
parent
commit
df897988d1

+ 47
- 49
openstack_dashboard/dashboards/admin/rbac_policies/forms.py View File

@@ -26,56 +26,54 @@ from openstack_dashboard import api
26 26
 LOG = logging.getLogger(__name__)
27 27
 
28 28
 
29
-# Predefined provider types.
30
-ACTIONS = [
29
+ACTION_OBJECT_TYPE_LIST = [
31 30
     {
32
-        'name': 'access_as_shared',
33
-        'value': _('Access as Shared')
31
+        'choice': 'shared_network',
32
+        'label': _("Shared Network"),
33
+        'object_type': 'network',
34
+        'action': 'access_as_shared',
34 35
     },
35 36
     {
36
-        'name': 'access_as_external',
37
-        'value': _('Access as External')
38
-    }
39
-]
40
-
41
-# Predefined provider object types.
42
-OBJECT_TYPES = [
37
+        'choice': 'external_network',
38
+        'label': _("External Network"),
39
+        'object_type': 'network',
40
+        'action': 'access_as_external',
41
+    },
43 42
     {
44
-        'name': 'network',
45
-        'value': _('Network')
43
+        'choice': 'shared_qos_policy',
44
+        'label': _("Shared QoS Policy"),
45
+        'object_type': 'qos_policy',
46
+        'action': 'access_as_shared',
46 47
     }
47 48
 ]
48 49
 
49
-QOS_POLICY_TYPE = {
50
-    'name': 'qos_policy',
51
-    'value': _('QoS Policy')
52
-}
53
-
54 50
 
55 51
 class CreatePolicyForm(forms.SelfHandlingForm):
56 52
     target_tenant = forms.ThemableChoiceField(label=_("Target Project"))
57
-    object_type = forms.ThemableChoiceField(
58
-        label=_("Object Type"),
53
+    action_object_type = forms.ThemableChoiceField(
54
+        label=_("Action and Object Type"),
59 55
         widget=forms.ThemableSelectWidget(
60 56
             attrs={
61 57
                 'class': 'switchable',
62
-                'data-slug': 'object_type'
58
+                'data-slug': 'action_object_type'
63 59
             }))
64 60
     network_id = forms.ThemableChoiceField(
65 61
         label=_("Network"),
66 62
         widget=forms.ThemableSelectWidget(attrs={
67 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 68
         required=False)
71 69
     qos_policy_id = forms.ThemableChoiceField(
72 70
         label=_("QoS Policy"),
73 71
         widget=forms.ThemableSelectWidget(attrs={
74 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 76
         required=False)
78
-    action = forms.ThemableChoiceField(label=_("Action"))
79 77
 
80 78
     def __init__(self, request, *args, **kwargs):
81 79
         super(CreatePolicyForm, self).__init__(request, *args, **kwargs)
@@ -85,48 +83,47 @@ class CreatePolicyForm(forms.SelfHandlingForm):
85 83
         for tenant in tenants:
86 84
             tenant_choices.append((tenant.id, tenant.name))
87 85
         self.fields['target_tenant'].choices = tenant_choices
88
-        action_choices = [('', _("Select an action"))]
89
-        for action in ACTIONS:
90
-            action_choices.append((action['name'],
91
-                                   action['value']))
92
-        self.fields['action'].choices = action_choices
93
-        network_choices = []
86
+
94 87
         networks = api.neutron.network_list(request)
95
-        for network in networks:
96
-            network_choices.append((network.id, network.name))
88
+        network_choices = [(network.id, network.name)
89
+                           for network in networks]
90
+        network_choices.insert(0, ('', _("Select a network")))
97 91
         self.fields['network_id'].choices = network_choices
98 92
 
99 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 97
             qos_policies = api.neutron.policy_list(request)
102 98
             qos_choices = [(qos_policy['id'], qos_policy['name'])
103 99
                            for qos_policy in qos_policies]
100
+            qos_choices.insert(0, ('', _("Select a QoS policy")))
104 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"))]
109
-        for object_type in OBJECT_TYPES:
110
-            object_type_choices.append((object_type['name'],
111
-                                        object_type['value']))
112
-        self.fields['object_type'].choices = object_type_choices
103
+        action_object_type_choices = [('', _("Select action and object type"))]
104
+        for x in ACTION_OBJECT_TYPE_LIST:
105
+            if x['choice'] == 'shared_qos_policy' and not qos_supported:
106
+                continue
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
115
-        self.fields['network_id'].widget.attrs.update(
116
-            {'data-object_type-network': _('Network')})
117
-        self.fields['qos_policy_id'].widget.attrs.update(
118
-            {'data-object_type-qos_policy': _('QoS Policy')})
110
+    def _get_action_and_object_type(self, action_object_type):
111
+        _map = dict((x['choice'], x) for x in ACTION_OBJECT_TYPE_LIST)
112
+        selected = _map[action_object_type]
113
+        return (selected['action'], selected['object_type'])
119 114
 
120 115
     def handle(self, request, data):
121 116
         try:
117
+            action, object_type = self._get_action_and_object_type(
118
+                data['action_object_type'])
122 119
             params = {
123 120
                 'target_tenant': data['target_tenant'],
124
-                'action': data['action'],
125
-                'object_type': data['object_type'],
121
+                'action': action,
122
+                'object_type': object_type,
126 123
             }
127
-            if data['object_type'] == 'network':
124
+            if object_type == 'network':
128 125
                 params['object_id'] = data['network_id']
129
-            elif data['object_type'] == 'qos_policy':
126
+            elif object_type == 'qos_policy':
130 127
                 params['object_id'] = data['qos_policy_id']
131 128
 
132 129
             rbac_policy = api.neutron.rbac_policy_create(request, **params)
@@ -147,6 +144,7 @@ class UpdatePolicyForm(forms.SelfHandlingForm):
147 144
     def __init__(self, request, *args, **kwargs):
148 145
         super(UpdatePolicyForm, self).__init__(request, *args, **kwargs)
149 146
         tenant_choices = [('', _("Select a project"))]
147
+        tenant_choices.append(("*", "*"))
150 148
         tenants, has_more = api.keystone.tenant_list(request)
151 149
         for tenant in tenants:
152 150
             tenant_choices.append((tenant.id, tenant.name))

+ 2
- 4
openstack_dashboard/dashboards/admin/rbac_policies/tests.py View File

@@ -63,8 +63,7 @@ class RBACPolicyTests(test.BaseAdminViewTests):
63 63
         self.mock_rbac_policy_create.return_value = rbac_policy
64 64
 
65 65
         form_data = {'target_tenant': rbac_policy.target_tenant,
66
-                     'action': 'access_as_external',
67
-                     'object_type': 'network',
66
+                     'action_object_type': 'external_network',
68 67
                      'network_id': network.id}
69 68
         url = reverse('horizon:admin:rbac_policies:create')
70 69
         res = self.client.post(url, form_data)
@@ -100,8 +99,7 @@ class RBACPolicyTests(test.BaseAdminViewTests):
100 99
         self.mock_rbac_policy_create.return_value = rbac_policy
101 100
 
102 101
         form_data = {'target_tenant': rbac_policy.target_tenant,
103
-                     'action': 'access_as_shared',
104
-                     'object_type': 'qos_policy',
102
+                     'action_object_type': 'shared_qos_policy',
105 103
                      'qos_policy_id': qos_policy.id}
106 104
         url = reverse('horizon:admin:rbac_policies:create')
107 105
         res = self.client.post(url, form_data)

Loading…
Cancel
Save