diff --git a/simulators/extras/ArgumentsAndCredentialsHandler.py b/simulators/extras/ArgumentsAndCredentialsHandler.py index fdf1c5b1..71f34f57 100644 --- a/simulators/extras/ArgumentsAndCredentialsHandler.py +++ b/simulators/extras/ArgumentsAndCredentialsHandler.py @@ -124,6 +124,7 @@ def HandleImportArguments(): parser.add_argument('--password', '-p', type=str, help='C8Y Password') parser.add_argument('--baseurl', '-b', type=str, help='C8Y Baseurl') parser.add_argument('--tenant-id', '-t', type=str, help='C8Y TenantID (optional)') + parser.add_argument('--disable-ssl-verification', action='store_false') args = parser.parse_args() @@ -162,12 +163,14 @@ def HandleImportArguments(): if not TENANT: TENANT = Environment.C8Y_TENANT + VERIFY_SSL_CERTIFICATE = args.disable_ssl_verification + c8y = CumulocityApi(base_url=BASEURL, # the url of your Cumulocity tenant here tenant_id=TENANT, # the tenant ID of your Cumulocity tenant here username=USERNAME, # your Cumulocity IoT username password=PASSWORD) # your Cumulocity IoT password - return INPUT_FILE_LIST, LOG_LEVEL, c8y, PASSWORD + return INPUT_FILE_LIST, LOG_LEVEL, c8y, PASSWORD, VERIFY_SSL_CERTIFICATE def RemoveTrailingSlashFromBaseUrl(baseUrl): @@ -176,10 +179,11 @@ def RemoveTrailingSlashFromBaseUrl(baseUrl): return baseUrl -def CheckTenantConnection(baseUrl, C8Y_HEADERS): +def CheckTenantConnection(baseUrl, C8Y_HEADERS, session): # Check if connection to tenant can be created try: - response = requests.get(f'{baseUrl}/tenant/currentTenant', headers=C8Y_HEADERS) + response = session.get(f'{baseUrl}/tenant/currentTenant', headers=C8Y_HEADERS) return response - except: + except Exception as e: + print(e) return None diff --git a/simulators/extras/ExportProfileData.py b/simulators/extras/ExportProfileData.py index 2ed5e388..d3620fa9 100644 --- a/simulators/extras/ExportProfileData.py +++ b/simulators/extras/ExportProfileData.py @@ -18,8 +18,11 @@ filePath = os.path.join(os.path.dirname(__file__), relativeFilePath) consoleLogger = ArgumentsAndCredentialsHandler.SetupLogger(console_logger_name='ConsoleExportProfileData', console_log_level=console_log_level) ##################################################### + +session = requests.Session() + # Check if connection to tenant can be created -tenantConnectionResponse = ArgumentsAndCredentialsHandler.CheckTenantConnection(baseUrl=c8y.base_url, C8Y_HEADERS=C8Y_HEADERS) +tenantConnectionResponse = ArgumentsAndCredentialsHandler.CheckTenantConnection(baseUrl=c8y.base_url, C8Y_HEADERS=C8Y_HEADERS, session=session) if tenantConnectionResponse: consoleLogger.info(f"Connect to tenant {c8y.tenant_id} successfully") else: @@ -82,7 +85,7 @@ def ExportSpecificProfileDataWithDeviceId(createFrom, createTo, deviceId): def FindDeviceNameById(deviceId, baseUrl): - response = requests.get(f'{baseUrl}/inventory/managedObjects/{deviceId}', + response = session.get(f'{baseUrl}/inventory/managedObjects/{deviceId}', headers=C8Y_HEADERS) if not response.ok: consoleLogger.error(response.json()) @@ -131,7 +134,7 @@ def AppendDataToJsonFile(jsonDataList, filePath, data_type, json_data={}): def GetExternalIdReponse(deviceId, baseUrl): - externalIdResponse = requests.get(f'{baseUrl}/identity/globalIds/{deviceId}/externalIds', + externalIdResponse = session.get(f'{baseUrl}/identity/globalIds/{deviceId}/externalIds', headers=C8Y_HEADERS) if not externalIdResponse.ok: consoleLogger.error(externalIdResponse.json()) diff --git a/simulators/extras/ImportData.py b/simulators/extras/ImportData.py index be34d7e5..4e15d0bd 100644 --- a/simulators/extras/ImportData.py +++ b/simulators/extras/ImportData.py @@ -1,4 +1,5 @@ import logging, urllib, json, requests, os, sys +from urllib3.exceptions import InsecureRequestWarning from os.path import isfile, join import ArgumentsAndCredentialsHandler @@ -12,7 +13,7 @@ logTimeFormat = "%Y%m%d%H%M%S_%f" file_log_level = logging.DEBUG C8Y_PROFILE_GROUP = 'c8y_EventBasedSimulatorProfile' -json_filename_list_to_import, console_log_level, c8y, password = ArgumentsAndCredentialsHandler.HandleImportArguments() +json_filename_list_to_import, console_log_level, c8y, password, verifySslCertificate = ArgumentsAndCredentialsHandler.HandleImportArguments() C8Y_HEADERS, MEASUREMENTS_HEADERS = ArgumentsAndCredentialsHandler.SetupHeadersForAPIRequest(tenant_id=c8y.tenant_id, username= c8y.username, password=password) #################################################### # Setup Log @@ -20,8 +21,12 @@ logFilePath = os.path.join(os.path.dirname(__file__), relativeFilePath) consoleLogger = ArgumentsAndCredentialsHandler.SetupLogger(console_logger_name='ConsoleImportProfileData', console_log_level=console_log_level) ##################################################### + +session = requests.Session() +session.verify = verifySslCertificate + # Check if connection to tenant can be created -tenantConnectionResponse = ArgumentsAndCredentialsHandler.CheckTenantConnection(baseUrl=c8y.base_url, C8Y_HEADERS=C8Y_HEADERS) +tenantConnectionResponse = ArgumentsAndCredentialsHandler.CheckTenantConnection(baseUrl=c8y.base_url, C8Y_HEADERS=C8Y_HEADERS, session=session) if tenantConnectionResponse: consoleLogger.info(f"Connected successfully to tenant \"{c8y.tenant_id}\" with user {c8y.username} on {c8y.base_url}") else: @@ -37,8 +42,7 @@ def GetDeviceIdByExternalId(external_id): consoleLogger.info(f'Searching for device with ext ID {external_id}') encoded_external_id = EncodeUrl(external_id) - response = requests.get( - f'{c8y.base_url}/identity/externalIds/{C8Y_PROFILE_GROUP}/{encoded_external_id}', headers=C8Y_HEADERS) + response = session.get(f'{c8y.base_url}/identity/externalIds/{C8Y_PROFILE_GROUP}/{encoded_external_id}', headers=C8Y_HEADERS) if response.ok: device_id = response.json()['managedObject']['id'] consoleLogger.info(f'Device({device_id}) has been found by its external id "{C8Y_PROFILE_GROUP}/{external_id}".') @@ -48,8 +52,7 @@ def GetDeviceIdByExternalId(external_id): def CreateAlarm(alarm): - response = requests.post( - f'{c8y.base_url}/alarm/alarms', headers=C8Y_HEADERS, data=json.dumps(alarm)) + response = session.post(f'{c8y.base_url}/alarm/alarms', headers=C8Y_HEADERS, data=json.dumps(alarm)) if response.ok: return response.json() consoleLogger.warning(response.json()) @@ -57,8 +60,7 @@ def CreateAlarm(alarm): def CreateMeasurements(measurements): - response = requests.post(f'{c8y.base_url}/measurement/measurements', - headers=MEASUREMENTS_HEADERS, data=json.dumps(measurements)) + response = session.post(f'{c8y.base_url}/measurement/measurements', headers=MEASUREMENTS_HEADERS, data=json.dumps(measurements)) if response.ok: return response.json() consoleLogger.warning(response.json()) @@ -80,7 +82,7 @@ def DeleteUnwantedAlarmFields(alarm): def ImportAlarms(alarms, id): - consoleLogger.debug(f'Importing [{len(alarms)}] alarms for {id}') + consoleLogger.info(f'Importing [{len(alarms)}] alarms for {id}') timeShift = GetTimeDifference(alarms[0], 'creationTime') for alarm in alarms: alarm['source']['id'] = id @@ -91,7 +93,7 @@ def ImportAlarms(alarms, id): def ImportMeasurements(measurements, id): - consoleLogger.debug(f'Importing [{len(measurements)}] alarms for {id}') + consoleLogger.info(f'Importing [{len(measurements)}] alarms for {id}') timeShift = GetTimeDifference(measurements[len(measurements) - 1], 'time') for i in range(len(measurements)): measurements[i]['time'] = (datetime.strptime(measurements[i]['time'], timeFormat) + timeShift).strftime(timeFormat) @@ -167,6 +169,7 @@ def AddJsonExtensionToFileNameList(list_of_filenames): id = GetDeviceIdByExternalId(external_id=external_id) if (id is not None): + requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) if len(alarms) > 0: ImportAlarms(alarms=alarms, id=id) else: