Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IotHub] Manage distributed settings per-device #11226

Merged
merged 6 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/IotHub/IotHub.Test/ScenarioTests/IotHubDPTracingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.ServiceManagement.Common.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Azure.Commands.IotHub.Test.ScenarioTests
{
public class IotHubDPTracingTests : RMTestBase
{
public XunitTracingInterceptor _logger;

public IotHubDPTracingTests(ITestOutputHelper output)
{
_logger = new XunitTracingInterceptor(output);
XunitTracingInterceptor.AddToContext(_logger);
}

[Fact]
[Trait(Category.AcceptanceType, Category.LiveOnly)]
public void TestAzureIotHubTracing()
{
IotHubController.NewInstance.RunPsTest(_logger, "Test-AzureRmIotHubTracing");
}
}
}
60 changes: 60 additions & 0 deletions src/IotHub/IotHub.Test/ScenarioTests/IotHubDPTracingTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------


#################################
## IotHub Tracing Cmdlets ##
#################################

<#
.SYNOPSIS
Test all iothub device tracing cmdlets
#>
function Test-AzureRmIotHubTracing
{
$Location = Get-Location "Microsoft.Devices" "IotHubs" "WEST US 2"
$IotHubName = getAssetName
$ResourceGroupName = getAssetName
$Sku = "S1"
$device1 = getAssetName

# Create Resource Group
$resourceGroup = New-AzResourceGroup -Name $ResourceGroupName -Location $Location

# Create Iot Hub
$iothub = New-AzIotHub -Name $IotHubName -ResourceGroupName $ResourceGroupName -Location $Location -SkuName $Sku -Units 1

# Add iot device
$newDevice1 = Add-AzIotHubDevice -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device1 -AuthMethod 'shared_private_key'
Assert-True { $newDevice1.Id -eq $device1 }
Assert-False { $newDevice1.Capabilities.IotEdge }

# Get device tracing detail
$deviceTracing = Get-AzIotHubDistributedTracing -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device1
Assert-True { $deviceTracing.DeviceId -eq $device1}
Assert-True { $deviceTracing.TracingOption.SamplingMode -eq 'Disabled'}
Assert-True { $deviceTracing.TracingOption.SamplingRate -eq ''}
Assert-False { $deviceTracing.IsSynced}

# Set device tracing option
$updatedDeviceTracing = Set-AzIotHubDistributedTracing -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device1 -mode 'Enabled' -rate 45
Assert-True { $updatedDeviceTracing.DeviceId -eq $device1}
Assert-True { $updatedDeviceTracing.TracingOption.SamplingMode -eq 'Enabled'}
Assert-True { $updatedDeviceTracing.TracingOption.SamplingRate -eq 45}
Assert-False { $updatedDeviceTracing.IsSynced}

# Delete all devices
$result = Remove-AzIotHubDevice -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -Passthru
Assert-True { $result }
}

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/IotHub/IotHub/Az.IotHub.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ CmdletsToExport = 'Add-AzIotHubKey', 'Get-AzIotHubEventHubConsumerGroup',
'Set-AzIotHubModule', 'Get-AzIotHubDeviceConnectionString',
'Get-AzIotHubModuleConnectionString', 'Get-AzIotHubDeviceParent',
'Set-AzIotHubDeviceParent', 'Add-AzIotHubDeviceChildren',
'Remove-AzIotHubDeviceChildren', 'Get-AzIotHubDeviceChildren'

'Remove-AzIotHubDeviceChildren', 'Get-AzIotHubDeviceChildren',
'Get-AzIotHubDistributedTracing', 'Set-AzIotHubDistributedTracing'
# Variables to export from this module
# VariablesToExport = @()

Expand All @@ -109,7 +109,8 @@ AliasesToExport = 'Get-AzIotHubEHCG', 'Add-AzIotHubEHCG', 'Remove-AzIotHubEHCG',
'Set-AzIotHubVC', 'Get-AzIotHubCVC', 'Add-AzIotHubMsgEnrich',
'Get-AzIotHubMsgEnrich', 'Remove-AzIotHubMsgEnrich',
'Set-AzIotHubMsgEnrich', 'Get-AzIotHubDCS', 'Get-AzIotHubMCS',
'Add-AzIotHubDCL', 'Remove-AzIotHubDCL', 'Get-AzIotHubDCL'
'Add-AzIotHubDCL', 'Remove-AzIotHubDCL', 'Get-AzIotHubDCL',
'Get-AzIotHubTracing', 'Set-AzIotHubTracing'

