|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available. |
| 4 | +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 5 | +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at https://opensource.org/licenses/MIT |
| 7 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 8 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +See the License for the specific language governing permissions and limitations under the License. |
| 10 | +""" |
| 11 | +import hashlib |
| 12 | +import hmac |
| 13 | +import time |
| 14 | +from datetime import timedelta |
| 15 | +from typing import Dict |
| 16 | + |
| 17 | +from django.utils.translation import ugettext_lazy as _ |
| 18 | + |
| 19 | +from backend import env |
| 20 | +from backend.components.base import BaseApi |
| 21 | +from backend.components.domains import XWORK_APIGW_DOMAIN |
| 22 | +from backend.utils.time import timestamp2datetime |
| 23 | + |
| 24 | + |
| 25 | +class _XworkApi(BaseApi): |
| 26 | + MODULE = _("Xwork 服务") |
| 27 | + BASE = XWORK_APIGW_DOMAIN |
| 28 | + |
| 29 | + # 待授权/已预约/处理中 |
| 30 | + XWORK_UNFINISHED_CODE = [4, 5, 6] |
| 31 | + # 已结束/已避免/已取消 |
| 32 | + XWORK_FINISHED_CODE = [7, 8, 9] |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + self.xwork_list = self.generate_data_api( |
| 36 | + method="POST", |
| 37 | + url="/fault_task/search", |
| 38 | + description=_("【查询】故障告警数据接口"), |
| 39 | + ) |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def __generate_signature(): |
| 43 | + """生成请求签名""" |
| 44 | + now = int(time.time()) |
| 45 | + caller_key = bytes(env.XWORK_CALLER_KEY, encoding="utf8") |
| 46 | + hd_str = (str(now)).encode("utf-8") |
| 47 | + signature = hmac.new(caller_key, hd_str, digestmod=hashlib.sha512).hexdigest() |
| 48 | + return now, signature |
| 49 | + |
| 50 | + def check_xwork_list(self, host_ip_map: Dict[str, int]): |
| 51 | + """ |
| 52 | + 检查主机是否有xwork单据 |
| 53 | + @param host_ip_map: 主机ip和主机ID的映射 |
| 54 | + """ |
| 55 | + if not XWORK_APIGW_DOMAIN: |
| 56 | + return {} |
| 57 | + |
| 58 | + # 查询xwork单据,默认查询一年以内 |
| 59 | + now, signature = self.__generate_signature() |
| 60 | + end_time = timestamp2datetime(now) |
| 61 | + start_time = end_time - timedelta(days=365) |
| 62 | + params = { |
| 63 | + "CallerName": env.XWORK_CALLER_USER, |
| 64 | + "Authorization": {"TimeStamp": now, "Signature": signature}, |
| 65 | + "ServiceParam": { |
| 66 | + "Filters": { |
| 67 | + "BsiIp": list(host_ip_map.keys()), |
| 68 | + "TaskStatusList": self.XWORK_UNFINISHED_CODE, |
| 69 | + "SearchStartTime": start_time.strftime("%Y-%m-%d %H:%M:%S"), |
| 70 | + "SearchEndTime": end_time.strftime("%Y-%m-%d %H:%M:%S"), |
| 71 | + } |
| 72 | + }, |
| 73 | + "Order": {}, |
| 74 | + "Offset": 0, |
| 75 | + "Limit": len(host_ip_map), |
| 76 | + } |
| 77 | + data = self.xwork_list(params, raw=True)["ServiceResult"]["Data"] |
| 78 | + # 映射xwork与host |
| 79 | + has_xwork_hosts_map = {host_ip_map[d["BsiIp"]]: d for d in data} |
| 80 | + return has_xwork_hosts_map |
| 81 | + |
| 82 | + |
| 83 | +XworkApi = _XworkApi() |
0 commit comments