Skip to content

Commit 86c3114

Browse files
authored
Merge pull request #10044 from anusapan/enrich
Add support of message enrichment in IotHub
2 parents 696edfc + ccc6508 commit 86c3114

20 files changed

+5353
-2337
lines changed

src/IotHub/IotHub.Test/ScenarioTests/IotHubRoutingTests.ps1

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function Test-AzureRmIotHubRoutingLifecycle
3232
$containerName2 = 'container2'
3333
##############################################################
3434

35-
$Location = Get-Location "Microsoft.Devices" "IotHub"
35+
$Location = Get-Location "Microsoft.Devices" "IotHubs" "WEST US 2"
3636
$IotHubName = getAssetName
3737
$ResourceGroupName = getAssetName
3838
$namespaceName = getAssetName 'eventHub'
@@ -42,6 +42,10 @@ function Test-AzureRmIotHubRoutingLifecycle
4242
$endpointName1 = getAssetName
4343
$endpointName2 = getAssetName
4444
$routeName = getAssetName
45+
$enrichmentName = getAssetName
46+
$enrichmentValue = getAssetName
47+
$enrichmentUpdatedValue = getAssetName
48+
$endpoints = @($endpointName1,$endpointName2)
4549
$Sku = "S1"
4650
$EndpointTypeEventHub = [Microsoft.Azure.Commands.Management.IotHub.Models.PSEndpointType] "EventHub"
4751
$EndpointTypeAzureStorage = [Microsoft.Azure.Commands.Management.IotHub.Models.PSEndpointType] "AzureStorageContainer"
@@ -107,6 +111,44 @@ function Test-AzureRmIotHubRoutingLifecycle
107111
Assert-True { $updatedRoutingEndpoints[2].Name -eq $endpointName2}
108112
Assert-True { $updatedRoutingEndpoints[2].EndpointType -eq $EndpointTypeAzureStorage}
109113

114+
# Get all message enrichments
115+
$enrichments = Get-AzIotHubMessageEnrichment -ResourceGroupName $ResourceGroupName -Name $IotHubName
116+
Assert-True { $enrichments.Count -eq 0}
117+
118+
# Add new message enrichment
119+
$newEnrichment = Add-AzIotHubMessageEnrichment -ResourceGroupName $ResourceGroupName -Name $IotHubName -Key $enrichmentName -Value $enrichmentValue -Endpoint $endpointName1
120+
Assert-True { $newEnrichment.Key -eq $enrichmentName}
121+
Assert-True { $newEnrichment.Value -eq $enrichmentValue}
122+
Assert-True { $newEnrichment.EndpointNames -eq $endpointName1}
123+
124+
# Get all message enrichments
125+
$enrichments = Get-AzIotHubMessageEnrichment -ResourceGroupName $ResourceGroupName -Name $IotHubName
126+
Assert-True { $enrichments.Count -eq 1}
127+
128+
# Get specific message enrichment
129+
$enrichment = Get-AzIotHubMessageEnrichment -ResourceGroupName $ResourceGroupName -Name $IotHubName -Key $enrichmentName
130+
Assert-True { $enrichment.Key -eq $enrichmentName}
131+
Assert-True { $enrichment.Value -eq $enrichmentValue}
132+
Assert-True { $enrichment.EndpointNames -eq $endpointName1}
133+
134+
# Update message enrichment's value
135+
$updatedEnrichment = Set-AzIotHubMessageEnrichment -ResourceGroupName $ResourceGroupName -Name $IotHubName -Key $enrichmentName -Value $enrichmentUpdatedValue
136+
Assert-True { $updatedEnrichment.Key -eq $enrichmentName}
137+
Assert-True { $updatedEnrichment.Value -eq $enrichmentUpdatedValue}
138+
Assert-True { $updatedEnrichment.EndpointNames -eq $endpointName1}
139+
140+
# Update message enrichment's endpoint
141+
$updatedEnrichment = Set-AzIotHubMessageEnrichment -ResourceGroupName $ResourceGroupName -Name $IotHubName -Key $enrichmentName -Endpoint $endpointName1,$endpointName2
142+
Assert-True { $updatedEnrichment.Key -eq $enrichmentName}
143+
Assert-True { $updatedEnrichment.Value -eq $enrichmentUpdatedValue}
144+
Assert-True { $updatedEnrichment.EndpointNames.Count -eq 2}
145+
Assert-True { $updatedEnrichment.EndpointNames[0] -eq $endpointName1}
146+
Assert-True { $updatedEnrichment.EndpointNames[1] -eq $endpointName2}
147+
148+
# Delete message enrichment
149+
$result = Remove-AzIotHubMessageEnrichment -ResourceGroupName $ResourceGroupName -Name $IotHubName -Key $enrichmentName -Passthru
150+
Assert-True { $result }
151+
110152
# Delete routing endpoints
111153
$result = Remove-AzureRmIotHubRoutingEndpoint -ResourceGroupName $ResourceGroupName -Name $IotHubName -EndpointName $endpointName1 -Passthru
112154
Assert-True { $result }

