Skip to content

Commit

Permalink
fix(biz/backend): 修复后端服务未关联资源但触发环境发布问题 (TencentBlueKing#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
F-cq authored and Carlmac committed Oct 30, 2024
1 parent 7c66564 commit 67ec88f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/dashboard/apigateway/apigateway/biz/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@ def update(backend: Backend, data: Dict[str, Any], updated_by: str) -> Tuple[Bac

backend_configs = []
now = now_datetime()
resource_count = Proxy.objects.filter(backend_id=backend.id).count()

for config in data["configs"]:
backend_config = stage_configs[config["stage_id"]]
new_config = {key: value for key, value in config.items() if key != "stage_id"}
if new_config == backend_config.config:
continue
updated_stage_ids.append(config["stage_id"])
if resource_count:
updated_stage_ids.append(config["stage_id"])
backend_config.config = new_config
backend_config.updated_by = updated_by
backend_config.updated_time = now
Expand All @@ -86,12 +89,13 @@ def update(backend: Backend, data: Dict[str, Any], updated_by: str) -> Tuple[Bac
BackendConfig.objects.bulk_update(backend_configs, fields=["config", "updated_by", "updated_time"])

# 触发变更的stage的发布流程
for backend_config in backend_configs:
gateway_id = backend.gateway.id
for stage_id in updated_stage_ids:
trigger_gateway_publish(
PublishSourceEnum.BACKEND_UPDATE,
updated_by,
backend_config.gateway_id,
backend_config.stage_id,
gateway_id,
stage_id,
)
return backend, updated_stage_ids

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_update(self, request_view, fake_stage):
result = response.json()
assert result["data"] == {
"bound_stages": [{"id": fake_stage.id, "name": fake_stage.name}],
"updated_stages": [{"id": fake_stage.id, "name": fake_stage.name}],
"updated_stages": [],
}

def test_delete(self, request_view, fake_stage):
Expand Down
21 changes: 19 additions & 2 deletions src/dashboard/apigateway/apigateway/tests/biz/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
# to the current version of the project delivered to anyone in the future.
#

from django_dynamic_fixture import G

from apigateway.biz.backend import BackendHandler
from apigateway.core.models import Backend, BackendConfig
from apigateway.core.models import Backend, BackendConfig, Proxy, Resource


class TestBackendHandler:
Expand Down Expand Up @@ -76,9 +77,24 @@ def test_update(self, fake_stage):
)

backend = Backend.objects.filter(gateway=fake_stage.gateway, name="backend-test").first()

r = G(
Resource,
name="backend-test",
gateway=fake_stage.gateway,
method="/test/",
path="/test/"
)

G(
Proxy,
resource=r,
backend=backend
)

assert backend

BackendHandler.update(
backend, updated_stage_ids = BackendHandler.update(
backend,
{
"gateway": fake_stage.gateway,
Expand All @@ -98,6 +114,7 @@ def test_update(self, fake_stage):
"admin",
)

assert updated_stage_ids != []
assert backend.name == "backend-update"
assert backend.description == "update"

Expand Down

0 comments on commit 67ec88f

Please sign in to comment.