def test_add_firewall_group_post(self): firewall_group = self.firewall_groups_v2.first() policies = self.fw_policies_v2.list() tenant_id = self.tenant.id form_data = { 'name': firewall_group.name, 'description': firewall_group.description, 'ingress_firewall_policy_id': firewall_group.ingress_firewall_policy_id, 'egress_firewall_policy_id': firewall_group.egress_firewall_policy_id, 'admin_state_up': firewall_group.admin_state_up } api_fwaas_v2.policy_list_for_tenant(IsA(http.HttpRequest), tenant_id).AndReturn(policies) api_fwaas_v2.fwg_port_list_for_tenant(IsA(http.HttpRequest), tenant_id).AndReturn([]) api_fwaas_v2.firewall_group_create(IsA( http.HttpRequest), **form_data).AndReturn(firewall_group) self.mox.ReplayAll() res = self.client.post(reverse(self.ADDFIREWALLGROUP_PATH), form_data) self.assertNoFormErrors(res) self.assertRedirectsNoFollow(res, str(self.INDEX_URL))
def test_update_firewall_post(self): firewall_group = self.firewall_groups_v2.first() tenant_id = self.tenant.id api_fwaas_v2.firewall_get(IsA(http.HttpRequest), firewall_group.id).AndReturn(firewall_group) data = { 'name': 'new name', 'description': 'new desc', 'ingress_firewall_policy_id': firewall_group.ingress_firewall_policy_id, 'admin_state_up': False } policies = self.fw_policies_v2.list() api_fwaas_v2.policy_list_for_tenant(IsA(http.HttpRequest), tenant_id).AndReturn(policies) api_fwaas_v2.firewall_update( IsA(http.HttpRequest), firewall_group.id, **data)\ .AndReturn(firewall_group) self.mox.ReplayAll() res = self.client.post( reverse(self.UPDATEFIREWALLGROUP_PATH, args=(firewall_group.id, )), data) self.assertNoFormErrors(res) self.assertRedirectsNoFollow(res, str(self.INDEX_URL))
def set_up_expect_with_exception(self): tenant_id = self.tenant.id api_fwaas_v2.rule_list_for_tenant(IsA( http.HttpRequest), tenant_id).AndRaise(self.exceptions.neutron) api_fwaas_v2.policy_list_for_tenant(IsA( http.HttpRequest), tenant_id).AndRaise(self.exceptions.neutron) api_fwaas_v2.firewall_list_for_tenant(IsA( http.HttpRequest), tenant_id).AndRaise(self.exceptions.neutron)
def test_delete_policy(self): policy = self.fw_policies_v2.first() api_fwaas_v2.policy_list_for_tenant(IsA(http.HttpRequest), self.tenant.id).AndReturn( self.fw_policies_v2.list()) api_fwaas_v2.policy_delete(IsA(http.HttpRequest), policy.id) self.mox.ReplayAll() form_data = {"action": "policiestable__deletepolicy__%s" % policy.id} res = self.client.post(self.INDEX_URL, form_data) self.assertNoFormErrors(res)
def test_delete_firewall_group(self): fwl = self.firewall_groups_v2.first() api_fwaas_v2.firewall_list_for_tenant(IsA(http.HttpRequest), self.tenant.id).AndReturn([fwl]) api_fwaas_v2.policy_list_for_tenant(IsA(http.HttpRequest), self.tenant.id).AndReturn( self.fw_policies_v2.list()) api_fwaas_v2.firewall_delete(IsA(http.HttpRequest), fwl.id) self.mox.ReplayAll() form_data = { "action": "FirewallGroupsTable__deletefirewallgroup__%s" % fwl.id } res = self.client.post(self.INDEX_URL, form_data) self.assertNoFormErrors(res)
def test_update_firewall_group_get(self): firewall_group = self.firewall_groups_v2.first() policies = self.fw_policies_v2.list() tenant_id = self.tenant.id api_fwaas_v2.policy_list_for_tenant(IsA(http.HttpRequest), tenant_id).AndReturn(policies) api_fwaas_v2.firewall_get(IsA(http.HttpRequest), firewall_group.id).AndReturn(firewall_group) self.mox.ReplayAll() res = self.client.get( reverse(self.UPDATEFIREWALLGROUP_PATH, args=(firewall_group.id, ))) self.assertTemplateUsed(res, 'project/firewalls_v2/updatefirewall.html')
def set_up_expect(self): tenant_id = self.tenant.id # retrieves firewallgroups firewallgroups = self.firewall_groups_v2.list() api_fwaas_v2.firewall_list_for_tenant(IsA( http.HttpRequest), tenant_id).AndReturn(firewallgroups) # retrieves policies # TODO(amotoki): get_firewallgroupstable_data() also calls # policy_list_for_tenant(). This needs to be clean up. policies = self.fw_policies_v2.list() api_fwaas_v2.policy_list_for_tenant(IsA(http.HttpRequest), tenant_id).AndReturn(policies) api_fwaas_v2.policy_list_for_tenant(IsA(http.HttpRequest), tenant_id).AndReturn(policies) # retrieve rules api_fwaas_v2.rule_list_for_tenant(IsA( http.HttpRequest), tenant_id).AndReturn(self.fw_rules_v2.list())
def get_policiestable_data(self): try: tenant_id = self.request.user.tenant_id request = self.tab_group.request policies = api_fwaas_v2.policy_list_for_tenant(request, tenant_id) except Exception: policies = [] exceptions.handle(self.tab_group.request, _('Unable to retrieve policies list.')) return policies
def test_policy_list_for_tenant(self): tenant_id = self.request.user.project_id exp_policies = self.fw_policies_v2.list() policies_dict = {'firewall_policies': self.api_fw_policies_v2.list()} rules_dict = {'firewall_rules': self.api_fw_rules_v2.list()} neutronclient.list_fwaas_firewall_policies(tenant_id=tenant_id, shared=False).AndReturn({ 'firewall_policies': [] }) neutronclient.list_fwaas_firewall_policies( shared=True).AndReturn(policies_dict) neutronclient.list_fwaas_firewall_rules().AndReturn(rules_dict) self.mox.ReplayAll() ret_val = api_fwaas_v2.policy_list_for_tenant(self.request, tenant_id) for (v, d) in zip(ret_val, exp_policies): self._assert_policy_return_value(v, d)
def __init__(self, request, *args, **kwargs): super(AddFirewallGroupAction, self).__init__(request, *args, **kwargs) firewall_policy_id_choices = [('', _("Select a Policy"))] try: tenant_id = self.request.user.tenant_id policies = api_fwaas_v2.policy_list_for_tenant(request, tenant_id) policies = sorted(policies, key=attrgetter('name')) except Exception as e: exceptions.handle(request, _('Unable to retrieve policy list (%s).') % e) policies = [] for p in policies: firewall_policy_id_choices.append((p.id, p.name_or_id)) self.fields['ingress_firewall_policy_id'].choices = \ firewall_policy_id_choices self.fields['egress_firewall_policy_id'].choices = \ firewall_policy_id_choices
def __init__(self, request, *args, **kwargs): super(UpdateFirewall, self).__init__(request, *args, **kwargs) try: tenant_id = self.request.user.tenant_id policies = api_fwaas_v2.policy_list_for_tenant(request, tenant_id) policies = sorted(policies, key=attrgetter('name')) except Exception: exceptions.handle(request, _('Unable to retrieve policy list.')) policies = [] egress_policy_id_choices = [] ingress_policy_id_choices = [] ingress_policy_id = kwargs['initial']['ingress_firewall_policy_id'] if ingress_policy_id: ingress_policy_name = [ p.name for p in policies if p.id == ingress_policy_id ][0] ingress_policy_id_choices.append( (ingress_policy_id, ingress_policy_name)) egress_policy_id = kwargs['initial']['egress_firewall_policy_id'] if egress_policy_id: egress_policy_name = [ p.name for p in policies if p.id == egress_policy_id ][0] egress_policy_id_choices.append( (egress_policy_id, egress_policy_name)) ingress_policy_id_choices.append(('', _('None'))) egress_policy_id_choices.append(('', _('None'))) for p in policies: if p.id != ingress_policy_id: ingress_policy_id_choices.append((p.id, p.name_or_id)) if p.id != egress_policy_id: egress_policy_id_choices.append((p.id, p.name_or_id)) self.fields['ingress_firewall_policy_id'].choices = \ ingress_policy_id_choices self.fields['egress_firewall_policy_id'].choices = \ egress_policy_id_choices
def get_FirewallGroupsTable_data(self): try: tenant_id = self.request.user.tenant_id request = self.tab_group.request fw_groups = api_fwaas_v2.firewall_list_for_tenant( request, tenant_id) tenant_policies = api_fwaas_v2.policy_list_for_tenant( request, tenant_id) policy_dict = self.get_policy_dict(policies=tenant_policies) for fw_group in fw_groups: if fw_group['ingress_firewall_policy_id'] in policy_dict: fw_group.ingress_policy = \ policy_dict[fw_group['ingress_firewall_policy_id']] if fw_group['egress_firewall_policy_id'] in policy_dict: fw_group.egress_policy = \ policy_dict[fw_group['egress_firewall_policy_id']] except Exception: fw_groups = [] exceptions.handle(self.tab_group.request, _('Unable to retrieve firewall list.')) return fw_groups
def test_policy_list_for_tenant(self): tenant_id = self.request.user.project_id exp_policies = self.fw_policies_v2.list() policies_dict = {'firewall_policies': self.api_fw_policies_v2.list()} rules_dict = {'firewall_rules': self.api_fw_rules_v2.list()} self.mock_list_fwaas_firewall_policies.side_effect = [ { 'firewall_policies': [] }, policies_dict, ] self.mock_list_fwaas_firewall_rules.return_value = rules_dict ret_val = api_fwaas_v2.policy_list_for_tenant(self.request, tenant_id) for (v, d) in zip(ret_val, exp_policies): self._assert_policy_return_value(v, d) self.assertEqual(2, self.mock_list_fwaas_firewall_policies.call_count) self.mock_list_fwaas_firewall_policies.assert_has_calls([ mock.call(tenant_id=tenant_id, shared=False), mock.call(shared=True), ]) self.mock_list_fwaas_firewall_rules.assert_called_once_with()