src/IotHub/IotHub.Test/SessionRecords/Microsoft.Azure.Commands.IotHub.Test.ScenarioTests.IotHubRoutingTests/TestAzureIotHubRoutingLifeCycle.json

Lines changed: 2288 additions & 854 deletions
Large diffs are not rendered by default.

src/IotHub/IotHub.Test/SessionRecords/Microsoft.Azure.Commands.IotHub.Test.ScenarioTests.IotHubTests/TestAzureIotHubLifeCycle.json

Lines changed: 1536 additions & 1479 deletions
Large diffs are not rendered by default.

src/IotHub/IotHub/Az.IotHub.psd1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,18 @@ CmdletsToExport = 'Add-AzIotHubKey', 'Get-AzIotHubEventHubConsumerGroup',
8888
'Get-AzIotHubRoutingEndpoint', 'Add-AzIotHubRoutingEndpoint',
8989
'Remove-AzIotHubRoutingEndpoint', 'Get-AzIotHubRoute',
9090
'Add-AzIotHubRoute', 'Remove-AzIotHubRoute', 'Set-AzIotHubRoute',
91-
'Test-AzIotHubRoute', 'New-AzIotHubKey', 'Invoke-AzIotHubManualFailover'
91+
'Test-AzIotHubRoute', 'New-AzIotHubKey', 'Invoke-AzIotHubManualFailover',
92+
'Add-AzIotHubMessageEnrichment', 'Get-AzIotHubMessageEnrichment',
93+
'Remove-AzIotHubMessageEnrichment', 'Set-AzIotHubMessageEnrichment'
9294

9395
# Variables to export from this module
9496
# VariablesToExport = @()
9597

9698
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
9799
AliasesToExport = 'Get-AzIotHubEHCG', 'Add-AzIotHubEHCG', 'Remove-AzIotHubEHCG',
98100
'Set-AzIotHubVC', 'Get-AzIotHubCVC', 'New-AzIotHubExportDevices',
99-
'New-AzIotHubImportDevices'
101+
'New-AzIotHubImportDevices', 'Add-AzIotHubMsgEnrich', 'Get-AzIotHubMsgEnrich',
102+
'Remove-AzIotHubMsgEnrich', 'Set-AzIotHubMsgEnrich'
100103

101104
# DSC resources to export from this module
102105
# DscResourcesToExport = @()

src/IotHub/IotHub/ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
-->
2020
## Upcoming Release
2121
* Add support to invoke failover for an IotHub to the geo-paired disaster recovery region.
22+
* Add support to manage message enrichment for an IotHub. New cmdlets are:
23+
- Add-AzIotHubMessageEnrichment
24+
- Get-AzIotHubMessageEnrichment
25+
- Remove-AzIotHubMessageEnrichment
26+
- Set-AzIotHubMessageEnrichment
2227

2328
## Version 1.2.1
2429
* Fixed miscellaneous typos across module

