Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit c30480f

Browse files
committed
Merge branch 'master' of https://github.com/Azure-Samples/azure-iot-samples-csharp into robinsh-csharp-impexp
2 parents 9ec117b + 12335c0 commit c30480f

File tree

13 files changed

+621
-0
lines changed

13 files changed

+621
-0
lines changed

build.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ try {
112112
#RunApp iot-hub\Samples\module\MessageSample "IoTHub\Module\MessageSample"
113113
#RunApp iot-hub\Samples\module\MethodSample "IoTHub\Module\MethodSample"
114114

115+
RunApp iot-hub\Samples\service\CleanUpDevicesSample "IoTHub\Service\CleanUpDevicesSample"
115116
RunApp iot-hub\Samples\service\AutomaticDeviceManagementSample "IoTHub\Service\AutomaticDeviceManagementSample"
116117
RunApp iot-hub\Samples\service\JobsSample "IoTHub\Service\JobsSample"
117118
RunApp iot-hub\Samples\service\RegistryManagerSample "IoTHub\Service\RegistryManagerSample"
@@ -122,6 +123,7 @@ try {
122123

123124
# TODO #11: Modify Provisioning\device samples to run unattended.
124125

126+
RunApp provisioning\Samples\service\CleanupEnrollmentsSample "Provisioning\Service\CleanupEnrollmentsSample"
125127
RunApp provisioning\Samples\service\BulkOperationSample "Provisioning\Service\BulkOperationSample"
126128
# TODO #11 :RunApp provisioning\Samples\service\EnrollmentGroupSample "Provisioning\Service\EnrollmentGroupSample"
127129
RunApp provisioning\Samples\service\EnrollmentSample "Provisioning\Service\EnrollmentSample"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
6+
namespace Microsoft.Azure.Devices.Samples
7+
{
8+
public class CleanUpDevicesSample
9+
{
10+
private RegistryManager _rm;
11+
private List<string> devicesToBeRetained =
12+
new List<string>{
13+
"DoNotDelete1"
14+
};
15+
16+
public CleanUpDevicesSample(RegistryManager rm)
17+
{
18+
_rm = rm ?? throw new ArgumentNullException(nameof(rm));
19+
}
20+
21+
public async Task RunSampleAsync()
22+
{
23+
try
24+
{
25+
string countSqlQuery = "SELECT COUNT() AS numberOfDevices FROM devices";
26+
IQuery countQuery = _rm.CreateQuery(countSqlQuery);
27+
while (countQuery.HasMoreResults)
28+
{
29+
IEnumerable<string> result = await countQuery.GetNextAsJsonAsync().ConfigureAwait(false);
30+
foreach (var item in result)
31+
{
32+
Console.WriteLine($"Total no of devices on the hub: \n{item}");
33+
}
34+
}
35+
36+
int devicesDeleted = 0;
37+
Console.WriteLine("Clean up devices:");
38+
string sqlQueryString = "select * from devices";
39+
IQuery query = _rm.CreateQuery(sqlQueryString);
40+
while (query.HasMoreResults)
41+
{
42+
IEnumerable<Shared.Twin> result = await query.GetNextAsTwinAsync();
43+
foreach (var twinResult in result)
44+
{
45+
string deviceId = twinResult.DeviceId;
46+
if (!devicesToBeRetained.Contains(deviceId))
47+
{
48+
Console.WriteLine($"Removing Device ID: {deviceId}");
49+
await _rm.RemoveDeviceAsync(deviceId).ConfigureAwait(false);
50+
devicesDeleted++;
51+
}
52+
}
53+
}
54+
Console.Write($"Total no of devices deleted: {devicesDeleted}");
55+
}
56+
catch (Exception ex)
57+
{
58+
Console.WriteLine($"Exception {ex.Message}");
59+
}
60+
}
61+
}
62+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Azure.Devices" Version="1.17.2" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
namespace Microsoft.Azure.Devices.Samples
4+
{
5+
public class Program
6+
{
7+
// The IoT Hub connection string. This is available under the "Shared access policies" in the Azure portal.
8+
9+
// For this sample either:
10+
// - pass this value as a command-prompt argument
11+
// - set the IOTHUB_CONN_STRING_CSHARP environment variable
12+
// - create a launchSettings.json (see launchSettings.json.template) containing the variable
13+
private static string s_connectionString = Environment.GetEnvironmentVariable("IOTHUB_CONN_STRING_CSHARP");
14+
15+
public static int Main(string[] args)
16+
{
17+
if (args.Length > 0)
18+
{
19+
s_connectionString = args[0];
20+
}
21+
22+
RegistryManager rm = RegistryManager.CreateFromConnectionString(s_connectionString);
23+
24+
var sample = new CleanUpDevicesSample(rm);
25+
sample.RunSampleAsync().GetAwaiter().GetResult();
26+
27+
Console.WriteLine("Done.\n");
28+
Console.ReadLine();
29+
return 0;
30+
}
31+
}
32+
}

iot-hub/Samples/service/IoTHubServiceSamples.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceClientSample", "Serv
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceClientStreamingSample", "DeviceStreamingSample\ServiceClientStreamingSample.csproj", "{8F047127-FCB6-4D55-BDD1-DBBEFCF1F74A}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CleanUpDevicesSample", "CleanUpDevicesSample\CleanUpDevicesSample.csproj", "{2C710192-E36A-4413-BE53-D1F4A00265BC}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -69,6 +71,18 @@ Global
6971
{8F047127-FCB6-4D55-BDD1-DBBEFCF1F74A}.Release|x64.Build.0 = Release|Any CPU
7072
{8F047127-FCB6-4D55-BDD1-DBBEFCF1F74A}.Release|x86.ActiveCfg = Release|Any CPU
7173
{8F047127-FCB6-4D55-BDD1-DBBEFCF1F74A}.Release|x86.Build.0 = Release|Any CPU
74+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
76+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Debug|x64.ActiveCfg = Debug|Any CPU
77+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Debug|x64.Build.0 = Debug|Any CPU
78+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Debug|x86.ActiveCfg = Debug|Any CPU
79+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Debug|x86.Build.0 = Debug|Any CPU
80+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
81+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Release|Any CPU.Build.0 = Release|Any CPU
82+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Release|x64.ActiveCfg = Release|Any CPU
83+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Release|x64.Build.0 = Release|Any CPU
84+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Release|x86.ActiveCfg = Release|Any CPU
85+
{2C710192-E36A-4413-BE53-D1F4A00265BC}.Release|x86.Build.0 = Release|Any CPU
7286
EndGlobalSection
7387
GlobalSection(SolutionProperties) = preSolution
7488
HideSolutionNode = FALSE
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Microsoft.Azure.Devices.Provisioning.Service.Samples
11+
{
12+
public class CleanupEnrollmentsSample
13+
{
14+
private ProvisioningServiceClient _provisioningServiceClient;
15+
// Maximum number of elements per query.
16+
private const int QueryPageSize = 10;
17+
private static int _individualEnrollmentsDeleted;
18+
private static int _enrollmentGroupsDeleted;
19+
private List<string> individualEnrollmentsToBeRetained =
20+
new List<string>{
21+
"iothubx509device1"
22+
};
23+
private List<string> groupEnrollmentsToBeRetained =
24+
new List<string>{
25+
"group-certificate-x509",
26+
"group1"
27+
};
28+
29+
public CleanupEnrollmentsSample(ProvisioningServiceClient provisioningServiceClient)
30+
{
31+
_provisioningServiceClient = provisioningServiceClient;
32+
_individualEnrollmentsDeleted = 0;
33+
_enrollmentGroupsDeleted = 0;
34+
}
35+
36+
public async Task RunSampleAsync()
37+
{
38+
await QueryAndDeleteIndividualEnrollments().ConfigureAwait(false);
39+
Console.WriteLine($"Individual Enrollments Deleted: {_individualEnrollmentsDeleted}");
40+
await QueryAndDeleteEnrollmentGroups().ConfigureAwait(false);
41+
Console.WriteLine($"Enrollment Groups Deleted: {_enrollmentGroupsDeleted}");
42+
}
43+
44+
private async Task QueryAndDeleteIndividualEnrollments()
45+
{
46+
Console.WriteLine("\nCreating a query for enrollments...");
47+
QuerySpecification querySpecification = new QuerySpecification("SELECT * FROM enrollments");
48+
using (Query query = _provisioningServiceClient.CreateIndividualEnrollmentQuery(querySpecification, QueryPageSize))
49+
{
50+
while (query.HasNext())
51+
{
52+
Console.WriteLine("\nQuerying the next enrollments...");
53+
QueryResult queryResult = await query.NextAsync().ConfigureAwait(false);
54+
var items = queryResult.Items;
55+
List<IndividualEnrollment> individualEnrollments = new List<IndividualEnrollment>();
56+
foreach (IndividualEnrollment enrollment in items)
57+
{
58+
if (!individualEnrollmentsToBeRetained.Contains(enrollment.RegistrationId))
59+
{
60+
individualEnrollments.Add(enrollment);
61+
Console.WriteLine($"Individual Enrollment to be deleted: {enrollment.RegistrationId}");
62+
_individualEnrollmentsDeleted++;
63+
}
64+
}
65+
if (individualEnrollments.Count > 0)
66+
{
67+
await DeleteBulkIndividualEnrollments(individualEnrollments).ConfigureAwait(false);
68+
}
69+
}
70+
}
71+
}
72+
73+
private async Task QueryAndDeleteEnrollmentGroups()
74+
{
75+
Console.WriteLine("\nCreating a query for enrollmentGroups...");
76+
QuerySpecification querySpecification = new QuerySpecification("SELECT * FROM enrollmentGroups");
77+
using (Query query = _provisioningServiceClient.CreateEnrollmentGroupQuery(querySpecification, QueryPageSize))
78+
{
79+
while (query.HasNext())
80+
{
81+
Console.WriteLine("\nQuerying the next enrollmentGroups...");
82+
QueryResult queryResult = await query.NextAsync().ConfigureAwait(false);
83+
var items = queryResult.Items;
84+
foreach (EnrollmentGroup enrollment in items)
85+
{
86+
if (!groupEnrollmentsToBeRetained.Contains(enrollment.EnrollmentGroupId))
87+
{
88+
Console.WriteLine($"EnrollmentGroup to be deleted: {enrollment.EnrollmentGroupId}");
89+
_enrollmentGroupsDeleted++;
90+
await _provisioningServiceClient.DeleteEnrollmentGroupAsync(enrollment.EnrollmentGroupId).ConfigureAwait(false);
91+
}
92+
}
93+
}
94+
}
95+
}
96+
97+
private async Task DeleteBulkIndividualEnrollments(List<IndividualEnrollment> individualEnrollments)
98+
{
99+
Console.WriteLine("\nDeleting the set of individualEnrollments...");
100+
BulkEnrollmentOperationResult bulkEnrollmentOperationResult =
101+
await _provisioningServiceClient.RunBulkEnrollmentOperationAsync(BulkOperationMode.Delete, individualEnrollments).ConfigureAwait(false);
102+
Console.WriteLine(bulkEnrollmentOperationResult);
103+
}
104+
}
105+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Azure.Devices.Provisioning.Service" Version="1.5.0" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
6+
namespace Microsoft.Azure.Devices.Provisioning.Service.Samples
7+
{
8+
public class Program
9+
{
10+
// The Provisioning Service connection string. This is available under the "Shared access policies" in the Azure portal.
11+
12+
// For this sample either:
13+
// - pass this value as a command-prompt argument
14+
// - set the PROVISIONING_CONNECTION_STRING environment variable
15+
// - create a launchSettings.json (see launchSettings.json.template) containing the variable
16+
private static string s_connectionString = Environment.GetEnvironmentVariable("PROVISIONING_CONNECTION_STRING");
17+
18+
public static int Main(string[] args)
19+
{
20+
if (args.Length > 0)
21+
{
22+
s_connectionString = args[0];
23+
}
24+
25+
using (var provisioningServiceClient = ProvisioningServiceClient.CreateFromConnectionString(s_connectionString))
26+
{
27+
var sample = new CleanupEnrollmentsSample(provisioningServiceClient);
28+
sample.RunSampleAsync().GetAwaiter().GetResult();
29+
}
30+
31+
Console.WriteLine("Done.\n");
32+
Console.ReadLine();
33+
return 0;
34+
}
35+
}
36+
}

provisioning/Samples/service/ProvisioningServiceSamples.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EnrollmentSample", "Enrollm
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EnrollmentGroupSample", "EnrollmentGroupSample\EnrollmentGroupSample.csproj", "{844A3605-C7DA-4E5B-A89A-4EF2DD2DBD21}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CleanupEnrollmentsSample", "CleanupEnrollmentsSample\CleanupEnrollmentsSample.csproj", "{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -55,6 +57,18 @@ Global
5557
{844A3605-C7DA-4E5B-A89A-4EF2DD2DBD21}.Release|x64.Build.0 = Release|Any CPU
5658
{844A3605-C7DA-4E5B-A89A-4EF2DD2DBD21}.Release|x86.ActiveCfg = Release|Any CPU
5759
{844A3605-C7DA-4E5B-A89A-4EF2DD2DBD21}.Release|x86.Build.0 = Release|Any CPU
60+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
61+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Debug|Any CPU.Build.0 = Debug|Any CPU
62+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Debug|x64.ActiveCfg = Debug|Any CPU
63+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Debug|x64.Build.0 = Debug|Any CPU
64+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Debug|x86.ActiveCfg = Debug|Any CPU
65+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Debug|x86.Build.0 = Debug|Any CPU
66+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Release|Any CPU.ActiveCfg = Release|Any CPU
67+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Release|Any CPU.Build.0 = Release|Any CPU
68+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Release|x64.ActiveCfg = Release|Any CPU
69+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Release|x64.Build.0 = Release|Any CPU
70+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Release|x86.ActiveCfg = Release|Any CPU
71+
{CA5D3AAF-2E72-46DA-87C8-75137D7D701C}.Release|x86.Build.0 = Release|Any CPU
5872
EndGlobalSection
5973
GlobalSection(SolutionProperties) = preSolution
6074
HideSolutionNode = FALSE

vsts/CredScanSuppressions.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"tool": "Credential Scanner",
3+
"suppressions": [
4+
{
5+
"placeholder": "dGVzdFN0cmluZzE=",
6+
"_justification": "Dummy secret used in unit tests, it is fake"
7+
},
8+
{
9+
"placeholder": "dGVzdFN0cmluZzI=",
10+
"_justification": "Dummy secret used in unit tests, it is fake"
11+
},
12+
{
13+
"placeholder": "dGVzdFN0cmluZzM=",
14+
"_justification": "Dummy secret used in unit tests, it is fake"
15+
},
16+
{
17+
"placeholder": "dGVzdFN0cmluZzQ=",
18+
"_justification": "Dummy secret used in unit tests, it is fake"
19+
},
20+
{
21+
"placeholder": "dGVzdFN0cmluZzU=",
22+
"_justification": "Dummy secret used in unit tests, it is fake"
23+
},
24+
{
25+
"placeholder": "dGVzdFN0cmluZzY=",
26+
"_justification": "Dummy secret used in unit tests, it is fake"
27+
},
28+
{
29+
"placeholder": "notsafe",
30+
"_justification": "Dummy secret used in unit tests, it is fake"
31+
}
32+
]
33+
}

0 commit comments

Comments
 (0)