Skip to content

Commit b7fa581

Browse files
authored
[IoT Hub] Invoke a query in an IoT hub. (#11567)
* Invoke a query in an IoT hub.
1 parent b0e0bac commit b7fa581

File tree

12 files changed

+1496
-909
lines changed

12 files changed

+1496
-909
lines changed

src/IotHub/IotHub.Test/ScenarioTests/IotHubDPDeviceTests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ function Test-AzureRmIotHubDeviceLifecycle
6363
Assert-True { $newDevice3.Authentication.Type -eq 'CertificateAuthority' }
6464
Assert-False { $newDevice3.Capabilities.IotEdge }
6565

66+
# Count devices
67+
$totalDevices = Invoke-AzIotHubQuery -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -Query "select * from devices"
68+
Assert-True { $totalDevices.Count -eq 3}
69+
6670
# Get device twin
6771
$device1twin = Get-AzIotHubDeviceTwin -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device1
6872
Assert-True { $device1twin.DeviceId -eq $device1}

src/IotHub/IotHub.Test/ScenarioTests/IotHubDPModuleTests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ function Test-AzureRmIotHubModuleLifecycle
6161
Assert-True { $newModule2.DeviceId -eq $device1 }
6262
Assert-True { $newModule2.Authentication.Type -eq 'SelfSigned' }
6363

64+
# Count device modules
65+
$totalModules = Invoke-AzIotHubQuery -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -Query "select * from devices.modules where devices.Id='$device1'"
66+
Assert-True { $totalModules.Count -eq 2}
67+
6468
# Get module twin
6569
$module1twin = Get-AzIotHubModuleTwin -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device1 -ModuleId $module1
6670
Assert-True { $module1twin.DeviceId -eq $device1}

src/IotHub/IotHub.Test/SessionRecords/Microsoft.Azure.Commands.IotHub.Test.ScenarioTests.IotHubDPDeviceTests/TestAzureIotHubDeviceLifecycle.json

Lines changed: 587 additions & 518 deletions
Large diffs are not rendered by default.

src/IotHub/IotHub.Test/SessionRecords/Microsoft.Azure.Commands.IotHub.Test.ScenarioTests.IotHubDPModuleTests/TestAzureIotHubModuleLifecycle.json

Lines changed: 557 additions & 374 deletions
Large diffs are not rendered by default.

src/IotHub/IotHub/Az.IotHub.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ CmdletsToExport = 'Add-AzIotHubKey', 'Get-AzIotHubEventHubConsumerGroup',
105105
'Get-AzIotHubModuleTwin', 'Update-AzIotHubModuleTwin',
106106
'Add-AzIotHubConfiguration', 'Get-AzIotHubConfiguration',
107107
'Remove-AzIotHubConfiguration', 'Set-AzIotHubConfiguration',
108-
'Invoke-AzIotHubModuleMethod'
108+
'Invoke-AzIotHubModuleMethod', 'Invoke-AzIotHubQuery'
109109
# Variables to export from this module
110110
# VariablesToExport = @()
111111

src/IotHub/IotHub/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- `Remove-AzIotHubConfiguration`
3232
- `Set-AzIotHubConfiguration`
3333
* Added cmdlet to invoke an edge module method in an Iot Hub.
34+
* Added cmdlet to invoke a query in an IoT hub to retrieve information using a SQL-like language.
3435

3536
## Version 2.3.0
3637
* Added support to manage distributed settings per-device. New Cmdlets are:
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.Management.IotHub
16+
{
17+
using System.Collections.Generic;
18+
using System.Management.Automation;
19+
using Microsoft.Azure.Commands.Management.IotHub.Common;
20+
using Microsoft.Azure.Commands.Management.IotHub.Models;
21+
using Microsoft.Azure.Devices;
22+
using Microsoft.Azure.Management.IotHub;
23+
using Microsoft.Azure.Management.IotHub.Models;
24+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
25+
using ResourceManager.Common.ArgumentCompleters;
26+
27+
[Cmdlet("Invoke", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubQuery", DefaultParameterSetName = ResourceParameterSet, SupportsShouldProcess = true)]
28+
[OutputType(typeof(string))]
29+
public class InvokeAzIotHubQuery : IotHubBaseCmdlet
30+
{
31+
private const string ResourceIdParameterSet = "ResourceIdSet";
32+
private const string ResourceParameterSet = "ResourceSet";
33+
private const string InputObjectParameterSet = "InputObjectSet";
34+
35+
[Parameter(Position = 0, Mandatory = true, ParameterSetName = InputObjectParameterSet, ValueFromPipeline = true, HelpMessage = "IotHub object")]
36+
[ValidateNotNullOrEmpty]
37+
public PSIotHub InputObject { get; set; }
38+
39+
[Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Resource Group")]
40+
[ValidateNotNullOrEmpty]
41+
[ResourceGroupCompleter]
42+
public string ResourceGroupName { get; set; }
43+
44+
[Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceIdParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "IotHub Resource Id")]
45+
[ValidateNotNullOrEmpty]
46+
[ResourceIdCompleter("Microsoft.Devices/IotHubs")]
47+
public string ResourceId { get; set; }
48+
49+
[Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Iot Hub")]
50+
[ValidateNotNullOrEmpty]
51+
public string IotHubName { get; set; }
52+
53+
[Parameter(Position = 1, Mandatory = true, ParameterSetName = InputObjectParameterSet, HelpMessage = "User query to be executed.")]
54+
[Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "User query to be executed.")]
55+
[Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "User query to be executed.")]
56+
[ValidateNotNullOrEmpty]
57+
public string Query { get; set; }
58+
59+
[Parameter(Mandatory = false, HelpMessage = "Maximum number of elements to return. By default query has no cap.")]
60+
public int Top { get; set; }
61+
62+
public override void ExecuteCmdlet()
63+
{
64+
if (ShouldProcess(this.IotHubName, Properties.Resources.InvokeIotHubQuery))
65+
{
66+
IotHubDescription iotHubDescription;
67+
if (ParameterSetName.Equals(InputObjectParameterSet))
68+
{
69+
this.ResourceGroupName = this.InputObject.Resourcegroup;
70+
this.IotHubName = this.InputObject.Name;
71+
iotHubDescription = IotHubUtils.ConvertObject<PSIotHub, IotHubDescription>(this.InputObject);
72+
}
73+
else
74+
{
75+
if (ParameterSetName.Equals(ResourceIdParameterSet))
76+
{
77+
this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId);
78+
this.IotHubName = IotHubUtils.GetIotHubName(this.ResourceId);
79+
}
80+
81+
iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName);
82+
}
83+
84+
IEnumerable<SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName);
85+
SharedAccessSignatureAuthorizationRule policy = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.RegistryRead);
86+
PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName);
87+
RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString);
88+
89+
IQuery query;
90+
if (this.IsParameterBound(c => c.Top))
91+
{
92+
query = registryManager.CreateQuery(this.Query, this.Top);
93+
}
94+
else
95+
{
96+
query = registryManager.CreateQuery(this.Query);
97+
}
98+
99+
this.WriteObject(query.GetNextAsJsonAsync().GetAwaiter().GetResult(), true);
100+
}
101+
}
102+
}
103+
}