src/IotHub/IotHub/Common/IotHubUtils.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ public static PSRouteMetadata ToPSRouteMetadata(RouteProperties routeProperties)
159159
return ConvertObject<RouteProperties, PSRouteMetadata>(routeProperties);
160160
}
161161

162+
public static PSEnrichmentMetadata ToPSEnrichmentMetadata(EnrichmentProperties enrichmentProperties)
163+
{
164+
return ConvertObject<EnrichmentProperties, PSEnrichmentMetadata>(enrichmentProperties);
165+
}
166+
167+
public static IEnumerable<PSEnrichmentProperties> ToPSEnrichmentProperties(IEnumerable<EnrichmentProperties> enrichmentProperties)
168+
{
169+
return ConvertObject<IEnumerable<EnrichmentProperties>, IEnumerable<PSEnrichmentProperties>>(enrichmentProperties);
170+
}
171+
162172
public static FallbackRouteProperties ToFallbackRouteProperty(PSFallbackRouteMetadata psRouteProperty)
163173
{
164174
return ConvertObject<PSFallbackRouteMetadata, FallbackRouteProperties>(psRouteProperty);

src/IotHub/IotHub/IotHub.format.ps1xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,5 +621,70 @@
621621
</TableRowEntries>
622622
</TableControl>
623623
</View>
624+
<View>
625+
<Name>
626+
Microsoft.Azure.Commands.Management.IotHub.Models.PSEnrichmentMetadata
627+
</Name>
628+
<ViewSelectedBy>
629+
<TypeName>Microsoft.Azure.Commands.Management.IotHub.Models.PSEnrichmentMetadata</TypeName>
630+
</ViewSelectedBy>
631+
<ListControl>
632+
<ListEntries>
633+
<ListEntry>
634+
<ListItems>
635+
<ListItem>
636+
<Label>Key</Label>
637+
<ScriptBlock>$_.Key</ScriptBlock>
638+
</ListItem>
639+
<ListItem>
640+
<Label>Value</Label>
641+
<ScriptBlock>$_.Value</ScriptBlock>
642+
</ListItem>
643+
<ListItem>
644+
<Label>Endpoint(s)</Label>
645+
<ScriptBlock>$_.EndpointNames</ScriptBlock>
646+
</ListItem>
647+
</ListItems>
648+
</ListEntry>
649+
</ListEntries>
650+
</ListControl>
651+
</View>
652+
<View>
653+
<Name>
654+
Microsoft.Azure.Commands.Management.IotHub.Models.PSEnrichmentProperties
655+
</Name>
656+
<ViewSelectedBy>
657+
<TypeName>Microsoft.Azure.Commands.Management.IotHub.Models.PSEnrichmentProperties</TypeName>
658+
</ViewSelectedBy>
659+
<TableControl>
660+
<TableHeaders>
661+
<TableColumnHeader>
662+
<Label>Key</Label>
663+
</TableColumnHeader>
664+
<TableColumnHeader>
665+
<Label>Value</Label>
666+
</TableColumnHeader>
667+
<TableColumnHeader>
668+
<Label>Endpoint(s)</Label>
669+
</TableColumnHeader>
670+
</TableHeaders>
671+
<TableRowEntries>
672+
<TableRowEntry>
673+
<Wrap/>
674+
<TableColumnItems>
675+
<TableColumnItem>
676+
<ScriptBlock>$_.Key</ScriptBlock>
677+
</TableColumnItem>
678+
<TableColumnItem>
679+
<ScriptBlock>$_.Value</ScriptBlock>
680+
</TableColumnItem>
681+
<TableColumnItem>
682+
<ScriptBlock>$_.EndpointNames</ScriptBlock>
683+
</TableColumnItem>
684+
</TableColumnItems>
685+
</TableRowEntry>
686+
</TableRowEntries>
687+
</TableControl>
688+
</View>
624689
</ViewDefinitions>
625690
</Configuration>
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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;
18+
using System.Collections.Generic;
19+
using System.Linq;
20+
using System.Management.Automation;
21+
using Microsoft.Azure.Commands.Management.IotHub.Common;
22+
using Microsoft.Azure.Commands.Management.IotHub.Models;
23+
using Microsoft.Azure.Management.IotHub;
24+
using Microsoft.Azure.Management.IotHub.Models;
25+
using ResourceManager.Common.ArgumentCompleters;
26+
27+
[Cmdlet("Add", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubMessageEnrichment", DefaultParameterSetName = ResourceParameterSet, SupportsShouldProcess = true), OutputType(typeof(PSEnrichmentMetadata))]
28+
[Alias("Add-" + ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubMsgEnrich")]
29+
public class AddAzureRmIotHubMessageEnrichment : 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 Name { get; set; }
52+
53+
[Parameter(Position = 1, Mandatory = true, ParameterSetName = InputObjectParameterSet, HelpMessage = "The enrichment's key.")]
54+
[Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "The enrichment's key.")]
55+
[Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "The enrichment's key.")]
56+
[ValidateNotNullOrEmpty]
57+
public string Key { get; set; }
58+
59+
[Parameter(Mandatory = true, ParameterSetName = InputObjectParameterSet, HelpMessage = "The enrichment's value.")]
60+
[Parameter(Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "The enrichment's value.")]
61+
[Parameter(Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "The enrichment's value.")]
62+
[ValidateNotNullOrEmpty]
63+
public string Value { get; set; }
64+
65+
[Parameter(Mandatory = true, ParameterSetName = InputObjectParameterSet, HelpMessage = "Endpoint(s) to apply enrichments to.")]
66+
[Parameter(Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Endpoint(s) to apply enrichments to.")]
67+
[Parameter(Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Endpoint(s) to apply enrichments to.")]
68+
[ValidateNotNullOrEmpty]
69+
public string[] Endpoint { get; set; }
70+
71+
public override void ExecuteCmdlet()
72+
{
73+
if (ShouldProcess(this.Key, Properties.Resources.AddIotHubMessageEnrichment))
74+
{
75+
IotHubDescription iotHubDescription;
76+
if (ParameterSetName.Equals(InputObjectParameterSet))
77+
{
78+
this.ResourceGroupName = this.InputObject.Resourcegroup;
79+
this.Name = this.InputObject.Name;
80+
iotHubDescription = IotHubUtils.ConvertObject<PSIotHub, IotHubDescription>(this.InputObject);
81+
}
82+
else
83+
{
84+
if (ParameterSetName.Equals(ResourceIdParameterSet))
85+
{
86+
this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId);
87+
this.Name = IotHubUtils.GetIotHubName(this.ResourceId);
88+
}
89+
90+
iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.Name);
91+
}
92+
93+
if(iotHubDescription.Properties.Routing.Enrichments == null)
94+
{
95+
iotHubDescription.Properties.Routing.Enrichments = new List<EnrichmentProperties>();
96+
}
97+
98+
if(!iotHubDescription.Properties.Routing.Enrichments.Any(x => x.Key.Equals(this.Key.Trim(), StringComparison.OrdinalIgnoreCase)))
99+
{
100+
IList<string> endpointNames = new List<string>();
101+
foreach (string endpoint in this.Endpoint)
102+
{
103+
endpointNames.Add(endpoint.Trim());
104+
}
105+
106+
iotHubDescription.Properties.Routing.Enrichments.Add(
107+
new EnrichmentProperties(
108+
this.Key.Trim(),
109+
this.Value.Trim(),
110+
endpointNames
111+
));
112+
113+
this.IotHubClient.IotHubResource.CreateOrUpdate(this.ResourceGroupName, this.Name, iotHubDescription);
114+
IotHubDescription updatedIotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.Name);
115+
this.WriteObject(IotHubUtils.ToPSEnrichmentMetadata(updatedIotHubDescription.Properties.Routing.Enrichments.FirstOrDefault(x => x.Key.Equals(this.Key, StringComparison.OrdinalIgnoreCase))), false);
116+
}
117+
else
118+
{
119+
throw new ArgumentException(string.Format(Properties.Resources.MessageEnrichmentKeyExist, this.Key));
120+
}
121+
}
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)