Skip to content

Health API #26549

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

Merged
merged 5 commits into from
Apr 9, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ----------------------------------------------------------------------------------
//
// 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.Commands.Network.Test.ScenarioTests;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;

namespace Commands.Network.Test.ScenarioTests
{
public class LoadBalancerHealthPerRuleTests : NetworkTestRunner
{
public LoadBalancerHealthPerRuleTests(Xunit.Abstractions.ITestOutputHelper output)
: base(output)
{
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
[Trait(Category.Owner, NrpTeamAlias.slbdev)]
public void TestLoadBalancerHealthPerRule()
{
TestRunner.RunTestScript("Test-LoadBalancerHealthPerRule");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# ----------------------------------------------------------------------------------
#
# 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.
# ----------------------------------------------------------------------------------

<#
.SYNOPSIS
Tests to query load balancer health per rule..
#>
function Test-LoadBalancerHealthPerRule
{
# Setup
$rgname = Get-ResourceGroupName
$vnetName = Get-ResourceName
$subnetName = Get-ResourceName
$publicIpName = Get-ResourceName
$lbName = Get-ResourceName
$lbRuleName = Get-ResourceName
$probeName = Get-ResourceName
$frontendName = Get-ResourceName
$backendAddressPoolName = Get-ResourceName
$rglocation = Get-ProviderLocation ResourceManagement
$resourceTypeParent = "Microsoft.Network/loadBalancers"
$location = Get-ProviderLocation $resourceTypeParent
$vmUsername = "azureuser"
$vmPassword = ConvertTo-SecureString "$vmUsername@123" -AsPlainText -Force
$ipConfigName = Get-ResourceName
$vmsize = "Standard_A2"
$ImagePublisher = "MicrosoftWindowsServer"
$imageOffer = "WindowsServer"
$imageSKU = "2019-Datacenter-GS"

try
{
# Create the resource group
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $rglocation -Tags @{ testtag = "testval"}

# Create the Virtual Network
$subnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.1.0/24
$vnet = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgname -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet

# Create the publicip
$publicip = New-AzPublicIpAddress -ResourceGroupName $rgname -name $publicIpName -location $location -AllocationMethod Static -sku Standard

# Create LoadBalancer
$frontend = New-AzLoadBalancerFrontendIpConfig -Name $frontendName -PublicIpAddress $publicip

$bepool = New-AzLoadBalancerBackendAddressPoolConfig -Name $backendAddressPoolName

$probe = @{
Name = $probeName
Protocol = 'tcp'
Port = '80'
IntervalInSeconds = '360'
ProbeCount = '5'
}
$healthprobe = New-AzLoadBalancerProbeConfig @probe

$lbrule = @{
Name = $lbRuleName
Protocol = 'tcp'
FrontendPort = '80'
BackendPort = '80'
IdleTimeoutInMinutes = '15'
FrontendIpConfiguration = $frontend
BackendAddressPool = $bePool
Probe = $healthprobe
}
$rule = New-AzLoadBalancerRuleConfig @lbrule -EnableTcpReset -DisableOutboundSNAT

$loadbalancer = @{
ResourceGroupName = $rgName
Name = $lbName
Location = $rglocation
Sku = 'Standard'
FrontendIpConfiguration = $frontend
BackendAddressPool = $bePool
LoadBalancingRule = $rule
Probe = $healthprobe
}
$lb = New-AzLoadBalancer @loadbalancer

$cred = New-Object System.Management.Automation.PSCredential ($vmUsername, $vmPassword);

$RdpPublicIP = New-AzPublicIpAddress -Name "RdpPublicIP" -ResourceGroupName $resourceGroup.ResourceGroupName -Location $resourceGroup.Location -AllocationMethod Static -Sku Standard -IpAddressVersion IPv4

# both LB pool and Instance Level public ip for RPD
$Ip4Config = New-AzNetworkInterfaceIpConfig -Name $ipConfigName -Subnet $vnet.subnets[0] -PrivateIpAddressVersion IPv4 -LoadBalancerBackendAddressPool $bepool -PublicIpAddress $RdpPublicIP

$NIC = New-AzNetworkInterface -Name "NIC" -ResourceGroupName $resourceGroup.ResourceGroupName -Location $resourceGroup.Location -NetworkSecurityGroupId $nsg.Id -IpConfiguration $Ip4Config

$vmName = "myVM"
$VMconfig = New-AzVMConfig -VMName $vmName -VMSize $vmsize
| Set-AzVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent 3> $null
| Set-AzVMSourceImage -PublisherName $ImagePublisher -Offer $imageOffer -Skus $imageSKU -Version "latest" 3> $null
| Set-AzVMOSDisk -Name "$vmName.vhd" -CreateOption fromImage 3> $null | Add-AzVMNetworkInterface -Id $NIC.Id 3> $null

$VM = New-AzVM -ResourceGroupName $resourceGroup.ResourceGroupName -Location $resourceGroup.Location -VM $VMconfig

# install IIS (port 80)
$ext = @{
Publisher = 'Microsoft.Compute'
ExtensionType = 'CustomScriptExtension'
ExtensionName = 'IIS'
ResourceGroupName = $rgName
VMName = "myVM"
Location = $region
TypeHandlerVersion = '1.8'
SettingString = '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}'
}
Set-AzVMExtension @ext -AsJob

# List
$list = Get-AzLoadBalancerRuleHealth -ResourceGroupName $rgname -LoadBalancerName $lbName -Name $lbruleName

Assert-True { @($list.LoadBalancerBackendAddresses).Count -eq 1}
Assert-AreEqual $list.LoadBalancerBackendAddresses[0].IpAddress "10.0.1.4"
Assert-True { $list.Down.Count -eq 1}
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Network/Network/Az.Network.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ CmdletsToExport = 'Add-AzApplicationGatewayAuthenticationCertificate',
'Get-AzLoadBalancerInboundNatRuleConfig',
'Get-AzLoadBalancerOutboundRuleConfig',
'Get-AzLoadBalancerProbeConfig', 'Get-AzLoadBalancerRuleConfig',
'Get-AzLoadBalancerRuleHealth',
'Get-AzLocalNetworkGateway', 'Get-AzNatGateway',
'Get-AzNetworkInterface', 'Get-AzNetworkInterfaceIpConfig',
'Get-AzNetworkInterfaceTapConfig', 'Get-AzNetworkManager',
Expand Down
1 change: 1 addition & 0 deletions src/Network/Network/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
--->

## Upcoming Release
* Added cmdlet `Get-AzLoadBalancerRuleHealth` for Load Balancer Rule Health.
* Added property "EnableConnectionTracking" to Load Balancing Rule, as well as support for it for the following cmdlets:
- `Add-AzLoadBalancerRuleConfig`
- `New-AzLoadBalancerRuleConfig`
Expand Down
16 changes: 16 additions & 0 deletions src/Network/Network/Common/NetworkResourceManagerProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,22 @@ private static void Initialize()
// MNM to CNM
cfg.CreateMap<MNM.InboundNatRulePortMapping, CNM.PSInboundNatRulePortMapping>();

// LoadBalancerHealthPerRule
// CNM to MNM
cfg.CreateMap<CNM.PSLoadBalancerHealthPerRule, MNM.LoadBalancerHealthPerRule>();

// LoadBalancerHealthPerRule
// MNM to CNM
cfg.CreateMap<MNM.LoadBalancerHealthPerRule, CNM.PSLoadBalancerHealthPerRule>();

// LoadBalancerHealthPerRulePerBackendAddress
// CNM to MNM
cfg.CreateMap<CNM.PSLoadBalancerHealthPerRulePerBackendAddress, MNM.LoadBalancerHealthPerRulePerBackendAddress>();

// LoadBalancerHealthPerRulePerBackendAddress
// MNM to CNM
cfg.CreateMap<MNM.LoadBalancerHealthPerRulePerBackendAddress, CNM.PSLoadBalancerHealthPerRulePerBackendAddress>();

// NatRulePortMapping
// CNM to MNM
cfg.CreateMap<CNM.PSNatRulePortMapping, MNM.NatRulePortMapping>();
Expand Down
63 changes: 63 additions & 0 deletions src/Network/Network/LoadBalancer/GetAzureLoadBalancerRuleHealth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// ----------------------------------------------------------------------------------
//
// 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.Commands.ResourceManager.Common.Tags;
using Microsoft.Rest.Azure;
using Microsoft.Azure.Commands.Network.Models;
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using System;
using System.Collections.Generic;
using System.Management.Automation;
using CNM = Microsoft.Azure.Commands.Network.Models;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using System.Linq;
using Microsoft.WindowsAzure.Commands.Utilities.Common;

namespace Microsoft.Azure.Commands.Network
{
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "LoadBalancerRuleHealth", DefaultParameterSetName = "GetByNameParameterSet"), OutputType(typeof(PSLoadBalancerHealthPerRule))]
public partial class GetAzureLoadBalancerRuleHealth : NetworkBaseCmdlet
{
private const string GetByNameParameterSet = "GetByNameParameterSet";

[Parameter(
Mandatory = true,
HelpMessage = "The resource group name of the load balancer.",
ParameterSetName = GetByNameParameterSet)]
[ResourceGroupCompleter]
public string ResourceGroupName { get; set; }

[Parameter(Mandatory = true,
HelpMessage = "The name of the load balancer.",
ParameterSetName = GetByNameParameterSet)]
[ValidateNotNullOrEmpty]
public string LoadBalancerName { get; set; }

[Parameter(Mandatory = true,
HelpMessage = "The name of the load balancing rule.",
ParameterSetName = GetByNameParameterSet)]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

public override void Execute()
{
base.Execute();
var getHealth = this.NetworkClient.NetworkManagementClient.LoadBalancerLoadBalancingRules.Health(this.ResourceGroupName, this.LoadBalancerName, this.Name);
var psHealth = NetworkResourceManagerProfile.Mapper.Map<PSLoadBalancerHealthPerRule>(getHealth);
WriteObject(psHealth, true);
}
}
}
38 changes: 38 additions & 0 deletions src/Network/Network/Models/PSLoadBalancerHealthPerRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ----------------------------------------------------------------------------------
//
// 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.WindowsAzure.Commands.Common.Attributes;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Microsoft.Azure.Commands.Network.Models
{
public partial class PSLoadBalancerHealthPerRule : PSTopLevelResource
{
[JsonProperty(Order = 1)]
[Ps1Xml(Target = ViewControl.Table)]
public int Up { get; set; }

[JsonProperty(Order = 1)]
[Ps1Xml(Target = ViewControl.Table)]
public int Down { get; set; }

public List<PSLoadBalancerHealthPerRulePerBackendAddress> LoadBalancerBackendAddresses { get; set; }

[JsonIgnore]
public string LoadBalancerBackendAddressesText
{
get { return JsonConvert.SerializeObject(LoadBalancerBackendAddresses, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ----------------------------------------------------------------------------------
//
// 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.Network.Models
{
public partial class PSLoadBalancerHealthPerRulePerBackendAddress : PSChildResource
{
public string IpAddress { get; set; }

public string NetworkInterfaceIPConfigurationId { get; set; }

public string State { get; set; }

public string Reason { get; set; }
}
}
26 changes: 26 additions & 0 deletions src/Network/Network/Network.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -3180,6 +3180,32 @@
</ListEntry>
</ListEntries>
</ListControl>
</View>
<View>
<Name>Microsoft.Azure.Commands.Network.Models.PSLoadBalancerHealthPerRule</Name>
<ViewSelectedBy>
<TypeName>Microsoft.Azure.Commands.Network.Models.PSLoadBalancerHealthPerRule</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<Label>Up</Label>
<PropertyName>Up</PropertyName>
</ListItem>
<ListItem>
<Label>Down</Label>
<PropertyName>Down</PropertyName>
</ListItem>
<ListItem>
<Label>LoadBalancerBackendAddresses</Label>
<PropertyName>LoadBalancerBackendAddressesText</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
<View>
<Name>Microsoft.Azure.Commands.Network.Models.PSRouteTable</Name>
Expand Down
Loading