Skip to content

Commit 8d0a27b

Browse files
committed
Add IP rule cmdlets
1 parent 04ccb6f commit 8d0a27b

25 files changed

+2626
-128
lines changed

src/SignalR/SignalR.Test/ScenarioTests/AzureRmSignalRTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,12 @@ public void TestAzureRmSignalRCustomCertificateAndCustomDomain() =>
5757
[Trait(Category.AcceptanceType, Category.CheckIn)]
5858
public void TestAzSignalRReplica() =>
5959
TestRunner.RunTestScript("Test-AzSignalRReplica");
60+
61+
[Fact]
62+
[Trait(Category.AcceptanceType, Category.CheckIn)]
63+
public void TestAzSignalRNetworkIpRule()
64+
{
65+
TestRunner.RunTestScript("Test-AzSignalRNetworkIpRule");
66+
}
6067
}
6168
}

src/SignalR/SignalR.Test/ScenarioTests/AzureRmSignalRTests.ps1

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function Test-AzureRmSignalR
7373
$newKeys1 = Get-AzSignalRKey -ResourceGroupName $resourceGroupName -Name $signalrName
7474
Assert-NotNull $newKeys1
7575

76-
if( GetTestMode -eq "Record")
76+
if( $env:AZURE_TEST_MODE -eq "Record")
7777
{
7878
Assert-AreNotEqual $keys.PrimaryKey $newKeys1.PrimaryKey
7979
Assert-AreNotEqual $keys.PrimaryConnectionString $newKeys1.PrimaryConnectionString
@@ -89,7 +89,7 @@ function Test-AzureRmSignalR
8989
Assert-NotNull $newKeys2
9090
Assert-AreEqual $newKeys1.PrimaryKey $newKeys2.PrimaryKey
9191
Assert-AreEqual $newKeys1.PrimaryConnectionString $newKeys2.PrimaryConnectionString
92-
if( GetTestMode -eq "Record")
92+
if( $env:AZURE_TEST_MODE -eq "Record")
9393
{
9494
Assert-AreNotEqual $newKeys1.SecondaryKey $newKeys2.SecondaryKey
9595
Assert-AreNotEqual $newKeys1.SecondaryConnectionString $newKeys2.SecondaryConnectionString
@@ -159,7 +159,7 @@ function Test-AzureRmSignalRWithDefaultArgs
159159
# The following two lines don't work in "playback" mode because all the connection strings are sanitized to the same value.
160160
# If test mode is playback , skip the test
161161

162-
if( GetTestMode -eq "Record" )
162+
if( $env:AZURE_TEST_MODE -eq "Record" )
163163
{
164164
Assert-AreNotEqual $keys.PrimaryKey $newKeys1.PrimaryKey
165165
Assert-AreNotEqual $keys.PrimaryConnectionString $newKeys1.PrimaryConnectionString
@@ -498,43 +498,43 @@ function Test-AzureRmSignalRCustomCertificateAndCustomDomain
498498

499499
# ===== Remove domain =====
500500
# Remove domain via resource group parameter set
501-
$result = Remove-AzSignalRCustomDomain -ResourceGroupName $resourceGroupName -SignalRName $signalrName -Name "domain1"
501+
$result = Remove-AzSignalRCustomDomain -ResourceGroupName $resourceGroupName -SignalRName $signalrName -Name "domain1" -PassThru
502502
Assert-True { $result }
503503

504504
# Remove domain via SignalR object parameter set
505-
$result = $signalr | Remove-AzSignalRCustomDomain -Name "domain2"
505+
$result = $signalr | Remove-AzSignalRCustomDomain -Name "domain2" -PassThru
506506
Assert-True { $result }
507507

508508
# Remove domain via input object parameter set
509509
$domain3 = $signalr | New-AzSignalRCustomDomain -Name "domain3" -DomainName $customDomainName -CustomCertificateId $cert1.Id
510-
$result = $domain3 | Remove-AzSignalRCustomDomain
510+
$result = $domain3 | Remove-AzSignalRCustomDomain -PassThru
511511
Assert-True { $result }
512512

513513
# Remove domain via Resource ID parameter set
514514
$domain4 = $signalr | New-AzSignalRCustomDomain -Name "domain4" -DomainName $customDomainName -CustomCertificateId $cert1.Id
515515
$domain4Resource = [pscustomobject]@{ResourceId = $domain4.Id }
516-
$result = $domain4Resource | Remove-AzSignalRCustomDomain
516+
$result = $domain4Resource | Remove-AzSignalRCustomDomain -PassThru
517517
Assert-True { $result }
518518

519519
# ====== Remove cert =====
520520

521521
# Remove the certificate via resource group parameter set
522-
$result = Remove-AzSignalRCustomCertificate -ResourceGroupName $resourceGroupName -SignalRName $signalrName -Name "cert1"
522+
$result = Remove-AzSignalRCustomCertificate -ResourceGroupName $resourceGroupName -SignalRName $signalrName -Name "cert1" -PassThru
523523
Assert-True { $result }
524524

525525
# Remove the certificate via SignalR object parameter set
526-
$result = $signalr | Remove-AzSignalRCustomCertificate -Name "cert2"
526+
$result = $signalr | Remove-AzSignalRCustomCertificate -Name "cert2" -PassThru
527527
Assert-True { $result }
528528

529529
# Remove the certificate via input Object parameter set
530530
$cert2 = $signalr | New-AzSignalRCustomCertificate -Name "cert2" -KeyVaultBaseUri $keyVaultBaseUri -KeyVaultSecretName $keyVaultSecretName
531-
$result = $cert2 | Remove-AzSignalRCustomCertificate
531+
$result = $cert2 | Remove-AzSignalRCustomCertificate -PassThru
532532
Assert-True { $result }
533533

534534
# Remove the certificate via Resource ID parameter set
535535
$cert2 = $signalr | New-AzSignalRCustomCertificate -Name "cert2" -KeyVaultBaseUri $keyVaultBaseUri -KeyVaultSecretName $keyVaultSecretName
536536
$certResource3 = [pscustomobject]@{ResourceId = $cert2.Id }
537-
$result = $certResource3 | Remove-AzSignalRCustomCertificate
537+
$result = $certResource3 | Remove-AzSignalRCustomCertificate -PassThru
538538
Assert-True { $result }
539539

540540
} finally
@@ -571,7 +571,7 @@ function Test-AzSignalRReplica
571571
Assert-AreEqual 2 $replica1.Sku.Capacity
572572

573573
# Replica may be in "updating" internally, wait until update finishes.
574-
if( GetTestMode -eq "Record")
574+
if( $env:AZURE_TEST_MODE -eq "Record")
575575
{
576576
Start-Sleep -Seconds 120
577577
}
@@ -592,7 +592,7 @@ function Test-AzSignalRReplica
592592
Assert-AreEqual 2 $replicas.Count
593593

594594
# Replica may be in "updating" internally, wait until update finishes.
595-
if( GetTestMode -eq "Record")
595+
if( $env:AZURE_TEST_MODE -eq "Record")
596596
{
597597
Start-Sleep -Seconds 120
598598
}
@@ -602,7 +602,7 @@ function Test-AzSignalRReplica
602602
Assert-AreEqual "Disabled" $updatedReplica.RegionEndpointEnabled
603603

604604
# Replica may be in "updating" internally, wait until update finishes.
605-
if( GetTestMode -eq "Record")
605+
if( $env:AZURE_TEST_MODE -eq "Record")
606606
{
607607
Start-Sleep -Seconds 120
608608
}
@@ -612,7 +612,7 @@ function Test-AzSignalRReplica
612612
Assert-True { $result }
613613

614614
# Replica may be in "updating" internally, wait until update finishes.
615-
if( GetTestMode -eq "Record")
615+
if( $env:AZURE_TEST_MODE -eq "Record")
616616
{
617617
Start-Sleep -Seconds 120
618618
}
@@ -623,7 +623,7 @@ function Test-AzSignalRReplica
623623
Assert-True { $result }
624624

625625
# Replica may be in "updating" internally, wait until update finishes.
626-
if( GetTestMode -eq "Record")
626+
if( $env:AZURE_TEST_MODE -eq "Record")
627627
{
628628
Start-Sleep -Seconds 120
629629
}
@@ -633,7 +633,7 @@ function Test-AzSignalRReplica
633633
Assert-True { $result }
634634

635635
# Replica may be in "updating" internally, wait until update finishes.
636-
if( GetTestMode -eq "Record")
636+
if( $env:AZURE_TEST_MODE -eq "Record")
637637
{
638638
Start-Sleep -Seconds 120
639639
}
@@ -643,7 +643,7 @@ function Test-AzSignalRReplica
643643
Assert-True { $result }
644644

645645
# Replica may be in "updating" internally, wait until update finishes.
646-
if( GetTestMode -eq "Record")
646+
if( $env:AZURE_TEST_MODE -eq "Record")
647647
{
648648
Start-Sleep -Seconds 120
649649
}
@@ -653,7 +653,7 @@ function Test-AzSignalRReplica
653653
Assert-True { $result }
654654

655655
# Replica may be in "updating" internally, wait until update finishes.
656-
if( GetTestMode -eq "Record")
656+
if( $env:AZURE_TEST_MODE -eq "Record")
657657
{
658658
Start-Sleep -Seconds 120
659659
}
@@ -756,4 +756,42 @@ function Remove-Environment
756756
[string] $resourceGroupName
757757
)
758758
Remove-AzResourceGroup -Name $resourceGroupName
759+
}
760+
761+
<#!
762+
.SYNOPSIS
763+
Test IP rule cmdlets for SignalR (New/Add/Remove-AzSignalRNetworkIpRule).
764+
#>
765+
function Test-AzSignalRNetworkIpRule
766+
{
767+
$location = Get-ProviderLocation "Microsoft.SignalRService/SignalR"
768+
$nameSuffix = "iprule-test"
769+
$resourceGroupName = Get-RandomResourceGroupName $nameSuffix
770+
$signalrName = Get-RandomSignalRName $nameSuffix
771+
772+
New-AzResourceGroup -Name $resourceGroupName -Location $location
773+
$signalr = New-AzSignalR -ResourceGroupName $resourceGroupName -Name $signalrName -Sku "Premium_P1"
774+
775+
# Create IP rule objects
776+
$rule1 = New-AzSignalRNetworkIpRuleObject -Value "10.1.0.0/16" -Action Allow
777+
$rule2 = New-AzSignalRNetworkIpRuleObject -Value "20.2.2.2" -Action Deny
778+
779+
# a. Add via resource group parameter set
780+
$acls = Add-AzSignalRNetworkIpRule -ResourceGroupName $resourceGroupName -Name $signalrName -IpRule $rule1, $rule2
781+
Assert-NotNull $acls
782+
Assert-AreEqual 4 $acls.IPRules.Count
783+
Assert-True { $acls.IPRules | Where-Object { $_.Value -eq "10.1.0.0/16" -and $_.Action -eq "Allow" } }
784+
Assert-True { $acls.IPRules | Where-Object { $_.Value -eq "20.2.2.2" -and $_.Action -eq "Deny" } }
785+
786+
# Remove one via resource group parameter set
787+
$acls = Remove-AzSignalRNetworkIpRule -ResourceGroupName $resourceGroupName -Name $signalrName -IpRule $rule1
788+
Assert-AreEqual 3 $acls.IPRules.Count
789+
Assert-False { $acls.IPRules | Where-Object { $_.Value -eq "10.1.0.0/16" } }
790+
791+
# Remove one via resource ID parameter set
792+
$signalr = Get-AzSignalR -ResourceGroupName $resourceGroupName -Name $signalrName
793+
$acls = Remove-AzSignalRNetworkIpRule -ResourceId $signalr.Id -IpRule $rule2
794+
Assert-AreEqual 2 $acls.IPRules.Count
795+
Assert-False { $acls.IPRules | Where-Object { $_.Value -eq "20.2.2.2" } }
796+
759797
}

