/openstack_dashboard/dashboards/project/firewalls/views.py

https://github.com/gabrielhurley/horizon · Python · 287 lines · 227 code · 47 blank · 13 comment · 30 complexity · f271291045237e24d44f5a0bce551084 MD5 · raw file

  1. # Copyright 2013, Big Switch Networks, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import re
  15. from django.core.urlresolvers import reverse_lazy
  16. from django.utils.translation import ugettext_lazy as _
  17. from horizon import exceptions
  18. from horizon import forms
  19. from horizon import messages
  20. from horizon import tabs
  21. from horizon.utils import memoized
  22. from horizon import workflows
  23. from openstack_dashboard import api
  24. from openstack_dashboard.dashboards.project.firewalls \
  25. import forms as fw_forms
  26. from openstack_dashboard.dashboards.project.firewalls \
  27. import tabs as fw_tabs
  28. from openstack_dashboard.dashboards.project.firewalls \
  29. import workflows as fw_workflows
  30. InsertRuleToPolicy = fw_forms.InsertRuleToPolicy
  31. RemoveRuleFromPolicy = fw_forms.RemoveRuleFromPolicy
  32. UpdateFirewall = fw_forms.UpdateFirewall
  33. UpdatePolicy = fw_forms.UpdatePolicy
  34. UpdateRule = fw_forms.UpdateRule
  35. FirewallDetailsTabs = fw_tabs.FirewallDetailsTabs
  36. FirewallTabs = fw_tabs.FirewallTabs
  37. PolicyDetailsTabs = fw_tabs.PolicyDetailsTabs
  38. RuleDetailsTabs = fw_tabs.RuleDetailsTabs
  39. AddFirewall = fw_workflows.AddFirewall
  40. AddPolicy = fw_workflows.AddPolicy
  41. AddRule = fw_workflows.AddRule
  42. class IndexView(tabs.TabView):
  43. tab_group_class = (FirewallTabs)
  44. template_name = 'project/firewalls/details_tabs.html'
  45. def post(self, request, *args, **kwargs):
  46. obj_ids = request.POST.getlist('object_ids')
  47. action = request.POST['action']
  48. obj_type = re.search('.delete([a-z]+)', action).group(1)
  49. if not obj_ids:
  50. obj_ids.append(re.search('([0-9a-z-]+)$', action).group(1))
  51. if obj_type == 'rule':
  52. for obj_id in obj_ids:
  53. try:
  54. api.fwaas.rule_delete(request, obj_id)
  55. messages.success(request, _('Deleted rule %s') % obj_id)
  56. except Exception as e:
  57. exceptions.handle(request,
  58. _('Unable to delete rule. %s') % e)
  59. if obj_type == 'policy':
  60. for obj_id in obj_ids:
  61. try:
  62. api.fwaas.policy_delete(request, obj_id)
  63. messages.success(request, _('Deleted policy %s') % obj_id)
  64. except Exception as e:
  65. exceptions.handle(request,
  66. _('Unable to delete policy. %s') % e)
  67. if obj_type == 'firewall':
  68. for obj_id in obj_ids:
  69. try:
  70. api.fwaas.firewall_delete(request, obj_id)
  71. messages.success(request,
  72. _('Deleted firewall %s') % obj_id)
  73. except Exception as e:
  74. exceptions.handle(request,
  75. _('Unable to delete firewall. %s') % e)
  76. return self.get(request, *args, **kwargs)
  77. class AddRuleView(workflows.WorkflowView):
  78. workflow_class = AddRule
  79. template_name = "project/firewalls/addrule.html"
  80. class AddPolicyView(workflows.WorkflowView):
  81. workflow_class = AddPolicy
  82. template_name = "project/firewalls/addpolicy.html"
  83. class AddFirewallView(workflows.WorkflowView):
  84. workflow_class = AddFirewall
  85. template_name = "project/firewalls/addfirewall.html"
  86. class RuleDetailsView(tabs.TabView):
  87. tab_group_class = (RuleDetailsTabs)
  88. template_name = 'project/firewalls/details_tabs.html'
  89. class PolicyDetailsView(tabs.TabView):
  90. tab_group_class = (PolicyDetailsTabs)
  91. template_name = 'project/firewalls/details_tabs.html'
  92. class FirewallDetailsView(tabs.TabView):
  93. tab_group_class = (FirewallDetailsTabs)
  94. template_name = 'project/firewalls/details_tabs.html'
  95. class UpdateRuleView(forms.ModalFormView):
  96. form_class = UpdateRule
  97. template_name = "project/firewalls/updaterule.html"
  98. context_object_name = 'rule'
  99. success_url = reverse_lazy("horizon:project:firewalls:index")
  100. page_title = _("Edit Rule")
  101. def get_context_data(self, **kwargs):
  102. context = super(UpdateRuleView, self).get_context_data(**kwargs)
  103. context['rule_id'] = self.kwargs['rule_id']
  104. obj = self._get_object()
  105. if obj:
  106. context['name'] = obj.name_or_id
  107. context['page_title'] = _("Edit Rule "
  108. "%(rule_name)s") % {'rule_name':
  109. obj.name}
  110. return context
  111. @memoized.memoized_method
  112. def _get_object(self, *args, **kwargs):
  113. rule_id = self.kwargs['rule_id']
  114. try:
  115. rule = api.fwaas.rule_get(self.request, rule_id)
  116. return rule
  117. except Exception:
  118. redirect = self.success_url
  119. msg = _('Unable to retrieve rule details.')
  120. exceptions.handle(self.request, msg, redirect=redirect)
  121. def get_initial(self):
  122. rule = self._get_object()
  123. initial = rule.get_dict()
  124. protocol = initial['protocol']
  125. initial['protocol'] = protocol.upper() if protocol else 'ANY'
  126. initial['action'] = initial['action'].upper()
  127. return initial
  128. class UpdatePolicyView(forms.ModalFormView):
  129. form_class = UpdatePolicy
  130. template_name = "project/firewalls/updatepolicy.html"
  131. context_object_name = 'policy'
  132. success_url = reverse_lazy("horizon:project:firewalls:index")
  133. page_title = _("Edit Policy")
  134. def get_context_data(self, **kwargs):
  135. context = super(UpdatePolicyView, self).get_context_data(**kwargs)
  136. context["policy_id"] = self.kwargs['policy_id']
  137. obj = self._get_object()
  138. if obj:
  139. context['name'] = obj.name_or_id
  140. context['page_title'] = _("Edit Policy %s") % obj.name
  141. return context
  142. @memoized.memoized_method
  143. def _get_object(self, *args, **kwargs):
  144. policy_id = self.kwargs['policy_id']
  145. try:
  146. policy = api.fwaas.policy_get(self.request, policy_id)
  147. return policy
  148. except Exception:
  149. redirect = self.success_url
  150. msg = _('Unable to retrieve policy details.')
  151. exceptions.handle(self.request, msg, redirect=redirect)
  152. def get_initial(self):
  153. policy = self._get_object()
  154. initial = policy.get_dict()
  155. return initial
  156. class UpdateFirewallView(forms.ModalFormView):
  157. form_class = UpdateFirewall
  158. template_name = "project/firewalls/updatefirewall.html"
  159. context_object_name = 'firewall'
  160. success_url = reverse_lazy("horizon:project:firewalls:index")
  161. page_title = _("Edit Firewall")
  162. def get_context_data(self, **kwargs):
  163. context = super(UpdateFirewallView, self).get_context_data(**kwargs)
  164. context["firewall_id"] = self.kwargs['firewall_id']
  165. obj = self._get_object()
  166. if obj:
  167. context['name'] = obj.name
  168. context['page_title'] = _("Edit Firewall %s") % obj.name
  169. return context
  170. @memoized.memoized_method
  171. def _get_object(self, *args, **kwargs):
  172. firewall_id = self.kwargs['firewall_id']
  173. try:
  174. firewall = api.fwaas.firewall_get(self.request,
  175. firewall_id)
  176. return firewall
  177. except Exception:
  178. redirect = self.success_url
  179. msg = _('Unable to retrieve firewall details.')
  180. exceptions.handle(self.request, msg, redirect=redirect)
  181. def get_initial(self):
  182. firewall = self._get_object()
  183. initial = firewall.get_dict()
  184. return initial
  185. class InsertRuleToPolicyView(forms.ModalFormView):
  186. form_class = InsertRuleToPolicy
  187. template_name = "project/firewalls/insert_rule_to_policy.html"
  188. context_object_name = 'policy'
  189. success_url = reverse_lazy("horizon:project:firewalls:index")
  190. def get_context_data(self, **kwargs):
  191. context = super(InsertRuleToPolicyView,
  192. self).get_context_data(**kwargs)
  193. context["policy_id"] = self.kwargs['policy_id']
  194. obj = self._get_object()
  195. if obj:
  196. context['name'] = obj.name_or_id
  197. return context
  198. @memoized.memoized_method
  199. def _get_object(self, *args, **kwargs):
  200. policy_id = self.kwargs['policy_id']
  201. try:
  202. policy = api.fwaas.policy_get(self.request, policy_id)
  203. return policy
  204. except Exception:
  205. redirect = self.success_url
  206. msg = _('Unable to retrieve policy details.')
  207. exceptions.handle(self.request, msg, redirect=redirect)
  208. def get_initial(self):
  209. policy = self._get_object()
  210. initial = policy.get_dict()
  211. initial['policy_id'] = initial['id']
  212. return initial
  213. class RemoveRuleFromPolicyView(forms.ModalFormView):
  214. form_class = RemoveRuleFromPolicy
  215. template_name = "project/firewalls/remove_rule_from_policy.html"
  216. context_object_name = 'policy'
  217. success_url = reverse_lazy("horizon:project:firewalls:index")
  218. def get_context_data(self, **kwargs):
  219. context = super(RemoveRuleFromPolicyView,
  220. self).get_context_data(**kwargs)
  221. context["policy_id"] = self.kwargs['policy_id']
  222. obj = self._get_object()
  223. if obj:
  224. context['name'] = obj.name_or_id
  225. return context
  226. @memoized.memoized_method
  227. def _get_object(self, *args, **kwargs):
  228. policy_id = self.kwargs['policy_id']
  229. try:
  230. policy = api.fwaas.policy_get(self.request, policy_id)
  231. return policy
  232. except Exception:
  233. redirect = self.success_url
  234. msg = _('Unable to retrieve policy details.')
  235. exceptions.handle(self.request, msg, redirect=redirect)
  236. def get_initial(self):
  237. policy = self._get_object()
  238. initial = policy.get_dict()
  239. initial['policy_id'] = initial['id']
  240. return initial