Ejemplo n.º 1
0
def cc_search_create_object_attribute(request, obj_id, biz_cc_id,
                                      supplier_account):
    client = get_client_by_request(request)
    kwargs = {'bk_obj_id': obj_id, 'bk_supplier_account': supplier_account}
    cc_result = client.cc.search_object_attribute(kwargs)
    if not cc_result['result']:
        message = handle_api_error('cc', 'cc.search_object_attribute', kwargs,
                                   cc_result['message'])
        logger.error(message)
        result = {'result': False, 'data': [], 'message': message}
        return JsonResponse(result)

    obj_property = []
    for item in cc_result['data']:
        if item['editable']:
            prop_dict = {
                'tag_code': item['bk_property_id'],
                'type': "input",
                'attrs': {
                    'name': item['bk_property_name'],
                    'editable': 'true',
                },
            }
            if item['bk_property_id'] in ['bk_set_name']:
                prop_dict["attrs"]["validation"] = [{"type": "required"}]
            obj_property.append(prop_dict)

    return JsonResponse({'result': True, 'data': obj_property})
Ejemplo n.º 2
0
def job_get_script_list(request, biz_cc_id):
    """
    查询业务脚本列表
    :param request:
    :param biz_cc_id:
    :return:
    """
    # 查询脚本列表
    client = get_client_by_request(request)
    script_type = request.GET.get('type')
    kwargs = {
        'bk_biz_id': biz_cc_id,
        'is_public': True if script_type == 'public' else False
    }
    script_result = client.job.get_script_list(kwargs)

    if not script_result['result']:
        message = handle_api_error('cc', 'job.get_script_list', kwargs,
                                   script_result['message'])
        logger.error(message)
        result = {'result': False, 'message': message}
        return JsonResponse(result)

    script_dict = {}
    for script in script_result['data']['data']:
        script_dict.setdefault(script['name'], []).append(script['id'])

    version_data = []
    for name, version in script_dict.items():
        version_data.append({"text": name, "value": max(version)})

    return JsonResponse({'result': True, 'data': version_data})
Ejemplo n.º 3
0
def cc_search_object_attribute(request, obj_id, biz_cc_id, supplier_account):
    """
    @summary: 获取对象自定义属性
    @param request:
    @param biz_cc_id:
    @return:
    """
    client = get_client_by_request(request)
    kwargs = {'bk_obj_id': obj_id, 'bk_supplier_account': supplier_account}
    cc_result = client.cc.search_object_attribute(kwargs)
    if not cc_result['result']:
        message = handle_api_error('cc', 'cc.search_object_attribute', kwargs,
                                   cc_result['message'])
        logger.error(message)
        result = {'result': False, 'data': [], 'message': message}
        return JsonResponse(result)

    obj_property = []
    for item in cc_result['data']:
        if item['editable']:
            obj_property.append({
                'value': item['bk_property_id'],
                'text': item['bk_property_name']
            })

    return JsonResponse({'result': True, 'data': obj_property})
Ejemplo n.º 4
0
def job_get_job_tasks_by_biz(request, biz_cc_id):
    client = get_client_by_request(request)
    job_result = client.job.get_job_list({'bk_biz_id': biz_cc_id})
    if not job_result['result']:
        message = _(u"查询作业平台(JOB)的作业模板[app_id=%s]接口job.get_task返回失败: %s") % (
            biz_cc_id, job_result['message'])
        logger.error(message)
        result = {'result': False, 'data': [], 'message': message}
        return JsonResponse(result)
    task_list = []
    for task in job_result['data']:
        task_list.append({
            'value': task['bk_job_id'],
            'text': task['name'],
        })
    return JsonResponse({'result': True, 'data': task_list})
Ejemplo n.º 5
0
def cc_search_topo(request, obj_id, category, biz_cc_id, supplier_account):
    """
    @summary: 查询对象拓扑
    @param request:
    @param biz_cc_id:
    @return:
    """
    client = get_client_by_request(request)
    kwargs = {'bk_biz_id': biz_cc_id, 'bk_supplier_account': supplier_account}
    cc_result = client.cc.search_biz_inst_topo(kwargs)
    if not cc_result['result']:
        message = handle_api_error('cc', 'cc.search_biz_inst_topo', kwargs,
                                   cc_result['message'])
        logger.error(message)
        result = {'result': False, 'data': [], 'message': message}
        return JsonResponse(result)

    if category in ["normal", "prev", "picker"]:
        cc_topo = cc_format_topo_data(cc_result['data'], obj_id, category)
    else:
        cc_topo = []

    return JsonResponse({'result': True, 'data': cc_topo})
Ejemplo n.º 6
0
def cmdb_get_mainline_object_topo(request, bk_biz_id, bk_supplier_account=''):
    """
    @summary: 获取配置平台业务拓扑模型
    @param request:
    @param bk_biz_id:
    @param bk_supplier_account:
    @return:
    """
    kwargs = {
        'bk_biz_id': bk_biz_id,
        'bk_supplier_account': bk_supplier_account,
    }
    client = get_client_by_request(request)
    cc_result = client.cc.get_mainline_object_topo(kwargs)
    if not cc_result:
        message = handle_api_error(_(u"配置平台(CMDB)"),
                                   'cc.get_mainline_object_topo',
                                   kwargs,
                                   cc_result['message'])
    else:
        message = cc_result['message']
    result = {'result': cc_result['result'], 'code': cc_result['code'], 'data': cc_result['data'], 'message': message}
    return JsonResponse(result)