src/SignalR/SignalR.Test/ScenarioTests/Common.ps1

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,4 @@ function Get-ProviderLocation([string]$provider)
7373
}
7474

7575
return "East US"
76-
}
77-
78-
<#
79-
.SYNOPSIS
80-
Gets the current test mode, either "Record" or "Playback"
81-
#>
82-
function GetTestMode()
83-
{
84-
if( $env:AZURE_TEST_MODE -eq "Record" )
85-
{
86-
return "Record"
87-
}
88-
else
89-
{
90-
return "Playback"
91-
}
9276
}

src/SignalR/SignalR.Test/SessionRecords/Microsoft.Azure.Commands.SignalR.Test.ScenarioTests.AzureRmSignalRTests/TestAzSignalRNetworkIpRule.json

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

src/SignalR/SignalR/Az.SignalR.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ CmdletsToExport = 'Get-AzSignalR', 'Get-AzSignalRKey', 'Get-AzSignalRUsage',
9494
'Update-AzSignalR', 'Update-AzSignalRNetworkAcl',
9595
'New-AzSignalRCustomDomain', 'Get-AzSignalRCustomDomain', 'Remove-AzSignalRCustomDomain', 'Update-AzSignalRCustomDomain',
9696
'New-AzSignalRCustomCertificate', 'Get-AzSignalRCustomCertificate', 'Remove-AzSignalRCustomCertificate',
97-
'Get-AzSignalRReplica', 'New-AzSignalRReplica', 'Remove-AzSignalRReplica', 'Restart-AzSignalRReplica', 'Start-AzSignalRReplica', 'Stop-AzSignalRReplica', 'Update-AzSignalRReplica'
97+
'Get-AzSignalRReplica', 'New-AzSignalRReplica', 'Remove-AzSignalRReplica', 'Restart-AzSignalRReplica', 'Start-AzSignalRReplica', 'Stop-AzSignalRReplica', 'Update-AzSignalRReplica',
98+
'New-AzSignalRNetworkIpRuleObject', 'Remove-AzSignalRNetworkIpRule', 'Add-AzSignalRNetworkIpRule'
9899

