From 4a0fce7da6d47a7e9094c5efd1769d3d9395b540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Mon, 19 Jun 2023 12:06:27 +0200 Subject: [PATCH] feat: add deprecation field to ServerType (#192) --- docs/api.deprecation.rst | 5 +++++ docs/api.rst | 9 ++++++++ hcloud/deprecation/__init__.py | 0 hcloud/deprecation/domain.py | 31 ++++++++++++++++++++++++++ hcloud/server_types/domain.py | 11 ++++++++- tests/unit/server_types/conftest.py | 17 ++++++++++++++ tests/unit/server_types/test_client.py | 11 ++++++++- 7 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 docs/api.deprecation.rst create mode 100644 hcloud/deprecation/__init__.py create mode 100644 hcloud/deprecation/domain.py diff --git a/docs/api.deprecation.rst b/docs/api.deprecation.rst new file mode 100644 index 0000000..844a60c --- /dev/null +++ b/docs/api.deprecation.rst @@ -0,0 +1,5 @@ +Deprecation Info +================== + +.. autoclass:: hcloud.deprecation.domain.DeprecationInfo + :members: diff --git a/docs/api.rst b/docs/api.rst index e999a32..831f932 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -27,3 +27,12 @@ Exceptions .. autoclass:: hcloud.actions.domain.ActionTimeoutException :members: + +Other +------------- + +.. toctree:: + :maxdepth: 3 + + api.helpers + api.deprecation diff --git a/hcloud/deprecation/__init__.py b/hcloud/deprecation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hcloud/deprecation/domain.py b/hcloud/deprecation/domain.py new file mode 100644 index 0000000..da96774 --- /dev/null +++ b/hcloud/deprecation/domain.py @@ -0,0 +1,31 @@ +from dateutil.parser import isoparse + +from hcloud.core.domain import BaseDomain + + +class DeprecationInfo(BaseDomain): + """Describes if, when & how the resources was deprecated. If this field is set to ``None`` the resource is not + deprecated. If it has a value, it is considered deprecated. + + :param announced: datetime + Date of when the deprecation was announced. + :param unavailable_after: datetime + After the time in this field, the resource will not be available from the general listing endpoint of the + resource type, and it can not be used in new resources. For example, if this is an image, you can not create + new servers with this image after the mentioned date. + """ + + __slots__ = ( + "announced", + "unavailable_after", + ) + + def __init__( + self, + announced=None, + unavailable_after=None, + ): + self.announced = isoparse(announced) if announced else None + self.unavailable_after = ( + isoparse(unavailable_after) if unavailable_after else None + ) diff --git a/hcloud/server_types/domain.py b/hcloud/server_types/domain.py index 5293b8d..d584312 100644 --- a/hcloud/server_types/domain.py +++ b/hcloud/server_types/domain.py @@ -1,4 +1,5 @@ from hcloud.core.domain import BaseDomain, DomainIdentityMixin +from hcloud.deprecation.domain import DeprecationInfo class ServerType(BaseDomain, DomainIdentityMixin): @@ -25,7 +26,10 @@ class ServerType(BaseDomain, DomainIdentityMixin): :param architecture: string Architecture of cpu. Choices: `x86`, `arm` :param deprecated: bool - True if server type is deprecated + True if server type is deprecated. This field is deprecated. Use `deprecation` instead. + :param deprecation: :class:`DeprecationInfo `, None + Describes if, when & how the resources was deprecated. If this field is set to None the resource is not + deprecated. If it has a value, it is considered deprecated. :param included_traffic: int Free traffic per month in bytes """ @@ -42,6 +46,7 @@ class ServerType(BaseDomain, DomainIdentityMixin): "cpu_type", "architecture", "deprecated", + "deprecation", "included_traffic", ) @@ -58,6 +63,7 @@ def __init__( cpu_type=None, architecture=None, deprecated=None, + deprecation=None, included_traffic=None, ): self.id = id @@ -71,4 +77,7 @@ def __init__( self.cpu_type = cpu_type self.architecture = architecture self.deprecated = deprecated + self.deprecation = ( + DeprecationInfo.from_dict(deprecation) if deprecation is not None else None + ) self.included_traffic = included_traffic diff --git a/tests/unit/server_types/conftest.py b/tests/unit/server_types/conftest.py index 559e8d2..5735f0e 100644 --- a/tests/unit/server_types/conftest.py +++ b/tests/unit/server_types/conftest.py @@ -28,6 +28,11 @@ def server_type_response(): "cpu_type": "shared", "architecture": "x86", "included_traffic": 21990232555520, + "deprecated": True, + "deprecation": { + "announced": "2023-06-01T00:00:00+00:00", + "unavailable_after": "2023-09-01T00:00:00+00:00", + }, } } @@ -60,6 +65,11 @@ def two_server_types_response(): "cpu_type": "shared", "architecture": "x86", "included_traffic": 21990232555520, + "deprecated": True, + "deprecation": { + "announced": "2023-06-01T00:00:00+00:00", + "unavailable_after": "2023-09-01T00:00:00+00:00", + }, }, { "id": 2, @@ -96,6 +106,8 @@ def two_server_types_response(): "cpu_type": "shared", "architecture": "x86", "included_traffic": 21990232555520, + "deprecated": False, + "deprecation": None, }, ] } @@ -129,6 +141,11 @@ def one_server_types_response(): "cpu_type": "shared", "architecture": "x86", "included_traffic": 21990232555520, + "deprecated": True, + "deprecation": { + "announced": "2023-06-01T00:00:00+00:00", + "unavailable_after": "2023-09-01T00:00:00+00:00", + }, } ] } diff --git a/tests/unit/server_types/test_client.py b/tests/unit/server_types/test_client.py index 182dd33..442da4b 100644 --- a/tests/unit/server_types/test_client.py +++ b/tests/unit/server_types/test_client.py @@ -1,3 +1,4 @@ +from datetime import datetime, timezone from unittest import mock import pytest @@ -5,7 +6,7 @@ from hcloud.server_types.client import BoundServerType, ServerTypesClient -class TestBoundIso: +class TestBoundServerType: @pytest.fixture() def bound_server_type(self, hetzner_client): return BoundServerType(client=hetzner_client.server_types, data=dict(id=14)) @@ -24,6 +25,14 @@ def test_bound_server_type_init(self, server_type_response): assert bound_server_type.storage_type == "local" assert bound_server_type.cpu_type == "shared" assert bound_server_type.architecture == "x86" + assert bound_server_type.deprecated is True + assert bound_server_type.deprecation is not None + assert bound_server_type.deprecation.announced == datetime( + 2023, 6, 1, tzinfo=timezone.utc + ) + assert bound_server_type.deprecation.unavailable_after == datetime( + 2023, 9, 1, tzinfo=timezone.utc + ) assert bound_server_type.included_traffic == 21990232555520