Esempio n. 1
0
    def hosts(self, request, project_id):
        """查询指定业务拓扑下主机列表"""
        params = self.params_validate(FetchCCHostSLZ)
        username = request.user.username
        access_token = request.user.token.access_token
        bk_biz_id = request.project.cc_app_id

        # 从 CMDB 获取可用主机信息,业务名称信息
        try:
            host_list = utils.fetch_cc_app_hosts(username, bk_biz_id,
                                                 params['set_id'],
                                                 params['module_id'])
        except CompParseBkCommonResponseError as e:
            raise error_codes.ComponentError(str(e))
        except BaseCompError as e:
            logger.error('获取主机信息失败:%s', e)
            raise error_codes.ComponentError(_('发生未知错误,请稍候再试'))
        cc_app_name = cc.get_application_name(bk_biz_id)

        # 根据指定的 IP 过滤
        host_list = filter_by_ips(host_list,
                                  params['ip_list'],
                                  key='bk_host_innerip',
                                  fuzzy=params['fuzzy'])

        response_data = {'count': 0, 'results': [], 'cc_app_name': cc_app_name}
        # 补充节点使用情况,包含使用的项目 & 集群
        project_cluster_info = utils.fetch_project_cluster_info(access_token)
        all_cluster_nodes = utils.fetch_all_cluster_nodes(access_token)
        host_list = utils.attach_project_cluster_info(host_list,
                                                      all_cluster_nodes,
                                                      project_cluster_info)

        # 更新 可选择的机器 数量信息
        response_data['selectable_cnt'] = len(
            [h for h in host_list if utils.is_host_selectable(h)])

        # 如没有符合过滤条件的,直接返回默认值
        if not host_list:
            return Response(response_data)

        # 被使用 / agent 异常的机器均视为 不可使用
        response_data['unavailable_ip_count'] = len(
            [h for h in host_list if h['is_used'] or not h['is_valid']])

        # 支持获取全量数据(仅跨页全选时候使用)
        if params['desire_all_data']:
            ret = {'count': len(host_list), 'results': host_list}
        else:
            ret = custom_paginator(host_list, params['offset'],
                                   params['limit'])
        # 更新 Host 的 GSE Agent 状态信息
        ret['results'] = utils.update_gse_agent_status(username,
                                                       ret['results'])

        response_data['results'] = ret['results']
        response_data['count'] = ret['count']
        return Response(response_data)
Esempio n. 2
0
 def info(self, request, project_id):
     """单个项目信息"""
     data = request.project
     data["created_at"], data[
         "updated_at"] = self.normalize_create_update_time(
             data["created_at"], data["updated_at"])
     # 添加业务名称
     data["cc_app_name"] = cc.get_application_name(
         request.project.cc_app_id)
     data["can_edit"] = self.can_edit(request, project_id)
     return Response(data)
Esempio n. 3
0
 def test_get_application_name(self):
     ret = get_application_name(1001)
     assert ret == '测试业务'
Esempio n. 4
0
def get_application_name(request):
    return cc.get_application_name(request.user.username,
                                   request.project.cc_app_id)
Esempio n. 5
0
 def get_project(self, request, project_id):
     """单个项目信息"""
     data = request.project
     # 添加业务名称
     data["cc_app_name"] = cc.get_application_name(request.project.cc_app_id)
     return Response(data)
Esempio n. 6
0
 def get_cc_application_name(self):
     return cc.get_application_name(self.username, self.cc_app_id)