Skip to content

Commit 82e5484

Browse files
Gizachew-EshetieGizachew Eshetie
andauthored
Added Get-AzFirewallLearnedPrefix cmdlet and Route Server Id parameter to firewall (Azure#19825)
* Implemented learned IP prefixes * Added Route Server Id to firewall * Added unit test * Added Get-AzFirewallLearnedPrefix cmdlet * Added help and change log * Added example section * Implemented learned IP prefixes * Added Route Server Id to firewall * Added unit test * Added Get-AzFirewallLearnedPrefix cmdlet * Added help and change log * Added example section Co-authored-by: Gizachew Eshetie <[email protected]>
1 parent 43bd025 commit 82e5484

File tree

13 files changed

+4072
-3
lines changed

13 files changed

+4072
-3
lines changed

src/Network/Network.Test/ScenarioTests/AzureFirewallTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,20 @@ public void TestAzureFirewallCRUDEnableUDPLogOptimization()
178178
TestRunner.RunTestScript("Test-AzureFirewallCRUDEnableUDPLogOptimization");
179179
}
180180

181+
[Fact]
182+
[Trait(Category.AcceptanceType, Category.CheckIn)]
183+
[Trait(Category.Owner, NrpTeamAlias.azurefirewall)]
184+
public void TestAzureFirewallCRUDRouteServerId()
185+
{
186+
TestRunner.RunTestScript("Test-AzureFirewallCRUDRouteServerId");
187+
}
188+
189+
[Fact(Skip = "Skipped due to LearnedIpPrefixes feature not available in most regions")]
190+
[Trait(Category.AcceptanceType, Category.CheckIn)]
191+
[Trait(Category.Owner, NrpTeamAlias.azurefirewall)]
192+
public void TestGetAzureFirewallLearnedIpPrefixes()
193+
{
194+
TestRunner.RunTestScript("Test-GetAzureFirewallLearnedIpPrefixes");
195+
}
181196
}
182197
}

