Skip to content

Powershell changes for supporting ExpressRoutePort authorization #17392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,12 @@ public void TestExpressRoutePortGenerateLOA()
TestRunner.RunTestScript("Test-ExpressRoutePortGenerateLOA");
}

[Fact(Skip = "No bandwidth available")]
[Trait(Category.AcceptanceType, Category.CheckIn)]
[Trait(Category.Owner, NrpTeamAlias.pgtm)]
public void TestExpressRoutePortAuthorizationCRUD()
{
TestRunner.RunTestScript("Test-ExpressRoutePortAuthorizationCRUD");
}
}
}
66 changes: 66 additions & 0 deletions src/Network/Network.Test/ScenarioTests/ExpressRoutePortTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,69 @@ function Test-ExpressRoutePortGenerateLOA
}
}

<#
.SYNOPSIS
Tests ExpressRoutePortAuthorizationCRUD.
#>
function Test-ExpressRoutePortAuthorizationCRUD
{
# Setup
$rgname = Get-ResourceGroupName
$rglocation = Get-ProviderLocation ResourceManagement
$rname = Get-ResourceName
$resourceTypeParent = "Microsoft.Network/expressRoutePorts"
$location = Get-ProviderLocation $resourceTypeParent
$peeringLocation = "Area51-ERDirect"
$encapsulation = "QinQ"
$bandwidthInGbps = 100.0
$authorizationName = "testkey"

try
{
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $rglocation

# Create the ExpressRoutePort
$expressRoutePort = New-AzExpressRoutePort -ResourceGroupName $rgname -Name $rname -Location $location -PeeringLocation $peeringLocation -Encapsulation $encapsulation -BandwidthInGbps $bandwidthInGbps

#verification
Assert-NotNull $expressRoutePort
Assert-AreEqual $rname $expressRoutePort.Name

# add a new authorization
Get-AzExpressRoutePort -ResourceGroupName $rgname -Name $rname | Add-AzExpressRoutePortAuthorization -Name "testkey1"

# get the authorization
$a = $expressRoutePort | Get-AzExpressRoutePortAuthorization -Name "testkey1"
Assert-AreEqual "testkey1" $a.Name

# add another new authorization
Get-AzExpressRoutePort -ResourceGroupName $rgname -Name $rname | Add-AzExpressRoutePortAuthorization -Name "testkey2"

$a = $expressRoutePort | Get-AzExpressRoutePortAuthorization -Name "testkey2"
Assert-AreEqual "testkey2" $a.Name

# list authorizations
$listAuthorization = $expressRoutePort | Get-AzExpressRoutePortAuthorization
Assert-AreEqual 2 @($listAuthorization).Count

# delete an authorization
Get-AzExpressRoutePort -ResourceGroupName $rgname -Name $rname | Remove-AzExpressRoutePortAuthorization -Name "testkey1" -Force

# list authorizations again
$listAuthorization = $expressRoutePort | Get-AzExpressRoutePortAuthorization
Assert-AreEqual 1 @($listAuthorization).Count

# Delete ExpressRoutePort
$removeExpressRoutePort = Remove-AzExpressRoutePort -ResourceGroupName $rgname -Name $rname -PassThru -Force
Assert-AreEqual $true $removeExpressRoutePort

$list = Get-AzExpressRoutePort -ResourceGroupName $rgname
Assert-AreEqual 0 @($list).Count
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/Network/Network/Az.Network.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ CmdletsToExport = 'Add-AzApplicationGatewayAuthenticationCertificate',
'Get-AzExpressRoutePortIdentity', 'New-AzExpressRoutePortIdentity',
'Remove-AzExpressRoutePortIdentity',
'Set-AzExpressRoutePortIdentity',
'Add-AzExpressRoutePortAuthorization',
'Get-AzExpressRoutePortAuthorization',
'Remove-AzExpressRoutePortAuthorization',
'Get-AzEffectiveNetworkSecurityGroup', 'Get-AzEffectiveRouteTable',
'Add-AzNetworkInterfaceIpConfig', 'Get-AzNetworkInterfaceIpConfig',
'New-AzNetworkInterfaceIpConfig',
Expand Down
5 changes: 5 additions & 0 deletions src/Network/Network/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
--->

## Upcoming Release
* Added new cmdlets to create/manage authorization objects for ExpressRoutePort:
- `Add-AzExpressRoutePortAuthorization`
- `Get-AzExpressRoutePortAuthorization`
- `Remove-AzExpressRoutePortAuthorization`
* Added option parameter `AuthorizationKey` to cmdlet `New-AzExpressRouteCircuit` to allow creating ExpressRoute Circuit on a ExpressRoutePort with a different owner.
* Fixed `ArgumentNullException` in `Add-AzureRmRouteConfig` when `RouteTable.Routes` is null.

## Version 4.16.0
Expand Down
2 changes: 2 additions & 0 deletions src/Network/Network/Common/NetworkResourceManagerProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -792,10 +792,12 @@ private static void Initialize()
// CNM to MNM
cfg.CreateMap<CNM.PSExpressRoutePort, MNM.ExpressRoutePort>();
cfg.CreateMap<CNM.PSExpressRouteLink, MNM.ExpressRouteLink>();
cfg.CreateMap<CNM.PSExpressRoutePortAuthorization, MNM.ExpressRoutePortAuthorization>();

// MNM to CNM
cfg.CreateMap<MNM.ExpressRoutePort, CNM.PSExpressRoutePort>();
cfg.CreateMap<MNM.ExpressRouteLink, CNM.PSExpressRouteLink>();
cfg.CreateMap<MNM.ExpressRoutePortAuthorization, CNM.PSExpressRoutePortAuthorization>();

// ExpressRouteCircuit
// CNM to MNM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public class NewAzureExpressRouteCircuitCommand : ExpressRouteCircuitBaseCmdlet
ValueFromPipelineByPropertyName = true)]
public double BandwidthInGbps { get; set; }

