示例#1
0
    def test_list_with_project(self):
        project_id = '0af70a4d22cf4652824ddc1f2435dd85'
        security_groups_list = {'security_groups': []}

        self.mocked_client.list_security_groups.return_value = (
            security_groups_list)
        sg_api.list(self.context, project=project_id)

        self.mocked_client.list_security_groups.assert_called_once_with(
            tenant_id=project_id)
示例#2
0
    def test_list_without_all_tenants_and_admin_context(self):
        project_id = '0af70a4d22cf4652824ddc1f2435dd85'
        security_groups_list = {'security_groups': []}
        admin_context = context.RequestContext('user1', project_id, True)

        with mock.patch.object(
                self.mocked_client,
                'list_security_groups',
                return_value=security_groups_list) as mock_list_secgroup:
            sg_api.list(admin_context, project=project_id)

            mock_list_secgroup.assert_called_once_with(tenant_id=project_id)
示例#3
0
    def test_list_with_all_tenants_not_admin(self):
        search_opts = {'all_tenants': 1}
        security_groups_list = {'security_groups': []}

        with mock.patch.object(
                self.mocked_client,
                'list_security_groups',
                return_value=security_groups_list) as mock_list_secgroup:
            sg_api.list(self.context, project=self.context.project_id,
                        search_opts=search_opts)

            mock_list_secgroup.assert_called_once_with(
                tenant_id=self.context.project_id)
示例#4
0
    def test_list_security_group_with_no_port_range_and_not_tcp_udp_icmp(self):
        project_id = '0af70a4d22cf4652824ddc1f2435dd85'
        sg1 = {'description': 'default',
               'id': '07f1362f-34f6-4136-819a-2dcde112269e',
               'name': 'default',
               'tenant_id': 'c166d9316f814891bcb66b96c4c891d6',
               'security_group_rules':
                   [{'direction': 'ingress',
                     'ethertype': 'IPv4',
                     'id': '0a4647f1-e1aa-488d-90e1-97a7d0293beb',
                      'port_range_max': None,
                      'port_range_min': None,
                      'protocol': '51',
                      'remote_group_id': None,
                      'remote_ip_prefix': None,
                      'security_group_id':
                           '07f1362f-34f6-4136-819a-2dcde112269e',
                      'tenant_id': 'c166d9316f814891bcb66b96c4c891d6'}]}

        self.mocked_client.list_security_groups.return_value = (
            {'security_groups': [sg1]})
        result = sg_api.list(self.context, project=project_id)
        expected = [{'rules':
            [{'from_port': -1, 'protocol': '51', 'to_port': -1,
              'parent_group_id': '07f1362f-34f6-4136-819a-2dcde112269e',
              'cidr': '0.0.0.0/0', 'group_id': None,
              'id': '0a4647f1-e1aa-488d-90e1-97a7d0293beb'}],
            'project_id': 'c166d9316f814891bcb66b96c4c891d6',
            'id': '07f1362f-34f6-4136-819a-2dcde112269e',
            'name': 'default', 'description': 'default'}]
        self.assertEqual(expected, result)
        self.mocked_client.list_security_groups.assert_called_once_with(
            tenant_id=project_id)
示例#5
0
    def test_list_with_all_tenants_sec_name_and_admin_context(self):
        project_id = '0af70a4d22cf4652824ddc1f2435dd85'
        search_opts = {'all_tenants': 1}
        security_group_names = ['secgroup_ssh']
        security_groups_list = {'security_groups': []}
        admin_context = context.RequestContext('user1', project_id, True)

        with mock.patch.object(
                self.mocked_client,
                'list_security_groups',
                return_value=security_groups_list) as mock_list_secgroup:
            sg_api.list(admin_context,
                        project=project_id,
                        names=security_group_names,
                        search_opts=search_opts)

            mock_list_secgroup.assert_called_once_with(
                name=security_group_names, tenant_id=project_id)
示例#6
0
    def index(self, req):
        """Returns a list of security groups."""
        context = _authorize_context(req)

        search_opts = {}
        search_opts.update(req.GET)

        project_id = context.project_id
        raw_groups = security_group_api.list(
            context, project=project_id, search_opts=search_opts)

        limited_list = common.limited(raw_groups, req)
        result = [self._format_security_group(context, group)
                    for group in limited_list]

        return {'security_groups':
                list(sorted(result,
                            key=lambda k: (k['tenant_id'], k['name'])))}
    def index(self, req):
        """Returns a list of security groups."""
        context = req.environ['nova.context']
        context.can(sg_policies.POLICY_NAME % 'get',
                    target={'project_id': context.project_id})

        search_opts = {}
        search_opts.update(req.GET)

        project_id = context.project_id
        raw_groups = security_group_api.list(context,
                                             project=project_id,
                                             search_opts=search_opts)

        limited_list = common.limited(raw_groups, req)
        result = [
            self._format_security_group(context, group)
            for group in limited_list
        ]

        return {
            'security_groups':
            list(sorted(result, key=lambda k: (k['tenant_id'], k['name'])))
        }