Skip to content

Commit 9ca39c3

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

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

conftest.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
KeyValueIntHWM,
1616
)
1717
from horizon.client.auth import LoginPassword
18-
from horizon.commons.schemas.v1 import NamespaceCreateRequestV1
1918
from packaging.version import Version
2019

2120
from horizon_hwm_store import HorizonHWMStore
@@ -149,6 +148,11 @@ def file_with_mtime(mtime: datetime) -> Path:
149148

150149

151150
@pytest.fixture(params=HWMS_WITH_VALUE)
151+
def hwm_new_values(request):
152+
return request.param
153+
154+
155+
@pytest.fixture(params=[HWMS_WITH_VALUE[0]])
152156
def hwm_new_value(request):
153157
return request.param
154158

@@ -164,16 +168,10 @@ def hwm_store():
164168

165169
@pytest.fixture(scope="module")
166170
def ensure_namespace():
167-
from requests.exceptions import HTTPError
168171

169172
store = HorizonHWMStore(
170173
api_url=HORIZON_URL,
171174
auth=LoginPassword(login=HORIZON_USER, password=HORIZON_PASSWORD),
172175
namespace=HORIZON_NAMESPACE,
173176
)
174-
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
177+
store.create_namespace(HORIZON_NAMESPACE)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``create_namespace`` method

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

horizon_hwm_store/horizon_hwm_store.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
HWMCreateRequestV1,
1313
HWMPaginateQueryV1,
1414
HWMUpdateRequestV1,
15+
NamespaceCreateRequestV1,
1516
NamespacePaginateQueryV1,
1617
)
1718

@@ -33,7 +34,7 @@ class HorizonHWMStore(BaseHWMStore):
3334
3435
.. warning::
3536
36-
It is required to create namespace in Horizon BEFORE using this class.
37+
It is required to create a namespace BEFORE working with HWMs.
3738
3839
Parameters
3940
----------
@@ -213,6 +214,24 @@ def check(self) -> HorizonHWMStore:
213214
self._get_namespace_id()
214215
return self
215216

217+
def create_namespace(self, name: str) -> None:
218+
"""
219+
Create a namespace.
220+
221+
Parameters
222+
----------
223+
name : str
224+
The name of the namespace to create.
225+
226+
Examples
227+
--------
228+
.. code:: python
229+
230+
hwm_store.create_namespace(namespace_unique_name)
231+
"""
232+
if not self.client.paginate_namespaces(query=NamespacePaginateQueryV1(name=name)).items:
233+
self.client.create_namespace(NamespaceCreateRequestV1(name=name))
234+
216235
# LoginPassword, RetryConfig and TimeoutConfig can be inherited from Pydantic v2 BaseModel
217236
# which is detected by Pydantic v1 as arbitrary type. So we need to parse them manually.
218237
@validator("auth", pre=True)

tests/tests_integration/test_horizon_hwm_store_integration.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
HORIZON_NAMESPACE = os.environ.get("HORIZON_NAMESPACE")
1515

1616

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

2121
hwm_store.set_hwm(hwm)
@@ -65,3 +65,19 @@ def test_hwm_store_unexisting_namespace(hwm_new_value):
6565

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

0 commit comments

Comments
 (0)