-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 向cmdb同步云区域服务商 (closed #2386) #2414
Open
Huayeaaa
wants to merge
1
commit into
TencentBlueKing:v2.4.8-dev
Choose a base branch
from
Huayeaaa:_V2.4.X/dev_issue#2386
base: v2.4.8-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() |
52 changes: 52 additions & 0 deletions
52
apps/node_man/migrations/0084_update_isp_and_accesspoint_regionid_cityid.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,52 @@ | ||
# -*- 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.db import migrations | ||
|
||
|
||
def update_isp_and_ap_region_city_id(apps, schema_editor): | ||
"""更新全局配置中的ISP和存量接入点的region_id和city_id""" | ||
isp_list = [ | ||
{"isp": "PrivateCloud", "isp_name": "企业私有云"}, | ||
{"isp": "AWS", "isp_name": "亚马逊云"}, | ||
{"isp": "Azure", "isp_name": "微软云"}, | ||
{"isp": "GoogleCloud", "isp_name": "谷歌云"}, | ||
{"isp": "SalesForce", "isp_name": "SalesForce"}, | ||
{"isp": "OracleCloud", "isp_name": "Oracle Cloud"}, | ||
{"isp": "IBMCloud", "isp_name": "IBM Cloud"}, | ||
{"isp": "AlibabaCloud", "isp_name": "阿里云"}, | ||
{"isp": "TencentCloud", "isp_name": "腾讯云"}, | ||
{"isp": "ECloud", "isp_name": "中国电信"}, | ||
{"isp": "UCloud", "isp_name": "UCloud"}, | ||
{"isp": "MOS", "isp_name": "美团云"}, | ||
{"isp": "KSyun", "isp_name": "金山云"}, | ||
{"isp": "BaiduCloud", "isp_name": "百度云"}, | ||
{"isp": "HuaweiCloud", "isp_name": "华为云"}, | ||
{"isp": "capitalonline", "isp_name": "首都云"}, | ||
{"isp": "TencentPrivateCloud", "isp_name": "腾讯自研云"}, | ||
{"isp": "Zenlayer", "isp_name": "Zenlayer"}, | ||
] | ||
# 创建or更新ISP | ||
GlobalSettings = apps.get_model("node_man", "GlobalSettings") | ||
GlobalSettings.objects.update_or_create(defaults={"v_json": isp_list}, **{"key": "isp"}) | ||
# 更新存量接入点的region_id和city_id | ||
AccessPoint = apps.get_model("node_man", "AccessPoint") | ||
AccessPoint.objects.filter(region_id="test").update(region_id="default") | ||
AccessPoint.objects.filter(city_id="test").update(city_id="default") | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("node_man", "0083_subscription_operate_info"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_isp_and_ap_region_city_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
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内置云区域不更新,默认为直连区域与未分配管控区域,如有其他内置云区域通过GlobalSettings配置 | ||
cmdb_internal_cloud_ids = GlobalSettings.get_config( | ||
key=GlobalSettings.KeyEnum.CMDB_INTERNAL_CLOUD_IDS.value, | ||
default=[constants.DEFAULT_CLOUD, constants.UNASSIGNED_CLOUD_ID], | ||
) | ||
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="TencentCloud") | ||
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, 19)] | ||
self.assertIn(call_args[0][0]["bk_cloud_vendor"], bk_cloud_vendor_scope) | ||
self.assertNotIn(1, call_args[0][0]) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
新文件的开源信息