Skip to content

Commit

Permalink
COE-842: allow disabling the ssl verification for ImportData.py (#59)
Browse files Browse the repository at this point in the history
* COE-842: allow disabling the ssl verification

* add session to ExportProfileData to be consistent

---------

Co-authored-by: Michael Voigt <[email protected]>
  • Loading branch information
mvoigt-sag and Michael Voigt authored Feb 8, 2023
1 parent fe4f936 commit 5b394fb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
12 changes: 8 additions & 4 deletions simulators/extras/ArgumentsAndCredentialsHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand All @@ -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
9 changes: 6 additions & 3 deletions simulators/extras/ExportProfileData.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
23 changes: 13 additions & 10 deletions simulators/extras/ImportData.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging, urllib, json, requests, os, sys
from urllib3.exceptions import InsecureRequestWarning
from os.path import isfile, join

import ArgumentsAndCredentialsHandler
Expand All @@ -12,16 +13,20 @@
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
relativeFilePath = f"logs\import_{datetime.strftime(datetime.now(), logTimeFormat)}.log"
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:
Expand All @@ -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}".')
Expand All @@ -48,17 +52,15 @@ 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())
return None


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())
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 5b394fb

Please sign in to comment.