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

feat: iot hub quota metric (🚧) #353

Closed
Closed
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
34 changes: 34 additions & 0 deletions docs/preview/03-Features/powershell/azure-iot-hub.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: "Azure Iot Hub"
layout: default
---

# Azure Iot Hub

## Installation

To have access to the following features, you have to import the module:

```powershell
PS> Install-Module -Name Arcus.Scripting.IotHub
```

## Getting quota metrics from Azure IoT Hub
As part of the setup for alert-creations for Azure IoT Hub, a script has been included which allows you to determine the current quota of messages.
The result can be used when creating an alert on IoT Hub that is triggered when the daily amount of messages that you have received, is reaching the quota / limit of that IoT Hub.

| Parameter | Mandatory | Description |
| ------------------- | --------- | -------------------------------------------------------------------------------------- |
| `IotHubName` | yes | The name of the Azure IoT Hub from where the message quota metric should be retrieved. |
| `ResourceGroupName` | yes | The resource group containing the Azure IoT Hub. |
| `QuotaPercentage` | yes | The requested percentage of the quota metric of the Azure IoT Hub's total messages. |

**Example**

```powershell
PS> Get-AzIotHubDailyMessageQuotaThreshold `
-IotHubName "<my-iot-hub>"
-ResourceGroupName "<my-resource-grou>"
-QuotaPercentage 0.3
# Calculated '321' as quota percentage of Azure IoT Hub metric from '<my-iot-hub>' in resource group '<my-resource-group>'
```
26 changes: 26 additions & 0 deletions src/Arcus.Scripting.IotHub/Arcus.Scripting.IoTHub.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<#
.Synopsis
Return the the current quota of Azure IoT Hub messages.

.Description
Retrieves one specific percentage quota metric of an Azure IoT Hub's total messages.

.Parameter IotHubName
The name of the Azure IoT Hub from where the message quota metric should be retrieved.

.Parameter ResourceGroupName
The resource group containing the Azure IoT Hub.

.Parameter QuotaPercentage
The requested percentage of the quota metric of the Azure IoT Hub's total messages.
#>
function Get-AzIotHubDailyMessageQuotaThreshold {
param(
[Parameter(Mandatory=$True)][string] $IotHubName = $(throw "Name of the Azure IoT Hub instance is required"),
[Parameter(Mandatory=$True)][single] $QuotaPercentage = $(throw "Quota percentage is required"),
[Parameter(Mandatory=$False)][string] $ResourceGroupName = ""
)
. $PSScriptRoot\Scripts\Get-AzIotHubDailyMessageQuotaThreshold.ps1 -IotHubName $IoTHubName -QuotaPercentage $QuotaPercentage -ResourceGroupName $ResourceGroupName
}

Export-ModuleMember -Function Get-AzIotHubDailyMessageQuotaThreshold
40 changes: 40 additions & 0 deletions src/Arcus.Scripting.IotHub/Arcus.Scripting.IoTHub.pssproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{336aa7bf-1bd2-4067-bbf7-f2844766a8dd}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Arcus.Scripting.IotHub</RootNamespace>
<AssemblyName>Arcus.Scripting.IotHub</AssemblyName>
<Name>Arcus.Scripting.IotHub</Name>
<ProjectHome />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Folder Include="Scripts\" />
</ItemGroup>
<ItemGroup>
<Compile Include="Arcus.Scripting.IotHub.psd1" />
<Compile Include="Arcus.Scripting.IotHub.psm1" />
<Compile Include="Scripts\Get-AzIotHubDailyMessageQuotaThreshold.ps1" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Target Name="Build" />
<Import Project="$(MSBuildExtensionsPath)\PowerShell Tools for Visual Studio\PowerShellTools.targets" Condition="Exists('$(MSBuildExtensionsPath)\PowerShell Tools for Visual Studio\PowerShellTools.targets')" />
</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
param(
[Parameter(Mandatory=$true)][string] $IotHubName = $(throw "Name of the Azure IoT Hub instance is required"),
[Parameter(Mandatory=$true)][single] $QuotaPercentage = $(throw "Quota percentage is required"),
[Parameter(Mandatory=$false)][string] $ResourceGroupName = ""
)

Write-Verbose "Getting single Azure IoT hub metric from '$IotHubName' in resource group '$ResourceGroupName'..."
$quotaMetric = Get-AzIotHubQuotaMetric -Name $IotHubName -ResourceGroupName $ResourceGroupName

Write-Verbose "Calculate quota percentage of Azure IoT hub metric from '$IotHubName' in resource group '$ResourceGroupName'..."
$result = [Math]::Round($quotaMetric.MaxValue * $QuotaPercentage)

Write-Host "Calculated '$result' as quota percentage of Azure IoT Hub metric from '$IotHubName' in resource group '$ResourceGroupName'"
return $result
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Import-Module Az.IotHub
Import-Module -Name $PSScriptRoot\..\Arcus.Scripting.IotHub -ErrorAction Stop

InModuleScope Arcus.Scripting.IotHub {
Describe "Arcus Azure IoT Hub integration tests" {
BeforeEach {
$config = & $PSScriptRoot\Load-JsonAppsettings.ps1
& $PSScriptRoot\Connect-AzAccountFromConfig.ps1 -config $config
}
Context "Get daily IoT Hub quota threshold" {
It "Get daily IoT Hub quota threshold gets quota percentage" {
# Act
$result = Get-AzIotHubDailyMessageQuotaThreshold `
-IoTHubName $config.Arcus.IotHub.Name `
-ResourceGroupName $config.Arcus.ResourceGroupName `
-QuotaPercentage 0.1

