def __init__(self): self.__param = { "app_code": APP_ID, "app_secret": APP_TOKEN, 'bk_username': "******" } common_args = {'bk_username': '******'} self.client = ComponentClient( # APP_ID 应用ID app_code=APP_ID, # APP_TOKEN 应用TOKEN app_secret=APP_TOKEN, common_args=common_args)
def create_business(cls, request): """新建业务""" bk_app_code = settings.APP_CODE bk_app_secret = settings.SECRET_KEY bk_token = request.COOKIES['bk_token'] # bk_token # 获取client方法1 # client = get_client_by_request(request) # 获取client方法2 client = ComponentClient(bk_app_code=bk_app_code, bk_app_secret=bk_app_secret, common_args={'bk_token': bk_token}) kwargs = { "bk_supplier_account": "0", "data": { "bk_biz_name": "cc_app_test", "bk_biz_maintainer": "admin", "bk_biz_productor": "admin", "bk_biz_developer": "admin", "bk_biz_tester": "admin", "time_zone": "Asia/Shanghai", "language": "1" } } info = client.cc.create_business(kwargs) return JsonResponse(info)
def monitor_cpu(): try: client = ComponentClient(app_code=settings.APP_ID, app_secret=settings.APP_TOKEN, common_args={'username': '******'}) resp = client.job.get_task_detail({'app_id': '3', 'task_id': '2'}) logger.info(str(resp)) for ip in ['10.0.1.109', '10.0.1.220', '10.0.1.188']: kwargs = { 'app_id': '3', 'task_id': '2', 'steps': [{ "scriptTimeout": 1000, "ipList": "1:" + ip, "stepId": 2, "account": "root", }] } result = client.job.execute_task(kwargs) logger.info('exec task:' + str(result)) if result.get('result', False): data = result['data'] task_instance_id = data['taskInstanceId'] complete_flag = False while not complete_flag: time.sleep(3) task_result = client.job.get_task_result( {'task_instance_id': task_instance_id}) logger.info('fetch task result:' + str(task_result)) if task_result.get('result', False): task_result_data = task_result['data'] complete_flag = task_result_data.get( 'isFinished', False) task_log = client.job.get_task_ip_log({ 'app_id': '3', 'task_instance_id': task_instance_id }) task_log_analize(task_log) except Exception as ex: logger.exception(ex)
def __init__(self): self.__param = { "app_code": APP_ID, "app_secret": APP_TOKEN, "bk_username": '******', } # common_args = {'bk_username': '******'} self.client = ComponentClient( # APP_ID 应用ID app_code=APP_ID, # APP_TOKEN 应用TOKEN app_secret=APP_TOKEN, # common_args=common_args )
def test_get_all_user(self): # APP信息 app_code = 'testframework' app_secret = '9bff9124-ad33-4001-a578-02260a3e4ce7' # 用户信息 common_args = { 'bk_token': 'XpQC3Jalk_yAxEgl0VhwjZVSRU9DrDwxHdy80RdoPdM' } # APP信息app_code, app_secret如未提供,从环境配置获取 client = ComponentClient(app_code=app_code, app_secret=app_secret, common_args=common_args) response = client.bk_login.get_all_user() print response
def get_user(cls, request): bk_app_code = settings.APP_CODE bk_app_secret = settings.SECRET_KEY bk_token = request.COOKIES['bk_token'] # bk_token # 获取client方法1 # client = get_client_by_request(request) # 获取client方法2 client = ComponentClient(bk_app_code=bk_app_code, bk_app_secret=bk_app_secret, common_args={'bk_token': bk_token}) info = client.bk_login.get_user() print(info['data']['bk_username']) # 用户名 print(info['data']['email']) # 邮箱 return JsonResponse(info)
class ESBComponentApi(object): ''' 不需要request参数的esb ''' def __init__(self): self.__param = { "app_code": APP_ID, "app_secret": APP_TOKEN, 'bk_username': "******" } common_args = {'bk_username': '******'} self.client = ComponentClient( # APP_ID 应用ID app_code=APP_ID, # APP_TOKEN 应用TOKEN app_secret=APP_TOKEN, common_args=common_args) def fast_execute_script(self, bk_biz_id=None, script_id=None, script_content=None, ip_list=None, script_param=None, account=None, script_type=None): ''' 执行脚本 :param bk_biz_id: :param script_id: :param script_content: :param ip_list: :param script_param: :param account: :return: ''' param = self.__param if account is None: account = 'root' param["bk_biz_id"] = bk_biz_id if script_id is not None: param["script_id"] = script_id if script_content is not None: param['script_content'] = script_content param["account"] = account param["ip_list"] = ip_list param["script_type"] = script_type if script_param is not None: param['script_param'] = script_param self.client.set_bk_api_ver("v2") print param result = self.client.job.fast_execute_script(param) #print result, '--------快速执行脚本结果' return result def get_job_instance_log(self, bk_biz_id=None, job_instance_id=None): param = { "bk_app_code": self.__param['app_code'], "bk_app_secret": self.__param['app_secret'], "bk_biz_id": bk_biz_id, "job_instance_id": job_instance_id } result = self.client.job.get_job_instance_log(param) return result
# -*- coding: utf-8 -*- from blueking.component.client import ComponentClient from conf.default import APP_ID, APP_TOKEN from models import Business,UHost app_code = APP_ID app_secret = APP_TOKEN # 用户信息 common_args = {'bk_token': 'SgNRKBhFN4ImtGd28dNkEzMb_91MetH1Fgs0TWosnRo'} client = ComponentClient( app_code=app_code, app_secret=app_secret, common_args=common_args ) def init_business(total=10, start=0): client.cc.search_business.path = "http://paas.bk.com:80/" + client.cc.search_business.path kwargs = {"page": {"total": total, "start": start}} result = client.cc.search_business(kwargs) count = result.get("data").get("count", 0) if result.get("data") else None for key in result.get("data").get("info"): print key if count == total: init_business(total=total, start=start+count) def init_host(total=10, start=0): client.cc.search_host.path = "http://paas.bk.com:80/" + client.cc.search_host.path kwargs = {"page": {"total": total, "start": start}}
def get_client(bk_token): return ComponentClient( app_code="herokingfsaas", app_secret="d9664192-989a-424e-b0e6-5acb404fee2d", common_args={"bk_token": bk_token} )