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
+
16
+ # ################################
17
+ # # IotHub Routing Cmdlets ##
18
+ # ################################
19
+
20
+ <#
21
+ . SYNOPSIS
22
+ Test all iothub routing cmdlets
23
+ #>
24
+ function Test-AzureRmIotHubRoutingLifecycle
25
+ {
26
+ $Location = Get-Location " Microsoft.Devices" " IotHub"
27
+ $IotHubName = getAssetName
28
+ $ResourceGroupName = getAssetName
29
+ $namespaceName = getAssetName ' eventHub'
30
+ $eventHubName = getAssetName
31
+ $authRuleName = getAssetName
32
+ $endpointName = getAssetName
33
+ $routeName = getAssetName
34
+ $Sku = " S1"
35
+ $EndpointTypeEventHub = [Microsoft.Azure.Commands.Management.IotHub.Models.PSEndpointType ] " EventHub"
36
+ $RoutingSourceTwinChangeEvents = [Microsoft.Azure.Commands.Management.IotHub.Models.PSRoutingSource ] " TwinChangeEvents"
37
+ $RoutingSourceDeviceMessages = [Microsoft.Azure.Commands.Management.IotHub.Models.PSRoutingSource ] " DeviceMessages"
38
+
39
+ # Create or Update Resource Group
40
+ $resourceGroup = New-AzureRmResourceGroup - Name $ResourceGroupName - Location $Location
41
+
42
+ # Create Iot Hub
43
+ $iothub = New-AzureRmIotHub - Name $IotHubName - ResourceGroupName $ResourceGroupName - Location $Location - SkuName $Sku - Units 1
44
+
45
+ # Create new eventHub namespace
46
+ $eventHubNamespace = New-AzureRmEventHubNamespace - ResourceGroup $ResourceGroupName - NamespaceName $namespaceName - Location $Location
47
+ Wait-Seconds 15
48
+ Assert-True {$eventHubNamespace.ProvisioningState -eq " Succeeded" }
49
+ $regexMatches = $eventHubNamespace.Id | Select-String - Pattern ' ^/subscriptions/(.*)/resourceGroups/(.*)/providers/(.*)$'
50
+ $eventHubSubscriptionId = $regexMatches.Matches.Groups [1 ].Value
51
+ $eventHubResourceGroup = $regexMatches.Matches.Groups [2 ].Value
52
+
53
+ # Create new eventHub
54
+ $msgRetentionInDays = 3
55
+ $partionCount = 2
56
+ $eventHub = New-AzureRmEventHub - ResourceGroup $ResourceGroupName - NamespaceName $namespaceName - EventHubName $eventHubName - MessageRetentionInDays $msgRetentionInDays - PartitionCount $partionCount
57
+
58
+ # Create AuthRule for eventhub
59
+ $rights = " Listen" , " Send"
60
+ $authRule = New-AzureRmEventHubAuthorizationRule - ResourceGroup $ResourceGroupName - NamespaceName $namespaceName - EventHubName $eventHubName - AuthorizationRuleName $authRuleName - Rights $rights
61
+ $keys = Get-AzureRmEventHubKey - ResourceGroup $ResourceGroupName - NamespaceName $namespaceName - EventHubName $eventHubName - AuthorizationRuleName $authRuleName
62
+ $ehConnectionString = $keys.PrimaryConnectionString
63
+
64
+ # Get all routing endpoints
65
+ $routingEndpoints = Get-AzureRmIotHubRoutingEndpoint - ResourceGroupName $ResourceGroupName - Name $IotHubName
66
+ Assert-True { $routingEndpoints.Count -eq 0 }
67
+
68
+ # Add event hub endpoint
69
+ $newRoutingEndpoint = Add-AzureRmIotHubRoutingEndpoint - ResourceGroupName $ResourceGroupName - Name $IotHubName - EndpointName $endpointName - EndpointType $EndpointTypeEventHub - EndpointResourceGroup $eventHubResourceGroup - EndpointSubscriptionId $eventHubSubscriptionId - ConnectionString $ehConnectionString
70
+ Assert-True { $newRoutingEndpoint.ResourceGroup -eq $eventHubResourceGroup }
71
+ Assert-True { $newRoutingEndpoint.SubscriptionId -eq $eventHubSubscriptionId }
72
+ Assert-True { $newRoutingEndpoint.Name -eq $endpointName }
73
+
74
+ # Get all routing endpoints
75
+ $updatedRoutingEndpoints = Get-AzureRmIotHubRoutingEndpoint - ResourceGroupName $ResourceGroupName - Name $IotHubName
76
+ Assert-True { $updatedRoutingEndpoints.Count -eq 1 }
77
+ Assert-True { $updatedRoutingEndpoints [0 ].ResourceGroup -eq $eventHubResourceGroup }
78
+ Assert-True { $updatedRoutingEndpoints [0 ].SubscriptionId -eq $eventHubSubscriptionId }
79
+ Assert-True { $updatedRoutingEndpoints [0 ].Name -eq $endpointName }
80
+
81
+ # Get all routes
82
+ $routes = Get-AzureRmIotHubRoute - ResourceGroupName $ResourceGroupName - Name $IotHubName
83
+ Assert-True { $routingEndpoints.Count -eq 0 }
84
+
85
+ # Add new route
86
+ $newRoute = Add-AzureRmIotHubRoute - ResourceGroupName $ResourceGroupName - Name $IotHubName - RouteName $routeName - Source $RoutingSourceDeviceMessages - EndpointName $endpointName
87
+ Assert-True { $newRoute.Name -eq $routeName }
88
+ Assert-True { $newRoute.Source -eq $RoutingSourceDeviceMessages }
89
+ Assert-True { $newRoute.EndpointNames -eq $endpointName }
90
+ Assert-False { $newRoute.IsEnabled }
91
+
92
+ # Get all routes
93
+ $routes = Get-AzureRmIotHubRoute - ResourceGroupName $ResourceGroupName - Name $IotHubName
94
+ Assert-True { $routes.Count -eq 1 }
95
+ Assert-True { $routes [0 ].Name -eq $routeName }
96
+ Assert-True { $routes [0 ].Source -eq $RoutingSourceDeviceMessages }
97
+ Assert-True { $routes [0 ].EndpointNames -eq $endpointName }
98
+ Assert-False { $routes [0 ].IsEnabled }
99
+
100
+ # Set route
101
+ $updatedRoute = Set-AzureRmIotHubRoute - ResourceGroupName $ResourceGroupName - Name $IotHubName - RouteName $routeName - Source $RoutingSourceTwinChangeEvents - Enabled
102
+ Assert-True { $updatedRoute.Name -eq $routeName }
103
+ Assert-True { $updatedRoute.Source -eq $RoutingSourceTwinChangeEvents }
104
+ Assert-True { $updatedRoute.EndpointNames -eq $endpointName }
105
+ Assert-True { $updatedRoute.IsEnabled }
106
+
107
+ # Test All Routes
108
+ $testRouteOutput = Test-AzureRmIotHubRoute - ResourceGroupName $ResourceGroupName - Name $IotHubName - Source $RoutingSourceTwinChangeEvents
109
+ Assert-True { $testRouteOutput.Count -eq 1 }
110
+ Assert-True { $testRouteOutput [0 ].Name -eq $routeName }
111
+ Assert-True { $testRouteOutput [0 ].Source -eq $RoutingSourceTwinChangeEvents }
112
+ Assert-True { $testRouteOutput [0 ].EndpointNames -eq $endpointName }
113
+ Assert-True { $testRouteOutput [0 ].IsEnabled }
114
+
115
+ # Delete Route
116
+ $result = Remove-AzureRmIotHubRoute - ResourceGroupName $ResourceGroupName - Name $IotHubName - RouteName $routeName - Passthru
117
+ Assert-True { $result }
118
+
119
+ # Delete routing endpoint
120
+ $result = Remove-AzureRmIotHubRoutingEndpoint - ResourceGroupName $ResourceGroupName - Name $IotHubName - EndpointName $endpointName - Passthru
121
+ Assert-True { $result }
122
+
123
+ # Remove IotHub
124
+ Remove-AzureRmIotHub - ResourceGroupName $ResourceGroupName - Name $IotHubName
125
+ }
0 commit comments