src/Network/Network.Test/ScenarioTests/AzureFirewallTests.ps1

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,4 +2009,89 @@ function Test-AzureFirewallCRUDEnableUDPLogOptimization {
20092009
# Cleanup
20102010
Clean-ResourceGroup $rgname
20112011
}
2012+
}
2013+
<#
2014+
.SYNOPSIS
2015+
Tests AzureFirewall RouteServerId
2016+
#>
2017+
function Test-AzureFirewallCRUDRouteServerId {
2018+
$rgname = Get-ResourceGroupName
2019+
$azureFirewallName = Get-ResourceName
2020+
$resourceTypeParent = "Microsoft.Network/AzureFirewalls"
2021+
$location = Get-ProviderLocation $resourceTypeParent "eastus2euap"
2022+
2023+
$vnetName = Get-ResourceName
2024+
$subnetName = "AzureFirewallSubnet"
2025+
$publicIpName = Get-ResourceName
2026+
$routeServerId="/subscriptions/aeb5b02a-0f18-45a4-86d6-81808115cacf/resourceGroups/testRG/providers/Microsoft.Network/virtualHubs/TestRS"
2027+
2028+
try {
2029+
# Create the resource group
2030+
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $location
2031+
2032+
# Create the Virtual Network
2033+
$subnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24
2034+
$vnet = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgname -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet
2035+
2036+
# Create public ip
2037+
$publicip = New-AzPublicIpAddress -ResourceGroupName $rgname -name $publicIpName -location $location -AllocationMethod Static -Sku Standard
2038+
2039+
# Create AzureFirewall
2040+
$azureFirewall = New-AzFirewall -Name $azureFirewallName -ResourceGroupName $rgname -Location $location -RouteServerId $routeServerId
2041+
2042+
# Verify
2043+
$azFirewall = Get-AzFirewall -Name $azureFirewallName -ResourceGroupName $rgname
2044+
Assert-AreEqual $routeServerId $azFirewall.RouteServerId
2045+
2046+
# Reset the RouteServerId
2047+
$azFirewall.RouteServerId = ""
2048+
Set-AzFirewall -AzureFirewall $azFirewall
2049+
$azfw = Get-AzFirewall -Name $azureFirewallName -ResourceGroupName $rgname
2050+
2051+
Assert-AreEqual "" $azfw.RouteServerId
2052+
}
2053+
finally {
2054+
# Cleanup
2055+
Clean-ResourceGroup $rgname
2056+
}
2057+
}
2058+
2059+
<#
2060+
.SYNOPSIS
2061+
Tests Get Azure Firewall LearnedPrefixes
2062+
#>
2063+
function Test-GetAzureFirewallLearnedIpPrefixes {
2064+
$rgname = Get-ResourceGroupName
2065+
$azureFirewallName = Get-ResourceName
2066+
$resourceTypeParent = "Microsoft.Network/AzureFirewalls"
2067+
$location = Get-ProviderLocation $resourceTypeParent "Eastus2euap"
2068+
2069+
$vnetName = Get-ResourceName
2070+
$subnetName = "AzureFirewallSubnet"
2071+
$publicIpName = Get-ResourceName
2072+
2073+
try {
2074+
# Create the resource group
2075+
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $location
2076+
2077+
# Create the Virtual Network
2078+
$subnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24
2079+
$vnet = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgname -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet
2080+
2081+
# Create public ip
2082+
$publicip = New-AzPublicIpAddress -ResourceGroupName $rgname -name $publicIpName -location $location -AllocationMethod Static -Sku Standard
2083+
2084+
# Create AzureFirewall
2085+
$azureFirewall = New-AzFirewall -Name $azureFirewallName -ResourceGroupName $rgname -Location $location
2086+
2087+
# Get Firewall Learned Prefixes
2088+
$learnedPrefixes = Get-AzFirewallLearnedIpPrefix -Name $azureFirewallName -ResourceGroupName $rgname
2089+
2090+
# Verify
2091+
Assert-NotNull $learnedPrefixes
2092+
}
2093+
finally {
2094+
# Cleanup
2095+
Clean-ResourceGroup $rgname
2096+
}
20122097
}

src/Network/Network.Test/SessionRecords/Commands.Network.Test.ScenarioTests.AzureFirewallTests/TestAzureFirewallCRUDRouteServerId.json

Lines changed: 2056 additions & 0 deletions
Large diffs are not rendered by default.

src/Network/Network.Test/SessionRecords/Commands.Network.Test.ScenarioTests.AzureFirewallTests/TestGetAzureFirewallLearnedIpPrefixes.json

Lines changed: 1715 additions & 0 deletions
Large diffs are not rendered by default.

src/Network/Network/Az.Network.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ CmdletsToExport = 'Add-AzApplicationGatewayAuthenticationCertificate',
642642
'New-AzNetworkManagerManagementGroupConnection',
643643
'Get-AzNetworkManagerManagementGroupConnection',
644644
'Remove-AzNetworkManagerManagementGroupConnection',
645-
'Set-AzNetworkManagerManagementGroupConnection'
645+
'Set-AzNetworkManagerManagementGroupConnection',
646+
"Get-AzFirewallLearnedIpPrefix"
646647