# Assert
$result | Should -Be 0
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Compile Include="Arcus.Scripting.Storage.Blob.tests.ps1" />
<Compile Include="Arcus.Scripting.Storage.FileShare.tests.ps1" />
<Compile Include="Arcus.Scripting.Storage.Table.tests.ps1" />
<Compile Include="Arcus.Scripting.IotHub.tests.ps1" />
<Compile Include="Connect-MgGraphFromConfig.ps1" />
<Compile Include="Connect-AzAccountFromConfig.ps1" />
<Compile Include="Load-JsonAppsettings.ps1" />
Expand Down
3 changes: 3 additions & 0 deletions src/Arcus.Scripting.Tests.Integration/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"IntegrationAccount": {
"Name": "#{Arcus.IntegrationAccount.Name}#"
},
"IotHub": {
"Name": "#{Arcus.IotHub.Name}#"
},
"AppService": {
"Name": "#{Arcus.AppService.Name}#"
},
Expand Down
10 changes: 8 additions & 2 deletions src/Arcus.Scripting.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.32916.344
# Visual Studio Version 16
VisualStudioVersion = 16.0.31702.278
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{5C8B7391-D202-4B09-8991-3EBAC09A585E}"
EndProject
Expand Down Expand Up @@ -43,6 +43,8 @@ Project("{F5034706-568F-408A-B7B3-4D38C6DB8A32}") = "Arcus.Scripting.Management"
EndProject
Project("{F5034706-568F-408A-B7B3-4D38C6DB8A32}") = "Arcus.Scripting.ActiveDirectory", "Arcus.Scripting.ActiveDirectory\Arcus.Scripting.ActiveDirectory.pssproj", "{8C0379AD-D886-491F-9E1C-FCE0A7D144E8}"
EndProject
Project("{F5034706-568F-408A-B7B3-4D38C6DB8A32}") = "Arcus.Scripting.IotHub", "Arcus.Scripting.IotHub\Arcus.Scripting.IotHub.pssproj", "{336AA7BF-1BD2-4067-BBF7-F2844766A8DD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -125,6 +127,10 @@ Global
{8C0379AD-D886-491F-9E1C-FCE0A7D144E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C0379AD-D886-491F-9E1C-FCE0A7D144E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C0379AD-D886-491F-9E1C-FCE0A7D144E8}.Release|Any CPU.Build.0 = Release|Any CPU
{336AA7BF-1BD2-4067-BBF7-F2844766A8DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{336AA7BF-1BD2-4067-BBF7-F2844766A8DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{336AA7BF-1BD2-4067-BBF7-F2844766A8DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{336AA7BF-1BD2-4067-BBF7-F2844766A8DD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down