public override void ExecuteCmdlet() { IotHubDescription iotHubDescription; if (ParameterSetName.Equals(InputObjectTestRouteParameterSet) || ParameterSetName.Equals(InputObjectTestAllRouteParameterSet)) { this.ResourceGroupName = this.InputObject.Resourcegroup; this.Name = this.InputObject.Name; iotHubDescription = IotHubUtils.ConvertObject <PSIotHub, IotHubDescription>(this.InputObject); } else { if (ParameterSetName.Equals(ResourceIdTestRouteParameterSet) || ParameterSetName.Equals(ResourceIdTestAllRouteParameterSet)) { this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId); this.Name = IotHubUtils.GetIotHubName(this.ResourceId); } iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.Name); } RoutingMessage routingMessage = new RoutingMessage(this.Body, this.AppProperty != null ? this.AppProperty.Cast <DictionaryEntry>().ToDictionary(kvp => (string)kvp.Key, kvp => (string)kvp.Value) : null, this.SystemProperty != null ? this.SystemProperty.Cast <DictionaryEntry>().ToDictionary(kvp => (string)kvp.Key, kvp => (string)kvp.Value) : null); switch (ParameterSetName) { case InputObjectTestRouteParameterSet: case ResourceIdTestRouteParameterSet: case TestRouteParameterSet: TestRouteInput testRouteInput = new TestRouteInput(); testRouteInput.Message = routingMessage; testRouteInput.Route = iotHubDescription.Properties.Routing.Routes.FirstOrDefault(x => x.Name.Equals(this.RouteName, StringComparison.OrdinalIgnoreCase)); PSTestRouteResult psTestRouteResult = IotHubUtils.ToPSTestRouteResult(this.IotHubClient.IotHubResource.TestRoute(testRouteInput, this.Name, this.ResourceGroupName)); if (this.ShowError.IsPresent && psTestRouteResult.Details != null) { this.WriteObject(psTestRouteResult.Details.CompilationErrors, true); } else { this.WriteObject(psTestRouteResult, false); } break; case InputObjectTestAllRouteParameterSet: case ResourceIdTestAllRouteParameterSet: case TestAllRouteParameterSet: TestAllRoutesInput testAllRoutesInput = new TestAllRoutesInput(); testAllRoutesInput.RoutingSource = this.Source.ToString(); testAllRoutesInput.Message = routingMessage; this.WriteObject(IotHubUtils.ToPSRouteProperties(this.IotHubClient.IotHubResource.TestAllRoutes(testAllRoutesInput, this.Name, this.ResourceGroupName).Routes), true); break; } }
public void TestIotHubCreateLifeCycle() { using (MockContext context = MockContext.Start(this.GetType().FullName)) { this.Initialize(context); // Create Resource Group var resourceGroup = this.CreateResourceGroup(IotHubTestUtilities.DefaultResourceGroupName); // Check if Hub Exists and Delete var operationInputs = new OperationInputs() { Name = IotHubTestUtilities.DefaultIotHubName }; var iotHubNameAvailabilityInfo = this.iotHubClient.IotHubResource.CheckNameAvailability(operationInputs); if (!(bool)iotHubNameAvailabilityInfo.NameAvailable) { this.iotHubClient.IotHubResource.Delete( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName); iotHubNameAvailabilityInfo = this.iotHubClient.IotHubResource.CheckNameAvailability(operationInputs); Assert.True(iotHubNameAvailabilityInfo.NameAvailable); } //CreateEH and AuthRule var properties = new IotHubProperties(); properties.Routing = this.GetIotHubRoutingProperties(resourceGroup); var iotHub = this.CreateIotHub(resourceGroup, IotHubTestUtilities.DefaultLocation, IotHubTestUtilities.DefaultIotHubName, properties); Assert.NotNull(iotHub); Assert.Equal(IotHubSku.S1, iotHub.Sku.Name); Assert.Equal(IotHubTestUtilities.DefaultIotHubName, iotHub.Name); // Add and Get Tags IDictionary <string, string> tags = new Dictionary <string, string>(); tags.Add("key1", "value1"); tags.Add("key2", "value2"); var tag = new TagsResource(tags); iotHub = this.iotHubClient.IotHubResource.Update(IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, tag); Assert.NotNull(iotHub); Assert.True(iotHub.Tags.Count().Equals(2)); Assert.Equal("value2", iotHub.Tags["key2"]); var subscriptionQuota = this.iotHubClient.ResourceProviderCommon.GetSubscriptionQuota(); Assert.Equal(2, subscriptionQuota.Value.Count); Assert.Equal(1, subscriptionQuota.Value.FirstOrDefault(x => x.Name.Value.Equals("freeIotHubCount")).Limit); Assert.Equal(100, subscriptionQuota.Value.FirstOrDefault(x => x.Name.Value.Equals("paidIotHubCount")).Limit); var endpointHealth = this.iotHubClient.IotHubResource.GetEndpointHealth(IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName); Assert.Equal(4, endpointHealth.Count()); Assert.Contains(endpointHealth, q => q.EndpointId.Equals("events", StringComparison.OrdinalIgnoreCase)); TestAllRoutesInput testAllRoutesInput = new TestAllRoutesInput(RoutingSource.DeviceMessages, new RoutingMessage(), new RoutingTwin()); TestAllRoutesResult testAllRoutesResult = this.iotHubClient.IotHubResource.TestAllRoutes(testAllRoutesInput, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.DefaultResourceGroupName); Assert.Equal(4, testAllRoutesResult.Routes.Count); TestRouteInput testRouteInput = new TestRouteInput(properties.Routing.Routes[0], new RoutingMessage(), new RoutingTwin()); TestRouteResult testRouteResult = this.iotHubClient.IotHubResource.TestRoute(testRouteInput, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.DefaultResourceGroupName); Assert.Equal("true", testRouteResult.Result); // Get quota metrics var quotaMetrics = this.iotHubClient.IotHubResource.GetQuotaMetrics( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName); Assert.True(quotaMetrics.Count() > 0); Assert.Contains(quotaMetrics, q => q.Name.Equals("TotalMessages", StringComparison.OrdinalIgnoreCase) && q.CurrentValue == 0 && q.MaxValue == 400000); Assert.Contains(quotaMetrics, q => q.Name.Equals("TotalDeviceCount", StringComparison.OrdinalIgnoreCase) && q.CurrentValue == 0 && q.MaxValue == 500000); // Get all Iot Hubs in a resource group var iotHubs = this.iotHubClient.IotHubResource.ListByResourceGroup(IotHubTestUtilities.DefaultResourceGroupName); Assert.True(iotHubs.Count() > 0); // Get all Iot Hubs in a subscription var iotHubBySubscription = this.iotHubClient.IotHubResource.ListBySubscription(); Assert.True(iotHubBySubscription.Count() > 0); // Get Registry Stats var regStats = this.iotHubClient.IotHubResource.GetStats(IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName); Assert.True(regStats.TotalDeviceCount.Value.Equals(0)); Assert.True(regStats.EnabledDeviceCount.Value.Equals(0)); Assert.True(regStats.DisabledDeviceCount.Value.Equals(0)); // Get Valid Skus var skus = this.iotHubClient.IotHubResource.GetValidSkus( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName); Assert.Equal(3, skus.Count()); // Get All Iothub Keys var keys = this.iotHubClient.IotHubResource.ListKeys( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName); Assert.True(keys.Count() > 0); Assert.Contains(keys, k => k.KeyName.Equals("iothubowner", StringComparison.OrdinalIgnoreCase)); // Get specific IotHub Key var key = this.iotHubClient.IotHubResource.GetKeysForKeyName( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, "iothubowner"); Assert.Equal("iothubowner", key.KeyName); // Get All EH consumer groups var ehConsumerGroups = this.iotHubClient.IotHubResource.ListEventHubConsumerGroups( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.EventsEndpointName); Assert.True(ehConsumerGroups.Count() > 0); Assert.Contains(ehConsumerGroups, e => e.Name.Equals("$Default", StringComparison.OrdinalIgnoreCase)); // Add EH consumer group var ehConsumerGroup = this.iotHubClient.IotHubResource.CreateEventHubConsumerGroup( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.EventsEndpointName, "testConsumerGroup"); Assert.Equal("testConsumerGroup", ehConsumerGroup.Name); // Get EH consumer group ehConsumerGroup = this.iotHubClient.IotHubResource.GetEventHubConsumerGroup( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.EventsEndpointName, "testConsumerGroup"); Assert.Equal("testConsumerGroup", ehConsumerGroup.Name); // Delete EH consumer group this.iotHubClient.IotHubResource.DeleteEventHubConsumerGroup( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.EventsEndpointName, "testConsumerGroup"); // Get all of the available IoT Hub REST API operations var operationList = this.iotHubClient.Operations.List(); Assert.True(operationList.Count() > 0); Assert.Contains(operationList, e => e.Name.Equals("Microsoft.Devices/iotHubs/Read", StringComparison.OrdinalIgnoreCase)); // Get IoT Hub REST API read operation var hubReadOperation = operationList.Where(e => e.Name.Equals("Microsoft.Devices/iotHubs/Read", StringComparison.OrdinalIgnoreCase)); Assert.True(hubReadOperation.Count().Equals(1)); Assert.Equal("Microsoft Devices", hubReadOperation.First().Display.Provider, ignoreCase: true); Assert.Equal("Get IotHub(s)", hubReadOperation.First().Display.Operation, ignoreCase: true); } }
public async Task IotHubClient_HubCreate() { try { using var context = MockContext.Start(GetType()); Initialize(context); // Create resource group ResourceGroup resourceGroup = await CreateResourceGroupAsync(IotHubTestUtilities.DefaultResourceGroupName) .ConfigureAwait(false); // Check if hub exists and delete IotHubNameAvailabilityInfo iotHubNameAvailabilityInfo = await _iotHubClient.IotHubResource .CheckNameAvailabilityAsync(IotHubTestUtilities.DefaultIotHubName) .ConfigureAwait(false); if (!iotHubNameAvailabilityInfo.NameAvailable.Value) { _ = await _iotHubClient.IotHubResource .DeleteAsync( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName) .ConfigureAwait(false); iotHubNameAvailabilityInfo = await _iotHubClient.IotHubResource .CheckNameAvailabilityAsync(IotHubTestUtilities.DefaultIotHubName) .ConfigureAwait(false); iotHubNameAvailabilityInfo.NameAvailable.Should().BeTrue(); } // Create EH and AuthRule var properties = new IotHubProperties { Routing = await GetIotHubRoutingPropertiesAsync(resourceGroup).ConfigureAwait(false), EnableDataResidency = false }; IotHubDescription iotHub = await _iotHubClient.IotHubResource .CreateOrUpdateAsync( resourceGroup.Name, IotHubTestUtilities.DefaultIotHubName, new IotHubDescription { Location = IotHubTestUtilities.DefaultLocation, Sku = new IotHubSkuInfo { Name = IotHubSku.S1, Capacity = 1, }, Properties = properties, }) .ConfigureAwait(false); iotHub.Should().NotBeNull(); iotHub.Name.Should().Be(IotHubTestUtilities.DefaultIotHubName); iotHub.Location.Should().Be(IotHubTestUtilities.DefaultLocation); iotHub.Sku.Name.Should().Be(IotHubSku.S1); iotHub.Sku.Capacity.Should().Be(1); iotHub.SystemData.CreatedAt.Should().NotBeNull(); iotHub.Properties.EnableDataResidency.Should().BeFalse(); // Add and get tags var tags = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } }; iotHub = await _iotHubClient.IotHubResource .UpdateAsync(IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, tags) .ConfigureAwait(false); iotHub.Should().NotBeNull(); iotHub.Tags.Count().Should().Be(tags.Count); iotHub.Tags.Should().BeEquivalentTo(tags); UserSubscriptionQuotaListResult subscriptionQuota = await _iotHubClient.ResourceProviderCommon .GetSubscriptionQuotaAsync() .ConfigureAwait(false); subscriptionQuota.Value.Count.Should().Be(2); subscriptionQuota.Value.FirstOrDefault(x => x.Name.Value.Equals("freeIotHubCount")).Limit.Should().BeGreaterThan(0); subscriptionQuota.Value.FirstOrDefault(x => x.Name.Value.Equals("paidIotHubCount")).Limit.Should().BeGreaterThan(0); IPage <EndpointHealthData> endpointHealth = await _iotHubClient .IotHubResource .GetEndpointHealthAsync(IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName); endpointHealth.Count().Should().Be(4); Assert.Contains(endpointHealth, q => q.EndpointId.Equals("events", StringComparison.OrdinalIgnoreCase)); var testAllRoutesInput = new TestAllRoutesInput(RoutingSource.DeviceMessages, new RoutingMessage(), new RoutingTwin()); TestAllRoutesResult testAllRoutesResult = await _iotHubClient.IotHubResource .TestAllRoutesAsync(testAllRoutesInput, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.DefaultResourceGroupName) .ConfigureAwait(false); testAllRoutesResult.Routes.Count.Should().Be(4); var testRouteInput = new TestRouteInput(properties.Routing.Routes[0], new RoutingMessage(), new RoutingTwin()); TestRouteResult testRouteResult = await _iotHubClient.IotHubResource .TestRouteAsync(testRouteInput, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.DefaultResourceGroupName) .ConfigureAwait(false); testRouteResult.Result.Should().Be("true"); // Get quota metrics var quotaMetrics = await _iotHubClient.IotHubResource .GetQuotaMetricsAsync( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName) .ConfigureAwait(false); quotaMetrics.Count().Should().BeGreaterThan(0); Assert.Contains(quotaMetrics, q => q.Name.Equals("TotalMessages", StringComparison.OrdinalIgnoreCase) && q.CurrentValue == 0 && q.MaxValue == 400000); Assert.Contains(quotaMetrics, q => q.Name.Equals("TotalDeviceCount", StringComparison.OrdinalIgnoreCase) && q.CurrentValue == 0 && q.MaxValue == 1000000); // Get all IoT hubs in a resource group IPage <IotHubDescription> iotHubs = await _iotHubClient.IotHubResource .ListByResourceGroupAsync(IotHubTestUtilities.DefaultResourceGroupName) .ConfigureAwait(false); iotHubs.Count().Should().BeGreaterThan(0); // Get all IoT hubs in a subscription IPage <IotHubDescription> iotHubBySubscription = await _iotHubClient.IotHubResource .ListBySubscriptionAsync() .ConfigureAwait(false); iotHubBySubscription.Count().Should().BeGreaterThan(0); // Get registry stats on newly created hub RegistryStatistics regStats = await _iotHubClient.IotHubResource .GetStatsAsync(IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName) .ConfigureAwait(false); regStats.TotalDeviceCount.Value.Should().Be(0); regStats.EnabledDeviceCount.Value.Should().Be(0); regStats.DisabledDeviceCount.Value.Should().Be(0); // Get valid skus IPage <IotHubSkuDescription> skus = await _iotHubClient.IotHubResource .GetValidSkusAsync( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName) .ConfigureAwait(false); skus.Count().Should().Be(3); // Get all IoT hub keys const string hubOwnerRole = "iothubowner"; IPage <SharedAccessSignatureAuthorizationRule> keys = await _iotHubClient.IotHubResource .ListKeysAsync( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName) .ConfigureAwait(false); keys.Count().Should().BeGreaterThan(0); Assert.Contains(keys, k => k.KeyName.Equals(hubOwnerRole, StringComparison.OrdinalIgnoreCase)); // Get specific IoT Hub key SharedAccessSignatureAuthorizationRule key = await _iotHubClient.IotHubResource .GetKeysForKeyNameAsync( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, hubOwnerRole) .ConfigureAwait(false); key.KeyName.Should().Be(hubOwnerRole); // Get all EH consumer groups IPage <EventHubConsumerGroupInfo> ehConsumerGroups = await _iotHubClient.IotHubResource .ListEventHubConsumerGroupsAsync( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.EventsEndpointName) .ConfigureAwait(false); ehConsumerGroups.Count().Should().BeGreaterThan(0); Assert.Contains(ehConsumerGroups, e => e.Name.Equals("$Default", StringComparison.OrdinalIgnoreCase)); // Add EH consumer group const string ehCgName = "testConsumerGroup"; var ehConsumerGroup = await _iotHubClient.IotHubResource .CreateEventHubConsumerGroupAsync( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.EventsEndpointName, ehCgName, new EventHubConsumerGroupName(ehCgName)) .ConfigureAwait(false); ehConsumerGroup.Name.Should().Be(ehCgName); // Get EH consumer group ehConsumerGroup = await _iotHubClient.IotHubResource .GetEventHubConsumerGroupAsync( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.EventsEndpointName, ehCgName) .ConfigureAwait(false); ehConsumerGroup.Name.Should().Be(ehCgName); // Delete EH consumer group await _iotHubClient.IotHubResource.DeleteEventHubConsumerGroupAsync( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.EventsEndpointName, ehCgName) .ConfigureAwait(false); // Get all of the available IoT Hub REST API operations IPage <Operation> operationList = _iotHubClient.Operations.List(); operationList.Count().Should().BeGreaterThan(0); Assert.Contains(operationList, e => e.Name.Equals("Microsoft.Devices/iotHubs/Read", StringComparison.OrdinalIgnoreCase)); // Get IoT Hub REST API read operation IEnumerable <Operation> hubReadOperation = operationList.Where(e => e.Name.Equals("Microsoft.Devices/iotHubs/Read", StringComparison.OrdinalIgnoreCase)); hubReadOperation.Count().Should().Be(1); hubReadOperation.First().Display.Provider.Should().Be("Microsoft Devices"); hubReadOperation.First().Display.Operation.Should().Be("Get IotHub(s)"); // Initiate manual failover IotHubDescription iotHubBeforeFailover = await _iotHubClient.IotHubResource .GetAsync( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName) .ConfigureAwait(false); await _iotHubClient.IotHub .ManualFailoverAsync( IotHubTestUtilities.DefaultIotHubName, IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultFailoverLocation) .ConfigureAwait(false); var iotHubAfterFailover = await _iotHubClient.IotHubResource .GetAsync( IotHubTestUtilities.DefaultResourceGroupName, IotHubTestUtilities.DefaultIotHubName) .ConfigureAwait(false); iotHubBeforeFailover.Properties.Locations[0].Role.ToLower().Should().Be("primary"); iotHubBeforeFailover.Properties.Locations[0].Location.Replace(" ", "").ToLower().Should().Be(IotHubTestUtilities.DefaultLocation.ToLower()); iotHubBeforeFailover.Properties.Locations[1].Role.ToLower().Should().Be("secondary"); iotHubBeforeFailover.Properties.Locations[1].Location.Replace(" ", "").ToLower().Should().Be(IotHubTestUtilities.DefaultFailoverLocation.ToLower()); iotHubAfterFailover.Properties.Locations[0].Role.ToLower().Should().Be("primary"); iotHubAfterFailover.Properties.Locations[0].Location.Replace(" ", "").ToLower().Should().Be(IotHubTestUtilities.DefaultFailoverLocation.ToLower()); iotHubAfterFailover.Properties.Locations[1].Role.ToLower().Should().Be("secondary"); iotHubAfterFailover.Properties.Locations[1].Location.Replace(" ", "").ToLower().Should().Be(IotHubTestUtilities.DefaultLocation.ToLower()); } catch (ErrorDetailsException ex) { Console.WriteLine(ex.ToString()); Console.WriteLine($"{ex.Body.Message}: {ex.Body.Details}"); throw; } catch (Exception ex) { Console.WriteLine(ex.ToString()); throw; } }