647648
# Variables to export from this module
648649
VariablesToExport = '*'
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
using System;
16+
using System.Collections.Generic;
17+
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.Network.Models;
19+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
20+
using Microsoft.Azure.Management.Network;
21+
using Microsoft.Azure.Management.Network.Models;
22+
23+
24+
namespace Microsoft.Azure.Commands.Network
25+
{
26+
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "FirewallLearnedIpPrefix"), OutputType(typeof(PSAzureFirewallIpPrefix), typeof(IEnumerable<PSAzureFirewallIpPrefix>))]
27+
public class GetAzureFirewallLearnedIpPrefixCommand : AzureFirewallBaseCmdlet
28+
{
29+
[Alias("ResourceName")]
30+
[Parameter(
31+
Mandatory = false,
32+
ValueFromPipelineByPropertyName = true,
33+
HelpMessage = "The firewall resource name.")]
34+
[ResourceNameCompleter("Microsoft.Network/azureFirewalls", "ResourceGroupName")]
35+
[ValidateNotNullOrEmpty]
36+
[SupportsWildcards]
37+
public string Name { get; set; }
38+
39+
[Parameter(
40+
Mandatory = false,
41+
ValueFromPipelineByPropertyName = true,
42+
HelpMessage = "The resource group name.")]
43+
[ValidateNotNullOrEmpty]
44+
[SupportsWildcards]
45+
public virtual string ResourceGroupName { get; set; }
46+
public override void ExecuteCmdlet()
47+
{
48+
base.ExecuteCmdlet();
49+
if (ShouldGetByName(ResourceGroupName, Name))
50+
{
51+
var learnedIPPrefixes = this.AzureFirewallClient.ListLearnedPrefixes(this.ResourceGroupName, this.Name);
52+
var pslearnedIPPrefixes = new PSAzureFirewallIpPrefix();
53+
if (learnedIPPrefixes != null)
54+
{
55+
pslearnedIPPrefixes = NetworkResourceManagerProfile.Mapper.Map<PSAzureFirewallIpPrefix>(learnedIPPrefixes);
56+
}
57+
WriteObject(pslearnedIPPrefixes);
58+
}
59+
else
60+
{
61+
throw new ArgumentException($" Name and ResourceGroupName should be provided to get firewall learned IP prefixes.");
62+
}
63+
}
64+
}
65+
}

src/Network/Network/AzureFirewall/AzureFirewallBaseCmdlet.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public bool IsAzureFirewallPresent(string resourceGroupName, string name)
8787
public PSAzureFirewall GetAzureFirewall(string resourceGroupName, string name)
8888
{
8989
var azureFirewall = this.AzureFirewallClient.Get(resourceGroupName, name);
90-
9190
var psAzureFirewall = NetworkResourceManagerProfile.Mapper.Map<PSAzureFirewall>(azureFirewall);
9291
psAzureFirewall.ResourceGroupName = resourceGroupName;
9392
psAzureFirewall.Tag = TagsConversionHelper.CreateTagHashtable(azureFirewall.Tags);

src/Network/Network/AzureFirewall/NewAzureFirewallCommand.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ public class NewAzureFirewallCommand : AzureFirewallBaseCmdlet
244244
)]
245245
public SwitchParameter EnableUDPLogOptimization { get; set; }
246246

247+
[Parameter(
248+
Mandatory = false,
249+
HelpMessage = "The Route Server Id for the firewall")]
250+
public string RouteServerId { get; set; }
251+
247252
public override void Execute()
248253
{
249254
// Old params provided - Get the virtual network, get the public IP address
@@ -336,7 +341,8 @@ private PSAzureFirewall CreateAzureFirewall()
336341
AllowActiveFTP = (this.AllowActiveFTP.IsPresent ? "true" : null),
337342
Sku = sku,
338343
EnableFatFlowLogging = (this.EnableFatFlowLogging.IsPresent ? "True" : null),
339-
EnableUDPLogOptimization = (this.EnableUDPLogOptimization.IsPresent ? "True" : null)
344+
EnableUDPLogOptimization = (this.EnableUDPLogOptimization.IsPresent ? "True" : null),
345+
RouteServerId = this.RouteServerId
340346
};
341347

342348
if (this.Zone != null)

src/Network/Network/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
--->
2020

