diff --git a/ops-scripts/tencent_operator.py b/ops-scripts/tencent_operator.py index ab2bf73..40fd30e 100755 --- a/ops-scripts/tencent_operator.py +++ b/ops-scripts/tencent_operator.py @@ -352,9 +352,7 @@ class CloudOperator: "Weight": int(weight), } - location_id = str(clb_cfg.get("location_id", "")).strip() - domain = str(clb_cfg.get("domain", "")).strip() - url = str(clb_cfg.get("url", "")).strip() + location_id, domain, url = self.resolve_gateway_rule(clb_cfg) if location_id: payload["LocationId"] = location_id elif domain and url: @@ -367,6 +365,49 @@ class CloudOperator: request_id = sdk_body(resp)["RequestId"] self.wait_for_clb_task(request_id) + def resolve_gateway_rule(self, clb_cfg: dict[str, Any]) -> tuple[str, str, str]: + location_id = str(clb_cfg.get("location_id", "")).strip() + domain = str(clb_cfg.get("domain", "")).strip() + url = str(clb_cfg.get("url", "")).strip() + if location_id or (domain and url): + return location_id, domain, url + + req = clb_models.DescribeListenersRequest() + req.from_json_string( + json.dumps( + { + "LoadBalancerId": str(clb_cfg["load_balancer_id"]), + "ListenerIds": [str(clb_cfg["listener_id"])], + } + ) + ) + resp = self.clb.DescribeListeners(req) + body = sdk_body(resp) + listeners = body.get("Listeners", []) + if not listeners: + raise RuntimeError( + f"CLB listener {clb_cfg['listener_id']} not found under load balancer {clb_cfg['load_balancer_id']}" + ) + + listener = listeners[0] + protocol = str(listener.get("Protocol", "")).upper() + if protocol not in {"HTTP", "HTTPS"}: + return "", "", "" + + rules = listener.get("Rules") or [] + if len(rules) == 1: + rule = rules[0] + return ( + str(rule.get("LocationId", "")).strip(), + str(rule.get("Domain", "")).strip(), + str(rule.get("Url", "")).strip(), + ) + + raise RuntimeError( + f"listener {clb_cfg['listener_id']} is {protocol} and has {len(rules)} rules; " + "set gateway.clb.location_id or gateway.clb.domain+url in config/prod.yaml" + ) + def wait_for_clb_task(self, request_id: str) -> None: deadline = time.time() + 300 while time.time() < deadline: