-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
198 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from apps.node_man.periodic_tasks import sync_all_isp_to_cmdb_periodic_task | ||
|
||
|
||
class Command(BaseCommand): | ||
def handle(self, **kwargs): | ||
sync_all_isp_to_cmdb_periodic_task() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
|
||
import time | ||
from typing import Any, Dict, List | ||
|
||
from celery.task import periodic_task | ||
|
||
from apps.component.esbclient import client_v2 | ||
from apps.exceptions import ComponentCallError | ||
from apps.node_man import constants | ||
from apps.node_man.models import Cloud, GlobalSettings | ||
from apps.utils.basic import chunk_lists | ||
from common.log import logger | ||
|
||
|
||
def sync_all_isp_to_cmdb(task_id): | ||
logger.info(f"{task_id} | Start syncing cloud isp info.") | ||
# CMDB内置云区域不更新 | ||
cmdb_internal_cloud_ids = GlobalSettings.get_config( | ||
key=GlobalSettings.KeyEnum.CMDB_INTERNAL_CLOUD_IDS.value, default=[] | ||
) | ||
cloud_info: List[Dict[str, Any]] = list(Cloud.objects.values("bk_cloud_id", "isp")) | ||
# 分片请求:一次五十条 | ||
for chunk_clouds in chunk_lists(cloud_info, constants.UPDATE_CMDB_CLOUD_AREA_LIMIT): | ||
for cloud in chunk_clouds: | ||
bk_cloud_id: int = cloud["bk_cloud_id"] | ||
if bk_cloud_id in cmdb_internal_cloud_ids: | ||
continue | ||
bk_cloud_vendor: str = constants.CMDB_CLOUD_VENDOR_MAP.get(cloud["isp"]) | ||
try: | ||
client_v2.cc.update_cloud_area({"bk_cloud_id": bk_cloud_id, "bk_cloud_vendor": bk_cloud_vendor}) | ||
except ComponentCallError as e: | ||
logger.error("esb->call update_cloud_area error %s" % e.message) | ||
client_v2.cc.update_inst(bk_obj_id="plat", bk_inst_id=bk_cloud_id, bk_cloud_vendor=bk_cloud_vendor) | ||
# 休眠1秒避免一次性全量请求导致接口超频 | ||
time.sleep(1) | ||
|
||
logger.info(f"{task_id} | Sync cloud isp info task complete.") | ||
|
||
|
||
@periodic_task( | ||
queue="default", | ||
options={"queue": "default"}, | ||
run_every=constants.SYNC_ISP_TO_CMDB_INTERVAL, | ||
) | ||
def sync_all_isp_to_cmdb_periodic_task(): | ||
""" | ||
同步云服务商至CMDB | ||
""" | ||
task_id = sync_all_isp_to_cmdb_periodic_task.request.id | ||
sync_all_isp_to_cmdb(task_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
apps/node_man/tests/test_pericdic_tasks/test_sync_all_isp_to_cmdb.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-节点管理(BlueKing-BK-NODEMAN) available. | ||
Copyright (C) 2017-2022 THL A29 Limited, a Tencent company. All rights reserved. | ||
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at https://opensource.org/licenses/MIT | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
""" | ||
|
||
from unittest.mock import patch | ||
|
||
from django.test import TestCase | ||
|
||
from apps.node_man import models | ||
from apps.node_man.periodic_tasks.sync_all_isp_to_cmdb import ( | ||
sync_all_isp_to_cmdb_periodic_task, | ||
) | ||
from apps.node_man.tests.utils import MockClient, create_cloud_area | ||
|
||
|
||
class TestSyncAllIspToCmdb(TestCase): | ||
@staticmethod | ||
def init_db(): | ||
create_cloud_area(2) | ||
|
||
@patch("apps.node_man.periodic_tasks.sync_all_isp_to_cmdb.client_v2", MockClient) | ||
def test_sync_all_isp_to_cmdb(self): | ||
self.init_db() | ||
# 构造CMDB内置云区域ID | ||
models.GlobalSettings.set_config(key=models.GlobalSettings.KeyEnum.CMDB_INTERNAL_CLOUD_IDS.value, value=[1]) | ||
models.Cloud.objects.filter(bk_cloud_id=2).update(isp="Tencent") | ||
with patch("apps.node_man.periodic_tasks.sync_all_isp_to_cmdb.client_v2.cc.update_cloud_area") as update_cloud: | ||
update_cloud.return_value = {"result": True} | ||
sync_all_isp_to_cmdb_periodic_task() | ||
call_args = update_cloud.call_args | ||
bk_cloud_vendor_scope = [str(bk_cloud_vendor) for bk_cloud_vendor in range(1, 17)] | ||
self.assertIn(call_args[0][0]["bk_cloud_vendor"], bk_cloud_vendor_scope) | ||
self.assertNotIn(1, call_args[0][0]) |