2121
## Upcoming Release
22+
* Added new cmdlet to get firewall learned ip prefixes
23+
* `Get-AzFirewallLearnedIpPrefix`
2224
* Fixed a bug that does not update firewall policy application, network and nat rules' descriptions even though description is provided via description parameter
2325
* Added new cmdlet `Get-AzNetworkSecurityPerimeterAssociableResourceType`
2426
* Updated `New-AzIpConfigurationBgpPeeringAddressObject` to remove validate null or empty check for CustomAddress in Azure Virtual Network Gateway

src/Network/Network/Common/NetworkResourceManagerProfile.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,9 +1321,11 @@ private static void Initialize()
13211321
{ "Network.DNS.Servers", src.DNSServer?.Aggregate((result, item) => result + "," + item) },
13221322
{ "Network.AdditionalLogs.EnableFatFlowLogging", src.EnableFatFlowLogging },
13231323
{ "Network.Logging.EnableUDPLogOptimization", src.EnableUDPLogOptimization },
1324+
{ "Network.RouteServerInfo.RouteServerID", src.RouteServerId },
13241325
}.Where(kvp => kvp.Value != null).ToDictionary(key => key.Key, val => val.Value); // TODO: remove after backend code is refactored
13251326
});
13261327
cfg.CreateMap<CNM.PSAzureFirewallSku, MNM.AzureFirewallSku>();
1328+
cfg.CreateMap<CNM.PSAzureFirewallIpPrefix, MNM.IPPrefixesList>();
13271329
cfg.CreateMap<CNM.PSAzureFirewallIpConfiguration, MNM.AzureFirewallIPConfiguration>();
13281330
cfg.CreateMap<CNM.PSAzureFirewallApplicationRuleCollection, MNM.AzureFirewallApplicationRuleCollection>();
13291331
cfg.CreateMap<CNM.PSAzureFirewallNatRuleCollection, MNM.AzureFirewallNatRuleCollection>();
@@ -1369,6 +1371,7 @@ private static void Initialize()
13691371
dest.DNSEnableProxy = src.AdditionalProperties?.SingleOrDefault(kvp => kvp.Key.Equals("Network.DNS.EnableProxy", StringComparison.OrdinalIgnoreCase)).Value;
13701372
dest.EnableFatFlowLogging = src.AdditionalProperties?.SingleOrDefault(kvp => kvp.Key.Equals("Network.AdditionalLogs.EnableFatFlowLogging", StringComparison.OrdinalIgnoreCase)).Value;
13711373
dest.EnableUDPLogOptimization = src.AdditionalProperties?.SingleOrDefault(kvp => kvp.Key.Equals("Network.Logging.EnableUDPLogOptimization", StringComparison.OrdinalIgnoreCase)).Value;
1374+
dest.RouteServerId = src.AdditionalProperties?.SingleOrDefault(kvp => kvp.Key.Equals("Network.RouteServerInfo.RouteServerID", StringComparison.OrdinalIgnoreCase)).Value;
13721375
try
13731376
{
13741377
dest.DNSServer = src.AdditionalProperties?.SingleOrDefault(kvp => kvp.Key.Equals("Network.DNS.Servers", StringComparison.OrdinalIgnoreCase)).Value?.Split(',').Select(str => str.Trim()).ToArray();
@@ -1379,6 +1382,7 @@ private static void Initialize()
13791382
}
13801383
});
13811384
cfg.CreateMap<MNM.AzureFirewallSku, CNM.PSAzureFirewallSku>();
1385+
cfg.CreateMap<MNM.IPPrefixesList, CNM.PSAzureFirewallIpPrefix>();
13821386
cfg.CreateMap<MNM.AzureFirewallIPConfiguration, CNM.PSAzureFirewallIpConfiguration>();
13831387
cfg.CreateMap<MNM.AzureFirewallApplicationRuleCollection, CNM.PSAzureFirewallApplicationRuleCollection>();
13841388
cfg.CreateMap<MNM.AzureFirewallNatRuleCollection, CNM.PSAzureFirewallNatRuleCollection>();

0 commit comments

Comments
 (0)