Skip to content

Commit 1355f5e

Browse files
authored
[Az.Network] Add new cmdlet: Set-AzNetworkManagerIpamPoolStaticCidr (#28297)
1 parent a62aba4 commit 1355f5e

File tree

7 files changed

+850
-239
lines changed

7 files changed

+850
-239
lines changed

src/Network/Network.Test/ScenarioTests/NetworkManagerTests.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,8 @@ function Test-NetworkManagerIpamPoolStaticCidrCRUD
15401540
$rglocation = "centraluseuap"
15411541
$subscriptionId = "/subscriptions/08615b4b-bc9c-4a70-be1b-2ea10bc97b52"
15421542
$addressPrefixes = @("10.0.0.0/8")
1543+
$poolAddressPrefixes = @("10.0.0.0/8", "192.168.0.0/16")
1544+
$addAddressPrefix = @("192.168.0.0/24")
15431545

15441546
try{
15451547
#Create the resource group
@@ -1556,7 +1558,7 @@ function Test-NetworkManagerIpamPoolStaticCidrCRUD
15561558
New-AzNetworkManager -ResourceGroupName $rgName -Name $networkManagerName -NetworkManagerScope $scope -NetworkManagerScopeAccess $access -Location $rglocation
15571559

15581560
# Create ipam pool
1559-
New-AzNetworkManagerIpamPool -ResourceGroupName $rgName -NetworkManagerName $networkManagerName -Name $ipamPoolName -Location $rglocation -AddressPrefix $addressPrefixes
1561+
New-AzNetworkManagerIpamPool -ResourceGroupName $rgName -NetworkManagerName $networkManagerName -Name $ipamPoolName -Location $rglocation -AddressPrefix $poolAddressPrefixes
15601562

15611563
# Create static cidr
15621564
New-AzNetworkManagerIpamPoolStaticCidr -ResourceGroupName $rgName -NetworkManagerName $networkManagerName -PoolName $ipamPoolName -Name $staticCidrName -AddressPrefix $addressPrefixes
@@ -1567,6 +1569,11 @@ function Test-NetworkManagerIpamPoolStaticCidrCRUD
15671569
Assert-AreEqual $staticCidrName $staticCidr.Name;
15681570
Assert-AreEqual $staticCidr.Properties.AddressPrefixes[0] $addressPrefixes[0];
15691571

1572+
# Update static cidr
1573+
$staticCidr.Properties.AddressPrefixes.Add($addAddressPrefix);
1574+
$updatedStaticCidr = Set-AzNetworkManagerIpamPoolStaticCidr -InputObject $staticCidr
1575+
Assert-AreEqual $updatedStaticCidr.Properties.AddressPrefixes[1] $addAddressPrefix[0];
1576+
15701577
# Remove static cidr
15711578
$job = Remove-AzNetworkManagerIpamPoolStaticCidr -ResourceGroupName $rgName -NetworkManagerName $networkManagerName -IpamPoolName $ipamPoolName -Name $staticCidrName -PassThru -Force -AsJob;
15721579
$job | Wait-Job;

src/Network/Network.Test/SessionRecords/Commands.Network.Test.ScenarioTests.NetworkManagerTests/NetworkManagerIpamPoolStaticCidrCRUD.json

Lines changed: 565 additions & 238 deletions
Large diffs are not rendered by default.

src/Network/Network/Az.Network.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ CmdletsToExport = 'Add-AzApplicationGatewayAuthenticationCertificate',
693693
'Set-AzNetworkInterfaceTapConfig', 'Set-AzNetworkManager',
694694
'Set-AzNetworkManagerConnectivityConfiguration',
695695
'Set-AzNetworkManagerGroup', 'Set-AzNetworkManagerIpamPool',
696+
'Set-AzNetworkManagerIpamPoolStaticCidr',
696697
'Set-AzNetworkManagerManagementGroupConnection',
697698
'Set-AzNetworkManagerRoutingConfiguration',
698699
'Set-AzNetworkManagerRoutingRule',

src/Network/Network/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* Returned appgw and agc in waf policy
3434
* Updated cmdlet `New-AzFirewallPolicyApplicationRule` to use HTTPS as the default protocol when creating a new FQDN Tag application rule.
3535
* Added `EnableDnstapLogging` parameter to `New-AzFirewall`
36+
* Added cmdlet 'Set-AzNetworkManagerIpamPoolStaticCidr to enable updating an IPAM pool's static cidr address prefix space or description
3637

3738
## Version 7.18.0
3839
* Added a new command which creates an object for CaptureSetting, and added properties 'FileCount', 'FileSizeInBytes', and 'SessionTimeLimitInSeconds', which helps to configure the capture setting for packet capture as well as support for it for the following cmdlets:
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 AutoMapper;
16+
using Microsoft.Azure.Commands.Network.Models;
17+
using Microsoft.Azure.Commands.Network.Models.NetworkManager;
18+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
19+
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
20+
using Microsoft.Azure.Management.Network;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Linq;
24+
using System.Management.Automation;
25+
using MNM = Microsoft.Azure.Management.Network.Models;
26+
27+
namespace Microsoft.Azure.Commands.Network
28+
{
29+
[Cmdlet("Set", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "NetworkManagerIpamPoolStaticCidr", SupportsShouldProcess = true), OutputType(typeof(PSStaticCidr))]
30+
public class SetAzNetworkManagerIpamPoolStaticCidrCommand : IpamPoolStaticCidrBaseCmdlet
31+
{
32+
[Parameter(
33+
Mandatory = true,
34+
ValueFromPipeline = true,
35+
HelpMessage = "The Static CIDR object")]
36+
public PSStaticCidr InputObject { get; set; }
37+
38+
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
39+
public SwitchParameter AsJob { get; set; }
40+
41+
public override void Execute()
42+
{
43+
if (this.ShouldProcess(this.InputObject.Name, VerbsLifecycle.Restart))
44+
{
45+
base.Execute();
46+
47+
if (!this.IsStaticCidrPresent(this.InputObject.ResourceGroupName, this.InputObject.NetworkManagerName, this.InputObject.PoolName, this.InputObject.Name))
48+
{
49+
throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Network.Properties.Resources.ResourceNotFound, this.InputObject.Name));
50+
}
51+
52+
var existingStaticCidr = this.GetStaticCidr(this.InputObject.ResourceGroupName, this.InputObject.NetworkManagerName, this.InputObject.PoolName, this.InputObject.Name);
53+
54+
ApplyMutualExclusivityLogic(existingStaticCidr, this.InputObject);
55+
56+
var staticCidrModel = NetworkResourceManagerProfile.Mapper.Map<MNM.StaticCidr>(this.InputObject);
57+
58+
this.StaticCidrClient.Create(this.InputObject.ResourceGroupName, this.InputObject.NetworkManagerName, this.InputObject.PoolName, this.InputObject.Name, staticCidrModel);
59+
var psStaticCidr = this.GetStaticCidr(this.InputObject.ResourceGroupName, this.InputObject.NetworkManagerName, this.InputObject.PoolName, this.InputObject.Name);
60+
WriteObject(psStaticCidr);
61+
}
62+
}
63+
64+
private void ApplyMutualExclusivityLogic(PSStaticCidr existingStaticCidr, PSStaticCidr inputObject)
65+
{
66+
if (existingStaticCidr?.Properties == null || inputObject?.Properties == null)
67+
{
68+
return;
69+
}
70+
71+
var existingAddressPrefixes = existingStaticCidr.Properties.AddressPrefixes ?? new List<string>();
72+
var inputAddressPrefixes = inputObject.Properties.AddressPrefixes ?? new List<string>();
73+
74+
var existingNumberOfIPs = existingStaticCidr.Properties.NumberOfIPAddressesToAllocate;
75+
var inputNumberOfIPs = inputObject.Properties.NumberOfIPAddressesToAllocate;
76+
77+
// Check if AddressPrefixes property is being updated
78+
bool addressPrefixesChanged = !AreAddressPrefixesEqual(existingAddressPrefixes, inputAddressPrefixes);
79+
80+
// Check if NumberOfIPAddressesToAllocate property is being updated
81+
bool numberOfIPsChanged = !string.Equals(existingNumberOfIPs, inputNumberOfIPs, StringComparison.OrdinalIgnoreCase);
82+
83+
if (addressPrefixesChanged && !numberOfIPsChanged)
84+
{
85+
// AddressPrefixes is being updated and NumberOfIPAddressesToAllocate remains unchanged
86+
// Set NumberOfIPAddressesToAllocate to "0"
87+
inputObject.Properties.NumberOfIPAddressesToAllocate = "0";
88+
}
89+
else if (numberOfIPsChanged && !addressPrefixesChanged)
90+
{
91+
// NumberOfIPAddressesToAllocate is being updated and AddressPrefixes remains unchanged
92+
// Set AddressPrefixes to empty list
93+
inputObject.Properties.AddressPrefixes = new List<string>();
94+
}
95+
}
96+
97+
private bool AreAddressPrefixesEqual(IList<string> list1, IList<string> list2)
98+
{
99+
if (list1 == null && list2 == null) return true;
100+
if (list1 == null || list2 == null) return false;
101+
if (list1.Count != list2.Count) return false;
102+
103+
// Compare sorted lists to handle order differences
104+
var sorted1 = list1.OrderBy(x => x).ToList();
105+
var sorted2 = list2.OrderBy(x => x).ToList();
106+
107+
for (int i = 0; i < sorted1.Count; i++)
108+
{
109+
if (!string.Equals(sorted1[i], sorted2[i], StringComparison.OrdinalIgnoreCase))
110+
{
111+
return false;
112+
}
113+
}
114+
115+
return true;
116+
}
117+
}
118+
}

