Skip to content

Commit 058a2c2

Browse files
author
Ilyas Gasanov
committed
[DOP-24139] Add namespace create/delete methods
1 parent db5181f commit 058a2c2

File tree

5 files changed

+88
-11
lines changed

5 files changed

+88
-11
lines changed

conftest.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
KeyValueIntHWM,
1616
)
1717
from horizon.client.auth import LoginPassword
18-
from horizon.commons.schemas.v1 import NamespaceCreateRequestV1
18+
from horizon.commons.schemas.v1 import (
19+
NamespaceCreateRequestV1,
20+
NamespacePaginateQueryV1,
21+
)
1922
from packaging.version import Version
2023

2124
from horizon_hwm_store import HorizonHWMStore
@@ -149,6 +152,11 @@ def file_with_mtime(mtime: datetime) -> Path:
149152

150153

151154
@pytest.fixture(params=HWMS_WITH_VALUE)
155+
def hwm_new_values(request):
156+
return request.param
157+
158+
159+
@pytest.fixture(params=[HWMS_WITH_VALUE[0]])
152160
def hwm_new_value(request):
153161
return request.param
154162

@@ -164,16 +172,13 @@ def hwm_store():
164172

165173
@pytest.fixture(scope="module")
166174
def ensure_namespace():
167-
from requests.exceptions import HTTPError
168175

169176
store = HorizonHWMStore(
170177
api_url=HORIZON_URL,
171178
auth=LoginPassword(login=HORIZON_USER, password=HORIZON_PASSWORD),
172179
namespace=HORIZON_NAMESPACE,
173180
)
174181

175-
try:
176-
store.client.create_namespace(NamespaceCreateRequestV1(name=HORIZON_NAMESPACE)) # noqa: WPS437
177-
except HTTPError:
178-
# exception: 409 Client Error: Conflict for url: http://horizon/v1/namespaces/ - namespace already exists
179-
pass
182+
namespace_query = NamespacePaginateQueryV1(name=HORIZON_NAMESPACE)
183+
if not store.client.paginate_namespaces(query=namespace_query).items:
184+
store.client.create_namespace(NamespaceCreateRequestV1(name=HORIZON_NAMESPACE))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``create_namespace`` and ``delete_namespace`` methods

docs/horizon-hwm-store.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ Horizon HWM Store
66
.. currentmodule:: horizon_hwm_store.horizon_hwm_store
77

88
.. autoclass:: HorizonHWMStore
9-
:members: get_hwm, set_hwm
9+
:members: get_hwm, set_hwm, create_namespace, delete_namespace

horizon_hwm_store/horizon_hwm_store.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
from etl_entities.hwm_store import BaseHWMStore, register_hwm_store_class
99
from horizon.client.auth import LoginPassword
1010
from horizon.client.sync import HorizonClientSync, RetryConfig, TimeoutConfig
11+
from horizon.commons.exceptions import EntityNotFoundError
1112
from horizon.commons.schemas.v1 import (
1213
HWMCreateRequestV1,
1314
HWMPaginateQueryV1,
1415
HWMUpdateRequestV1,
16+
NamespaceCreateRequestV1,
1517
NamespacePaginateQueryV1,
1618
)
1719

@@ -213,6 +215,57 @@ def check(self) -> HorizonHWMStore:
213215
self._get_namespace_id()
214216
return self
215217

218+
def create_namespace(self, name: str) -> None:
219+
"""
220+
Create a namespace.
221+
222+
Parameters
223+
----------
224+
name : str
225+
The name of the namespace to create.
226+
227+
Raises
228+
------
229+
:obj:`EntityAlreadyExistsError <horizon.commons.exceptions.entity.EntityAlreadyExistsError>`
230+
Namespace with the same name already exists
231+
232+
Examples
233+
--------
234+
.. code:: python
235+
236+
hwm_store.create_namespace(namespace_unique_name)
237+
"""
238+
self.client.create_namespace(NamespaceCreateRequestV1(name=name))
239+
240+
def delete_namespace(self, name: str) -> None:
241+
"""
242+
Delete an existing namespace.
243+
244+
Parameters
245+
----------
246+
name : str
247+
The name of the namespace to delete.
248+
249+
Raises
250+
------
251+
:obj:`EntityNotFoundError <horizon.commons.exceptions.entity.EntityNotFoundError>`
252+
Namespace not found
253+
:obj:`PermissionDeniedError <horizon.commons.exceptions.permission.PermissionDeniedError>`
254+
Permission denied for performing the requested action.
255+
256+
Examples
257+
--------
258+
.. code:: python
259+
260+
hwm_store.delete_namespace(namespace_unique_name)
261+
"""
262+
namespace_query = NamespacePaginateQueryV1(name=name)
263+
namespaces = self.client.paginate_namespaces(query=namespace_query).items
264+
if not namespaces:
265+
raise EntityNotFoundError("Namespace", "name", name)
266+
267+
self.client.delete_namespace(namespaces[0].id)
268+
216269
# LoginPassword, RetryConfig and TimeoutConfig can be inherited from Pydantic v2 BaseModel
217270
# which is detected by Pydantic v1 as arbitrary type. So we need to parse them manually.
218271
@validator("auth", pre=True)

tests/tests_integration/test_horizon_hwm_store_integration.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pytest
44
from horizon.client.auth import LoginPassword
5-
from requests.exceptions import ConnectionError
65

76
from horizon_hwm_store import HorizonHWMStore
87

@@ -14,8 +13,8 @@
1413
HORIZON_NAMESPACE = os.environ.get("HORIZON_NAMESPACE")
1514

1615

17-
def test_hwm_store_integration(hwm_store, hwm_new_value, ensure_namespace):
18-
hwm, new_value = hwm_new_value
16+
def test_hwm_store_integration(hwm_store, hwm_new_values, ensure_namespace):
17+
hwm, new_value = hwm_new_values
1918
assert hwm_store.get_hwm(hwm.name) is None
2019

2120
hwm_store.set_hwm(hwm)
@@ -65,3 +64,22 @@ def test_hwm_store_unexisting_namespace(hwm_new_value):
6564

6665
with pytest.raises(RuntimeError, match=error_msg):
6766
store.set_hwm(hwm)
67+
68+
69+
def test_hwm_store_creates_namespace():
70+
namespace_name = "new_namespace"
71+
store = HorizonHWMStore(
72+
api_url=HORIZON_URL,
73+
auth=LoginPassword(login=HORIZON_USER, password=HORIZON_PASSWORD),
74+
namespace=namespace_name,
75+
)
76+
error_msg = "Namespace 'new_namespace' not found. Please create it before using."
77+
78+
with pytest.raises(RuntimeError, match=error_msg):
79+
store.get_hwm("some_hwm_name")
80+
81+
try: # noqa: WPS501
82+
store.create_namespace(namespace_name)
83+
assert store.get_hwm("some_hwm_name") is None
84+
finally:
85+
store.delete_namespace(namespace_name)

0 commit comments

Comments
 (0)