OpenStack Dashboard (Horizon)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. from collections import OrderedDict
  13. from django.urls import reverse
  14. from django.urls import reverse_lazy
  15. from django.utils.translation import ugettext_lazy as _
  16. from horizon import exceptions
  17. from horizon import forms
  18. from horizon import messages
  19. from horizon import tables
  20. from horizon import tabs
  21. from horizon.utils import memoized
  22. from openstack_dashboard import api
  23. from openstack_dashboard.dashboards.admin.rbac_policies \
  24. import forms as rbac_policy_forms
  25. from openstack_dashboard.dashboards.admin.rbac_policies \
  26. import tables as rbac_policy_tables
  27. from openstack_dashboard.dashboards.admin.rbac_policies \
  28. import tabs as rbac_policy_tabs
  29. class IndexView(tables.DataTableView):
  30. table_class = rbac_policy_tables.RBACPoliciesTable
  31. page_title = _("RBAC Policies")
  32. @memoized.memoized_method
  33. def _get_tenants(self):
  34. try:
  35. tenants, has_more = api.keystone.tenant_list(self.request)
  36. except Exception:
  37. tenants = []
  38. msg = _("Unable to retrieve information about the "
  39. "policies' projects.")
  40. exceptions.handle(self.request, msg)
  41. tenant_dict = OrderedDict([(t.id, t.name) for t in tenants])
  42. return tenant_dict
  43. def _get_networks(self):
  44. try:
  45. networks = api.neutron.network_list(self.request)
  46. except Exception:
  47. networks = []
  48. msg = _("Unable to retrieve information about the "
  49. "policies' networks.")
  50. exceptions.handle(self.request, msg)
  51. return dict((n.id, n.name) for n in networks)
  52. def _get_qos_policies(self):
  53. qos_policies = []
  54. try:
  55. if api.neutron.is_extension_supported(self.request,
  56. extension_alias='qos'):
  57. qos_policies = api.neutron.policy_list(self.request)
  58. except Exception:
  59. msg = _("Unable to retrieve information about the "
  60. "policies' qos policies.")
  61. exceptions.handle(self.request, msg)
  62. return dict((q.id, q.name) for q in qos_policies)
  63. def get_data(self):
  64. try:
  65. rbac_policies = api.neutron.rbac_policy_list(self.request)
  66. except Exception:
  67. rbac_policies = []
  68. messages.error(self.request,
  69. _("Unable to retrieve RBAC policies."))
  70. if rbac_policies:
  71. tenant_dict = self._get_tenants()
  72. network_dict = self._get_networks()
  73. qos_policy_dict = self._get_qos_policies()
  74. for p in rbac_policies:
  75. # Set tenant name and object name
  76. p.tenant_name = tenant_dict.get(p.tenant_id, p.tenant_id)
  77. p.target_tenant_name = tenant_dict.get(p.target_tenant,
  78. p.target_tenant)
  79. if p.object_type == "network":
  80. p.object_name = network_dict.get(p.object_id, p.object_id)
  81. elif p.object_type == "qos_policy":
  82. p.object_name = qos_policy_dict.get(p.object_id,
  83. p.object_id)
  84. return rbac_policies
  85. class CreateView(forms.ModalFormView):
  86. template_name = 'admin/rbac_policies/create.html'
  87. form_id = "create_rbac_policy_form"
  88. form_class = rbac_policy_forms.CreatePolicyForm
  89. submit_label = _("Create RBAC Policy")
  90. submit_url = reverse_lazy("horizon:admin:rbac_policies:create")
  91. success_url = reverse_lazy("horizon:admin:rbac_policies:index")
  92. page_title = _("Create A RBAC Policy")
  93. class UpdateView(forms.ModalFormView):
  94. context_object_name = 'rbac_policies'
  95. template_name = 'admin/rbac_policies/update.html'
  96. form_class = rbac_policy_forms.UpdatePolicyForm
  97. form_id = "update_rbac_policy_form"
  98. submit_label = _("Save Changes")
  99. submit_url = 'horizon:admin:rbac_policies:update'
  100. success_url = reverse_lazy('horizon:admin:rbac_policies:index')
  101. page_title = _("Update RBAC Policy")
  102. def get_context_data(self, **kwargs):
  103. context = super(UpdateView, self).get_context_data(**kwargs)
  104. args = (self.kwargs['rbac_policy_id'],)
  105. context["rbac_policy_id"] = self.kwargs['rbac_policy_id']
  106. context["submit_url"] = reverse(self.submit_url, args=args)
  107. return context
  108. @memoized.memoized_method
  109. def _get_object(self, *args, **kwargs):
  110. rbac_policy_id = self.kwargs['rbac_policy_id']
  111. try:
  112. return api.neutron.rbac_policy_get(self.request, rbac_policy_id)
  113. except Exception:
  114. redirect = self.success_url
  115. msg = _('Unable to retrieve rbac policy details.')
  116. exceptions.handle(self.request, msg, redirect=redirect)
  117. def get_initial(self):
  118. rbac_policy = self._get_object()
  119. return {'rbac_policy_id': rbac_policy['id'],
  120. 'target_tenant': rbac_policy['target_tenant']}
  121. class DetailView(tabs.TabView):
  122. tab_group_class = rbac_policy_tabs.RBACDetailsTabs
  123. template_name = 'horizon/common/_detail.html'
  124. page_title = "{{ rbac_policy.id }}"