From f6a7fdaab62ab4f0556d9bf59607a52fd0014df1 Mon Sep 17 00:00:00 2001 From: SystemsPurge Date: Tue, 29 Oct 2024 15:07:18 +0100 Subject: [PATCH 1/4] Added inexistant tenant creation through posting of dummy entity Signed-off-by: SystemsPurge --- filip/clients/ngsi_ld/cb.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/filip/clients/ngsi_ld/cb.py b/filip/clients/ngsi_ld/cb.py index be76fe54..65c4a94f 100644 --- a/filip/clients/ngsi_ld/cb.py +++ b/filip/clients/ngsi_ld/cb.py @@ -2,7 +2,7 @@ Context Broker Module for API Client """ import json -import re +import os import warnings from math import inf from enum import Enum @@ -63,6 +63,7 @@ def __init__(self, init_header = FiwareLDHeader() if fiware_header: init_header=fiware_header + super().__init__(url=url, session=session, fiware_header=init_header, @@ -71,6 +72,8 @@ def __init__(self, self._url_version = NgsiURLVersion.ld_url # init Content-Type header , account for @context field further down self.headers.update({'Content-Type':'application/json'}) + if init_header.ngsild_tenant is not None: + self.__make_tenant() def __pagination(self, *, @@ -158,6 +161,19 @@ def get_version(self) -> Dict: except requests.RequestException as err: self.logger.error(err) raise + + def __make_tenant(self): + """ + Create tenant if tenant + is given in headers + """ + idhex = f"urn:ngsi-ld:{os.urandom(6).hex()}" + e = ContextLDEntity(id=idhex,type=f"urn:ngsi-ld:{os.urandom(6).hex()}") + try: + self.post_entity(entity=e) + self.delete_entity_by_id(idhex) + except Exception as err: + self.log_error(err=err,msg="Error while creating default tenant") def get_statistics(self) -> Dict: """ From 2d9a8bc79671dd5067aedfe6aa9804c0d82dc718 Mon Sep 17 00:00:00 2001 From: SystemsPurge Date: Tue, 29 Oct 2024 15:48:18 +0100 Subject: [PATCH 2/4] Added raise in case entity creation fails Signed-off-by: SystemsPurge --- filip/clients/ngsi_ld/cb.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/filip/clients/ngsi_ld/cb.py b/filip/clients/ngsi_ld/cb.py index 65c4a94f..df02a798 100644 --- a/filip/clients/ngsi_ld/cb.py +++ b/filip/clients/ngsi_ld/cb.py @@ -173,7 +173,8 @@ def __make_tenant(self): self.post_entity(entity=e) self.delete_entity_by_id(idhex) except Exception as err: - self.log_error(err=err,msg="Error while creating default tenant") + self.log_error(err=err,msg="Error while creating tenant") + raise def get_statistics(self) -> Dict: """ From 0ab39004c0208107c3ad3216dbf374ae7c306760 Mon Sep 17 00:00:00 2001 From: JunsongDu Date: Wed, 30 Oct 2024 14:59:09 +0100 Subject: [PATCH 3/4] chore: adjust typehint for base http client --- filip/clients/base_http_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filip/clients/base_http_client.py b/filip/clients/base_http_client.py index ca1e3686..e1105628 100644 --- a/filip/clients/base_http_client.py +++ b/filip/clients/base_http_client.py @@ -25,7 +25,7 @@ def __init__(self, url: Union[AnyHttpUrl, str] = None, *, session: requests.Session = None, - fiware_header: Union[Dict, FiwareHeader] = None, + fiware_header: Union[Dict, FiwareHeader, FiwareLDHeader] = None, **kwargs): self.logger = logging.getLogger( From 80fa98028c8777aba706a2a71c60c540c55b9cc7 Mon Sep 17 00:00:00 2001 From: JunsongDu Date: Wed, 30 Oct 2024 15:09:45 +0100 Subject: [PATCH 4/4] chore: implement a test for tenant creation --- filip/clients/ngsi_ld/cb.py | 4 ++-- tests/clients/test_ngsi_ld_cb.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/filip/clients/ngsi_ld/cb.py b/filip/clients/ngsi_ld/cb.py index df02a798..8368a408 100644 --- a/filip/clients/ngsi_ld/cb.py +++ b/filip/clients/ngsi_ld/cb.py @@ -62,7 +62,7 @@ def __init__(self, #base_http_client overwrites empty header with FiwareHeader instead of FiwareLD init_header = FiwareLDHeader() if fiware_header: - init_header=fiware_header + init_header = fiware_header super().__init__(url=url, session=session, @@ -71,7 +71,7 @@ def __init__(self, # set the version specific url-pattern self._url_version = NgsiURLVersion.ld_url # init Content-Type header , account for @context field further down - self.headers.update({'Content-Type':'application/json'}) + self.headers.update({'Content-Type': 'application/json'}) if init_header.ngsild_tenant is not None: self.__make_tenant() diff --git a/tests/clients/test_ngsi_ld_cb.py b/tests/clients/test_ngsi_ld_cb.py index 2f9a6b7f..b688f406 100644 --- a/tests/clients/test_ngsi_ld_cb.py +++ b/tests/clients/test_ngsi_ld_cb.py @@ -85,6 +85,22 @@ def test_management_endpoints(self): self.assertIsNotNone(self.client.get_version()) # TODO: check whether there are other "management" endpoints + @unittest.skip("Only for local testing environment") + def test_not_existing_tenant(self): + """ + Test the expected behavior of the client when the tenant does not exist + This test will not be included in the CI/CD pipeline. For local testing please + comment out the decorator. + """ + # create uuid for the tenant + import uuid + tenant = str(uuid.uuid4()).split('-')[0] + fiware_header = FiwareLDHeader(ngsild_tenant=tenant) + client = ContextBrokerLDClient(fiware_header=fiware_header, + url=settings.LD_CB_URL) + entities = client.get_entity_list() + self.assertEqual(len(entities), 0) + def test_statistics(self): """ Test statistics of context broker client