99100
# Variables to export from this module
100101
# VariablesToExport = @()

src/SignalR/SignalR/ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
- Start-AzSignalRReplica: start a SignalR replica
2727
- Stop-AzSignalRReplica: stop a SignalR replica
2828
- Update-AzSignalRReplica: update a SignalR replica
29+
* Added cmdlets for managing network IP rules
30+
- New-AzSignalRNetworkIpRuleObject: create a new network IP rule object for SignalR
31+
- Add-AzSignalRNetworkIpRule: add network IP rule(s) to SignalR
32+
- Remove-AzSignalRNetworkIpRule: remove network IP rule(s) from SignalR
2933

3034
## Version 2.2.0
3135
* Added cmdlets for managing custom domains: `New-AzSignalRCustomDomain`, `Get-AzSignalRCustomDomain`, `Remove-AzSignalRCustomDomain`, `Update-AzSignalRCustomDomain`
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.ResourceManager.Common.ArgumentCompleters;
19+
using Microsoft.Azure.Commands.SignalR.Models;
20+
using Microsoft.Azure.Commands.SignalR.Properties;
21+
using Microsoft.Azure.Management.SignalR;
22+
23+
namespace Microsoft.Azure.Commands.SignalR.Cmdlets
24+
{
25+
[Cmdlet("Add", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SignalRNetworkIpRule", SupportsShouldProcess = true, DefaultParameterSetName = ResourceGroupParameterSet)]
26+
[OutputType(typeof(PSSignalRNetworkAcls))]
27+
public class AddAzureRmSignalRNetworkIpRule : SignalRCmdletBase, IWithResourceId, IWithSignalRInputObject
28+
{
29+
[Parameter(Mandatory = false, ParameterSetName = ResourceGroupParameterSet, HelpMessage = "The resource group name. The default one will be used if not specified.")]
30+
[ResourceGroupCompleter]
31+
[ValidateNotNullOrEmpty]
32+
public override string ResourceGroupName { get; set; }
33+
34+
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ResourceGroupParameterSet, HelpMessage = "The SignalR service name.")]
35+
[ValidateNotNullOrEmpty]
36+
[ResourceNameCompleter(Constants.SignalRResourceType, nameof(ResourceGroupName))]
37+
public string Name { get; set; }
38+
39+
[Parameter(Mandatory = true, ParameterSetName = ResourceIdParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The SignalR service resource ID.")]
40+
[ValidateNotNullOrEmpty]
41+
public string ResourceId { get; set; }
42+
43+
[Parameter(Mandatory = true, ParameterSetName = InputObjectParameterSet, ValueFromPipeline = true, HelpMessage = "The SignalR resource object.")]
44+
[ValidateNotNull]
45+
public PSSignalRResource InputObject { get; set; }
46+
47+
[Parameter(Mandatory = true, ParameterSetName = ResourceIdParameterSet, ValueFromPipeline = true, HelpMessage = "IP rule object(s) created by New-AzSignalRNetworkIpRule.")]
48+
[Parameter(Mandatory = true, ParameterSetName = ResourceGroupParameterSet, ValueFromPipeline = true, HelpMessage = "IP rule object(s) created by New-AzSignalRNetworkIpRule.")]
49+
[Parameter(Mandatory = true, ParameterSetName = InputObjectParameterSet, ValueFromPipeline = true, HelpMessage = "IP rule object(s) created by New-AzSignalRNetworkIpRule.")]
50+
[ValidateNotNull]
51+
public PSIpRule[] IpRule { get; set; }
52+
53+
public override void ExecuteCmdlet()
54+
{
55+
base.ExecuteCmdlet();
56+
57+
RunCmdlet(() =>
58+
{
59+
switch (ParameterSetName)
60+
{
61+
case ResourceGroupParameterSet:
62+
ResolveResourceGroupName();
63+
break;
64+
case ResourceIdParameterSet:
65+
this.LoadFromResourceId();
66+
break;
67+
case InputObjectParameterSet:
68+
this.LoadFromSignalRInputObject();
69+
break;
70+
default:
71+
throw new ArgumentException(Resources.ParameterSetError);
72+
}
73+
if (ShouldProcess($"SignalR service {ResourceGroupName}/{Name}", "add network IP rule(s)"))
74+
{
75+
var signalr = Client.SignalR.Get(ResourceGroupName, Name);
76+
var networkACLs = signalr.NetworkAcLs;
77+
if (networkACLs.IPRules == null)
78+
{
79+
networkACLs.IPRules = new List<Microsoft.Azure.Management.SignalR.Models.IPRule>();
80+
}
81+
foreach (var rule in IpRule)
82+
{
83+
networkACLs.IPRules.Add(rule.ToSdkModel());
84+
}
85+
signalr = Client.SignalR.Update(ResourceGroupName, Name, signalr);
86+
WriteObject(new PSSignalRNetworkAcls(signalr.NetworkAcLs));
87+
}
88+
});
89+
}
90+
}
91+
}

src/SignalR/SignalR/Cmdlets/NewAzureRmSignalR.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public sealed class NewAzureRmSignalR : SignalRCmdletBase
6161

6262
[Parameter(
6363
Mandatory = false,
64-
HelpMessage = "The SignalR service unit count, value only from {1, 2, 5, 10, 20, 50, 100}. Default to 1.")]
65-
[PSArgumentCompleter("1", "2", "5", "10", "20", "50", "100")]
64+
HelpMessage = "The SignalR service unit count. Free_F1: 1; Standard_S1: 1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100; Premium_P1: 1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100; Premium_P2: 100,200,300,400,500,600,700,800,900,1000. Default to 1.")]
65+
[PSArgumentCompleter("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100", "200", "300", "400", "500", "600", "700", "800", "900", "1000")]
6666
public int UnitCount { get; set; } = DefaultUnitCount;
6767

6868
[Parameter(
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Management.Automation;
2+
using Microsoft.Azure.Commands.SignalR.Models;
3+
4+
namespace Microsoft.Azure.Commands.SignalR.Cmdlets
5+
{
6+
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SignalRNetworkIpRuleObject", SupportsShouldProcess = false)]
7+
[OutputType(typeof(PSIpRule))]
8+
public class NewAzureRmSignalRNetworkIpRuleObject : PSCmdlet
9+
{
10+
[Parameter(Mandatory = true, HelpMessage = "IP rule value. Accepts IP, CIDR or ServiceTag.")]
11+
public string Value { get; set; }
12+
13+
[Parameter(Mandatory = false, HelpMessage = "Action for the IP rule. Allow or Deny. Default: Allow")]
14+
[ValidateSet("Allow", "Deny", IgnoreCase = true)]
15+
public string Action { get; set; } = "Allow";
16+
17+
protected override void ProcessRecord()
18+
{
19+
WriteObject(new PSIpRule { Value = Value, Action = Action });
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)