[Parameter(
ParameterSetName = "ExpressRoutePort",
Mandatory = false,
ValueFromPipelineByPropertyName = true)]
public string AuthorizationKey { get; set; }

[Parameter(
Mandatory = false,
ValueFromPipelineByPropertyName = true)]
Expand Down Expand Up @@ -161,7 +167,7 @@ private PSExpressRouteCircuit CreateExpressRouteCircuit()
circuit.Name = this.Name;
circuit.ResourceGroupName = this.ResourceGroupName;
circuit.Location = this.Location;

circuit.AuthorizationKey = this.AuthorizationKey;
// Construct sku
if (!string.IsNullOrEmpty(this.SkuTier))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Network.Models;
using System;
using System.Linq;
using System.Management.Automation;
using System.Net;
using MNM = Microsoft.Azure.Management.Network.Models;

namespace Microsoft.Azure.Commands.Network
{
[Cmdlet("Add", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "ExpressRoutePortAuthorization", SupportsShouldProcess = true), OutputType(typeof(PSExpressRoutePortAuthorization))]
public class AddAzureExpressRoutePortAuthorizationCommand : NetworkBaseCmdlet
{
[Parameter(
Mandatory = true,
HelpMessage = "The name of the Authorization")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

[Parameter(
Mandatory = true,
ValueFromPipeline = true,
HelpMessage = "The ExpressRoutePort Object")]
public PSExpressRoutePort ExpressRoutePortObject { get; set; }

public override void Execute()
{
base.Execute();
var present = true;

try
{
this.NetworkClient.NetworkManagementClient.ExpressRoutePortAuthorizations.GetWithHttpMessagesAsync(this.ExpressRoutePortObject.ResourceGroupName, this.ExpressRoutePortObject.Name, this.Name).GetAwaiter().GetResult();
}
catch (Microsoft.Rest.Azure.CloudException exception)
{
if (exception.Response.StatusCode == HttpStatusCode.NotFound)
{
// Resource is not present
present = false;
}
else
{
throw;
}
}

if (present)
{
throw new ArgumentException("Authorization with the specified name already exists");
}

var authorizationParameters = new MNM.ExpressRoutePortAuthorization();
authorizationParameters.Name = this.Name;

// Execute the PUT ExpressRoutePortAuthorizations call
var putExpressRoutePortAuthorization = this.NetworkClient.NetworkManagementClient.ExpressRoutePortAuthorizations.CreateOrUpdateWithHttpMessagesAsync(this.ExpressRoutePortObject.ResourceGroupName, this.ExpressRoutePortObject.Name, this.Name, authorizationParameters).GetAwaiter().GetResult().Body;
var psExpressRoutePortAuthorization = NetworkResourceManagerProfile.Mapper.Map<PSExpressRoutePortAuthorization>(putExpressRoutePortAuthorization);
WriteObject(psExpressRoutePortAuthorization, true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Network.Models;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.Network
{
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "ExpressRoutePortAuthorization"), OutputType(typeof(PSExpressRoutePortAuthorization))]
public class GetAzureExpressRoutePortAuthorizationCommand : NetworkBaseCmdlet
{
[Parameter(
Mandatory = false,
HelpMessage = "The name of the Authorization")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

[Parameter(
Mandatory = true,
ValueFromPipeline = true,
HelpMessage = "The ExpressRoutePort Object")]
public PSExpressRoutePort ExpressRoutePortObject { get; set; }

public override void Execute()
{
base.Execute();
if (!string.IsNullOrEmpty(this.Name))
{
var getExpressRoutePortAuthorization = this.NetworkClient.NetworkManagementClient.ExpressRoutePortAuthorizations.GetWithHttpMessagesAsync(this.ExpressRoutePortObject.ResourceGroupName, this.ExpressRoutePortObject.Name, this.Name).GetAwaiter().GetResult().Body;
var psExpressRoutePortAuthorization = NetworkResourceManagerProfile.Mapper.Map<PSExpressRoutePortAuthorization>(getExpressRoutePortAuthorization);
WriteObject(psExpressRoutePortAuthorization);
}
else
{

var listExpressRoutePortAuthorizations = this.NetworkClient.NetworkManagementClient.ExpressRoutePortAuthorizations.ListWithHttpMessagesAsync(this.ExpressRoutePortObject.ResourceGroupName, this.ExpressRoutePortObject.Name).GetAwaiter().GetResult().Body;
var psExpressRoutePortAuthorizations = new List<PSExpressRoutePortAuthorization>();
if (listExpressRoutePortAuthorizations != null)
{
psExpressRoutePortAuthorizations = NetworkResourceManagerProfile.Mapper.Map<List<PSExpressRoutePortAuthorization>>(listExpressRoutePortAuthorizations);
}

WriteObject(psExpressRoutePortAuthorizations, true);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Network.Models;
using System.Linq;
using System.Management.Automation;


namespace Microsoft.Azure.Commands.Network
{
[Cmdlet("Remove", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "ExpressRoutePortAuthorization", SupportsShouldProcess = true), OutputType(typeof(bool))]
public class RemoveAzureExpressRoutePortAuthorizationCommand : NetworkBaseCmdlet
{
[Parameter(
Mandatory = true,
HelpMessage = "The name of the Authorization")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

[Parameter(
Mandatory = true,
ValueFromPipeline = true,
HelpMessage = "The ExpressRoutePort Object")]
public PSExpressRoutePort ExpressRoutePortObject { get; set; }

[Parameter(
Mandatory = false,
HelpMessage = "Do not ask for confirmation if you want to delete resource")]
public SwitchParameter Force { get; set; }

[Parameter(Mandatory = false)]
public SwitchParameter PassThru { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

public override void Execute()
{
base.Execute();

ConfirmAction(
Force.IsPresent,
string.Format(Properties.Resources.RemovingResource, Name),
Properties.Resources.RemoveResourceMessage,
Name,
() =>
{
this.NetworkClient.NetworkManagementClient.ExpressRoutePortAuthorizations.DeleteWithHttpMessagesAsync(ExpressRoutePortObject.ResourceGroupName, ExpressRoutePortObject.Name, Name).GetAwaiter().GetResult();
if (PassThru.IsPresent)
{
WriteObject(true);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public override void Execute()
BandwidthInGbps = this.BandwidthInGbps,
Encapsulation = this.Encapsulation,
Location = this.Location,
Links = this.Link?.ToList(),
Links = this.Link?.ToList()
};

if (this.Identity != null)
Expand Down
2 changes: 2 additions & 0 deletions src/Network/Network/Generated/Models/PSExpressRouteCircuit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public partial class PSExpressRouteCircuit : PSTopLevelResource
public double? BandwidthInGbps { get; set; }
[Ps1Xml(Target = ViewControl.Table)]
public int? Stag { get; set; }
[Ps1Xml(Target = ViewControl.Table)]
public string AuthorizationKey { get; set; }

[JsonIgnore]
public string SkuText
Expand Down
Loading