Skip to content

Commit d747cc0

Browse files
authored
[Network] Add Cmdlets for BYOIP Feature (#12704)
* add BYOIP cmdlets to Network module
1 parent 891996d commit d747cc0

21 files changed

+1658
-2
lines changed

src/Network/Network.Test/NrpTeamAlias.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,8 @@ class NrpTeamAlias
6464

6565
// Azure Network IPAM dev team
6666
public const string ipam = "ipamdev";
67+
68+
// Azure Network Billing and Telemetry team
69+
public const string billingandtelemetry = "azurenetworkbilling";
6770
}
6871
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 Microsoft.Azure.Commands.Network.Test.ScenarioTests;
16+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
17+
using Xunit;
18+
19+
namespace Commands.Network.Test.ScenarioTests
20+
{
21+
22+
public class CustomIpPrefixTests : NetworkTestRunner
23+
{
24+
public CustomIpPrefixTests(Xunit.Abstractions.ITestOutputHelper output)
25+
: base(output)
26+
{
27+
}
28+
29+
[Fact(Skip = "Resource currently in private preview, testing requires very specific conditions, will implement these tests in future.")]
30+
[Trait(Category.AcceptanceType, Category.CheckIn)]
31+
[Trait(Category.Owner, NrpTeamAlias.billingandtelemetry)]
32+
public void TestCustomIpPrefixCRUD()
33+
{
34+
TestRunner.RunTestScript("Test-CustomIpPrefixCRUD");
35+
}
36+
}
37+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
<#
16+
.SYNOPSIS
17+
Tests CRUD operations on a simple customIpPrefix.
18+
19+
NOTE: This feature is currently in private preview, so updated binaries are on test slice in canary only and is currently un-reacheable via the production manifest.
20+
Testing has been done locally using the brazilus ARM endpoint and a specific subscription that has the necessary flags to run these cmdlets.
21+
#>
22+
function Test-CustomIpPrefixCRUD
23+
{
24+
# Setup
25+
$rgname = "powershell-test-rg"
26+
$rname = "testCustomIpPrefix"
27+
$location = "eastus2euap"
28+
$cidr = "40.40.40.0/24"
29+
30+
try
31+
{
32+
# Create the resource group
33+
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $rglocation
34+
35+
# Create customIpPrefix
36+
$job = New-AzCustomIpPrefix -Name $rname -ResourceGroupName $rgname -location $location -Cidr $cidr -AsJob
37+
$job | Wait-Job
38+
$job | Receive-Job
39+
40+
# get by name and resource group
41+
$actual = Get-AzCustomIpPrefix -ResourceGroupName $rgname -name $rname
42+
Assert-AreEqual $actual.ResourceGroupName $rgname
43+
Assert-AreEqual $actual.Name $rname
44+
Assert-AreEqual $actual.Location $location
45+
Assert-AreEqual $actual.Cidr $cidr
46+
Assert-AreEqual $actual.PublicIpPrefixes.Count 0
47+
Assert-NotNull $actual.ResourceGuid
48+
Assert-AreEqual $actual.ProvisioningState "Succeeded"
49+
50+
# get by resource id
51+
$actual = Get-AzCustomIpPrefix -ResourceId $actual.Id
52+
Assert-AreEqual $actual.ResourceGroupName $rgname
53+
Assert-AreEqual $actual.Name $rname
54+
Assert-AreEqual $actual.Location $location
55+
Assert-AreEqual $actual.Cidr $cidr
56+
Assert-AreEqual $actual.PublicIpPrefixes.Count 0
57+
Assert-NotNull $actual.ResourceGuid
58+
Assert-AreEqual $actual.ProvisioningState "Succeeded"
59+
60+
# get by input object
61+
$actual = Get-AzCustomIpPrefix -InputObject $actual
62+
Assert-AreEqual $actual.ResourceGroupName $rgname
63+
Assert-AreEqual $actual.Name $rname
64+
Assert-AreEqual $actual.Location $location
65+
Assert-AreEqual $actual.Cidr $cidr
66+
Assert-AreEqual $actual.PublicIpPrefixes.Count 0
67+
Assert-NotNull $actual.ResourceGuid
68+
Assert-AreEqual $actual.ProvisioningState "Succeeded"
69+
70+
# list
71+
$list = Get-AzCustomIpPrefix -ResourceGroupName $rgname
72+
Assert-AreEqual 1 @($list).Count
73+
Assert-AreEqual $list[0].ResourceGroupName $actual.ResourceGroupName
74+
Assert-AreEqual $list[0].Name $actual.Name
75+
Assert-AreEqual $list[0].Location $actual.Location
76+
Assert-AreEqual $list[0].Cidr $actual.Cidr
77+
Assert-AreEqual $list[0].PublicIpPrefixes.Count 0
78+
Assert-AreEqual $list[0].ProvisioningState "Succeeded"
79+
80+
# delete
81+
$job = Remove-AzCustomIpPrefix -InputObject $actual -PassThru -Force -AsJob
82+
$job | Wait-Job
83+
$delete = $job | Receive-Job
84+
Assert-AreEqual true $delete
85+
86+
$list = Get-AzPublicIpPrefix -ResourceGroupName $rgname
87+
Assert-AreEqual 0 @($list).Count
88+
89+
# Try setting unexisting
90+
Assert-ThrowsLike { Update-AzPublicIpPrefix -PublicIpPrefix $expected } "*not found*"
91+
92+
# Create one more time to test deletion with resource id parameter set
93+
$job = New-AzCustomIpPrefix -Name $rname -ResourceGroupName $rgname -location $location -Cidr $cidr -AsJob
94+
$job | Wait-Job
95+
$expected = $job | Receive-Job
96+
97+
$job = Remove-AzPublicIpPrefix -ResourceId $expected.Id -PassThru -Force -AsJob
98+
$job | Wait-Job
99+
$delete = $job | Receive-Job
100+
Assert-AreEqual true $delete
101+
102+
# Create one more time to test deletion with resource group and name
103+
$job = New-AzCustomIpPrefix -Name $rname -ResourceGroupName $rgname -location $location -Cidr $cidr -AsJob
104+
$job | Wait-Job
105+
$expected = $job | Receive-Job
106+
107+
$job = Remove-AzPublicIpPrefix -Name $rname -ResourceGroupName $rgname
108+
$job | Wait-Job
109+
$delete = $job | Receive-Job
110+
Assert-AreEqual true $delete
111+
}
112+
finally
113+
{
114+
# Cleanup
115+
Clean-ResourceGroup $rgname
116+
}
117+
}

src/Network/Network/Az.Network.psd1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,10 @@ CmdletsToExport = 'Add-AzApplicationGatewayAuthenticationCertificate',
496496
'New-AzVirtualApplianceSite', 'Remove-AzVirtualApplianceSite',
497497
'Update-AzVirtualApplianceSite', 'New-AzOffice365PolicyProperty',
498498
'Get-AzNetworkVirtualApplianceSku',
499-
'New-AzVirtualApplianceSkuProperty'
499+
'New-AzVirtualApplianceSkuProperty',
500+
'New-AzCustomIpPrefix', 'Update-AzCustomIpPrefix',
501+
'Get-AzCustomIpPrefix', 'Remove-AzCustomIpPrefix'
502+
500503

501504
# Variables to export from this module
502505
# VariablesToExport = @()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
namespace Microsoft.Azure.Commands.Network
16+
{
17+
using Microsoft.Azure.Commands.Network.Models;
18+
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
19+
using Microsoft.Azure.Management.Network;
20+
using Microsoft.Azure.Management.Network.Models;
21+
using System.Net;
22+
23+
public abstract class CustomIpPrefixBaseCmdlet : NetworkBaseCmdlet
24+
{
25+
public ICustomIPPrefixesOperations CustomIpPrefixClient
26+
{
27+
get
28+
{
29+
return NetworkClient.NetworkManagementClient.CustomIPPrefixes;
30+
}
31+
}
32+
33+
public PSCustomIpPrefix GetCustomIpPrefix(string resourceGroupName, string name, string expandResource = null)
34+
{
35+
var sdkModel = this.CustomIpPrefixClient.Get(resourceGroupName, name, expandResource);
36+
37+
var psModel = ToPsCustomIpPrefix(sdkModel);
38+
psModel.ResourceGroupName = resourceGroupName;
39+
40+
return psModel;
41+
}
42+
43+
public PSCustomIpPrefix ToPsCustomIpPrefix(CustomIpPrefix customIpPrefix)
44+
{
45+
var psModel = NetworkResourceManagerProfile.Mapper.Map<PSCustomIpPrefix>(customIpPrefix);
46+
47+
psModel.Tag = TagsConversionHelper.CreateTagHashtable(customIpPrefix.Tags);
48+
49+
return psModel;
50+
}
51+
}
52+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
namespace Microsoft.Azure.Commands.Network
16+
{
17+
using Microsoft.Azure.Commands.Network.Models;
18+
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
19+
using Microsoft.Azure.Management.Network;
20+
using System.Collections.Generic;
21+
using System.Management.Automation;
22+
using Microsoft.Azure.Management.Network.Models;
23+
using Microsoft.Rest.Azure;
24+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
25+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
26+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
27+
28+
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "CustomIpPrefix", DefaultParameterSetName = GetByNameParameterSet), OutputType(typeof(PSCustomIpPrefix))]
29+
public class GetAzureCustomIpPrefixCommand : CustomIpPrefixBaseCmdlet
30+
{
31+
private const string GetByNameParameterSet = "GetByNameParameterSet";
32+
private const string GetByResourceIdParameterSet = "GetByResourceIdParameterSet";
33+
34+
[Alias("ResourceName")]
35+
[Parameter(
36+
Mandatory = false,
37+
ValueFromPipelineByPropertyName = true,
38+
HelpMessage = "The resource name.",
39+
ParameterSetName = GetByNameParameterSet)]
40+
[ResourceNameCompleter("Microsoft.Network/customIpPrefix", "ResourceGroupName")]
41+
[ValidateNotNullOrEmpty]
42+
[SupportsWildcards]
43+
public virtual string Name { get; set; }
44+
45+
[Parameter(
46+
Mandatory = false,
47+
ValueFromPipelineByPropertyName = true,
48+
HelpMessage = "The resource group name.",
49+
ParameterSetName = GetByNameParameterSet)]
50+
[ResourceGroupCompleter]
51+
[ValidateNotNullOrEmpty]
52+
[SupportsWildcards]
53+
public virtual string ResourceGroupName { get; set; }
54+
55+
[Parameter(
56+
Mandatory = true,
57+
ValueFromPipelineByPropertyName = true,
58+
HelpMessage = "The resource id.",
59+
ParameterSetName = GetByResourceIdParameterSet)]
60+
[ValidateNotNullOrEmpty]
61+
public virtual string ResourceId { get; set; }
62+
63+
public override void Execute()
64+
{
65+
base.Execute();
66+
67+
if (this.IsParameterBound(c => c.ResourceId))
68+
{
69+
var resourceIdentifier = new ResourceIdentifier(this.ResourceId);
70+
this.ResourceGroupName = resourceIdentifier.ResourceGroupName;
71+
this.Name = resourceIdentifier.ResourceName;
72+
}
73+
74+
if (ShouldGetByName(this.ResourceGroupName, this.Name))
75+
{
76+
PSCustomIpPrefix psModel;
77+
78+
psModel = this.GetCustomIpPrefix(this.ResourceGroupName, this.Name);
79+
80+
WriteObject(psModel);
81+
}
82+
else
83+
{
84+
IPage<CustomIpPrefix> page;
85+
if (ShouldListByResourceGroup(this.ResourceGroupName, this.Name))
86+
{
87+
page = this.CustomIpPrefixClient.List(this.ResourceGroupName);
88+
}
89+
else
90+
{
91+
page = this.CustomIpPrefixClient.ListAll();
92+
}
93+
94+
// Get all resources by polling on next page link
95+
List<CustomIpPrefix> sdkModelList;
96+
97+
sdkModelList = ListNextLink<CustomIpPrefix>.GetAllResourcesByPollingNextLink(page, this.CustomIpPrefixClient.ListNext);
98+
99+
var psModelList = new List<PSCustomIpPrefix>();
100+
101+
// populate the publicIpPrefixes with the ResourceGroupName
102+
foreach (var sdkModel in sdkModelList)
103+
{
104+
var psModel = this.ToPsCustomIpPrefix(sdkModel);
105+
psModel.ResourceGroupName = NetworkBaseCmdlet.GetResourceGroup(psModel.Id);
106+
psModelList.Add(psModel);
107+
}
108+
109+
WriteObject(TopLevelWildcardFilter(this.ResourceGroupName, this.Name, psModelList), true);
110+
}
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)