Skip to content
This repository was archived by the owner on Nov 14, 2025. It is now read-only.

Commit f7cfbfc

Browse files
committed
Added tenant creation
1 parent 94d30e0 commit f7cfbfc

6 files changed

Lines changed: 44 additions & 5 deletions

File tree

RELEASE-NOTES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# ngsildclient 0.5.2
2+
## December 1, 2022
3+
4+
- Allow to create tenant (auto-create is default for the sync Client)
5+
16
# ngsildclient 0.5.1
27
## November 24, 2022
38

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ngsildclient"
3-
version = "0.5.1"
3+
version = "0.5.2"
44
description = "A Python library that helps building NGSI-LD entities and interacting with a NGSI-LD Context Broker"
55
license = "Apache-2.0"
66
readme="README.md"

src/ngsildclient/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import logging
1414
import sys
1515

16-
__version__ = "0.5.1"
16+
__version__ = "0.5.2"
1717

1818
from ngsildclient.settings import globalsettings as settings
1919
from .utils import iso8601, is_interactive

src/ngsildclient/api/asyn/client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from .contexts import Contexts
3838
from .subscriptions import Subscriptions
3939
from .temporal import Temporal
40+
from ..exceptions import NgsiClientTooManyResultsError
4041

4142
if TYPE_CHECKING:
4243
from ngsildclient.model.constants import EntityOrId
@@ -629,6 +630,18 @@ async def flush_all(self) -> None:
629630
await self.purge(type)
630631
await self.contexts.cleanup()
631632

633+
async def create_tenant(self, tenant: str) -> httpx.Response:
634+
payload = {
635+
"id": f"urn:ngsi-ld:__NGSILD-Tenant__:{tenant}",
636+
"type": "__NGSILD-Tenant__",
637+
"@context": ["https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld"],
638+
}
639+
return await self.client.post(
640+
f"{self.url}/{ENDPOINT_BATCH}/upsert/",
641+
json=[payload],
642+
headers={"Content-Type": "application/ld+json", "NGSILD-Tenant": tenant},
643+
)
644+
632645
# below the context manager methods
633646

634647
async def __aenter__(self):

src/ngsildclient/api/client.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def __init__(
9595
secure: bool = False,
9696
useragent: str = UA,
9797
tenant: str = None,
98+
tenant_autocreate: bool = True,
9899
overwrite: bool = False,
99100
ignore_errors: bool = False,
100101
proxy: str = None,
@@ -121,6 +122,8 @@ def __init__(
121122
the User Agent string sent in the HTTP headers, by default UA
122123
tenant : str, optional
123124
the tenant string in case you make use of multi-tenancy, by default None
125+
tenant_autocreate : boolean, optional
126+
creates the tenant if not exists, default is True
124127
overwrite : bool, optional
125128
if set create() will behave like upsert(), by default False
126129
ignore_errors : bool, optional
@@ -144,6 +147,7 @@ def __init__(
144147
self.basepath = f"{self.url}/{NGSILD_PATH}"
145148
self.useragent = useragent
146149
self.tenant = tenant
150+
self.tenant_autocreate = tenant_autocreate
147151
self.overwrite = overwrite
148152
self.ignore_errors = ignore_errors
149153
self.proxy = proxy
@@ -233,7 +237,12 @@ def is_connected(self, raise_for_disconnected=False) -> bool:
233237
}, # overrides session headers
234238
params=params,
235239
)
240+
241+
if not r.ok and self.tenant and self.tenant_autocreate:
242+
r = self.create_tenant(self.tenant)
243+
236244
r.raise_for_status()
245+
237246
except Exception as e:
238247
if is_interactive():
239248
self.console.print(str(e))
@@ -708,6 +717,16 @@ def flush_all(self) -> None:
708717
self.purge(type)
709718
self.contexts.cleanup()
710719

720+
def create_tenant(self, tenant: str) -> Response:
721+
payload = {
722+
"id": f"urn:ngsi-ld:__NGSILD-Tenant__:{tenant}",
723+
"type": "__NGSILD-Tenant__",
724+
"@context": ["https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld"],
725+
}
726+
return self.session.post(
727+
f"{self.url}/{ENDPOINT_BATCH}/upsert/", json=[payload], headers={"NGSILD-Tenant": tenant}
728+
)
729+
711730
def guess_vendor(self) -> tuple[Vendor, Version]:
712731
"""Try to guess the Context Broker vendor.
713732
@@ -790,10 +809,12 @@ def _broker_version_java_spring(self) -> Optional[Tuple[Vendor, str]]:
790809
return None
791810

792811
def _welcome_message(self) -> str:
793-
return f"[green]Connected[/] to Context Broker at [blue3]{self.hostname}:{self.port}[/] | vendor=[blue3]{self.broker.vendor.value}[/] | version=[blue3]{self.broker.version}[/]"
812+
tenant = self.tenant if self.tenant else "N/A"
813+
return f"[green]Connected[/] to Context Broker at [blue3]{self.hostname}:{self.port}[/] | tenant=[blue3]{tenant}[/] | vendor=[blue3]{self.broker.vendor.value}[/] | version=[blue3]{self.broker.version}[/]"
794814

795815
def _fail_message(self) -> str:
796-
return f"[red3]Failed[/] to connect to Context Broker at [blue3]{self.hostname}:{self.port}[/]"
816+
tenant = self.tenant if self.tenant else "N/A"
817+
return f"[red3]Failed[/] to connect to Context Broker at [blue3]{self.hostname}:{self.port}[/] | tenant=[blue3]{tenant}[/]"
797818

798819
def _warn_spring_message(self) -> str:
799820
return "Java-Spring based Context Broker detected. [orange3]Info endpoint disabled."

tests/test_ngsildclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313

1414

1515
def test_version():
16-
assert __version__ == "0.5.1"
16+
assert __version__ == "0.5.2"

0 commit comments

Comments
 (0)