Skip to content
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

fix(biz/backend): 修复后端服务未关联资源但触发环境发布问题 #1078

Merged
merged 7 commits into from
Oct 29, 2024
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"])
F-cq marked this conversation as resolved.
Show resolved Hide resolved
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
Loading