Skip to content

Commit

Permalink
[EventHubs] Fixing Set-AzEventHubNetworkRuleSet (#18024)
Browse files Browse the repository at this point in the history
* Fixing Set-AzEventHubNetworkRuleSet

* adding changelog

* Update ChangeLog.md

Co-authored-by: Yunchi Wang <[email protected]>
  • Loading branch information
damodaravadhani and wyunchi-ms authored May 6, 2022
1 parent 3510359 commit 1635bbe
Show file tree
Hide file tree
Showing 8 changed files with 956 additions and 666 deletions.
22 changes: 22 additions & 0 deletions src/EventHub/EventHub.Test/ScenarioTests/NetworkRuleSetTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ function NetworkRuleSetTests
Assert-AreEqual $setResult2.PublicNetworkAccess "Disabled"
Assert-AreEqual $setResult2.DefaultAction "Allow"

$setResult2 = Set-AzEventHubNetworkRuleSet -ResourceGroup $resourceGroupName -Name $namespaceName2 -PublicNetworkAccess "Enabled"
Assert-AreEqual $setResult2.VirtualNetworkRules.Count 3 "Set -VirtualNetworkRules count did not matched"
Assert-AreEqual $setResult2.IpRules.Count 3 "Set - IPRules count did not matched"
Assert-AreEqual $setResult2.PublicNetworkAccess "Enabled"
Assert-AreEqual $setResult2.DefaultAction "Allow"

$setResult2 = Set-AzEventHubNetworkRuleSet -ResourceGroup $resourceGroupName -Name $namespaceName2 -TrustedServiceAccessEnabled
Assert-AreEqual $setResult2.VirtualNetworkRules.Count 3 "Set -VirtualNetworkRules count did not matched"
Assert-AreEqual $setResult2.IpRules.Count 3 "Set - IPRules count did not matched"
Assert-AreEqual $setResult2.PublicNetworkAccess "Enabled"
Assert-AreEqual $setResult2.DefaultAction "Allow"
Assert-True {$setResult.TrustedServiceAccessEnabled}

$a, $b, $c = $setResult2.IpRules
$setResult2.IpRules = $c = [Microsoft.Azure.Commands.EventHub.Models.PSNWRuleSetIpRulesAttributes[]]@($a, $b)

$setResult2 = Set-AzEventHubNetworkRuleSet -ResourceGroup $resourceGroupName -Name $namespaceName2 -IPRule $setResult2.IpRules
Assert-AreEqual $setResult2.VirtualNetworkRules.Count 3 "Set -VirtualNetworkRules count did not matched"
Assert-AreEqual $setResult2.IpRules.Count 2 "Set - IPRules count did not matched"
Assert-AreEqual $setResult2.PublicNetworkAccess "Enabled"
Assert-AreEqual $setResult2.DefaultAction "Allow"
Assert-True {$setResult.TrustedServiceAccessEnabled}

Write-Debug "Add a new VirtualNetworkRule to the default NetworkRuleSet"
$result = Remove-AzEventHubVirtualNetworkRule -ResourceGroup $resourceGroupName -Name $namespaceName -SubnetId "/subscriptions/326100e2-f69d-4268-8503-075374f62b6e/resourcegroups/v-ajnavtest/providers/Microsoft.Network/virtualNetworks/sbehvnettest1/subnets/default"
Expand Down

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/EventHub/EventHub/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Made `IPRule` and `VirtualNetworkRule` optional in `Set-AzEventHubNetworkRuleSet`.

## Version 1.11.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class SetAzureEventHubNetworkrule : AzureEventHubsCmdletBase
[Parameter(Mandatory = false, ParameterSetName = NetwrokruleSetPropertiesParameterSet, HelpMessage = "Indicates whether TrustedServiceAccessEnabled is enabled")]
public SwitchParameter TrustedServiceAccessEnabled { get; set; }

[Parameter(Mandatory = true, ParameterSetName = NetwrokruleSetPropertiesParameterSet, Position = 2, HelpMessage = "List of IPRuleSet")]
[Parameter(Mandatory = false, ParameterSetName = NetwrokruleSetPropertiesParameterSet, Position = 2, HelpMessage = "List of IPRuleSet")]
[ValidateNotNullOrEmpty]
public PSNWRuleSetIpRulesAttributes[] IPRule { get; set; }

Expand All @@ -57,7 +57,7 @@ public class SetAzureEventHubNetworkrule : AzureEventHubsCmdletBase
[PSDefaultValue(Value = "Enabled")]
public string PublicNetworkAccess { get; set; }

[Parameter(Mandatory = true, ParameterSetName = NetwrokruleSetPropertiesParameterSet, Position = 3, HelpMessage = "List of VirtualNetworkRules")]
[Parameter(Mandatory = false, ParameterSetName = NetwrokruleSetPropertiesParameterSet, Position = 3, HelpMessage = "List of VirtualNetworkRules")]
[ValidateNotNullOrEmpty]
[Alias(AliasVirtualNetworkRule)]
public PSNWRuleSetVirtualNetworkRulesAttributes[] VirtualNetworkRule { get; set; }
Expand All @@ -81,16 +81,13 @@ public override void ExecuteCmdlet()
{
if (ParameterSetName.Equals(NetwrokruleSetPropertiesParameterSet))
{
PSNetworkRuleSetAttributes networkRuleSetAttributes = new PSNetworkRuleSetAttributes()
{
DefaultAction = DefaultAction,
TrustedServiceAccessEnabled = TrustedServiceAccessEnabled.IsPresent,
IpRules = IPRule.OfType<PSNWRuleSetIpRulesAttributes>().ToList(),
VirtualNetworkRules = VirtualNetworkRule.OfType<PSNWRuleSetVirtualNetworkRulesAttributes>().ToList(),
PublicNetworkAccess = PublicNetworkAccess
};

WriteObject(Client.CreateOrUpdateNetworkRuleSet(ResourceGroupName, Name, networkRuleSetAttributes));
WriteObject(Client.UpdateNetworkRuleSet(resourceGroupName: ResourceGroupName,
namespaceName: Name,
publicNetworkAccess: PublicNetworkAccess,
trustedServiceAccessEnabled: TrustedServiceAccessEnabled,
defaultAction: DefaultAction,
iPRule: IPRule,
virtualNetworkRule: VirtualNetworkRule));
}

if (ParameterSetName.Equals(NetwrokruleSetInputObjectParameterSet))
Expand Down
4 changes: 4 additions & 0 deletions src/EventHub/EventHub/EventHub.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@
<Label>PublicNetworkAccess</Label>
<PropertyName>PublicNetworkAccess</PropertyName>
</ListItem>
<ListItem>
<Label>TrustedServiceAccessEnabled</Label>
<PropertyName>TrustedServiceAccessEnabled</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
Expand Down
1 change: 1 addition & 0 deletions src/EventHub/EventHub/Models/PSNetworkRuleSetAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public PSNetworkRuleSetAttributes(NetworkRuleSet networkRuleSet)
Name = networkRuleSet.Name;
Type = networkRuleSet.Type;
PublicNetworkAccess = networkRuleSet?.PublicNetworkAccess;
TrustedServiceAccessEnabled = networkRuleSet?.TrustedServiceAccessEnabled;
}

/// <summary>
Expand Down
49 changes: 49 additions & 0 deletions src/EventHub/EventHub/Utilities/EventHubsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,55 @@ public PSNetworkRuleSetAttributes CreateOrUpdateNetworkRuleSet(string resourceGr
return new PSNetworkRuleSetAttributes(response);
}

public PSNetworkRuleSetAttributes UpdateNetworkRuleSet(string resourceGroupName, string namespaceName, string publicNetworkAccess, bool? trustedServiceAccessEnabled, string defaultAction, PSNWRuleSetIpRulesAttributes[] iPRule, PSNWRuleSetVirtualNetworkRulesAttributes[] virtualNetworkRule)
{
NetworkRuleSet networkRuleSet = Client.Namespaces.GetNetworkRuleSet(resourceGroupName, namespaceName);

if (networkRuleSet == null)
{
networkRuleSet = new NetworkRuleSet();
}

if (defaultAction != null)
{
networkRuleSet.DefaultAction = defaultAction;
}

if (publicNetworkAccess != null)
{
networkRuleSet.PublicNetworkAccess = publicNetworkAccess;
}

if (trustedServiceAccessEnabled == true)
{
networkRuleSet.TrustedServiceAccessEnabled = trustedServiceAccessEnabled;
}

if (iPRule != null)
{
networkRuleSet.IpRules = new List<NWRuleSetIpRules>();

foreach (PSNWRuleSetIpRulesAttributes psiprules in iPRule)
{
networkRuleSet.IpRules.Add(new NWRuleSetIpRules { Action = psiprules.Action, IpMask = psiprules.IpMask });
}
}

if (virtualNetworkRule != null)
{
networkRuleSet.VirtualNetworkRules = new List<NWRuleSetVirtualNetworkRules>();

foreach (PSNWRuleSetVirtualNetworkRulesAttributes psvisrtualnetworkrules in virtualNetworkRule)
{
networkRuleSet.VirtualNetworkRules.Add(new NWRuleSetVirtualNetworkRules { Subnet = new Subnet { Id = psvisrtualnetworkrules.Subnet.Id }, IgnoreMissingVnetServiceEndpoint = psvisrtualnetworkrules.IgnoreMissingVnetServiceEndpoint });
}
}

var response = Client.Namespaces.CreateOrUpdateNetworkRuleSet(resourceGroupName, namespaceName, networkRuleSet);
return new PSNetworkRuleSetAttributes(response);

}

#endregion

#region SchemaRegistry
Expand Down
28 changes: 21 additions & 7 deletions src/EventHub/EventHub/help/Set-AzEventHubNetworkRuleSet.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ Update the NetworkruleSet of the given Namespace in the current Azure subscripti
### NetworkRuleSetPropertiesSet (Default)
```
Set-AzEventHubNetworkRuleSet [-ResourceGroupName] <String> [-Name] <String> [-DefaultAction <String>]
[-TrustedServiceAccessEnabled] [-IPRule] <PSNWRuleSetIpRulesAttributes[]> [-PublicNetworkAccess <String>]
[-VirtualNetworkRule] <PSNWRuleSetVirtualNetworkRulesAttributes[]> [-DefaultProfile <IAzureContextContainer>]
[-WhatIf] [-Confirm] [<CommonParameters>]
[-TrustedServiceAccessEnabled] [[-IPRule] <PSNWRuleSetIpRulesAttributes[]>] [-PublicNetworkAccess <String>]
[[-VirtualNetworkRule] <PSNWRuleSetVirtualNetworkRulesAttributes[]>]
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
```

### NetwrokruleSetInputObjectSet
Expand Down Expand Up @@ -142,7 +142,7 @@ Type: Microsoft.Azure.Commands.EventHub.Models.PSNWRuleSetIpRulesAttributes[]
Parameter Sets: NetworkRuleSetPropertiesSet
Aliases:

Required: True
Required: False
Position: 2
Default value: None
Accept pipeline input: False
Expand All @@ -164,6 +164,21 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -PublicNetworkAccess
Public Network Access for NetwrokeuleSet
```yaml
Type: System.String
Parameter Sets: NetworkRuleSetPropertiesSet
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -ResourceGroupName
Resource Group Name.
Expand Down Expand Up @@ -217,7 +232,7 @@ Type: Microsoft.Azure.Commands.EventHub.Models.PSNWRuleSetVirtualNetworkRulesAtt
Parameter Sets: NetworkRuleSetPropertiesSet
Aliases: VirtualNteworkRule

Required: True
Required: False
Position: 3
Default value: None
Accept pipeline input: False
Expand Down Expand Up @@ -256,8 +271,7 @@ Accept wildcard characters: False
```
### CommonParameters
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).
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).
## INPUTS
Expand Down

0 comments on commit 1635bbe

Please sign in to comment.