src/Network/Network/help/Az.Network.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,6 +2336,9 @@ Updates a network manager group.
23362336
### [Set-AzNetworkManagerIpamPool](Set-AzNetworkManagerIpamPool.md)
23372337
Updates an IPAM pool.
23382338

2339+
### [Set-AzNetworkManagerIpamPoolStaticCidr](Set-AzNetworkManagerIpamPoolStaticCidr.md)
2340+
Updates an IPAM pool's static cidr.
2341+
23392342
### [Set-AzNetworkManagerManagementGroupConnection](Set-AzNetworkManagerManagementGroupConnection.md)
23402343
Update a network manger management group connection
23412344

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
external help file: Microsoft.Azure.PowerShell.Cmdlets.Network.dll-Help.xml
3+
Module Name: Az.Network
4+
online version: https://learn.microsoft.com/powershell/module/az.network/set-aznetworkmanageripampoolstaticcidr
5+
schema: 2.0.0
6+
---
7+
8+
# Set-AzNetworkManagerIpamPoolStaticCidr
9+
10+
## SYNOPSIS
11+
Updates a static CIDR allocation in an IPAM pool.
12+
13+
## SYNTAX
14+
15+
```
16+
Set-AzNetworkManagerIpamPoolStaticCidr -InputObject <PSStaticCidr> [-AsJob]
17+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
18+
```
19+
20+
## DESCRIPTION
21+
The **Set-AzNetworkManagerIpamPoolStaticCidr** cmdlet updates a static CIDR allocation in an IPAM pool.
22+
23+
## EXAMPLES
24+
25+
### Example 1: Update static CIDR description
26+
```powershell
27+
$ResourceGroupName = "testRG"
28+
$NetworkManagerName = "testNM"
29+
$IpamPoolName = "testPool"
30+
$StaticCidrName = "testStaticCidr"
31+
32+
$staticCidr = Get-AzNetworkManagerIpamPoolStaticCidr -ResourceGroupName $ResourceGroupName -NetworkManagerName $NetworkManagerName -IpamPoolName $IpamPoolName -Name $StaticCidrName
33+
34+
$staticCidr.Properties.Description = "Updated description"
35+
36+
Set-AzNetworkManagerIpamPoolStaticCidr -InputObject $staticCidr
37+
```
38+
39+
Updates the description of an existing static CIDR allocation.
40+
41+
### Example 2: Update static CIDR address prefixes
42+
```powershell
43+
$ResourceGroupName = "testRG"
44+
$NetworkManagerName = "testNM"
45+
$IpamPoolName = "testPool"
46+
$StaticCidrName = "testStaticCidr"
47+
48+
$staticCidr = Get-AzNetworkManagerIpamPoolStaticCidr -ResourceGroupName $ResourceGroupName -NetworkManagerName $NetworkManagerName -IpamPoolName $IpamPoolName -Name $StaticCidrName
49+
50+
$staticCidr.Properties.AddressPrefixes = @("10.0.0.0/24", "10.0.1.0/24")
51+
52+
Set-AzNetworkManagerIpamPoolStaticCidr -InputObject $staticCidr
53+
```
54+
55+
Updates the address prefixes of an existing static CIDR allocation.
56+
57+
## PARAMETERS
58+
59+
### -AsJob
60+
Run cmdlet in the background
61+
62+
```yaml
63+
Type: System.Management.Automation.SwitchParameter
64+
Parameter Sets: (All)
65+
Aliases:
66+
67+
Required: False
68+
Position: Named
69+
Default value: None
70+
Accept pipeline input: False
71+
Accept wildcard characters: False
72+
```
73+
74+
### -DefaultProfile
75+
The credentials, account, tenant, and subscription used for communication with Azure.
76+
77+
```yaml
78+
Type: Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer
79+
Parameter Sets: (All)
80+
Aliases: AzContext, AzureRmContext, AzureCredential
81+
82+
Required: False
83+
Position: Named
84+
Default value: None
85+
Accept pipeline input: False
86+
Accept wildcard characters: False
87+
```
88+
89+
### -InputObject
90+
The Static CIDR object.
91+
92+
```yaml
93+
Type: Microsoft.Azure.Commands.Network.Models.NetworkManager.PSStaticCidr
94+
Parameter Sets: (All)
95+
Aliases:
96+
97+
Required: True
98+
Position: Named
99+
Default value: None
100+
Accept pipeline input: True (ByValue)
101+
Accept wildcard characters: False
102+
```
103+
104+
### -Confirm
105+
Prompts you for confirmation before running the cmdlet.
106+
107+
```yaml
108+
Type: System.Management.Automation.SwitchParameter
109+
Parameter Sets: (All)
110+
Aliases: cf
111+
112+
Required: False
113+
Position: Named
114+
Default value: None
115+
Accept pipeline input: False
116+
Accept wildcard characters: False
117+
```
118+
119+
### -WhatIf
120+
Shows what would happen if the cmdlet runs.
121+
The cmdlet is not run.
122+
123+
```yaml
124+
Type: System.Management.Automation.SwitchParameter
125+
Parameter Sets: (All)
126+
Aliases: wi
127+
128+
Required: False
129+
Position: Named
130+
Default value: None
131+
Accept pipeline input: False
132+
Accept wildcard characters: False
133+
```
134+
135+
### CommonParameters
136+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
137+
138+
## INPUTS
139+
140+
### Microsoft.Azure.Commands.Network.Models.NetworkManager.PSStaticCidr
141+
142+
## OUTPUTS
143+
144+
### Microsoft.Azure.Commands.Network.Models.NetworkManager.PSStaticCidr
145+
146+
## NOTES
147+
148+
## RELATED LINKS
149+
150+
[Get-AzNetworkManagerIpamPoolStaticCidr](./Get-AzNetworkManagerIpamPoolStaticCidr.md)
151+
152+
[New-AzNetworkManagerIpamPoolStaticCidr](./New-AzNetworkManagerIpamPoolStaticCidr.md)
153+
154+
[Remove-AzNetworkManagerIpamPoolStaticCidr](./Remove-AzNetworkManagerIpamPoolStaticCidr.md)

0 commit comments

Comments
 (0)