修复 CLB SDK 返回体解析
This commit is contained in:
parent
463d45614d
commit
a059e76742
@ -101,7 +101,13 @@ def call_clb(method_name: str, payload: dict[str, Any]) -> dict[str, Any]:
|
|||||||
request = request_class()
|
request = request_class()
|
||||||
request.from_json_string(json.dumps(payload))
|
request.from_json_string(json.dumps(payload))
|
||||||
response = invoke_clb(lambda: getattr(client, method_name)(request), method_name)
|
response = invoke_clb(lambda: getattr(client, method_name)(request), method_name)
|
||||||
return dict(json.loads(response.to_json_string()).get("Response") or {})
|
payload = json.loads(response.to_json_string())
|
||||||
|
# 腾讯云 Python SDK 在不同产品/版本里有两种常见结构:
|
||||||
|
# 1. 顶层直接就是业务字段;2. 顶层包一层 Response。
|
||||||
|
# 这里同时兼容两种形态,避免把真实返回体误吃成空字典。
|
||||||
|
if isinstance(payload, dict) and isinstance(payload.get("Response"), dict):
|
||||||
|
return dict(payload.get("Response") or {})
|
||||||
|
return dict(payload or {})
|
||||||
|
|
||||||
|
|
||||||
def wait_clb_task(task_id: str, *, timeout_seconds: int = GATEWAY_CLB_TASK_TIMEOUT_SECONDS) -> dict[str, Any]:
|
def wait_clb_task(task_id: str, *, timeout_seconds: int = GATEWAY_CLB_TASK_TIMEOUT_SECONDS) -> dict[str, Any]:
|
||||||
|
|||||||
@ -1,10 +1,46 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from monitors.yumi.service_release import deploy_runtime_services_payload
|
from monitors.yumi.service_release import deploy_runtime_services_payload
|
||||||
from monitors.yumi.tencent_clb import collect_service_bindings, prepare_gateway_release_clb_snapshot
|
from monitors.yumi.tencent_clb import call_clb, collect_service_bindings, prepare_gateway_release_clb_snapshot
|
||||||
|
|
||||||
|
|
||||||
|
class GatewayClbClientTests(unittest.TestCase):
|
||||||
|
@patch("monitors.yumi.tencent_clb.invoke_clb")
|
||||||
|
@patch("monitors.yumi.tencent_clb.build_clb_client")
|
||||||
|
def test_call_clb_accepts_top_level_payload_shape(
|
||||||
|
self,
|
||||||
|
build_clb_client_mock,
|
||||||
|
invoke_clb_mock,
|
||||||
|
) -> None:
|
||||||
|
class DummyRequest:
|
||||||
|
def from_json_string(self, text: str) -> None:
|
||||||
|
self.text = text
|
||||||
|
|
||||||
|
class DummyModels:
|
||||||
|
DescribeTargetsRequest = DummyRequest
|
||||||
|
|
||||||
|
class DummyResponse:
|
||||||
|
def to_json_string(self) -> str:
|
||||||
|
return json.dumps({"Listeners": [{"ListenerId": "lbl-http"}], "RequestId": "req-1"})
|
||||||
|
|
||||||
|
class DummyClient:
|
||||||
|
def DescribeTargets(self, request):
|
||||||
|
return request
|
||||||
|
|
||||||
|
build_clb_client_mock.return_value = (DummyClient(), "")
|
||||||
|
invoke_clb_mock.return_value = DummyResponse()
|
||||||
|
|
||||||
|
import tencentcloud.clb.v20180317 as clb_pkg
|
||||||
|
|
||||||
|
with patch.object(clb_pkg, "models", DummyModels(), create=True):
|
||||||
|
payload = call_clb("DescribeTargets", {"LoadBalancerId": "lb-test"})
|
||||||
|
|
||||||
|
self.assertEqual(payload["RequestId"], "req-1")
|
||||||
|
self.assertEqual(payload["Listeners"][0]["ListenerId"], "lbl-http")
|
||||||
|
|
||||||
|
|
||||||
class GatewayClbDiscoveryTests(unittest.TestCase):
|
class GatewayClbDiscoveryTests(unittest.TestCase):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user