Ejemplo n.º 7
0
def job_get_job_task_detail(request, biz_cc_id, task_id):
    client = get_client_by_request(request)
    job_result = client.job.get_job_detail({
        'bk_biz_id': biz_cc_id,
        'bk_job_id': task_id
    })
    if not job_result['result']:
        message = _(
            u"查询作业平台(JOB)的作业模板详情[app_id=%s]接口job.get_task_detail返回失败: %s") % (
                biz_cc_id, job_result['message'])
        logger.error(message)
        result = {'result': False, 'data': [], 'message': message}
        return JsonResponse(result)

    job_step_type_name = {1: _(u"脚本"), 2: _(u"文件"), 4: u"SQL"}
    task_detail = job_result['data']
    global_var = []
    steps = []
    for var in task_detail.get('global_vars', []):
        # 1-字符串, 2-IP, 3-索引数组, 4-关联数组
        if var['type'] in [
                JOB_VAR_TYPE_STR, JOB_VAR_TYPE_IP, JOB_VAR_TYPE_ARRAY
        ]:
            value = var.get('value', '')
        else:
            value = [
                '{plat_id}:{ip}'.format(plat_id=ip_item['bk_cloud_id'],
                                        ip=ip_item['ip'])
                for ip_item in var.get('ip_list', [])
            ]
        global_var.append({
            'id': var['id'],
            # 全局变量类型:1:云参, 2:上下文参数,3:IP
            'category': var.get('category', 1),
            'name': var['name'],
            'type': var['type'],
            'value': value,
            'description': var['description']
        })
    for info in task_detail.get('steps', []):
        # 1-执行脚本, 2-传文件, 4-传SQL
        steps.append({
            'stepId':
            info['step_id'],
            'name':
            info['name'],
            'scriptParams':
            info.get('script_param', ''),
            'account':
            info.get('account', ''),
            'ipList':
            '',
            'type':
            info['type'],
            'type_name':
            job_step_type_name.get(info['type'], info['type'])
        })
    return JsonResponse({
        'result': True,
        'data': {
            'global_var': global_var,
            'steps': steps
        }
    })
Ejemplo n.º 8
0
def cmdb_search_host(request, bk_biz_id, bk_supplier_account='', bk_supplier_id=0):
    """
    @summary: 获取 CMDB 上业务的 IP 列表,以及 agent 状态等信息
    @param request:
    @param bk_biz_id: 业务 CMDB ID
    @param bk_supplier_account: 业务开发商账号
    @param bk_supplier_id: 业务开发商ID
    @params fields: list 查询字段,默认只返回 bk_host_innerip、bk_host_name、bk_host_id, 可以查询主机的任意字段,也可以查询
                set、module、cloud、agent等信息
    @return:
    """
    fields = json.loads(request.GET.get('fields', '[]'))
    client = get_client_by_request(request)
    condition = [{
        'bk_obj_id': 'host',
        'fields': [],
    }]
    if 'set' in fields:
        condition.append({
            'bk_obj_id': 'set',
            'fields': [],
        })
    if 'module' in fields:
        condition.append({
            'bk_obj_id': 'module',
            'fields': [],
        })
    kwargs = {
        'bk_biz_id': bk_biz_id,
        'bk_supplier_account': bk_supplier_account,
        'condition': condition
    }
    host_result = client.cc.search_host(kwargs)
    if not host_result['result']:
        message = handle_api_error(_(u"配置平台(CMDB)"), 'cc.search_host', kwargs, host_result['message'])
        result = {'result': False, 'code': 110, 'message': message}
        return JsonResponse(result)

    host_info = host_result['data']['info']
    data = []
    default_fields = ['bk_host_innerip', 'bk_host_name', 'bk_host_id']
    fields = list(set(default_fields + fields))
    for host in host_info:
        host_detail = {field: host['host'][field] for field in fields if field in host['host']}
        if 'set' in fields:
            host_detail['set'] = host['set']
        if 'module' in fields:
            host_detail['module'] = host['module']
        if 'cloud' in fields or 'agent' in fields:
            host_detail['cloud'] = host['host']['bk_cloud_id']
        data.append(host_detail)

    if 'agent' in fields:
        agent_kwargs = {
            'bk_biz_id': bk_biz_id,
            'bk_supplier_id': bk_supplier_id,
            'hosts': [{'bk_cloud_id': host['cloud'][0]['id'], 'ip': host['bk_host_innerip']} for host in data]
        }
        agent_result = client.gse.get_agent_status(agent_kwargs)
        if not agent_result['result']:
            message = handle_api_error(_(u"管控平台(GSE)"),
                                       'gse.get_agent_status',
                                       agent_kwargs,
                                       agent_result['message'])
            result = {'result': False, 'code': 111, 'message': message}
            return JsonResponse(result)

        agent_data = agent_result['data']
        for host in data:
            # agent在线状态,0为不在线,1为在线,-1为未知
            agent_info = agent_data.get('%s:%s' % (host['cloud'][0]['id'], host['bk_host_innerip']), {})
            host['agent'] = agent_info.get('bk_agent_alive', -1)

    result = {'result': True, 'code': 0, 'data': data}
    return JsonResponse(result)