def jobv3_get_job_plan_list(request, biz_cc_id, job_template_id): """ 查询执行方案列表 @param request: @param biz_cc_id: 业务 ID @param job_template_id: 作业模版 ID @return: """ client = get_client_by_user(request.user.username) bk_scope_type = request.GET.get("bk_scope_type", JobBizScopeType.BIZ.value) plan_list = batch_request( func=client.jobv3.get_job_plan_list, params={ "bk_scope_type": bk_scope_type, "bk_scope_id": str(biz_cc_id), "bk_biz_id": biz_cc_id, "job_template_id": job_template_id, }, get_data=lambda x: x["data"]["data"], get_count=lambda x: x["data"]["total"], page_param={ "cur_page_param": "start", "page_size_param": "length" }, is_page_merge=True, ) data = [] for plan in plan_list: data.append({"value": plan["id"], "text": plan["name"]}) return JsonResponse({"result": True, "data": data})
def execute(self, data, parent_data): executor = parent_data.get_one_of_inputs("executor") client = get_client_by_user(executor) bk_biz_id = parent_data.get_one_of_inputs("bk_biz_id") supplier_account = supplier_account_for_business(bk_biz_id) set_list = data.get_one_of_inputs("set_list") cc_set_select = set_list.split(",") set_select_method = data.get_one_of_inputs("set_select_method") set_status = data.get_one_of_inputs("set_status") set_attr_id = data.get_one_of_inputs("set_attr_id") or "bk_set_name" bk_set_ids = [] if set_select_method in ("name", "custom"): for set_name in cc_set_select: cc_search_set_kwargs = { "bk_biz_id": int(bk_biz_id), "fields": ["bk_set_id", set_attr_id], "condition": { set_attr_id: set_name }, } cc_search_set_result = batch_request(client.cc.search_set, cc_search_set_kwargs) if not cc_search_set_result: self.logger.error( "batch_request client.cc.search_set error") data.set_outputs( "ex_data", "batch_request client.cc.search_set error") return False for bk_set in cc_search_set_result: if bk_set[set_attr_id] == set_name: bk_set_ids.append(bk_set["bk_set_id"]) elif set_select_method == "id": bk_set_ids = cc_set_select for set_id in bk_set_ids: cc_kwargs = { "bk_biz_id": bk_biz_id, "bk_supplier_account": supplier_account, "bk_set_id": set_id, "data": { "bk_service_status": set_status }, } cc_result = client.cc.update_set(cc_kwargs) if not cc_result["result"]: message = cc_handle_api_error("cc.update_set", cc_kwargs, cc_result) self.logger.error(message) data.set_outputs("ex_data", message) return False return True
def cc_list_set_template(request, biz_cc_id, supplier_account): client = get_client_by_user(request.user.username) kwargs = { "bk_biz_id": int(biz_cc_id), "bk_supplier_account": supplier_account } set_templates = batch_request(client.cc.list_set_template, kwargs) template_list = [{ "value": set_template.get("id"), "text": set_template.get("name") } for set_template in set_templates] return JsonResponse({"result": True, "data": template_list})
def get_dynamic_group_host_list(username, bk_biz_id, bk_supplier_account, dynamic_group_id): """获取动态分组中对应主机列表""" client = get_client_by_user(username) kwargs = { "bk_biz_id": bk_biz_id, "bk_supplier_account": bk_supplier_account, "id": dynamic_group_id, "fields": ["bk_host_innerip", "bk_cloud_id", "bk_host_id"], } host_list = batch_request(client.cc.execute_dynamic_group, kwargs, limit=200) return True, {"code": 0, "message": "success", "data": host_list}
def cc_get_set(request, biz_cc_id): """ 批量获取业务下所有集群 @param request: 请求信息 @param biz_cc_id: 业务ID @return: """ client = get_client_by_user(request.user.username) kwargs = {"bk_biz_id": int(biz_cc_id), "fields": ["bk_set_name", "bk_set_id"]} cc_set_result = batch_request(client.cc.search_set, kwargs) logger.info("[cc_get_set] cc_set_result: {cc_set_result}".format(cc_set_result=cc_set_result)) result = [{"value": set_item["bk_set_id"], "text": set_item["bk_set_name"]} for set_item in cc_set_result] return JsonResponse({"result": True, "data": result})
def cc_get_set_group(request, biz_cc_id): """ 通过bk_biz_id获取当前业务下所有集群类型的动态分组 :param biz_cc_id: 业务ID :param request: :param operator: 操作者 :return: """ client = get_client_by_user(request.user.username) kwargs = {"bk_biz_id": int(biz_cc_id), "condition": {"bk_obj_id": "set"}} group_info = batch_request(client.cc.search_dynamic_group, kwargs, limit=200) group_data = [] for group in group_info: group_data.append({"text": group["name"], "value": group["id"]}) return JsonResponse({"result": True, "data": group_data})
def get_business_host(username, bk_biz_id, supplier_account, host_fields, ip_list=None): """根据主机内网 IP 过滤业务下的主机 :param username: 请求用户名 :type username: str :param bk_biz_id: 业务 CC ID :type bk_biz_id: int :param supplier_account: 开发商账号, defaults to 0 :type supplier_account: int :param host_fields: 主机过滤字段, defaults to None :type host_fields: list :param ip_list: 主机内网 IP 列表 :type ip_list: list :return: [ { "bk_cloud_id": 0, "bk_host_id": 1, "bk_host_innerip": "127.0.0.1", "bk_mac": "", "bk_os_type": null }, ... ] :rtype: [type] """ kwargs = { "bk_biz_id": bk_biz_id, "bk_supplier_account": supplier_account, "fields": list(host_fields or []) } if ip_list: kwargs["host_property_filter"] = { "condition": "AND", "rules": [{ "field": "bk_host_innerip", "operator": "in", "value": ip_list }], } client = get_client_by_user(username) return batch_request(client.cc.list_biz_hosts, kwargs)
def generate_group_data(self): """根据字符串生成动态分组数据""" client = get_client_by_user(self.username) group_names = set(re.split("[,\n]", self.raw_data)) result = batch_request(client.cc.search_dynamic_group, self.request_kwargs, limit=200) dynamic_groups = [] for dynamic_group in result: if dynamic_group["bk_obj_id"] == "host" and dynamic_group[ "name"] in group_names: dynamic_groups.append({ "id": dynamic_group["id"], "name": dynamic_group["name"] }) return {"result": True, "data": dynamic_groups, "message": ""}
def cmdb_search_dynamic_group(request, bk_biz_id, bk_supplier_account=""): """ @summary: 查询动态分组列表 @param request: @param bk_biz_id: @param bk_supplier_account: @return: """ client = get_client_by_user(request.user.username) kwargs = {"bk_biz_id": bk_biz_id, "bk_supplier_account": bk_supplier_account} result = batch_request(client.cc.search_dynamic_group, kwargs, limit=200) dynamic_groups = [] for dynamic_group in result: if dynamic_group["bk_obj_id"] == "host": dynamic_groups.append( {"id": dynamic_group["id"], "name": dynamic_group["name"], "create_user": dynamic_group["create_user"]} ) return JsonResponse({"result": True, "data": {"count": len(dynamic_groups), "info": dynamic_groups}})
def cc_execute_dynamic_group(operator, bk_biz_id, bk_group_id, set_field): """ 通过集群ID和模块ID查询对应的名字 :param operator: 操作者 :param bk_biz_id: 业务ID :param bk_group_id: 动态分组id :return: """ client = get_client_by_user(operator) set_data_dir = {} kwargs = {"bk_biz_id": bk_biz_id, "id": bk_group_id, "fields": set_field} group_info = batch_request(client.cc.execute_dynamic_group, kwargs, limit=200) for _field in set_field: set_data_dir[_field] = [] for set_data in group_info: for _field in set_field: set_data_dir[_field].append(str(set_data.get(_field, ""))) for _field in set_field: flat_field_name = "flat__{}".format(_field) set_data_dir[flat_field_name] = ",".join(set_data_dir[_field]) return set_data_dir
def cc_search_set_module_name_by_id(operator, bk_biz_id, bk_set_id, bk_module_ids): """ 通过集群ID和模块ID查询对应的名字 :param operator: 操作者 :param bk_biz_id: 业务ID :param bk_set_id: 集群ID :param bk_module_id: 模块ID :return: """ str_module_ids = [str(item) for item in bk_module_ids] set_module_info = {"set_id": bk_set_id, "module_id": bk_module_ids, "flat__module_id": ",".join(str_module_ids)} client = get_client_by_user(operator) set_kwargs = { "bk_biz_id": bk_biz_id, "fields": ["bk_set_name"], "condition": {"bk_set_id": bk_set_id}, "page": {"start": 0, "limit": 1}, } set_result = client.cc.search_set(set_kwargs) if set_result["result"] and set_result["data"]["info"]: set_module_info["set_name"] = set_result["data"]["info"][0]["bk_set_name"] else: err_msg = handle_api_error("CC", "search_set", set_kwargs, set_result) logger.error(err_msg) raise ApiRequestError(err_msg) module_kwargs = {"bk_biz_id": bk_biz_id, "bk_set_id": bk_set_id, "fields": ["bk_module_id", "bk_module_name"]} module_info = batch_request(client.cc.search_module, module_kwargs) bk_module_names = [] for item in module_info: if item.get("bk_module_id") in bk_module_ids: bk_module_names.append(item.get("bk_module_name")) set_module_info["module_name"] = bk_module_names set_module_info["flat__module_name"] = ",".join(bk_module_names) return set_module_info
def get_business_host_topo(username, bk_biz_id, supplier_account, host_fields, ip_list=None, property_filters=None): """获取业务下所有主机信息 :param username: 请求用户名 :type username: str :param bk_biz_id: 业务 CC ID :type bk_biz_id: int :param supplier_account: 开发商账号, defaults to 0 :type supplier_account: int :param host_fields: 主机过滤字段 :type host_fields: list :param ip_list: 主机内网 IP 列表 :type ip_list: list :param property_filters: 查询主机时的相关属性过滤条件, 当传递该参数时,ip_list参数生成的过滤条件失效 :type property_filters: dict :return: [ { "host": { "bk_host_id": 4, "bk_host_innerip": "127.0.0.1", "bk_cloud_id": 0, ... }, "module": [ { "bk_module_id": 2, "bk_module_name": "module_name" }, ... ], "set": [ { "bk_set_name": "set_name", "bk_set_id": 1 }, ... ] } ] :rtype: list """ client = get_client_by_user(username) kwargs = { "bk_biz_id": bk_biz_id, "bk_supplier_account": supplier_account, "fields": list(host_fields or []) } if property_filters is not None: kwargs.update(property_filters) elif ip_list: kwargs["host_property_filter"] = { "condition": "AND", "rules": [{ "field": "bk_host_innerip", "operator": "in", "value": ip_list }], } result = batch_request(client.cc.list_biz_hosts_topo, kwargs) host_info_list = [] for host_topo in result: host_info = {"host": host_topo["host"], "module": [], "set": []} for parent_set in host_topo["topo"]: host_info["set"].append({ "bk_set_id": parent_set["bk_set_id"], "bk_set_name": parent_set["bk_set_name"] }) for parent_module in parent_set["module"]: host_info["module"].append({ "bk_module_id": parent_module["bk_module_id"], "bk_module_name": parent_module["bk_module_name"] }) host_info_list.append(host_info) return host_info_list