def search_service_instance(bk_biz_id, bk_module_id=None):
    """
    根据模块获取服务实例(临时)
    """
    import requests
    import json
    url = 'https://cmdbee-dev.bktencent.com/api/v3/findmany/proc/service_instance'
    params = {
        "metadata": {
            "label": {
                "bk_biz_id": str(bk_biz_id)
            }
        }
    }
    if bk_module_id:
        params["bk_module_id"] = bk_module_id

    from bkmonitor.middlewares.request_middlewares import get_request
    cookie_dict = (getattr(get_request(), 'COOKIES', None))
    cookie = ''
    for key, value in cookie_dict.items():
        cookie += "{key}={value};".format(key=key, value=value)
    result = requests.post(url=url, data=json.dumps(params), headers={'Cookie': cookie}, verify=False)
    content = json.loads(result.content)
    if not content["result"]:
        raise CustomException("查询CMDB服务实例失败: %s" % content['bk_error_msg'])

    return content['data']['info']
def search_obj(bk_obj_id):
    """
    获取CMDB的对象信息
    """
    params = {
        "bk_obj_id": bk_obj_id,
        "bk_supplier_account": "0"
    }
    result = client.cc.search_objects(params)
    if not result["result"] or not result['data']:
        raise CustomException("查询对象信息失败: %s" % result['message'])

    return result["data"][0]
def get_blueking_biz_id():
    """
    获取蓝鲸业务所属的业务ID
    """
    result = client.cc.search_business(
        fields=[
            "bk_biz_id",
            "bk_biz_name"
        ],
        condition={
            "bk_biz_name": "蓝鲸"
        }
    )
    if not result['result'] or not result['data']['info']:
        raise CustomException("查询蓝鲸业务ID失败: %s" % result['message'])
    biz_id = result['data']['info'][0]['bk_biz_id']
    return biz_id
def search_inst(bk_obj_id, bk_inst_id):
    """
    获取CMDB的实例信息
    """
    params = {
        'bk_obj_id': bk_obj_id,
        'bk_supplier_account': "0",
        'condition': {
            bk_obj_id: [
                {
                    'field': "bk_%s_id" % bk_obj_id if bk_obj_id in ["module", "set", "biz"] else "bk_inst_id",
                    'operator': '$eq',
                    'value': bk_inst_id
                }
            ]
        }
    }
    result = client.cc.search_inst(params)
    if not result["result"] or not result['data']['info']:
        raise CustomException("查询实例信息失败: %s" % result['message'])

    return result["data"]["info"][0]
def _get_hosts_by_inst_id(bk_biz_id, bk_obj_id, bk_inst_id):
    condition = [
        {
            "bk_obj_id": bk_obj_id if bk_obj_id in ["module", "set", "biz"] else "object",
            "fields": [],
            "condition": [
                {
                    "field": "bk_%s_id" % bk_obj_id if bk_obj_id in ["module", "set", "biz"] else "bk_inst_id",
                    "operator": "$in" if isinstance(bk_inst_id, list) else "$eq",
                    "value": bk_inst_id
                }
            ]
        }
    ]

    if bk_obj_id != "module":
        condition.append({
            "bk_obj_id": "module",
            "fields": [],
        })

    if bk_obj_id != "set":
        condition.append({
            "bk_obj_id": "set",
            "fields": [],
        })

    result = client.cc.search_host(bk_biz_id=bk_biz_id, condition=condition)

    if not result["result"]:
        raise CustomException("查询主机失败: %s" % result['message'])

    _get_host_topo_inst(bk_biz_id, result['data']['info'])

    for host in result["data"]["info"]:
        host["cc_app_module"] = [x["bk_inst_id"] for x in host["module"]]
        host["cc_topo_set"] = [x["bk_inst_id"] for x in host["set"]]

    return result["data"]["info"]
def _host_detail(ip, bk_cloud_id, bk_biz_id):
    if isinstance(bk_cloud_id, list):
        bk_cloud_id = bk_cloud_id[0]["bk_inst_id"]

    condition = [
        {
            "bk_obj_id": "module",
            "fields": []
        },
        {
            "bk_obj_id": "host",
            "condition": [
                {
                    "field": "bk_host_innerip",
                    "operator": "$eq",
                    "value": ip
                },
                {
                    "field": "bk_cloud_id",
                    "operator": "$eq",
                    "value": bk_cloud_id
                }
            ]
        }
    ]

    result = client.cc.search_host(bk_biz_id=bk_biz_id, condition=condition)

    if not result['result']:
        raise CustomException("查询主机失败: %s" % result['message'])

    _get_host_topo_inst(bk_biz_id, result["data"]["info"])

    for host in result["data"]["info"]:
        host["cc_app_module"] = [x["bk_inst_id"] for x in host["module"]]
        host["cc_topo_set"] = [x["bk_inst_id"] for x in host["set"]]

    if result['data']['info']:
        return result['data']['info']