Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

337 initial tenant when tenant does not exist #344

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion filip/clients/base_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
23 changes: 20 additions & 3 deletions filip/clients/ngsi_ld/cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,15 +62,18 @@ 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,
fiware_header=init_header,
**kwargs)
# 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()

def __pagination(self,
*,
Expand Down Expand Up @@ -158,6 +161,20 @@ 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 tenant")
raise

def get_statistics(self) -> Dict:
"""
Expand Down
16 changes: 16 additions & 0 deletions tests/clients/test_ngsi_ld_cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down