# DSC resources to export from this module
# DscResourcesToExport = @()
Expand Down
3 changes: 3 additions & 0 deletions src/IotHub/IotHub/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
- `Get-AzIotHubDeviceParent`
- `Set-AzIotHubDeviceParent`
* Added support to manage device parent-child relationship.
* Added support to manage distributed settings per-device. New Cmdlets are:
- `Get-AzIotHubDistributedTracing`
- `Set-AzIotHubDistributedTracing`

## Version 2.1.0
* Added support to manage devices in an Iot Hub. New Cmdlets are:
Expand Down
51 changes: 51 additions & 0 deletions src/IotHub/IotHub/Common/IotHubDataPlaneUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@

namespace Microsoft.Azure.Commands.Management.IotHub.Common
{
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.Commands.Management.IotHub.Models;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
using Newtonsoft.Json;

public static class IotHubDataPlaneUtils
{
public const string DeviceScopePrefix = "ms-azure-iot-edge://";
public const string TracingAllowedForSku = "standard";
public const string TracingProperty = "azureiot*com^dtracing^1";

public static readonly string[] TracingAllowedForLocation = { "northeurope", "westus2", "west us 2", "southeastasia" };

public static string GetEdgeDevices()
{
return "select * from devices where capabilities.iotEdge=true";
Expand Down Expand Up @@ -60,5 +69,47 @@ public static IEnumerable<PSModules> ToPSModules(IEnumerable<Module> modules)
{
return IotHubUtils.ConvertObject<IEnumerable<Module>, IEnumerable<PSModules>>(modules.ToList());
}

public static void ValidateDeviceTracing(string DeviceId, string Sku, string Location, bool IsEdgeDevice)
{
if (!TracingAllowedForLocation.Any(location => location.Equals(Location, StringComparison.OrdinalIgnoreCase)))
{
throw new ArgumentException($"Distributed tracing isn\'t supported for the hub located at \"{Location}\"");
}
if (!TracingAllowedForSku.Equals(Sku, StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException($"Distributed tracing isn\'t supported for the hub belongs to \"{Sku}\" sku tier.");
}
if (IsEdgeDevice)
{
throw new ArgumentException($"The device \"{DeviceId}\" should be non-edge device.");
}
}

public static PSDeviceTracing GetDeviceTracing(string DeviceId, Twin deviceTwin)
{
PSDeviceTracing psDeviceTracing = new PSDeviceTracing
{
DeviceId = DeviceId,
TracingOption = new PSDistributedTracing() { SamplingMode = PSDistributedTracingSamplingMode.Disabled, SamplingRate = 0 },
IsSynced = false
};

if (deviceTwin.Properties.Desired.Contains(TracingProperty))
{
psDeviceTracing.TracingOption = JsonConvert.DeserializeObject<PSDistributedTracing>(deviceTwin.Properties.Desired[TracingProperty].ToString());
}

if (deviceTwin.Properties.Reported.Contains(TracingProperty))
{
PSDistributedTracing psReportedDistributedTracing = JsonConvert.DeserializeObject<PSDistributedTracing>(deviceTwin.Properties.Reported[TracingProperty].ToString());
if (psReportedDistributedTracing != null)
{
psDeviceTracing.IsSynced = (psDeviceTracing.TracingOption.SamplingMode.Equals(psReportedDistributedTracing.SamplingMode) && psDeviceTracing.TracingOption.SamplingRate.Equals(psReportedDistributedTracing.SamplingRate));
}
}

return psDeviceTracing;
}
}
}
30 changes: 30 additions & 0 deletions src/IotHub/IotHub/IotHub.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -898,5 +898,35 @@
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>
Microsoft.Azure.Commands.Management.IotHub.Models.PSDeviceTracing
</Name>
<ViewSelectedBy>
<TypeName>Microsoft.Azure.Commands.Management.IotHub.Models.PSDeviceTracing</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<PropertyName>DeviceId</PropertyName>
</ListItem>
<ListItem>
<Label>Sampling Mode</Label>
<ScriptBlock>$_.TracingOption.SamplingMode</ScriptBlock>
</ListItem>
<ListItem>
<Label>Sampling Rate</Label>
<ScriptBlock>if($_.TracingOption.SamplingMode.value__ -eq 1) { "$($_.TracingOption.SamplingRate)%" } else { "" }</ScriptBlock>
</ListItem>
<ListItem>
<PropertyName>IsSynced</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public override void ExecuteCmdlet()
{
throw new ArgumentException($"The entered device \"{this.DeviceId}\" doesn\'t support parent device functionality.");
}

string parentDeviceId = childDevice.Scope.Substring(Properties.Resources.DEVICE_DEVICESCOPE_PREFIX.Length, childDevice.Scope.LastIndexOf("-") - Properties.Resources.DEVICE_DEVICESCOPE_PREFIX.Length);
string parentDeviceId = childDevice.Scope.Substring(IotHubDataPlaneUtils.DeviceScopePrefix.Length, childDevice.Scope.LastIndexOf("-") - IotHubDataPlaneUtils.DeviceScopePrefix.Length);

this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(registryManager.GetDeviceAsync(parentDeviceId).GetAwaiter().GetResult()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Management.IotHub
{
using System;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.Azure.Commands.Management.IotHub.Common;
using Microsoft.Azure.Commands.Management.IotHub.Models;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Azure.Management.IotHub;
using Microsoft.Azure.Management.IotHub.Models;
using ResourceManager.Common.ArgumentCompleters;

[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubDistributedTracing", DefaultParameterSetName = ResourceParameterSet)]
[Alias("Get-" + ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubTracing")]
[OutputType(typeof(PSDeviceTracing))]
public class GetAzIotHubDistributedTracing : IotHubBaseCmdlet
{
private const string ResourceIdParameterSet = "ResourceIdSet";
private const string ResourceParameterSet = "ResourceSet";
private const string InputObjectParameterSet = "InputObjectSet";

[Parameter(Position = 0, Mandatory = true, ParameterSetName = InputObjectParameterSet, ValueFromPipeline = true, HelpMessage = "IotHub object")]
[ValidateNotNullOrEmpty]
public PSIotHub InputObject { get; set; }

[Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Resource Group")]
[ValidateNotNullOrEmpty]
[ResourceGroupCompleter]
public string ResourceGroupName { get; set; }

[Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceIdParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "IotHub Resource Id")]
[ValidateNotNullOrEmpty]
[ResourceIdCompleter("Microsoft.Devices/IotHubs")]
public string ResourceId { get; set; }

[Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Iot Hub")]
[ValidateNotNullOrEmpty]
public string IotHubName { get; set; }

[Parameter(Position = 1, Mandatory = true, ParameterSetName = InputObjectParameterSet, HelpMessage = "Target Device Id.")]
[Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Target Device Id.")]
[Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Target Device Id.")]
[ValidateNotNullOrEmpty]
public string DeviceId { get; set; }

public override void ExecuteCmdlet()
{
IotHubDescription iotHubDescription;
if (ParameterSetName.Equals(InputObjectParameterSet))
{
this.ResourceGroupName = this.InputObject.Resourcegroup;
this.IotHubName = this.InputObject.Name;
iotHubDescription = IotHubUtils.ConvertObject<PSIotHub, IotHubDescription>(this.InputObject);
}
else
{
if (ParameterSetName.Equals(ResourceIdParameterSet))
{
this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId);
this.IotHubName = IotHubUtils.GetIotHubName(this.ResourceId);
}

iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName);
}

IEnumerable<SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName);
SharedAccessSignatureAuthorizationRule policy = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.RegistryWrite);
PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName);
RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString);

Twin deviceTwin = registryManager.GetTwinAsync(this.DeviceId).GetAwaiter().GetResult();

if (deviceTwin == null)
{
throw new ArgumentException($"The entered non-edge device \"{this.DeviceId}\" doesn't exist.");
}

IotHubDataPlaneUtils.ValidateDeviceTracing(this.DeviceId, iotHubDescription.Sku.Tier.Value.ToString(), iotHubDescription.Location, deviceTwin.Capabilities.IotEdge);

this.WriteObject(IotHubDataPlaneUtils.GetDeviceTracing(this.DeviceId, deviceTwin));
}
}
}
Loading