Skip to content

Commit

Permalink
fix: fix release get hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
Han-Ya-Jun committed Oct 30, 2023
1 parent f44dc03 commit 738bbf2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.
#
from django.conf import settings
from django.db import transaction
from django.utils.decorators import method_decorator
from drf_yasg.utils import swagger_auto_schema
Expand All @@ -27,6 +28,8 @@
from apigateway.common.permissions import GatewayRelatedAppPermission
from apigateway.core.models import Release, ResourceVersion, Stage
from apigateway.utils.access_token import get_user_access_token_from_request
from apigateway.utils.exception import LockTimeout
from apigateway.utils.redis_utils import Lock
from apigateway.utils.responses import V1FailJsonResponse, V1OKJsonResponse

from .serializers import (
Expand Down Expand Up @@ -104,12 +107,13 @@ def post(self, request, gateway_name: str, *args, **kwargs):
slz.is_valid(raise_exception=True)

data = slz.validated_data
gateway_id = data["gateway"].id
stage_ids = data["stage_ids"]
resource_version = ResourceVersion.objects.get_object_fields(data["resource_version_id"])

# 如果环境已发布某版本,则不重复发布,且计入此次已发布环境
data["stage_ids"] = Release.objects.get_stage_ids_unreleased_the_version(
gateway_id=data["gateway"].id,
gateway_id=gateway_id,
stage_ids=stage_ids,
resource_version_id=data["resource_version_id"],
)
Expand All @@ -127,15 +131,22 @@ def post(self, request, gateway_name: str, *args, **kwargs):
for stage_id in data["stage_ids"]:
releaser = Releaser(access_token=get_user_access_token_from_request(request))
try:
releaser.release(
request.gateway,
stage_id,
data["resource_version_id"],
data["comment"],
request.user.username,
)
with Lock(
f"{gateway_id}_{stage_id}",
timeout=settings.REDIS_PUBLISH_LOCK_TIMEOUT,
try_get_times=settings.REDIS_PUBLISH_LOCK_RETRY_GET_TIMES,
):
releaser.release(
request.gateway,
stage_id,
data["resource_version_id"],
data["comment"],
request.user.username,
)
except LockTimeout as err:
return V1FailJsonResponse(str(err))

except ReleaseError as err:
# 因设置了 transaction,views 中不能直接抛出异常,否则,将导致数据不会写入 db
return V1FailJsonResponse(str(err))

return V1OKJsonResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def _resources_plugins(self) -> Dict[int, List[PluginData]]:
def get_resources_upstream(self, resource_proxy: Dict[str, Any], backend_id: int):
return resource_proxy.get("upstreams")

def get_upstream_host(self, upstream: Dict[str, Any]) -> str:

return upstream["host"]


class ReleaseDataV2(ReleaseData):
@cached_property
Expand Down Expand Up @@ -184,6 +188,9 @@ def get_resources_upstream(self, resource_proxy: Dict[str, Any], backend_id: int
.first()
)

def get_upstream_host(self, upstream: Dict[str, Any]) -> str:
return upstream["scheme"] + "://" + upstream["host"]

@property
def _resources_plugins(self) -> Dict[int, List[PluginData]]:
resource_id_to_plugins: Dict[int, List[PluginData]] = defaultdict(list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def _convert_http_resource_upstream(self, resource_proxy: Dict[str, Any], backen
)

for i in upstreams.get("hosts", []):
url_info = UrlInfo(i["host"])
url_info = UrlInfo(self._release_data.get_upstream_host(i))
upstream.scheme = UpstreamSchemeEnum(url_info.scheme)
upstream.nodes.append(UpstreamNode(host=url_info.domain, port=url_info.port, weight=i.get("weight", 1)))

Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/apigateway/apigateway/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ def object_display(self):
if not self.version:
return f"{self.name}({self.title})"

return f"{self.version}({self.title})"
return f"{self.version}"

@property
def is_schema_v2(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class TestReleaseHistoryOutputSLZ:
def test_to_representation(self):
gateway = G(Gateway)
stage = G(Stage, gateway=gateway)
resource_version = G(ResourceVersion, gateway=gateway, name="t1", version="1.0.0", title="测试", comment="test1")
resource_version = G(ResourceVersion, gateway=gateway, name="t1", version="1.0.0", comment="test1")
release_history = G(
ReleaseHistory,
gateway=gateway,
Expand Down Expand Up @@ -183,7 +183,7 @@ def test_to_representation(self):
"stage": {"id": stage.id, "name": stage.name},
"created_time": dummy_time.str,
"created_by": release_history.created_by,
"resource_version_display": "1.0.0(测试)",
"resource_version_display": "1.0.0",
"status": f"{event_1.status}",
"source": release_history.source,
"duration": (event_1.created_time - release_history.created_time).total_seconds(),
Expand Down

0 comments on commit 738bbf2

Please sign in to comment.