Codebase list neutron-fwaas-dashboard / 0b47a5e
Cleanup 'firewall' usage in v2 dashboard (part 1) This commit cleans up usage of 'firewall' (in favor of 'firewall group') in the API wrapper layer and messages. Change-Id: Icf496933ebfd00eac15e24c9c15249c4d18d86de Partial-Bug: #1723952 Akihiro Motoki 5 years ago
9 changed file(s) with 116 addition(s) and 110 deletion(s). Raw diff Collapse all Expand all
222222
223223 @profiler.trace
224224 def firewall_group_create(request, **kwargs):
225 """Create a firewall for specified policy
225 """Create a firewall group for specified policy
226226
227227 :param request: request context
228 :param name: name for firewall
229 :param description: description for firewall
230 :param firewall_policy_id: policy id used by firewall
228 :param name: name for firewall group
229 :param description: description for firewall group
230 :param firewall_policy_id: policy id used by firewall group
231231 :param shared: boolean (default false)
232232 :param admin_state_up: boolean (default true)
233 :return: Firewall object
233 :return: Firewall group object
234234 """
235235 body = {'firewall_group': kwargs}
236236 firewall_group = neutronclient(request).create_fwaas_firewall_group(body)
238238
239239
240240 @profiler.trace
241 def firewall_list(request, **kwargs):
242 return _firewall_list(request, **kwargs)
243
244
245 @profiler.trace
246 def firewall_list_for_tenant(request, tenant_id, **kwargs):
247 """Return a firewall list available for the tenant.
248
249 The list contains firewalls owned by the tenant and shared firewalls.
250 This is required because Neutron returns all resources including
241 def firewall_group_list(request, **kwargs):
242 return _firewall_group_list(request, **kwargs)
243
244
245 @profiler.trace
246 def firewall_group_list_for_tenant(request, tenant_id, **kwargs):
247 """Return a firewall group list available for the tenant.
248
249 The list contains firewall groups owned by the tenant and shared firewall
250 groups. This is required because Neutron returns all resources including
251251 all tenants if a user has admin role.
252252 """
253 fwg = firewall_list(request, tenant_id=tenant_id,
254 shared=False, **kwargs)
255 shared_fwg = firewall_list(request, shared=True, **kwargs)
253 fwg = firewall_group_list(request, tenant_id=tenant_id,
254 shared=False, **kwargs)
255 shared_fwg = firewall_group_list(request, shared=True, **kwargs)
256256 return fwg + shared_fwg
257257
258258
259 # TODO(SarathMekala): Support expand_policy for _firewall_list
260 def _firewall_list(request, **kwargs):
259 # TODO(SarathMekala): Support expand_policy for _firewall_group_list
260 def _firewall_group_list(request, **kwargs):
261261 firewall_groups = neutronclient(request).list_fwaas_firewall_groups(
262262 **kwargs).get('firewall_groups')
263263 return [FirewallGroup(f) for f in firewall_groups]
264264
265265
266266 @profiler.trace
267 def firewall_get(request, firewall_id):
268 return _firewall_get(request, firewall_id, expand_policy=True)
269
270
271 def _firewall_get(request, firewallgroup_id, expand_policy):
267 def firewall_group_get(request, firewallgroup_id):
268 return _firewall_group_get(request, firewallgroup_id, expand_policy=True)
269
270
271 def _firewall_group_get(request, firewallgroup_id, expand_policy):
272272 firewall_group = neutronclient(request).show_fwaas_firewall_group(
273273 firewallgroup_id).get('firewall_group')
274274 if expand_policy:
289289
290290
291291 @profiler.trace
292 def firewall_delete(request, firewallgroup_id):
292 def firewall_group_delete(request, firewallgroup_id):
293293 neutronclient(request).delete_fwaas_firewall_group(firewallgroup_id)
294294
295295
296296 @profiler.trace
297 def firewall_update(request, firewallgroup_id, **kwargs):
297 def firewall_group_update(request, firewallgroup_id, **kwargs):
298298 body = {'firewall_group': kwargs}
299299 firewall_group = neutronclient(request).update_fwaas_firewall_group(
300300 firewallgroup_id, body).get('firewall_group')
192192 name_or_id = context.get('name') or firewallgroup_id
193193 body = self._convert_req_body(_get_request_body(context, self.initial))
194194 try:
195 firewall = api_fwaas_v2.firewall_update(request, firewallgroup_id,
196 **body)
197 msg = _('Firewall %s was successfully updated.') % name_or_id
198 messages.success(request, msg)
199 return firewall
200 except Exception as e:
201 msg = (_('Failed to update firewall %(name)s: %(reason)s') %
195 fwg = api_fwaas_v2.firewall_group_update(request,
196 firewallgroup_id,
197 **body)
198 msg = _('Firewall group %s was successfully updated.') % name_or_id
199 messages.success(request, msg)
200 return fwg
201 except Exception as e:
202 msg = (_('Failed to update firewall group %(name)s: %(reason)s') %
202203 {'name': name_or_id, 'reason': e})
203204 redirect = reverse(self.failure_url)
204205 exceptions.handle(request, msg, redirect=redirect)
236237 ports.append(add_port)
237238 body['ports'] = ports
238239 try:
239 firewallgroup = api_fwaas_v2.firewall_update(
240 firewallgroup = api_fwaas_v2.firewall_group_update(
240241 request, firewallgroup_id, **body)
241 msg = _('FirewallGroup %s was successfully updated.') % name_or_id
242 msg = (_('Added the port(s) to the firewall group %s '
243 'successfully.') % name_or_id)
242244 messages.success(request, msg)
243245 return firewallgroup
244246 except Exception as e:
245 msg = (_('Failed to update firewallgroup %(name)s: %(reason)s') %
247 msg = (_('Failed to add the port(s) to the firewall group '
248 '%(name)s: %(reason)s') %
246249 {'name': name_or_id, 'reason': e})
247250 redirect = reverse(self.failure_url)
248251 exceptions.handle(request, msg, redirect=redirect)
275278 ports.remove(remove_port)
276279 body['ports'] = ports
277280 try:
278 firewallgroup = api_fwaas_v2.firewall_update(
281 firewallgroup = api_fwaas_v2.firewall_group_update(
279282 request, firewallgroup_id, **body)
280 msg = _('FirewallGroup %s was successfully updated.') % name_or_id
283 msg = _('Removed the port(s) from the firewall group %s '
284 'successfully.') % name_or_id
281285 messages.success(request, msg)
282286 return firewallgroup
283287 except Exception as e:
284 msg = (_('Failed to update firewallgroup %(name)s: %(reason)s') %
288 msg = (_('Failed to remove the port(s) from the firewall group '
289 '%(name)s: %(reason)s') %
285290 {'name': name_or_id, 'reason': e})
286291 redirect = reverse(self.failure_url)
287292 exceptions.handle(request, msg, redirect=redirect)
3535 except Exception:
3636 LOG.error("Call to list enabled services failed. This is likely "
3737 "due to a problem communicating with the Neutron "
38 "endpoint. Firewalls panel will not be displayed.")
38 "endpoint. Firewall Groups panel will not be displayed.")
3939 return False
4040 if not super(Firewall_V2, self).allowed(context):
4141 return False
140140
141141 def delete(self, request, obj_id):
142142 try:
143 api_fwaas_v2.firewall_delete(request, obj_id)
143 api_fwaas_v2.firewall_group_delete(request, obj_id)
144144 except Exception as e:
145 exceptions.handle(request, _('Unable to delete firewall. %s') % e)
145 exceptions.handle(request,
146 _('Unable to delete firewall group. %s') % e)
146147
147148
148149 class UpdateRuleLink(policy.PolicyTargetMixin, tables.LinkAction):
7676 try:
7777 tenant_id = self.request.user.tenant_id
7878 request = self.tab_group.request
79 fw_groups = api_fwaas_v2.firewall_list_for_tenant(request,
80 tenant_id)
79 fw_groups = api_fwaas_v2.firewall_group_list_for_tenant(request,
80 tenant_id)
8181 tenant_policies = api_fwaas_v2.policy_list_for_tenant(
8282 request, tenant_id)
8383 policy_dict = self.get_policy_dict(policies=tenant_policies)
9191 except Exception:
9292 fw_groups = []
9393 exceptions.handle(self.tab_group.request,
94 _('Unable to retrieve firewall list.'))
94 _('Unable to retrieve firewall group list.'))
9595
9696 return fw_groups
9797
4545
4646 def setup_mocks(self):
4747 firewallgroups = self.firewall_groups_v2.list()
48 self.mock_firewall_list_for_tenant.return_value = firewallgroups
48 self.mock_firewall_group_list_for_tenant.return_value = firewallgroups
4949 policies = self.fw_policies_v2.list()
5050 self.mock_policy_list_for_tenant.return_value = policies
5151 self.mock_rule_list_for_tenant.return_value = self.fw_rules_v2.list()
5353 def check_mocks(self):
5454 tenant_id = self.tenant.id
5555
56 self.mock_firewall_list_for_tenant.assert_called_once_with(
56 self.mock_firewall_group_list_for_tenant.assert_called_once_with(
5757 helpers.IsHttpRequest(), tenant_id)
5858 # TODO(amotoki): get_firewallgroupstable_data() also calls
5959 # policy_list_for_tenant(). This needs to be clean up.
6666 def setup_mocks_with_exception(self):
6767 self.mock_rule_list_for_tenant.side_effect = self.exceptions.neutron
6868 self.mock_policy_list_for_tenant.side_effect = self.exceptions.neutron
69 self.mock_firewall_list_for_tenant.side_effect = \
69 self.mock_firewall_group_list_for_tenant.side_effect = \
7070 self.exceptions.neutron
7171
7272 def check_mocks_with_exception(self):
7575 helpers.IsHttpRequest(), tenant_id)
7676 self.mock_policy_list_for_tenant.assert_called_once_with(
7777 helpers.IsHttpRequest(), tenant_id)
78 self.mock_firewall_list_for_tenant.assert_called_once_with(
79 helpers.IsHttpRequest(), tenant_id)
80
81 @helpers.create_mocks({api_fwaas_v2: ('firewall_list_for_tenant',
78 self.mock_firewall_group_list_for_tenant.assert_called_once_with(
79 helpers.IsHttpRequest(), tenant_id)
80
81 @helpers.create_mocks({api_fwaas_v2: ('firewall_group_list_for_tenant',
8282 'policy_list_for_tenant',
8383 'rule_list_for_tenant',)})
8484 def test_index_firewallgroups(self):
9494 len(self.firewall_groups_v2.list()))
9595 self.check_mocks()
9696
97 @helpers.create_mocks({api_fwaas_v2: ('firewall_list_for_tenant',
97 @helpers.create_mocks({api_fwaas_v2: ('firewall_group_list_for_tenant',
9898 'policy_list_for_tenant',
9999 'rule_list_for_tenant',)})
100100 def test_index_policies(self):
111111 len(self.fw_policies_v2.list()))
112112 self.check_mocks()
113113
114 @helpers.create_mocks({api_fwaas_v2: ('firewall_list_for_tenant',
114 @helpers.create_mocks({api_fwaas_v2: ('firewall_group_list_for_tenant',
115115 'policy_list_for_tenant',
116116 'rule_list_for_tenant',)})
117117 def test_index_rules(self):
128128 len(self.fw_rules_v2.list()))
129129 self.check_mocks()
130130
131 @helpers.create_mocks({api_fwaas_v2: ('firewall_list_for_tenant',
131 @helpers.create_mocks({api_fwaas_v2: ('firewall_group_list_for_tenant',
132132 'policy_list_for_tenant',
133133 'rule_list_for_tenant')})
134134 def test_index_exception_firewallgroups(self):
144144
145145 self.check_mocks_with_exception()
146146
147 @helpers.create_mocks({api_fwaas_v2: ('firewall_list_for_tenant',
147 @helpers.create_mocks({api_fwaas_v2: ('firewall_group_list_for_tenant',
148148 'policy_list_for_tenant',
149149 'rule_list_for_tenant')})
150150 def test_index_exception_policies(self):
162162
163163 self.check_mocks_with_exception()
164164
165 @helpers.create_mocks({api_fwaas_v2: ('firewall_list_for_tenant',
165 @helpers.create_mocks({api_fwaas_v2: ('firewall_group_list_for_tenant',
166166 'policy_list_for_tenant',
167167 'rule_list_for_tenant')})
168168 def test_index_exception_rules(self):
587587 self.mock_policy_update.assert_called_once_with(
588588 helpers.IsHttpRequest(), policy.id, **expected_put_data)
589589
590 @helpers.create_mocks({api_fwaas_v2: ('firewall_get',
590 @helpers.create_mocks({api_fwaas_v2: ('firewall_group_get',
591591 'policy_list_for_tenant')})
592592 def test_update_firewall_group_get(self):
593593 firewall_group = self.firewall_groups_v2.first()
595595 tenant_id = self.tenant.id
596596
597597 self.mock_policy_list_for_tenant.return_value = policies
598 self.mock_firewall_get.return_value = firewall_group
598 self.mock_firewall_group_get.return_value = firewall_group
599599
600600 res = self.client.get(
601601 reverse(self.UPDATEFIREWALLGROUP_PATH, args=(firewall_group.id,)))
605605
606606 self.mock_policy_list_for_tenant.assert_called_once_with(
607607 helpers.IsHttpRequest(), tenant_id)
608 self.mock_firewall_get.assert_called_once_with(
608 self.mock_firewall_group_get.assert_called_once_with(
609609 helpers.IsHttpRequest(), firewall_group.id)
610610
611 @helpers.create_mocks({api_fwaas_v2: ('firewall_get',
612 'policy_list_for_tenant',
613 'firewall_update')})
611 @helpers.create_mocks({api_fwaas_v2: ('firewall_group_get',
612 'policy_list_for_tenant',
613 'firewall_group_update')})
614614 def test_update_firewall_post(self):
615615 fwg = self.firewall_groups_v2.first()
616616 tenant_id = self.tenant.id
632632 'admin_state_up': False,
633633 }
634634
635 self.mock_firewall_get.return_value = fwg
635 self.mock_firewall_group_get.return_value = fwg
636636 self.mock_policy_list_for_tenant.return_value = policies
637 self.mock_firewall_update.return_value = fwg
637 self.mock_firewall_group_update.return_value = fwg
638638
639639 res = self.client.post(
640640 reverse(
647647 self.assertNoFormErrors(res)
648648 self.assertRedirectsNoFollow(res, str(self.INDEX_URL))
649649
650 self.mock_firewall_get.assert_called_once_with(
650 self.mock_firewall_group_get.assert_called_once_with(
651651 helpers.IsHttpRequest(), fwg.id)
652652 self.mock_policy_list_for_tenant.assert_called_once_with(
653653 helpers.IsHttpRequest(), tenant_id)
654 self.mock_firewall_update.assert_called_once_with(
654 self.mock_firewall_group_update.assert_called_once_with(
655655 helpers.IsHttpRequest(), fwg.id, **expected_put_data)
656656
657657 @helpers.create_mocks({api_fwaas_v2: ('policy_get', 'policy_insert_rule',
770770 self.mock_policy_delete.assert_called_once_with(
771771 helpers.IsHttpRequest(), policy.id)
772772
773 @helpers.create_mocks({api_fwaas_v2: ('firewall_list_for_tenant',
774 'policy_list_for_tenant',
775 'firewall_delete',)})
773 @helpers.create_mocks({api_fwaas_v2: ('firewall_group_list_for_tenant',
774 'policy_list_for_tenant',
775 'firewall_group_delete',)})
776776 def test_delete_firewall_group(self):
777777 fwl = self.firewall_groups_v2.first()
778778
779 self.mock_firewall_list_for_tenant.return_value = [fwl]
779 self.mock_firewall_group_list_for_tenant.return_value = [fwl]
780780 self.mock_policy_list_for_tenant.return_value = \
781781 self.fw_policies_v2.list()
782 self.mock_firewall_delete.return_value = None
782 self.mock_firewall_group_delete.return_value = None
783783
784784 form_data = {
785785 "action": "FirewallGroupsTable__deletefirewallgroup__%s" %
788788
789789 self.assertNoFormErrors(res)
790790
791 self.mock_firewall_list_for_tenant.assert_called_once_with(
791 self.mock_firewall_group_list_for_tenant.assert_called_once_with(
792792 helpers.IsHttpRequest(), self.tenant.id)
793793 self.mock_policy_list_for_tenant.assert_called_once_with(
794794 helpers.IsHttpRequest(), self.tenant.id)
795 self.mock_firewall_delete.assert_called_once_with(
795 self.mock_firewall_group_delete.assert_called_once_with(
796796 helpers.IsHttpRequest(), fwl.id)
161161 def get_data(self):
162162 try:
163163 firewallgroup_id = self.kwargs['firewallgroup_id']
164 firewall_group = api_fwaas_v2.firewall_get(self.request,
165 firewallgroup_id)
164 firewall_group = api_fwaas_v2.firewall_group_get(self.request,
165 firewallgroup_id)
166166 except Exception:
167167 exceptions.handle(self.request,
168 _('Unable to retrieve firewall details.'),
168 _('Unable to retrieve firewall group details.'),
169169 redirect=self.failure_url)
170170 return firewall_group
171171
274274
275275 @memoized.memoized_method
276276 def _get_object(self, *args, **kwargs):
277 firewall_id = self.kwargs['firewall_id']
278 try:
279 firewall = api_fwaas_v2.firewall_get(self.request,
280 firewall_id)
281 return firewall
282 except Exception:
283 redirect = self.success_url
284 msg = _('Unable to retrieve firewall details.')
285 exceptions.handle(self.request, msg, redirect=redirect)
286
287 def get_initial(self):
288 firewall = self._get_object()
289 initial = firewall.to_dict()
277 fwg_id = self.kwargs['firewall_id']
278 try:
279 fwg = api_fwaas_v2.firewall_group_get(self.request, fwg_id)
280 return fwg
281 except Exception:
282 redirect = self.success_url
283 msg = _('Unable to retrieve firewall group details.')
284 exceptions.handle(self.request, msg, redirect=redirect)
285
286 def get_initial(self):
287 fwg = self._get_object()
288 initial = fwg.to_dict()
290289 return initial
291290
292291
298297 submit_label = _("Save Changes")
299298 submit_url = "horizon:project:firewalls_v2:addport"
300299 success_url = reverse_lazy("horizon:project:firewalls_v2:index")
301 page_title = _("Add port to FirewallGroup {{ name }}")
300 page_title = _("Add port to Firewall Group {{ name }}")
302301
303302 def get_context_data(self, **kwargs):
304303 context = super(AddPortView, self).get_context_data(**kwargs)
314313 def _get_object(self, *args, **kwargs):
315314 firewallgroup_id = self.kwargs['firewallgroup_id']
316315 try:
317 firewallgroup = api_fwaas_v2.firewall_get(self.request,
318 firewallgroup_id)
316 firewallgroup = api_fwaas_v2.firewall_group_get(self.request,
317 firewallgroup_id)
319318 return firewallgroup
320319 except Exception:
321320 redirect = self.success_url
352351 def _get_object(self, *args, **kwargs):
353352 firewallgroup_id = self.kwargs['firewallgroup_id']
354353 try:
355 firewallgroup = api_fwaas_v2.firewall_get(self.request,
356 firewallgroup_id)
354 firewallgroup = api_fwaas_v2.firewall_group_get(self.request,
355 firewallgroup_id)
357356 return firewallgroup
358357 except Exception:
359358 redirect = self.success_url
360 msg = _('Unable to retrieve firewallgroup details.')
359 msg = _('Unable to retrieve firewall group details.')
361360 exceptions.handle(self.request, msg, redirect=redirect)
362361
363362 def get_initial(self):
347347 name = _("FirewallGroup")
348348 permissions = ('openstack.services.network',)
349349 help_text = _("Create a firewall group based on a policy.\n\n"
350 "A firewall represents a logical firewall resource that "
351 "a tenant can instantiate and manage. A firewall must "
352 "be associated with one policy, all other fields are "
353 "optional.")
350 "A firewall group represents a logical firewall "
351 "resource that a tenant can instantiate and manage. "
352 "A firewall group must be associated with one policy, "
353 "all other fields are optional.")
354354
355355
356356 class AddFirewallGroupStep(workflows.Step):
381381
382382 @helpers.create_mocks({neutronclient: ('list_fwaas_firewall_groups',
383383 'list_fwaas_firewall_policies')})
384 def test_firewall_list(self):
384 def test_firewall_group_list(self):
385385 exp_firewalls = self.firewall_groups_v2.list()
386386 firewalls_dict = {
387387 'firewall_groups': self.api_firewall_groups_v2.list()}
388388
389389 self.mock_list_fwaas_firewall_groups.return_value = firewalls_dict
390390
391 ret_val = api_fwaas_v2.firewall_list(self.request)
391 ret_val = api_fwaas_v2.firewall_group_list(self.request)
392392 for (v, d) in zip(ret_val, exp_firewalls):
393393 self._assert_firewall_return_value(v, d, expand_policy=False)
394394 self.mock_list_fwaas_firewall_groups.assert_called_once_with()
395395
396396 @helpers.create_mocks({neutronclient: ('list_fwaas_firewall_groups',
397397 'list_fwaas_firewall_policies')})
398 def test_firewall_list_for_tenant(self):
398 def test_firewall_group_list_for_tenant(self):
399399 tenant_id = self.request.user.project_id
400400 exp_firewalls = self.firewall_groups_v2.list()
401401 firewalls_dict = {
406406 firewalls_dict,
407407 ]
408408
409 ret_val = api_fwaas_v2.firewall_list_for_tenant(
409 ret_val = api_fwaas_v2.firewall_group_list_for_tenant(
410410 self.request, tenant_id)
411411 for (v, d) in zip(ret_val, exp_firewalls):
412412 self._assert_firewall_return_value(v, d, expand_policy=False)
547547
548548 @helpers.create_mocks({neutronclient: ('show_fwaas_firewall_group',
549549 'show_fwaas_firewall_policy')})
550 def test_firewall_get(self):
550 def test_firewall_group_get(self):
551551 exp_firewall = self.firewall_groups_v2.first()
552552 ret_dict = {'firewall_group': self.api_firewall_groups_v2.first()}
553553
565565 {'firewall_policy': egress_policy}
566566 ]
567567
568 ret_val = api_fwaas_v2.firewall_get(self.request, exp_firewall.id)
568 ret_val = api_fwaas_v2.firewall_group_get(self.request,
569 exp_firewall.id)
569570 self._assert_firewall_return_value(ret_val, exp_firewall)
570571
571572 self.mock_show_fwaas_firewall_group.assert_called_once_with(
577578 ])
578579
579580 @helpers.create_mocks({neutronclient: ('update_fwaas_firewall_group',)})
580 def test_firewall_update(self):
581 def test_firewall_group_update(self):
581582 firewall = self.firewall_groups_v2.first()
582583 firewall_dict = self.api_firewall_groups_v2.first()
583584
599600
600601 self.mock_update_fwaas_firewall_group.return_value = ret_dict
601602
602 ret_val = api_fwaas_v2.firewall_update(self.request,
603 firewall.id, **form_data)
603 ret_val = api_fwaas_v2.firewall_group_update(self.request,
604 firewall.id, **form_data)
604605 self.assertIsInstance(ret_val, api_fwaas_v2.FirewallGroup)
605606 self.assertEqual(firewall.name, ret_val.name)
606607 self.assertTrue(ret_val.id)