From 15f2b22920e309f319b533b992a6cc42e773b816 Mon Sep 17 00:00:00 2001 From: timtay-microsoft Date: Mon, 4 Oct 2021 11:31:20 -0700 Subject: [PATCH 1/2] fix(iot-serv): Fix bug where device scope and parent scopes set to device weren't used in bulk add operations --- .../iothub/service/RegistryManagerE2ETests.cs | 12 ++++++++++-- iothub/service/src/ExportImportDevice.cs | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/e2e/test/iothub/service/RegistryManagerE2ETests.cs b/e2e/test/iothub/service/RegistryManagerE2ETests.cs index 0e4c681fcb..f0c5af42bb 100644 --- a/e2e/test/iothub/service/RegistryManagerE2ETests.cs +++ b/e2e/test/iothub/service/RegistryManagerE2ETests.cs @@ -121,7 +121,10 @@ public async Task RegistryManager_BulkLifecycle() var devices = new List(); for (int i = 0; i < bulkCount; i++) { - devices.Add(new Device(_devicePrefix + Guid.NewGuid())); + var device = new Device(_devicePrefix + Guid.NewGuid()); + device.Scope = "someScope" + Guid.NewGuid(); + device.ParentScopes.Add("someParentScope" + Guid.NewGuid()); + devices.Add(device); } using RegistryManager registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString); @@ -132,8 +135,13 @@ public async Task RegistryManager_BulkLifecycle() foreach (Device device in devices) { + Device retrievedDevice = await registryManager.GetDeviceAsync(device.Id).ConfigureAwait(false); + // After a bulk add, every device should be able to be retrieved - Assert.IsNotNull(await registryManager.GetDeviceAsync(device.Id).ConfigureAwait(false)); + Assert.IsNotNull(retrievedDevice.Id); + Assert.AreEqual(device.Scope, retrievedDevice.Scope); + Assert.AreEqual(1, retrievedDevice.ParentScopes.Count); + Assert.AreEqual(device.ParentScopes.ElementAt(0), retrievedDevice.ParentScopes.ElementAt(0)); } var twins = new List(); diff --git a/iothub/service/src/ExportImportDevice.cs b/iothub/service/src/ExportImportDevice.cs index eacfc12b6d..79d1a60a9e 100644 --- a/iothub/service/src/ExportImportDevice.cs +++ b/iothub/service/src/ExportImportDevice.cs @@ -5,6 +5,7 @@ // --------------------------------------------------------------- using System; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Microsoft.Azure.Devices.Shared; using Newtonsoft.Json; @@ -64,6 +65,8 @@ public ExportImportDevice(Device device, ImportMode importmode) StatusReason = device.StatusReason; Authentication = device.Authentication; Capabilities = device.Capabilities; + DeviceScope = device.Scope; + ParentScopes = device.ParentScopes; } /// @@ -152,6 +155,21 @@ public string TwinETag [JsonProperty(PropertyName = "deviceScope", NullValueHandling = NullValueHandling.Include)] public string DeviceScope { get; set; } + /// + /// The scopes of the upper level edge devices if applicable. + /// + /// + /// For edge devices, the value to set a parent edge device can be retrieved from the parent edge device's property. + /// + /// For leaf devices, this could be set to the same value as or left for the service to copy over. + /// + /// For now, this list can only have 1 element in the collection. + /// + /// For more information, see . + /// + [JsonProperty(PropertyName = "parentScopes", NullValueHandling = NullValueHandling.Ignore)] + public IList ParentScopes { get; internal set; } = new List(); + private static string SanitizeETag(string eTag) { if (!string.IsNullOrWhiteSpace(eTag)) From 46543bc19fa17f609f003c4f3483ccc3acca29c0 Mon Sep 17 00:00:00 2001 From: timtay-microsoft Date: Mon, 4 Oct 2021 11:32:36 -0700 Subject: [PATCH 2/2] fixup --- e2e/test/iothub/service/RegistryManagerE2ETests.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/e2e/test/iothub/service/RegistryManagerE2ETests.cs b/e2e/test/iothub/service/RegistryManagerE2ETests.cs index f0c5af42bb..c8a1550281 100644 --- a/e2e/test/iothub/service/RegistryManagerE2ETests.cs +++ b/e2e/test/iothub/service/RegistryManagerE2ETests.cs @@ -135,9 +135,8 @@ public async Task RegistryManager_BulkLifecycle() foreach (Device device in devices) { - Device retrievedDevice = await registryManager.GetDeviceAsync(device.Id).ConfigureAwait(false); - // After a bulk add, every device should be able to be retrieved + Device retrievedDevice = await registryManager.GetDeviceAsync(device.Id).ConfigureAwait(false); Assert.IsNotNull(retrievedDevice.Id); Assert.AreEqual(device.Scope, retrievedDevice.Scope); Assert.AreEqual(1, retrievedDevice.ParentScopes.Count);