From 7099f4b4347c579470569424a4a38a891a68decb Mon Sep 17 00:00:00 2001 From: "Kunal Kulkarni (from Dev Box)" Date: Mon, 6 May 2024 11:55:28 -0700 Subject: [PATCH 1/5] Change parsing logic in ATC custom cmdlet --- .../New-AzNetworkFunctionCollectorPolicy.ps1 | 39 ++++++++++++------- ...pdate-AzNetworkFunctionCollectorPolicy.ps1 | 36 +++++++++++------ 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 b/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 index 42a877cd2459..af33430b6bca 100644 --- a/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 +++ b/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 @@ -168,22 +168,33 @@ param( process { $rg = $PSBoundParameters.ResourceGroupName - - # Ensure exr circuit bandwidth 1G or more - $cktname = $IngestionPolicyIngestionSource.ResourceId | Where {$IngestionPolicyIngestionSource.ResourceId -match "/*subscriptions/(?.*)/resourceGroups/(?.*)/providers/Microsoft.Network/expressRouteCircuits/(?.*)"} | Foreach {$Matches['circuitname']} Import-Module Az.Network -Force - $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $rg - $bandwidthInGbps = $exrCircuit.BandwidthInGbps - $bandwidthInMbps = $exrCircuit.ServiceProviderProperties.BandwidthInMbps - - if ($bandwidthInGbps -and ($bandwidthInGbps -lt 1)) { - throw "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." - } - - if ($bandwidthInMbps -and ($bandwidthInMbps -lt 1000)) { - throw "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + # Ensure exr circuit bandwidth 1G or more + $ResourceIdSplit = $IngestionPolicyIngestionSource.ResourceId.Split(' ') + foreach ($ResourceId in $ResourceIdSplit) + { + $cktname = ParseCircuitName $ResourceId + $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $rg + $bandwidthInGbps = $exrCircuit.BandwidthInGbps + $bandwidthInMbps = $exrCircuit.ServiceProviderProperties.BandwidthInMbps + + if ($bandwidthInGbps -and ($bandwidthInGbps -lt 1)) { + throw "CollectorPolicy can not be created because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + } + + if ($bandwidthInMbps -and ($bandwidthInMbps -lt 1000)) { + throw "CollectorPolicy can not be created because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + } } - Az.NetworkFunction.internal\New-AzNetworkFunctionCollectorPolicy @PSBoundParameters } +} + +function ParseCircuitName($Rid){ + $Splits = $Rid -split "/" + $ParsedResults = @{} + if ($Splits.length -gt 1){ + $ParsedResults["cktName"] = $Splits[8] + } + return $ParsedResults["cktName"] } \ No newline at end of file diff --git a/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 b/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 index 09e58824742a..7a445f14a1b3 100644 --- a/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 +++ b/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 @@ -176,18 +176,23 @@ process { $rg = $PSBoundParameters.ResourceGroupName # 2. Ensure exr circuit bandwidth 1G or more - $cktname = $IngestionPolicyIngestionSource.ResourceId | Where {$IngestionPolicyIngestionSource.ResourceId -match "/*subscriptions/(?.*)/resourceGroups/(?.*)/providers/Microsoft.Network/expressRouteCircuits/(?.*)"} | Foreach {$Matches['circuitname']} Import-Module Az.Network -Force - $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $rg - $bandwidthInGbps = $exrCircuit.BandwidthInGbps - $bandwidthInMbps = $exrCircuit.ServiceProviderProperties.BandwidthInMbps - - if ($bandwidthInGbps -and ($bandwidthInGbps -lt 1)) { - throw "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." - } - - if ($bandwidthInMbps -and ($bandwidthInMbps -lt 1000)) { - throw "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + # Ensure exr circuit bandwidth 1G or more + $ResourceIdSplit = $IngestionPolicyIngestionSource.ResourceId.Split(' ') + foreach ($ResourceId in $ResourceIdSplit) + { + $cktname = ParseCircuitName $ResourceId + $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $rg + $bandwidthInGbps = $exrCircuit.BandwidthInGbps + $bandwidthInMbps = $exrCircuit.ServiceProviderProperties.BandwidthInMbps + + if ($bandwidthInGbps -and ($bandwidthInGbps -lt 1)) { + throw "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + } + + if ($bandwidthInMbps -and ($bandwidthInMbps -lt 1000)) { + throw "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + } } $cp = Get-AzNetworkFunctionCollectorPolicy @PSBoundParameters @@ -219,4 +224,13 @@ process { throw } } +} + +function ParseCircuitName($Rid){ + $Splits = $Rid -split "/" + $ParsedResults = @{} + if ($Splits.length -gt 1){ + $ParsedResults["cktName"] = $Splits[8] + } + return $ParsedResults["cktName"] } \ No newline at end of file From b9d8934bc6ffe88acefa6adbd5703a12b18a8e06 Mon Sep 17 00:00:00 2001 From: "Kunal Kulkarni (from Dev Box)" Date: Mon, 6 May 2024 13:42:52 -0700 Subject: [PATCH 2/5] Add if check --- ...pdate-AzNetworkFunctionCollectorPolicy.ps1 | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 b/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 index 7a445f14a1b3..75ff4cb0c83b 100644 --- a/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 +++ b/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 @@ -176,25 +176,27 @@ process { $rg = $PSBoundParameters.ResourceGroupName # 2. Ensure exr circuit bandwidth 1G or more - Import-Module Az.Network -Force - # Ensure exr circuit bandwidth 1G or more - $ResourceIdSplit = $IngestionPolicyIngestionSource.ResourceId.Split(' ') - foreach ($ResourceId in $ResourceIdSplit) - { - $cktname = ParseCircuitName $ResourceId - $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $rg - $bandwidthInGbps = $exrCircuit.BandwidthInGbps - $bandwidthInMbps = $exrCircuit.ServiceProviderProperties.BandwidthInMbps - - if ($bandwidthInGbps -and ($bandwidthInGbps -lt 1)) { - throw "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." - } - - if ($bandwidthInMbps -and ($bandwidthInMbps -lt 1000)) { - throw "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + if ($hasIngestionPolicyIngestionSource) { + Import-Module Az.Network -Force + # Ensure exr circuit bandwidth 1G or more + $ResourceIdSplit = $IngestionPolicyIngestionSource.ResourceId.Split(' ') + foreach ($ResourceId in $ResourceIdSplit) + { + $cktname = ParseCircuitName $ResourceId + $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $rg + $bandwidthInGbps = $exrCircuit.BandwidthInGbps + $bandwidthInMbps = $exrCircuit.ServiceProviderProperties.BandwidthInMbps + + if ($bandwidthInGbps -and ($bandwidthInGbps -lt 1)) { + throw "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + } + + if ($bandwidthInMbps -and ($bandwidthInMbps -lt 1000)) { + throw "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + } } } - + $cp = Get-AzNetworkFunctionCollectorPolicy @PSBoundParameters # 3. PUT From 6cfad2ff815a8c891859139365d936b9ae4832d1 Mon Sep 17 00:00:00 2001 From: "Kunal Kulkarni (from Dev Box)" Date: Mon, 6 May 2024 14:11:52 -0700 Subject: [PATCH 3/5] add if check --- .../New-AzNetworkFunctionCollectorPolicy.ps1 | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 b/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 index af33430b6bca..80d83950f948 100644 --- a/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 +++ b/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 @@ -168,24 +168,28 @@ param( process { $rg = $PSBoundParameters.ResourceGroupName - Import-Module Az.Network -Force + $hasIngestionPolicyIngestionSource = $PSBoundParameters.Remove('IngestionPolicyIngestionSource') # Ensure exr circuit bandwidth 1G or more - $ResourceIdSplit = $IngestionPolicyIngestionSource.ResourceId.Split(' ') - foreach ($ResourceId in $ResourceIdSplit) - { - $cktname = ParseCircuitName $ResourceId - $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $rg - $bandwidthInGbps = $exrCircuit.BandwidthInGbps - $bandwidthInMbps = $exrCircuit.ServiceProviderProperties.BandwidthInMbps - - if ($bandwidthInGbps -and ($bandwidthInGbps -lt 1)) { - throw "CollectorPolicy can not be created because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." - } - - if ($bandwidthInMbps -and ($bandwidthInMbps -lt 1000)) { - throw "CollectorPolicy can not be created because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + if ($hasIngestionPolicyIngestionSource) { + Import-Module Az.Network -Force + $ResourceIdSplit = $IngestionPolicyIngestionSource.ResourceId.Split(' ') + foreach ($ResourceId in $ResourceIdSplit) + { + $cktname = ParseCircuitName $ResourceId + $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $rg + $bandwidthInGbps = $exrCircuit.BandwidthInGbps + $bandwidthInMbps = $exrCircuit.ServiceProviderProperties.BandwidthInMbps + + if ($bandwidthInGbps -and ($bandwidthInGbps -lt 1)) { + throw "CollectorPolicy can not be created because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + } + + if ($bandwidthInMbps -and ($bandwidthInMbps -lt 1000)) { + throw "CollectorPolicy can not be created because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + } } } + $PSBoundParameters.Add('IngestionPolicyIngestionSource', $IngestionPolicyIngestionSource) Az.NetworkFunction.internal\New-AzNetworkFunctionCollectorPolicy @PSBoundParameters } } From dcfca0af1105affded47a23d1ffa85f9ee75eee8 Mon Sep 17 00:00:00 2001 From: Kunal Kulkarni Date: Tue, 7 May 2024 11:51:12 -0700 Subject: [PATCH 4/5] Minor change --- .../New-AzNetworkFunctionCollectorPolicy.ps1 | 19 +++++++----------- ...pdate-AzNetworkFunctionCollectorPolicy.ps1 | 20 +++++++------------ 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 b/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 index 80d83950f948..b182fc703360 100644 --- a/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 +++ b/src/NetworkFunction/NetworkFunction.Autorest/custom/New-AzNetworkFunctionCollectorPolicy.ps1 @@ -167,7 +167,6 @@ param( ) process { - $rg = $PSBoundParameters.ResourceGroupName $hasIngestionPolicyIngestionSource = $PSBoundParameters.Remove('IngestionPolicyIngestionSource') # Ensure exr circuit bandwidth 1G or more if ($hasIngestionPolicyIngestionSource) { @@ -175,8 +174,12 @@ process { $ResourceIdSplit = $IngestionPolicyIngestionSource.ResourceId.Split(' ') foreach ($ResourceId in $ResourceIdSplit) { - $cktname = ParseCircuitName $ResourceId - $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $rg + $Splits = $ResourceId -split "/" + $cktsub = $Splits[2] + $cktrg = $Splits[4] + $cktname = $Splits[8] + Set-AzContext $cktsub -ErrorVariable notPresent -ErrorAction SilentlyContinue + $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $cktrg $bandwidthInGbps = $exrCircuit.BandwidthInGbps $bandwidthInMbps = $exrCircuit.ServiceProviderProperties.BandwidthInMbps @@ -189,16 +192,8 @@ process { } } } + Set-AzContext $SubscriptionId -ErrorVariable notPresent -ErrorAction SilentlyContinue $PSBoundParameters.Add('IngestionPolicyIngestionSource', $IngestionPolicyIngestionSource) Az.NetworkFunction.internal\New-AzNetworkFunctionCollectorPolicy @PSBoundParameters } -} - -function ParseCircuitName($Rid){ - $Splits = $Rid -split "/" - $ParsedResults = @{} - if ($Splits.length -gt 1){ - $ParsedResults["cktName"] = $Splits[8] - } - return $ParsedResults["cktName"] } \ No newline at end of file diff --git a/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 b/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 index 75ff4cb0c83b..6495f0e4c87c 100644 --- a/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 +++ b/src/NetworkFunction/NetworkFunction.Autorest/custom/Update-AzNetworkFunctionCollectorPolicy.ps1 @@ -173,7 +173,6 @@ process { $null = $PSBoundParameters.Remove('WhatIf') $null = $PSBoundParameters.Remove('Confirm') $null = $PSBoundParameters.Remove('Location') - $rg = $PSBoundParameters.ResourceGroupName # 2. Ensure exr circuit bandwidth 1G or more if ($hasIngestionPolicyIngestionSource) { @@ -182,8 +181,12 @@ process { $ResourceIdSplit = $IngestionPolicyIngestionSource.ResourceId.Split(' ') foreach ($ResourceId in $ResourceIdSplit) { - $cktname = ParseCircuitName $ResourceId - $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $rg + $Splits = $ResourceId -split "/" + $cktsub = $Splits[2] + $cktrg = $Splits[4] + $cktname = $Splits[8] + Set-AzContext $cktsub -ErrorVariable notPresent -ErrorAction SilentlyContinue + $exrCircuit = Get-AzExpressRouteCircuit -Name $cktname -ResourceGroupName $cktrg $bandwidthInGbps = $exrCircuit.BandwidthInGbps $bandwidthInMbps = $exrCircuit.ServiceProviderProperties.BandwidthInMbps @@ -196,7 +199,7 @@ process { } } } - + Set-AzContext $SubscriptionId -ErrorVariable notPresent -ErrorAction SilentlyContinue $cp = Get-AzNetworkFunctionCollectorPolicy @PSBoundParameters # 3. PUT @@ -226,13 +229,4 @@ process { throw } } -} - -function ParseCircuitName($Rid){ - $Splits = $Rid -split "/" - $ParsedResults = @{} - if ($Splits.length -gt 1){ - $ParsedResults["cktName"] = $Splits[8] - } - return $ParsedResults["cktName"] } \ No newline at end of file From 9342b427e614868776c1eb1f8d27436a73591813 Mon Sep 17 00:00:00 2001 From: Kunal Kulkarni Date: Tue, 7 May 2024 12:05:37 -0700 Subject: [PATCH 5/5] Add UT --- .../test/New-AzNetworkFunctionCollectorPolicy.Tests.ps1 | 6 ++++++ .../test/Update-AzNetworkFunctionCollectorPolicy.Tests.ps1 | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/NetworkFunction/NetworkFunction.Autorest/test/New-AzNetworkFunctionCollectorPolicy.Tests.ps1 b/src/NetworkFunction/NetworkFunction.Autorest/test/New-AzNetworkFunctionCollectorPolicy.Tests.ps1 index 570054154703..35a069386699 100644 --- a/src/NetworkFunction/NetworkFunction.Autorest/test/New-AzNetworkFunctionCollectorPolicy.Tests.ps1 +++ b/src/NetworkFunction/NetworkFunction.Autorest/test/New-AzNetworkFunctionCollectorPolicy.Tests.ps1 @@ -27,6 +27,12 @@ Describe 'New-AzNetworkFunctionCollectorPolicy' { } } + It 'CreateExpanded2' { + { + { New-AzNetworkFunctionCollectorPolicy -collectorpolicyname $env.collectorPolicyName -azuretrafficcollectorname $env.azureTrafficCollectorName -resourcegroupname $env.resourceGroup -location $env.location -IngestionPolicyIngestionSource @{ResourceId = $env.ResourceIdLessThan1G}, @{ResourceId = $env.ResourceId1G} -IngestionPolicyIngestionType $env.IngestionType } | Should -Throw -ExpectedMessage "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + } + } + It 'Create' { { { New-AzNetworkFunctionCollectorPolicy -collectorpolicyname $env.collectorPolicyName -azuretrafficcollectorname $env.azureTrafficCollectorName -resourcegroupname $env.resourceGroup -location $env.location -IngestionPolicyIngestionSource @{ResourceId = $env.ResourceId1G} -IngestionPolicyIngestionType $env.IngestionType } | Should -Not -Throw diff --git a/src/NetworkFunction/NetworkFunction.Autorest/test/Update-AzNetworkFunctionCollectorPolicy.Tests.ps1 b/src/NetworkFunction/NetworkFunction.Autorest/test/Update-AzNetworkFunctionCollectorPolicy.Tests.ps1 index b3423040fbe1..4a3c83c9d52e 100644 --- a/src/NetworkFunction/NetworkFunction.Autorest/test/Update-AzNetworkFunctionCollectorPolicy.Tests.ps1 +++ b/src/NetworkFunction/NetworkFunction.Autorest/test/Update-AzNetworkFunctionCollectorPolicy.Tests.ps1 @@ -26,4 +26,10 @@ Describe 'Update-AzNetworkFunctionCollectorPolicy' { { Update-AzNetworkFunctionCollectorPolicy -collectorpolicyname $env.collectorPolicyName -azuretrafficcollectorname $env.azureTrafficCollectorName -resourcegroupname $env.resourceGroup -location $env.location -IngestionPolicyIngestionSource @{ResourceId = $env.ResourceIdLessThan1G} -IngestionPolicyIngestionType $env.IngestionType } | Should Throw -ExpectedMessage "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." } } + + It 'UpdateExpanded2' { + { + { Update-AzNetworkFunctionCollectorPolicy -collectorpolicyname $env.collectorPolicyName -azuretrafficcollectorname $env.azureTrafficCollectorName -resourcegroupname $env.resourceGroup -location $env.location -IngestionPolicyIngestionSource @{ResourceId = $env.ResourceIdLessThan1G}, @{ResourceId = $env.ResourceId1G} -IngestionPolicyIngestionType $env.IngestionType } | Should Throw -ExpectedMessage "CollectorPolicy can not be updated because circuit has bandwidth less than 1G. Circuit size with a bandwidth of 1G or more is supported." + } + } } \ No newline at end of file