def iothub_registrymanager_modules_sample_run(): try: # RegistryManager iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING) # CreateModule primary_key = "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnoo" secondary_key = "111222333444555666777888999000aaabbbcccdddee" auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY new_module = iothub_registry_manager.create_module(DEVICE_ID, primary_key, secondary_key, MODULE_ID, auth_method) print_module_info("CreateModule", new_module) # GetModule iothub_module = iothub_registry_manager.get_module(DEVICE_ID, MODULE_ID) print_module_info("GetModule", iothub_module) # UpdateModule primary_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" secondary_key = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY managedBy = "testManagedBy" iothub_registry_manager.update_module(DEVICE_ID, primary_key, secondary_key, MODULE_ID, auth_method, managedBy) updated_module = iothub_registry_manager.get_module(DEVICE_ID, MODULE_ID) print_module_info("UpdateModule", updated_module) # GetModuleList print ( "GetModuleList" ) number_of_modules = 10 modules_list = iothub_registry_manager.get_module_list(DEVICE_ID) number_of_modules = len(modules_list) print ( "Number of modules : {0}".format(number_of_modules) ) for moduleIndex in range(0, number_of_modules): title = "Module " + str(moduleIndex) print_module_info(title, modules_list[moduleIndex]) print ( "" ) # DeleteModule print ( "DeleteModule" ) iothub_registry_manager.delete_module(DEVICE_ID, MODULE_ID) print ( "" ) except IoTHubError as iothub_error: print ( "Unexpected error {0}".format(iothub_error) ) return except KeyboardInterrupt: print ( "IoTHubRegistryManager sample stopped" )
def run_e2e_twin(iothub_connection_string, testing_modules): try: # prepare device_id = generate_device_name() assert isinstance(device_id, str), 'Invalid type returned!' primary_key = "" secondary_key = "" auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY iothub_registry_manager = IoTHubRegistryManager( iothub_connection_string) new_device = iothub_registry_manager.create_device( device_id, primary_key, secondary_key, auth_method) if testing_modules == True: new_module = iothub_registry_manager.create_module( device_id, primary_key, secondary_key, TEST_MODULE_ID, auth_method) ########################################################################### # IoTHubDeviceTwin # act iothub_device_twin = IoTHubDeviceTwin(IOTHUB_CONNECTION_STRING) # verify assert iothub_device_twin != None, "iothub_device_twin is NULL" ########################################################################### ########################################################################### # Wait before get twin... sleep_before_device_action() ########################################################################### # get_twin # act if testing_modules == True: twin_info = iothub_device_twin.get_twin(device_id, TEST_MODULE_ID) else: twin_info = iothub_device_twin.get_twin(device_id) # verify assert twin_info != None, "twin_info is NULL" json_ok = twin_info.find("deviceId") assert json_ok > 0, "twin_info does not contain deviceId tag" json_ok = twin_info.find(device_id) assert json_ok > 0, "twin_info does not contain the correct device id" if testing_modules == True: json_ok = twin_info.find("moduleId") assert json_ok > 0, "twin_info does not contain moduleId tag" json_ok = twin_info.find("etag") assert json_ok > 0, "twin_info does not contain etag tag" json_ok = twin_info.find("properties") assert json_ok > 0, "twin_info does not contain properties tag" ########################################################################### print("") print("Twin before update:") print("{0}".format(twin_info)) ########################################################################### # update_twin # prepare new_property_name = "telemetryInterval" new_property_value = "42" UPDATE_JSON = "{\"properties\":{\"desired\":{\"" + new_property_name + "\":" + new_property_value + "}}}" # act if testing_modules == True: twin_info = iothub_device_twin.update_twin(device_id, TEST_MODULE_ID, UPDATE_JSON) else: twin_info = iothub_device_twin.update_twin(device_id, UPDATE_JSON) # verify assert twin_info != None, "twin_info is NULL" json_ok = twin_info.find("deviceId") assert json_ok > 0, "twin_info does not contain deviceId tag" json_ok = twin_info.find(device_id) assert json_ok > 0, "twin_info does not contain the correct device id" json_ok = twin_info.find("etag") assert json_ok > 0, "twin_info does not contain etag tag" json_ok = twin_info.find("properties") assert json_ok > 0, "twin_info does not contain properties tag" json_ok = twin_info.find(new_property_name) assert json_ok > 0, "twin_info does not contain " + new_property_name + " tag" json_ok = twin_info.find(new_property_value) assert json_ok > 0, "twin_info does not contain " + new_property_value ########################################################################### print("") print("Device Twin after update:") print("{0}".format(twin_info)) print("") retval = 0 except Exception as e: print("") print("run_e2e_twin() failed with exception: {0}".format(e)) retval = 1 finally: # clean-up if testing_modules == True: iothub_registry_manager.delete_module(device_id, TEST_MODULE_ID) iothub_registry_manager.delete_device(device_id) return retval
def run_e2e_registrymanager(iothub_connection_string): try: # prepare device_id = generate_device_name() assert isinstance(device_id, str), 'Invalid type returned!' ########################################################################### # IoTHubRegistryManager # prepare # act iothub_registry_manager = IoTHubRegistryManager( iothub_connection_string) # verify assert isinstance(iothub_registry_manager, IoTHubRegistryManager), 'Invalid type returned!' assert iothub_registry_manager != None, "iothub_registry_manager is NULL" ########################################################################### print("IoTHubRegistryManager is created successfully") ########################################################################### # create_device # prepare primary_key = "" secondary_key = "" auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY # act new_device = iothub_registry_manager.create_device( device_id, primary_key, secondary_key, auth_method) # verify assert isinstance(new_device, IoTHubDevice), 'Invalid type returned!' assert new_device != None, "new_device is NULL" assert new_device.primaryKey != None, "new_device.primaryKey is NULL" assert new_device.primaryKey != "", "new_device.primaryKey is empty" assert new_device.secondaryKey != None, "new_device.secondaryKey is NULL" assert new_device.secondaryKey != "", "new_device.secondaryKey is empty" ########################################################################### print_device_or_module_info("CreateDevice", new_device, False) ########################################################################### # create_module_device # prepare primary_key = "" secondary_key = "" auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY # act new_module = iothub_registry_manager.create_module( device_id, primary_key, secondary_key, TEST_MODULE_ID, auth_method) # verify assert isinstance(new_module, IoTHubModule), 'Invalid type returned!' assert new_module != None, "new_module is NULL" assert new_module.primaryKey != None, "new_module.primaryKey is NULL" assert new_module.primaryKey != "", "new_module.primaryKey is empty" assert new_module.secondaryKey != None, "new_module.secondaryKey is NULL" assert new_module.secondaryKey != "", "new_module.secondaryKey is empty" ########################################################################### print_device_or_module_info("CreateModule", new_module, True) ########################################################################### # get_device get_device_info_and_verify("GetDevice", iothub_registry_manager, device_id) ########################################################################### # get_module get_module_info_and_verify("GetModule", iothub_registry_manager, device_id) ########################################################################### # update_device # prepare primary_key = ''.join([ random.choice(string.ascii_letters + string.digits) for n in range(44) ]) secondary_key = ''.join([ random.choice(string.ascii_letters + string.digits) for n in range(44) ]) status = IoTHubDeviceStatus.DISABLED auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY # act iothub_registry_manager.update_device(device_id, primary_key, secondary_key, status, auth_method) # verify get_device_info_and_verify("UpdateDevice", iothub_registry_manager, device_id) ########################################################################### # update_module # prepare primary_key = ''.join([ random.choice(string.ascii_letters + string.digits) for n in range(44) ]) secondary_key = ''.join([ random.choice(string.ascii_letters + string.digits) for n in range(44) ]) status = IoTHubDeviceStatus.DISABLED auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY # act iothub_registry_manager.update_module(device_id, primary_key, secondary_key, TEST_MODULE_ID, auth_method) # verify get_module_info_and_verify("UpdateModule", iothub_registry_manager, device_id) ########################################################################### # get_device_list # prepare req_number_of_devices = 10 # act device_list = iothub_registry_manager.get_device_list( req_number_of_devices) # verify assert device_list != None, "device_list is NULL" number_of_devices = len(device_list) assert number_of_devices != None, "device_list is NULL" assert number_of_devices > 0, "number_of_devices is incorrect" ########################################################################### print("Number of devices : {0}".format( number_of_devices)) ########################################################################### # get_statistics # prepare # act iothub_registry_statistics = iothub_registry_manager.get_statistics() # verify assert iothub_registry_statistics.totalDeviceCount >= 0, "iothub_registry_statistics.totalDeviceCount is incorrect" sum_device_count = iothub_registry_statistics.enabledDeviceCount + iothub_registry_statistics.disabledDeviceCount assert sum_device_count >= 0, "iothub_registry_statistics.totalDeviceCount is incorrect" ########################################################################### print("GetStatistics") print("Total device count : {0}".format( iothub_registry_statistics.totalDeviceCount)) print("Enabled device count : {0}".format( iothub_registry_statistics.enabledDeviceCount)) print("Disabled device count : {0}".format( iothub_registry_statistics.disabledDeviceCount)) retval = 0 except Exception as e: print("") print("run_e2e_registrymanager() failed with exception: {0}".format(e)) retval = 1 finally: ########################################################################### # delete_module # prepare # act iothub_registry_manager.delete_module(device_id, TEST_MODULE_ID) # verify ########################################################################### # delete_device # prepare # act iothub_registry_manager.delete_device(device_id) # verify ########################################################################### return retval