src/IotHub/IotHub/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/IotHub/IotHub/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,7 @@
237237
<data name="SetIotHubConfiguration" xml:space="preserve">
238238
<value>Set Configuration</value>
239239
</data>
240+
<data name="InvokeIotHubQuery" xml:space="preserve">
241+
<value>Query an Iot Hub</value>
242+
</data>
240243
</root>

src/IotHub/IotHub/help/Az.IotHub.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ Invoke a direct method on a device.
116116
### [Invoke-AzIotHubManualFailover](Invoke-AzIotHubManualFailover.md)
117117
Invoke failover process for the IoT Hub to the geo-paired disaster recovery region.
118118

119+
### [Invoke-AzIotHubModuleMethod](Invoke-AzIotHubModuleMethod.md)
120+
Invoke an Edge module method.
121+
122+
### [Invoke-AzIotHubQuery](Invoke-AzIotHubQuery.md)
123+
Query an IoT Hub using a powerful SQL-like language.
124+
119125
### [New-AzIotHub](New-AzIotHub.md)
120126
Creates a new IotHub.
121127

src/IotHub/IotHub/help/Invoke-AzIotHubModuleMethod.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
---
22
external help file: Microsoft.Azure.PowerShell.Cmdlets.IotHub.dll-Help.xml
33
Module Name: Az.IotHub
44
online version: https://docs.microsoft.com/en-us/powershell/module/az.iothub/invoke-aziothubmodulemethod
@@ -53,7 +53,7 @@ Number of seconds to wait until a connection is successfully made.
5353
Default is 10.
5454

