예제 #1
0
 def test_list_all_projects(self, requests_mock):
     requests_mock.get(
         ANY,
         json={
             'code': 0,
             'data': [
                 {
                     'description': 'asdfsdf',
                     'english_name': 'a1',
                     'project_id': 'e5e7c623798f41f18211c8b25895ef6f',
                     'project_name': 'asdfasdf',
                 },
                 {
                     'description': 'public scan',
                     'english_name': 'CODE_716498',
                     'project_id': '47ceebac77214c77b794f7a6bb63ba14',
                     'project_name': 'CODE_716498',
                 },
             ],
         },
     )
     client = PaaSCCClient(ComponentAuth("token"))
     data = client.list_all_projects()
     assert len(data) == 2
     assert "desire_all_data" in requests_mock.last_request.qs
예제 #2
0
def list_auth_projects(access_token: str, username: str = '') -> Dict:
    """获取用户有权限(project_view)的所有项目"""
    if not username:
        authorization = ssm.get_authorization_by_access_token(access_token)
        username = authorization['identity']['username']

    perm_filter = ProjectFilter().make_view_perm_filter(username)
    if not perm_filter:
        return {'code': 0, 'data': []}

    # 如果是 any, 表示所有项目
    if ProjectFilter.op_is_any(perm_filter):
        if settings.REGION != 'ce':
            # 非 ce 版本可能项目很多, PaaSCCClient 接口拉取超时, 降级处理不返回
            logger.error(f'{username} project filter match any!')
            return {'code': 0, 'data': []}

        client = PaaSCCClient(auth=ComponentAuth(access_token))
        projects = client.list_all_projects()

    else:
        project_id_list = perm_filter.get('value')
        if not project_id_list:
            return {'code': 0, 'data': []}

        client = PaaSCCClient(auth=ComponentAuth(access_token))
        projects = client.list_projects_by_ids(project_id_list)

    for p in projects:
        p['project_code'] = p['english_name']

    return {'code': 0, 'data': projects}