// Create a list of devices public async Task CreateListAsync(IEnumerable <string> deviceIds) { this.instance.InitRequired(); var batches = this.SplitArray(deviceIds.ToList(), REGISTRY_MAX_BATCH_SIZE).ToArray(); this.log.Info("Creating devices", () => new { Count = deviceIds.Count(), Batches = batches.Length, REGISTRY_MAX_BATCH_SIZE }); for (var batchNumber = 0; batchNumber < batches.Length; batchNumber++) { var batch = batches[batchNumber]; this.log.Debug("Creating devices batch", () => new { batchNumber, batchSize = batch.Count() }); BulkRegistryOperationResult result = await this.registry.AddDevices2Async( batch.Select(id => this.PrepareDeviceObject(id, this.fixedDeviceKey))); this.log.Debug("Devices batch created", () => new { batchNumber, result.IsSuccessful, ErrorsCount = result.Errors.Length }); var errors = this.AnalyzeBatchErrors(result); if (errors > 0) { throw new ExternalDependencyException($"Batch operation failed with {errors} errors"); } } this.log.Info("Device creation completed", () => new { Count = deviceIds.Count(), Batches = batches.Length, REGISTRY_MAX_BATCH_SIZE }); }
public async Task BulkOperations_UpdateTwins2Module_Ok() { string tagName = Guid.NewGuid().ToString(); string tagValue = Guid.NewGuid().ToString(); TestModule testModule = await TestModule.GetTestModuleAsync(DevicePrefix, ModulePrefix, Logger).ConfigureAwait(false); using var registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString); Twin twin = await registryManager.GetTwinAsync(testModule.DeviceId, testModule.Id).ConfigureAwait(false); twin.Tags = new TwinCollection(); twin.Tags[tagName] = tagValue; BulkRegistryOperationResult result = await registryManager.UpdateTwins2Async(new List <Twin> { twin }, true).ConfigureAwait(false); Assert.IsTrue(result.IsSuccessful, $"UpdateTwins2Async error:\n{ResultErrorsToString(result)}"); Twin twinUpd = await registryManager.GetTwinAsync(testModule.DeviceId, testModule.Id).ConfigureAwait(false); Assert.AreEqual(twin.DeviceId, twinUpd.DeviceId, "Device ID changed"); Assert.AreEqual(twin.ModuleId, twinUpd.ModuleId, "Module ID changed"); Assert.IsNotNull(twinUpd.Tags, "Twin.Tags is null"); Assert.IsTrue(twinUpd.Tags.Contains(tagName), "Twin doesn't contain the tag"); Assert.AreEqual((string)twin.Tags[tagName], (string)twinUpd.Tags[tagName], "Tag value changed"); await registryManager.CloseAsync().ConfigureAwait(false); }
/// <summary> /// Add a device with strongly typed settings (could be usefull in some scenarios) /// </summary> /// <param name="deviceId"></param> /// <param name="settings"></param> /// <returns></returns> public async Task <SIoT.Device> AddDeviceWithTagsAsync(string deviceId, SIoT.DeviceIoTSettings settings) { TwinJsonConverter converter = new TwinJsonConverter(); Twin twin = null; if (settings != null && settings.Twins != null) { twin = new Twin(deviceId); string jsonTags = JsonConvert.SerializeObject(settings.Twins, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); twin.Tags = new TwinCollection(jsonTags); } BulkRegistryOperationResult result = await _registryManager.AddDeviceWithTwinAsync(new Device(deviceId), twin); if (result != null && result.IsSuccessful) { return(await GetDeviceAsync(deviceId)); } else { throw new Exception(JsonConvert.SerializeObject(result.Errors, Formatting.Indented)); } }
public async Task RunSampleAsync() { try { var devices = GenerateEdgeDevices(DeviceIdPrefix, NumOfEdgeDevices); var conditionPropertyName = "condition-" + Guid.NewGuid().ToString("N"); var conditionPropertyValue = Guid.NewGuid().ToString(); var targetCondition = $"tags.{conditionPropertyName}='{conditionPropertyValue}'"; var edgeDevices = devices.ToList(); BulkRegistryOperationResult createResult = await CreateEdgeDevices(edgeDevices).ConfigureAwait(false); if (createResult.Errors.Length > 0) { foreach (var err in createResult.Errors) { Console.WriteLine($"Create failed: {err.DeviceId}-{err.ErrorCode}-{err.ErrorStatus}"); } } foreach (var device in edgeDevices) { var twin = await _registryManager.GetTwinAsync(device.Id).ConfigureAwait(false); twin.Tags[conditionPropertyName] = conditionPropertyValue; await _registryManager.UpdateTwinAsync(device.Id, twin, twin.ETag).ConfigureAwait(false); } var baseConfiguration = new Configuration($"{ConfigurationIdPrefix}base-{Guid.NewGuid().ToString()}") { Labels = new Dictionary <string, string> { { "App", "Mongo" } }, Content = GetBaseConfigurationContent(), Priority = BasePriority, TargetCondition = targetCondition }; var addOnConfiguration = new Configuration($"{ConfigurationIdPrefix}addon-{Guid.NewGuid().ToString()}") { Labels = new Dictionary <string, string> { { "AddOn", "Stream Analytics" } }, Content = GetAddOnConfigurationContent(), Priority = BasePriority + 1, TargetCondition = targetCondition }; var baseConfigTask = _registryManager.AddConfigurationAsync(baseConfiguration); var addOnConfigTask = _registryManager.AddConfigurationAsync(addOnConfiguration); Task.WaitAll(baseConfigTask, addOnConfigTask); } catch (Exception e) { Console.WriteLine(e); } }
public async Task RegistryManager_UpdateTwins2Async_Works() { // arrange var device1 = new Device(_idPrefix + Guid.NewGuid()); var device2 = new Device(_idPrefix + Guid.NewGuid()); using var registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString); try { await registryManager.AddDeviceAsync(device1).ConfigureAwait(false); Twin twin1 = await registryManager.GetTwinAsync(device1.Id).ConfigureAwait(false); await registryManager.AddDeviceAsync(device2).ConfigureAwait(false); Twin twin2 = await registryManager.GetTwinAsync(device2.Id).ConfigureAwait(false); // act const string expectedProperty = "someNewProperty"; const string expectedPropertyValue = "someNewPropertyValue"; twin1.Properties.Desired[expectedProperty] = expectedPropertyValue; twin2.Properties.Desired[expectedProperty] = expectedPropertyValue; BulkRegistryOperationResult result = await registryManager .UpdateTwins2Async(new[] { twin1, twin2 }) .ConfigureAwait(false); // assert result.IsSuccessful.Should().BeTrue(); var actualTwin1 = await registryManager.GetTwinAsync(device1.Id).ConfigureAwait(false); ((string)actualTwin1.Properties.Desired[expectedProperty]).Should().Be(expectedPropertyValue); var actualTwin2 = await registryManager.GetTwinAsync(device2.Id).ConfigureAwait(false); ((string)(actualTwin2.Properties.Desired[expectedProperty])).Should().Be(expectedPropertyValue); } finally { try { await registryManager.RemoveDeviceAsync(device1.Id).ConfigureAwait(false); await registryManager.RemoveDeviceAsync(device2.Id).ConfigureAwait(false); } catch (Exception ex) { Logger.Trace($"Failed to clean up devices due to {ex}"); } } }
/// <summary> /// Delete a list of devices /// </summary> public async Task DeleteListAsync(IEnumerable <string> deviceIds) { this.instance.InitRequired(); var batches = this.SplitArray(deviceIds.ToList(), REGISTRY_MAX_BATCH_SIZE).ToArray(); this.log.Info("Deleting devices", () => new { Count = deviceIds.Count(), Batches = batches.Length, REGISTRY_MAX_BATCH_SIZE }); try { for (var batchNumber = 0; batchNumber < batches.Length; batchNumber++) { var batch = batches[batchNumber]; this.log.Debug("Deleting devices batch", () => new { batchNumber, batchSize = batch.Count() }); BulkRegistryOperationResult result = await this.registry.RemoveDevices2Async( batch.Select(id => new Azure.Devices.Device(id)), forceRemove : true); this.log.Debug("Devices batch deleted", () => new { batchNumber, result.IsSuccessful, result.Errors }); var errors = this.AnalyzeBatchErrors(result); if (errors > 0) { throw new ExternalDependencyException($"Batch operation failed with {errors} errors"); } } } catch (TooManyDevicesException error) { var msg = "Failed to delete devices, the batch is too big"; this.log.Error(msg, error); this.diagnosticsLogger.LogServiceError(msg, error.Message); throw; } catch (IotHubCommunicationException error) { var msg = "Failed to delete devices (IotHubCommunicationException)"; this.log.Error(msg, () => new { error.InnerException, error }); this.diagnosticsLogger.LogServiceError(msg, new { error.Message }); throw; } catch (Exception error) { var msg = "Failed to delete devices"; this.log.Error(msg, error); this.diagnosticsLogger.LogServiceError(msg, error.Message); throw; } }
public async Task RegistryManager_UpdateDevices2Async_Works() { // arrange var device1 = new Device(_idPrefix + Guid.NewGuid()); var device2 = new Device(_idPrefix + Guid.NewGuid()); var edge = new Device(_idPrefix + Guid.NewGuid()); using var registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString); try { Device addedDevice1 = await registryManager.AddDeviceAsync(device1).ConfigureAwait(false); Device addedDevice2 = await registryManager.AddDeviceAsync(device2).ConfigureAwait(false); Device addedEdge = await registryManager.AddDeviceAsync(edge).ConfigureAwait(false); // act addedDevice1.Scope = addedEdge.Scope; addedDevice2.Scope = addedEdge.Scope; BulkRegistryOperationResult result = await registryManager .UpdateDevices2Async(new[] { addedDevice1, addedDevice2 }) .ConfigureAwait(false); // assert result.IsSuccessful.Should().BeTrue(); Device actualDevice1 = await registryManager.GetDeviceAsync(device1.Id).ConfigureAwait(false); actualDevice1.Scope.Should().Be(addedEdge.Scope); Device actualDevice2 = await registryManager.GetDeviceAsync(device2.Id).ConfigureAwait(false); actualDevice2.Scope.Should().Be(addedEdge.Scope); } finally { try { await registryManager.RemoveDeviceAsync(device1.Id).ConfigureAwait(false); await registryManager.RemoveDeviceAsync(device2.Id).ConfigureAwait(false); await registryManager.RemoveDeviceAsync(edge.Id).ConfigureAwait(false); } catch (Exception ex) { Logger.Trace($"Failed to clean up devices due to {ex}"); } } }
public async Task RegistryManager_AddDevices2Async_Works() { // arrange var edge = new Device(_idPrefix + Guid.NewGuid()) { Scope = "someScope" + Guid.NewGuid(), }; var device = new Device(_idPrefix + Guid.NewGuid()) { Scope = edge.Scope, }; using var registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString); try { // act BulkRegistryOperationResult bulkAddResult = await registryManager .AddDevices2Async(new List <Device> { edge, device }) .ConfigureAwait(false); // assert bulkAddResult.IsSuccessful.Should().BeTrue(); Device actualEdge = await registryManager.GetDeviceAsync(edge.Id).ConfigureAwait(false); actualEdge.Id.Should().Be(edge.Id); actualEdge.Scope.Should().Be(edge.Scope); Device actualDevice = await registryManager.GetDeviceAsync(device.Id).ConfigureAwait(false); actualDevice.Id.Should().Be(device.Id); actualDevice.Scope.Should().Be(device.Scope); actualDevice.ParentScopes.Count.Should().Be(1); actualDevice.ParentScopes.First().Should().Be(edge.Scope); } finally { try { await registryManager.RemoveDeviceAsync(device.Id).ConfigureAwait(false); await registryManager.RemoveDeviceAsync(edge.Id).ConfigureAwait(false); } catch (Exception ex) { Logger.Trace($"Failed to clean up devices due to {ex}"); } } }
private string ResultErrorsToString(BulkRegistryOperationResult result) { var errorString = ""; foreach (var error in result.Errors) { errorString += $"\t{error.ErrorCode} : {error.ErrorStatus}\n"; } return(errorString); }
/// <summary> /// Unregisters devices from the IoT Hub. /// </summary> /// <param name="registryManager">An instance of <c>RegistryManager</c></param> /// <param name="deviceIds">The list of device ids to unregister.</param> /// <returns>Any errors encountered during the bulk remove operation.</returns> /// <remarks>Ignores DeviceNotFound errors</remarks> private async Task <IEnumerable <DeviceRegistryOperationError> > UnregisterDevices(RegistryManager registryManager, List <string> deviceIds) { IEnumerable <Task <Device> > getDeviceTasks = deviceIds.Select( async deviceId => await registryManager.GetDeviceAsync(deviceId) ); IEnumerable <Device> deviceList = await Task.WhenAll(getDeviceTasks); BulkRegistryOperationResult unregisterResult = await registryManager.RemoveDevices2Async(deviceList); return(GetRegistryOperationErrors(unregisterResult, ErrorCode.DeviceNotFound)); }
public async Task <SIoT.Device> AddDeviceWithTwinAsync(string deviceId, Twin twin) { BulkRegistryOperationResult result = await _registryManager.AddDeviceWithTwinAsync(new Device(deviceId), twin); if (result != null && result.IsSuccessful) { return(await GetDeviceAsync(deviceId)); } else { throw new Exception(JsonConvert.SerializeObject(result.Errors, Formatting.Indented)); } }
/// <summary> /// Registers devices with the IoT Hub. /// </summary> /// <param name="registryManager">An instance of <c>RegistryManager</c></param> /// <param name="deviceIds">The list of device ids to register.</param> /// <param name="enable">Devices will be registered with enabled state if this is set to true.</param> /// <returns>Any errors returned during the bulk register operation (and bulk update operation if devices are being enabled also).</returns> private async Task <IEnumerable <DeviceRegistryOperationError> > RegisterDevices(RegistryManager registryManager, List <string> deviceIds, bool enable = false) { IEnumerable <DeviceRegistryOperationError> registerErrors = deviceIds.Where( id => !AzureIoTDeviceIdRegex.IsMatch(id) ) .Select( id => new DeviceRegistryOperationError() { ErrorCode = ErrorCode.ArgumentInvalid, DeviceId = id, ErrorStatus = String.Format(Labels.AzureIoTDeviceIdInvalid, id) } ); IEnumerable <DeviceRegistryOperationError> updateErrors = Enumerable.Empty <DeviceRegistryOperationError>(); List <Device> deviceList = deviceIds.Where( id => AzureIoTDeviceIdRegex.IsMatch(id) ).Select( id => { return(new Device(id) { Status = enable ? DeviceStatus.Enabled : DeviceStatus.Disabled }); } ).ToList(); if (deviceList.Count > 0) { BulkRegistryOperationResult registerResult = await registryManager.AddDevices2Async(deviceList); if (!registerResult.IsSuccessful) { // Some devices were already registered. // Explicity enable those devices only if the argument is set to true, otherwise skip this step. if (enable) { IEnumerable <string> deviceIdsToUpdate = registerResult.Errors .Where( error => error.ErrorCode == ErrorCode.DeviceAlreadyExists ) .Select( error => error.DeviceId ); updateErrors = await SetStatuses(registryManager, deviceIdsToUpdate, DeviceStatus.Enabled); } } registerErrors = registerErrors.Concat(GetRegistryOperationErrors(registerResult, ErrorCode.DeviceAlreadyExists)); } return(registerErrors.Concat(updateErrors)); }
private async Task RemoveDevicesAsync(string[] deviceIds) { if (File.Exists(CreatedDevicesJsonFileName)) { var contents = await File.ReadAllTextAsync(CreatedDevicesJsonFileName); var createdDevices = JsonConvert.DeserializeObject <List <ExportImportDevice> >(contents); const int maxDevicesPerCall = 100; int numberOfCallsRequired = (int)Math.Ceiling(deviceIds.Length / (double)maxDevicesPerCall); for (int i = 0; i < numberOfCallsRequired; i++) { string[] deviceIdsForCall = deviceIds.Skip(i * maxDevicesPerCall).Take(maxDevicesPerCall).ToArray(); BulkRegistryOperationResult bulkRemovalResult = await _registryManager.RemoveDevices2Async(deviceIdsForCall.Select(id => new Device(id) { ETag = createdDevices.FirstOrDefault(d => d.Id == id)?.ETag })); if (!bulkRemovalResult.IsSuccessful) { Console.WriteLine("Failed removing devices..."); foreach (DeviceRegistryOperationError creationError in bulkRemovalResult.Errors) { Console.WriteLine($"Device {creationError.DeviceId}: {creationError.ErrorStatus}"); } throw new Exception(); } } } else { foreach (var device in deviceIds.Select(id => new Device(id))) { try { Console.WriteLine($"{_actionMessage} device: {device.Id}"); await _registryManager.RemoveDeviceAsync(device.Id); Console.WriteLine($"Finished for device: {device.Id}"); } catch (DeviceNotFoundException) { Console.WriteLine("Device not found, continuing"); } } } }
private async Task CleanUpDevices() { int devicesDeleted = 0; Console.WriteLine("Clean up devices:"); var _bulkDeleteList = new List <Device>(DeleteBatchSize); while (_devicesToDelete.Count > 0) { try { int i; for (i = 0; (i < DeleteBatchSize) && (i < _devicesToDelete.Count); i++) { _bulkDeleteList.Add(_devicesToDelete[i]); Console.WriteLine($"\tRemove: {_devicesToDelete[i].Id}"); } BulkRegistryOperationResult ret = await _rm.RemoveDevices2Async(_bulkDeleteList, true, CancellationToken.None).ConfigureAwait(false); int successfulDeletionCount = _bulkDeleteList.Count - ret.Errors.Length; devicesDeleted += successfulDeletionCount; Console.WriteLine($"BATCH DELETE: Current batch - {successfulDeletionCount}"); Console.WriteLine($"BATCH DELETE: Running total - {devicesDeleted}"); if (!ret.IsSuccessful) { foreach (DeviceRegistryOperationError error in ret.Errors) { Console.WriteLine($"\tERROR: {error.DeviceId} - {error.ErrorCode}: {error.ErrorStatus}"); } } _devicesToDelete.RemoveRange(0, i); } catch (Exception ex) { Console.WriteLine($"Exception thrown, continue to next batch: {ex}"); } finally { _bulkDeleteList.Clear(); } } Console.WriteLine($"-- Total # of devices deleted: {devicesDeleted}"); }
public async Task RegistryManager_RemoveDevices2Async_Works() { // arrange var device1 = new Device(_idPrefix + Guid.NewGuid()); var device2 = new Device(_idPrefix + Guid.NewGuid()); using var registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString); try { await registryManager.AddDeviceAsync(device1).ConfigureAwait(false); await registryManager.AddDeviceAsync(device2).ConfigureAwait(false); // act BulkRegistryOperationResult bulkDeleteResult = await registryManager .RemoveDevices2Async(new[] { device1, device2 }, true, default) .ConfigureAwait(false); // assert bulkDeleteResult.IsSuccessful.Should().BeTrue(); Device actualDevice1 = await registryManager.GetDeviceAsync(device1.Id).ConfigureAwait(false); actualDevice1.Should().BeNull(); Device actualDevice2 = await registryManager.GetDeviceAsync(device1.Id).ConfigureAwait(false); actualDevice2.Should().BeNull(); } finally { try { await registryManager.RemoveDeviceAsync(device1.Id).ConfigureAwait(false); await registryManager.RemoveDeviceAsync(device2.Id).ConfigureAwait(false); } catch (Exception ex) { Logger.Trace($"Failed to clean up devices due to {ex}"); } } }
/// <summary> /// Delete a list of devices /// </summary> public async Task DeleteListAsync(IEnumerable <string> deviceIds) { this.SetupHub(); var batches = this.SplitArray(deviceIds.ToList(), REGISTRY_MAX_BATCH_SIZE).ToArray(); this.log.Info("Deleting devices", () => new { Count = deviceIds.Count(), Batches = batches.Length, REGISTRY_MAX_BATCH_SIZE }); try { for (var batchNumber = 0; batchNumber < batches.Length; batchNumber++) { var batch = batches[batchNumber]; this.log.Info("Deleting devices batch", () => new { batchNumber, batchSize = batch.Count() }); BulkRegistryOperationResult result = await this.registry.RemoveDevices2Async( batch.Select(id => new Azure.Devices.Device(id)), forceRemove : true); this.log.Info("Devices batch deleted", () => new { batchNumber, result.IsSuccessful, result.Errors }); } } catch (TooManyDevicesException error) { this.log.Error("Failed to delete devices, the batch is too big", () => new { error }); throw; } catch (IotHubCommunicationException error) { this.log.Error("Failed to delete devices (IotHubCommunicationException)", () => new { error.InnerException, error }); throw; } catch (Exception error) { this.log.Error("Failed to delete devices", () => new { error }); throw; } }
// Log the errors occurred during a batch operation private int AnalyzeBatchErrors(BulkRegistryOperationResult result) { if (result.Errors.Length == 0) { return(0); } var errorsByType = new Dictionary <string, int>(); // Ignore errors reporting that devices already exist var errorToIgnore = ErrorCode.DeviceAlreadyExists.ToString(); foreach (var error in result.Errors) { var k = error.ErrorCode.ToString(); if (k == errorToIgnore) { continue; } if (errorsByType.ContainsKey(k)) { errorsByType[k]++; } else { errorsByType[k] = 1; } } if (errorsByType.Count == 0) { return(0); } this.log.Error("Some errors occurred in the batch operation", () => new { errorsByType, result.Errors }); return(errorsByType.Count); }
/// <summary> /// Create a list of devices /// </summary> public async Task CreateListAsync(IEnumerable <string> deviceIds) { this.SetupHub(); var batches = this.SplitArray(deviceIds.ToList(), REGISTRY_MAX_BATCH_SIZE).ToArray(); this.log.Info("Creating devices", () => new { Count = deviceIds.Count(), Batches = batches.Length, REGISTRY_MAX_BATCH_SIZE }); for (var batchNumber = 0; batchNumber < batches.Length; batchNumber++) { var batch = batches[batchNumber]; this.log.Info("Creating devices batch", () => new { batchNumber, batchSize = batch.Count() }); BulkRegistryOperationResult result = await this.registry.AddDevices2Async( batch.Select(id => new Azure.Devices.Device(id))); this.log.Info("Devices batch created", () => new { batchNumber, result.IsSuccessful, result.Errors }); } }
/// <summary> /// Enable or disable devices. /// </summary> /// <param name="registryManager">An instance of <c>RegistryManager</c></param> /// <param name="deviceIds">The list of device ids to set the status for.</param> /// <param name="status">DeviceStatus enum to update to.</param> /// <returns>Any errors returned during the bulk update operation.</returns> private async Task <IEnumerable <DeviceRegistryOperationError> > SetStatuses(RegistryManager registryManager, IEnumerable <string> deviceIds, DeviceStatus status) { IEnumerable <Task <Device> > getDeviceTasks = deviceIds.Select( async deviceId => await registryManager.GetDeviceAsync(deviceId) ); IEnumerable <Device> deviceListToUpdate = await Task.WhenAll(getDeviceTasks); deviceListToUpdate = deviceListToUpdate .Where(device => device.Status != status) .Select(device => { device.Status = status; return(device); }).ToList(); if (deviceListToUpdate.Count() > 0) { BulkRegistryOperationResult updateResult = await registryManager.UpdateDevices2Async(deviceListToUpdate); return(GetRegistryOperationErrors(updateResult)); } else { return(Enumerable.Empty <DeviceRegistryOperationError>()); } }
/// <summary> /// Returns the list of <c>DeviceRegistryOperationError</c>. /// </summary> /// <param name="result">An instance of <c>BulkRegistryOperationResult</c> to read the errors from.</param> /// <param name="ignoreError">Optionally set this to ignore an error type.</param> /// <returns></returns> private IEnumerable <DeviceRegistryOperationError> GetRegistryOperationErrors(BulkRegistryOperationResult result, ErrorCode ignoreError = ErrorCode.InvalidErrorCode) { if (result.Errors != null && result.Errors.Length > 0) { return(result.Errors.Where( error => error.ErrorCode != ignoreError )); } else { return(Enumerable.Empty <DeviceRegistryOperationError>()); } }
public async Task RunSampleAsync() { try { await PrintDeviceCount(); int devicesDeleted = 0; Console.WriteLine("Clean up devices:"); string sqlQueryString = "select * from devices"; IQuery query = _rm.CreateQuery(sqlQueryString, QueryBatchSize); while (query.HasMoreResults) { IEnumerable <Shared.Twin> result = await query.GetNextAsTwinAsync(); foreach (var twinResult in result) { string deviceId = twinResult.DeviceId; foreach (string prefix in _deleteDeviceWithPrefix) { if (deviceId.StartsWith(prefix)) { _devicesToDelete.Add(new Device(deviceId)); } } } } var _bulkDeleteList = new List <Device>(DeleteBatchSize); while (_devicesToDelete.Count > 0) { int i; for (i = 0; (i < DeleteBatchSize) && (i < _devicesToDelete.Count); i++) { _bulkDeleteList.Add(_devicesToDelete[i]); Console.WriteLine($"\tRemove: {_devicesToDelete[i].Id}"); } _devicesToDelete.RemoveRange(0, i); BulkRegistryOperationResult ret = await _rm.RemoveDevices2Async(_bulkDeleteList, true, CancellationToken.None).ConfigureAwait(false); devicesDeleted += _bulkDeleteList.Count - ret.Errors.Length; Console.WriteLine($"BATCH DELETE: {devicesDeleted}/{_bulkDeleteList.Count}"); if (!ret.IsSuccessful) { foreach (DeviceRegistryOperationError error in ret.Errors) { Console.WriteLine($"\tERROR: {error.DeviceId} - {error.ErrorCode}: {error.ErrorStatus}"); } } _bulkDeleteList.Clear(); } Console.WriteLine($"-- Total no of devices deleted: {devicesDeleted}"); var configurations = await _rm.GetConfigurationsAsync(100, new CancellationToken()).ConfigureAwait(false); { foreach (var configuration in configurations) { string configurationId = configuration.Id; foreach (var prefix in _deleteConfigurationWithPrefix) { if (configurationId.StartsWith(prefix)) { _configurationsToDelete.Add(new Configuration(configurationId)); } } } } var removeConfigTasks = new List <Task>(); _configurationsToDelete.ForEach(configuration => { Console.WriteLine($"Remove: {configuration.Id}"); removeConfigTasks.Add(_rm.RemoveConfigurationAsync(configuration.Id)); }); Task.WaitAll(removeConfigTasks.ToArray()); Console.WriteLine($"-- Total no of configurations deleted: {_configurationsToDelete.Count}"); } catch (Exception ex) { Console.WriteLine(ex); } }
private async Task <IDictionary <string, string> > CreateIoTHubDevicesAndGetConnectionStringsAsync( IDictionary <string, List <DeviceDescription> > allDevices) { var result = new ConcurrentDictionary <string, string>(); string[] deviceIds = allDevices.Keys.ToArray(); Console.WriteLine($"{_actionMessage} devices..."); if (RemoveDevices) { await RemoveDevicesAsync(deviceIds); } else { const int maxDevicesPerCall = 100; int numberOfCallsRequired = (int)Math.Ceiling(deviceIds.Length / (double)maxDevicesPerCall); for (int i = 0; i < numberOfCallsRequired; i++) { string[] deviceIdsForCall = deviceIds.Skip(i * maxDevicesPerCall).Take(maxDevicesPerCall).ToArray(); BulkRegistryOperationResult bulkCreationResult = await _registryManager.AddDevices2Async(deviceIdsForCall.Select(id => new Device(id))); if (!bulkCreationResult.IsSuccessful) { Console.WriteLine("Failed creating devices..."); foreach (DeviceRegistryOperationError creationError in bulkCreationResult.Errors) { Console.WriteLine($"Device {creationError.DeviceId}: {creationError.ErrorStatus}"); } throw new Exception(); } } } Console.WriteLine($"{_actionMessage} devices completed."); if (!RemoveDevices) { Console.WriteLine("Retrieving connection strings..."); await InitializeBlobContainerAsync(); string sasToken = _deviceExportContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy(), "saspolicy"); var containerSasUri = $"{_deviceExportContainer.Uri}{sasToken}"; var job = await _registryManager.ExportDevicesAsync(containerSasUri, false); while (true) { job = await _registryManager.GetJobAsync(job.JobId); if (job.Status == JobStatus.Completed || job.Status == JobStatus.Failed || job.Status == JobStatus.Cancelled) { // Job has finished executing break; } await Task.Delay(TimeSpan.FromSeconds(5)); } var exportedDevices = new List <ExportImportDevice>(); var blob = _deviceExportContainer.GetBlobReference("devices.txt"); using (var streamReader = new StreamReader(await blob.OpenReadAsync(), Encoding.UTF8)) { while (streamReader.Peek() != -1) { string line = await streamReader.ReadLineAsync(); var device = JsonConvert.DeserializeObject <ExportImportDevice>(line); exportedDevices.Add(device); } } await _deviceExportContainer.DeleteIfExistsAsync(); await File.WriteAllTextAsync(CreatedDevicesJsonFileName, JsonConvert.SerializeObject(exportedDevices)); foreach (ExportImportDevice device in exportedDevices.OrderBy(d => d.Id)) { result[device.Id] = $"HostName={_iotHubConnectionStringBuilder.HostName};DeviceId={device.Id};SharedAccessKey={device.Authentication.SymmetricKey.PrimaryKey}"; } Console.WriteLine("Retrieval complete."); } return(result); }