5555
```yaml
56-
Type: Int32
56+
Type: System.Int32
5757
Parameter Sets: (All)
5858
Aliases:
5959

@@ -68,7 +68,7 @@ Accept wildcard characters: False
6868
The credentials, account, tenant, and subscription used for communication with Azure.
6969
7070
```yaml
71-
Type: IAzureContextContainer
71+
Type: Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer
7272
Parameter Sets: (All)
7373
Aliases: AzContext, AzureRmContext, AzureCredential
7474

@@ -83,7 +83,7 @@ Accept wildcard characters: False
8383
Target Device Id.
8484
8585
```yaml
86-
Type: String
86+
Type: System.String
8787
Parameter Sets: (All)
8888
Aliases:
8989

@@ -98,7 +98,7 @@ Accept wildcard characters: False
9898
IotHub object
9999
100100
```yaml
101-
Type: PSIotHub
101+
Type: Microsoft.Azure.Commands.Management.IotHub.Models.PSIotHub
102102
Parameter Sets: InputObjectSet
103103
Aliases:
104104

@@ -113,7 +113,7 @@ Accept wildcard characters: False
113113
Name of the Iot Hub
114114
115115
```yaml
116-
Type: String
116+
Type: System.String
117117
Parameter Sets: ResourceSet
118118
Aliases:
119119

@@ -128,7 +128,7 @@ Accept wildcard characters: False
128128
Target device's module id.
129129
130130
```yaml
131-
Type: String
131+
Type: System.String
132132
Parameter Sets: (All)
133133
Aliases:
134134

@@ -143,7 +143,7 @@ Accept wildcard characters: False
143143
The name of the method to invoke on this device module.
144144
145145
```yaml
146-
Type: String
146+
Type: System.String
147147
Parameter Sets: (All)
148148
Aliases:
149149

@@ -158,7 +158,7 @@ Accept wildcard characters: False
158158
The payload for the method to invoke on this device module.
159159
160160
```yaml
161-
Type: String
161+
Type: System.String
162162
Parameter Sets: (All)
163163
Aliases:
164164

@@ -173,7 +173,7 @@ Accept wildcard characters: False
173173
Name of the Resource Group
174174
175175
```yaml
176-
Type: String
176+
Type: System.String
177177
Parameter Sets: ResourceSet
178178
Aliases:
179179

@@ -188,7 +188,7 @@ Accept wildcard characters: False
188188
IotHub Resource Id
189189
190190
```yaml
191-
Type: String
191+
Type: System.String
192192
Parameter Sets: ResourceIdSet
193193
Aliases:
194194

@@ -204,7 +204,7 @@ Number of seconds to wait until a result is received from the direct method.
204204
Default is 10.
205205
206206
```yaml
207-
Type: Int32
207+
Type: System.Int32
208208
Parameter Sets: (All)
209209
Aliases:
210210

@@ -219,7 +219,7 @@ Accept wildcard characters: False
219219
Prompts you for confirmation before running the cmdlet.
220220
221221
```yaml
222-
Type: SwitchParameter
222+
Type: System.Management.Automation.SwitchParameter
223223
Parameter Sets: (All)
224224
Aliases: cf
225225

@@ -235,7 +235,7 @@ Shows what would happen if the cmdlet runs.
235235
The cmdlet is not run.
236236
237237
```yaml
238-
Type: SwitchParameter
238+
Type: System.Management.Automation.SwitchParameter
239239
Parameter Sets: (All)
240240
Aliases: wi
241241

@@ -247,8 +247,7 @@ Accept wildcard characters: False
247247
```
248248
249249
### CommonParameters
250-
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.
251-
For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).
250+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).
252251
253252
## INPUTS
254253

0 commit comments

Comments
 (0)