コード例 #1
0
def get_client_by_user_and_biz_id(username, biz_cc_id):
    """
    @summary: 根据用户和业务获取运维身份的client
    :param username:
    :param biz_cc_id:
    :return:
    """
    # 首先以存在auth_token的运维身份调用接口
    maintainer, __ = get_biz_maintainer_info(biz_cc_id, username)
    if maintainer:
        return settings.ESB_GET_CLIENT_BY_USER(maintainer)

    # 无任何业务的运维auth_token信息,只能以自己身份执行
    return settings.ESB_GET_CLIENT_BY_USER(username)
コード例 #2
0
ファイル: user_info.py プロジェクト: yuanfeng0905/bk-sops
def get_user_info(request):
    client = settings.ESB_GET_CLIENT_BY_USER(request.user.username)
    auth = getattr(client, settings.ESB_AUTH_COMPONENT_SYSTEM)
    _get_user_info = getattr(auth, settings.ESB_AUTH_GET_USER_INFO)
    user_info = _get_user_info({})
    if 'data' in user_info:
        user_info['data']['bk_supplier_account'] = 0
    return user_info
コード例 #3
0
ファイル: utils.py プロジェクト: wyyalt/bk-sops
def _get_user_business_list(request, use_cache=True):
    """Get authorized business list for a exact username.

    :param object request: django request object.
    :param bool use_cache: (Optional)
    """
    user = request.user
    cache_key = "%s_get_user_business_list_%s" % (CACHE_PREFIX, user.username)
    data = cache.get(cache_key)

    if not (use_cache and data):
        user_info = _get_user_info(request)
        client = settings.ESB_GET_CLIENT_BY_USER(request.user.username)
        result = client.cc.search_business({
            'bk_supplier_account':
            user_info['bk_supplier_account'],
            'condition': {
                'bk_data_status': {
                    '$in': ['enable', 'disabled', None]
                },
                '$or': [{
                    'bk_biz_developer': {
                        "$regex": user.username
                    }
                }, {
                    'bk_biz_productor': {
                        "$regex": user.username
                    }
                }, {
                    'bk_biz_maintainer': {
                        "$regex": user.username
                    }
                }, {
                    'bk_biz_tester': {
                        "$regex": user.username
                    }
                }]
            }
        })

        if result['result']:
            data = result['data']['info']
            cache.set(cache_key, data, DEFAULT_CACHE_TIME_FOR_CC)
        elif result.get('code') in ('20101', 20101):
            raise exceptions.Unauthorized(result['message'])
        elif result.get('code') in ('20103', 20103, '20201', 20201, '20202',
                                    20202):
            raise exceptions.Forbidden(result['message'])
        else:
            raise exceptions.APIError(
                'cc', 'search_business',
                result.get('detail_message', result['message']))

    return data
コード例 #4
0
ファイル: bk.py プロジェクト: selinagyan/bk-sops
    def execute(self, data, parent_data):
        executor = parent_data.get_one_of_inputs('executor')
        biz_cc_id = parent_data.get_one_of_inputs('biz_cc_id')
        supplier_account = parent_data.get_one_of_inputs(
            'biz_supplier_account')
        client = gcloud_settings.ESB_GET_CLIENT_BY_USER(executor)
        if parent_data.get_one_of_inputs('language'):
            translation.activate(parent_data.get_one_of_inputs('language'))

        notify_type = data.get_one_of_inputs('bk_notify_type')
        receiver_info = data.get_one_of_inputs('bk_receiver_info')
        # 兼容原有数据格式
        if receiver_info:
            receiver_group = receiver_info.get('bk_receiver_group')
            more_receiver = receiver_info.get('bk_more_receiver')
        else:
            receiver_group = data.get_one_of_inputs('bk_receiver_group')
            more_receiver = data.get_one_of_inputs('bk_more_receiver')
        title = data.get_one_of_inputs('bk_notify_title')
        content = data.get_one_of_inputs('bk_notify_content')

        code = ''
        message = ''
        result, msg, receivers = get_notify_receivers(client, biz_cc_id,
                                                      supplier_account,
                                                      receiver_group,
                                                      more_receiver)

        if not result:
            data.set_outputs('ex_data', msg)
            return False

        for t in notify_type:
            kwargs = self._args_gen[t](self, receivers, title, content)
            result = getattr(client.cmsi, self._send_func[t])(kwargs)

            if not result['result']:
                data.set_outputs('ex_data', result['message'])
                return False

            code = result['code']
            message = result['message']

        data.set_outputs('code', code)
        data.set_outputs('message', message)
        return True