Skip to content

Commit

Permalink
cherrypick Azure#4
Browse files Browse the repository at this point in the history
  • Loading branch information
dexiang-ms committed Aug 19, 2020
1 parent cac5f46 commit df074a2
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 176 deletions.
97 changes: 97 additions & 0 deletions src/Network/Network.Test/ScenarioTests/CustomIpPrefixTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# ----------------------------------------------------------------------------------
#
# 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.
# ----------------------------------------------------------------------------------

<#
.SYNOPSIS
Tests CRUD operations on a simple customIpPrefix.
#>
function Test-CustomIpPrefixCRUD
{
# Setup
$rgname = "powershell-test-rg"
$rname = "testCip"

# currently in private preview, updated binaries are on test slice in canary only
$location = "eastus2euap"

try
{
# Create the resource group
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $rglocation -Tags @{ testtag = "testval" }

# Create the masterCustomIpPrefix
$job = New-AzMasterCustomIpPrefix -Name $rname -ResourceGroupName $rgname -Cidr "40.40.40.0/22" -ValidationMessage "123" -SignedValidationMessage "456" -Location $location -AsJob
$job | Wait-Job
$mcip = $job | Receive-Job

# Create customIpPrefix
$job = New-AzCustomIpPrefix -Name $rname -ResourceGroupName $rgname -location $location -Cidr "40.40.40.0/24" -MasterCustomIpPrefix $mcip -AsJob
$job | Wait-Job
$actual = $job | Receive-Job
$expected = Get-AzCustomIpPrefix -ResourceGroupName $rgname -name $rname
Assert-AreEqual $expected.ResourceGroupName $actual.ResourceGroupName
Assert-AreEqual $expected.Name $actual.Name
Assert-AreEqual $expected.Location $actual.Location
Assert-AreEqual $expected.Cidr $actual.Cidr
Assert-AreEqual 0 $expected.PublicIpPrefixes.Count
Assert-NotNull $expected.ResourceGuid
Assert-AreEqual "Succeeded" $expected.ProvisioningState

# list
$list = Get-AzCustomIpPrefix -ResourceGroupName $rgname
Assert-AreEqual 1 @($list).Count
Assert-AreEqual $list[0].ResourceGroupName $actual.ResourceGroupName
Assert-AreEqual $list[0].Name $actual.Name
Assert-AreEqual $list[0].Location $actual.Location
Assert-AreEqual $list[0].Cidr $actual.Cidr
Assert-AreEqual $list[0].PublicIpPrefixes.Count 0
Assert-AreEqual "Succeeded" $list[0].ProvisioningState

$list = Get-AzCustomIpPrefix -ResourceId $actual.Id
Assert-AreEqual 1 @($list).Count
Assert-AreEqual $list[0].ResourceGroupName $actual.ResourceGroupName
Assert-AreEqual $list[0].Name $actual.Name
Assert-AreEqual $list[0].Location $actual.Location
Assert-AreEqual $list[0].Cidr $actual.Cidr
Assert-AreEqual $list[0].PublicIpPrefixes.Count 0
Assert-AreEqual "Succeeded" $list[0].ProvisioningState

# delete
$job = Remove-AzCustomIpPrefix -InputObject $actual -PassThru -Force -AsJob
$job | Wait-Job
$delete = $job | Receive-Job
Assert-AreEqual true $delete

$list = Get-AzPublicIpPrefix -ResourceGroupName $actual.ResourceGroupName
Assert-AreEqual 0 @($list).Count

# Try setting unexisting
Assert-ThrowsLike { Set-AzPublicIpPrefix -PublicIpPrefix $expected } "*not found*"

# Create one more time to test deletion with another parameter set
$job = New-AzPublicIpPrefix -ResourceGroupName $rgname -name $rname -location $location -Sku Standard -PrefixLength 30 -AsJob
$job | Wait-Job
$actual = $job | Receive-Job

$job = Remove-AzPublicIpPrefix -ResourceId $actual.Id -PassThru -Force -AsJob
$job | Wait-Job
$delete = $job | Receive-Job
Assert-AreEqual true $delete
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}
179 changes: 28 additions & 151 deletions src/Network/Network.Test/ScenarioTests/MasterCustomIpPrefixTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,180 +14,57 @@

<#
.SYNOPSIS
Tests creating new simple masterCustomIpPrefix.
Tests CRUD operations on a simple masterCustomIpPrefix.
#>
function Test-MasterCustomIpPrefixCRUD
{
# Setup
$rgname = Get-ResourceGroupName
$rname = Get-ResourceName
$rglocation = Get-ProviderLocation ResourceManagement
$resourceTypeParent = "Microsoft.Network/publicIpPrefixes"
$location = Get-ProviderLocation $resourceTypeParent "West Europe"
$ipTagType = "NetworkDomain"
$ipTagTag = "test"
$rgname = "powershell-test-rg"
$rname = "testMcip"

# currently in private preview, updated binaries are on test slice in canary only
$location = "eastus2euap"

try
{
# Create the resource group
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $rglocation -Tags @{ testtag = "testval" }

$ipTag = New-Object -TypeName Microsoft.Azure.Commands.Network.Models.PSPublicIpPrefixTag
$ipTag.IpTagType = $ipTagType
$ipTag.Tag = $ipTagTag

# Create publicIpPrefix
$job = New-AzPublicIpPrefix -ResourceGroupName $rgname -name $rname -location $location -Sku Standard -PrefixLength 30 -IpAddressVersion IPv4 -IpTag $ipTag -AsJob
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $location

# Create masterCustomIpPrefix
$job = New-AzMasterCustomIpPrefix -Name $rname -ResourceGroupName $rgname -Cidr "40.40.40.0/22" -ValidationMessage "123" -SignedValidationMessage "456" -Location $location -AsJob
$job | Wait-Job
$actual = $job | Receive-Job
$expected = Get-AzPublicIpPrefix -ResourceGroupName $rgname -name $rname
Assert-AreEqual $expected.ResourceGroupName $actual.ResourceGroupName
Assert-AreEqual $expected.Name $actual.Name
$expected = Get-AzMasterCustomIpPrefix -ResourceGroupName $rgname -Name $rname
Assert-AreEqual $expected.ResourceGroupName $actual.ResourceGroupName
Assert-AreEqual $expected.Name $actual.Name
Assert-AreEqual $expected.Location $actual.Location
Assert-AreEqual 30 $expected.PrefixLength
Assert-AreEqual $expected.Cidr $actual.Cidr
Assert-AreEqual $expected.ValidationMessage $actual.ValidationMessage
Assert-AreEqual $expected.SignedValidationMessage $actual.SignedValidationMessage
Assert-AreEqual $expected.ValidationState "Validating"
Assert-AreEqual 0 $expected.CustomIpPrefixes.Count
Assert-NotNull $expected.ResourceGuid
Assert-AreEqual "Succeeded" $expected.ProvisioningState
Assert-AreEqual $ipTagType $expected.IpTags[0].IpTagType
Assert-AreEqual $ipTagTag $expected.IpTags[0].Tag

# list
$list = Get-AzPublicIpPrefix -ResourceGroupName $rgname
Assert-AreEqual 1 @($list).Count
Assert-AreEqual $list[0].ResourceGroupName $actual.ResourceGroupName
Assert-AreEqual $list[0].Name $actual.Name
Assert-AreEqual $list[0].Location $actual.Location
Assert-AreEqual 30 $list[0].PrefixLength
Assert-AreEqual "Succeeded" $list[0].ProvisioningState

$expected = Get-AzPublicIpPrefix -ResourceId $actual.Id
$list = Get-AzMasterCustomIpPrefix -ResourceGroupName $rgname
Assert-AreEqual 1 @($list).Count
Assert-AreEqual $list[0].ResourceGroupName $actual.ResourceGroupName
Assert-AreEqual $list[0].Name $actual.Name
Assert-AreEqual $list[0].ResourceGroupName $actual.ResourceGroupName
Assert-AreEqual $list[0].Name $actual.Name
Assert-AreEqual $list[0].Location $actual.Location
Assert-AreEqual 30 $list[0].PrefixLength
Assert-AreEqual $list[0].Cidr $actual.Cidr
Assert-AreEqual $list[0].ValidationMessage $actual.ValidationMessage
Assert-AreEqual $list[0].SignedValidationMessage $actual.SignedValidationMessage
Assert-AreEqual "Succeeded" $list[0].ProvisioningState

#$list = Get-AzPublicIpPrefix
#Assert-NotNull $list

<#$list = Get-AzPublicIpPrefix -ResourceGroupName "*"
Assert-True { $list.Count -ge 0 }
$list = Get-AzPublicIpPrefix -Name "*"
Assert-True { $list.Count -ge 0 }
$list = Get-AzPublicIpPrefix -ResourceGroupName "*" -Name "*"
Assert-True { $list.Count -ge 0 }#>

$expected.Tag = @{ testtag = "testvalSet" }

$job = Set-AzPublicIpPrefix -PublicIpPrefix $expected -AsJob
$job | Wait-Job
$actual = $job | Receive-Job


# delete
$job = Remove-AzPublicIpPrefix -InputObject $actual -PassThru -Force -AsJob
$job = Remove-AzMasterCustomIpPrefix -ResourceGroupName $actual.ResourceGroupName -name $rname -PassThru -Force -AsJob
$job | Wait-Job
$delete = $job | Receive-Job
Assert-AreEqual true $delete

$list = Get-AzPublicIpPrefix -ResourceGroupName $actual.ResourceGroupName
$list = Get-AzMasterCustomIpPrefix -ResourceGroupName $actual.ResourceGroupName
Assert-AreEqual 0 @($list).Count

# Try setting unexisting
Assert-ThrowsLike { Set-AzPublicIpPrefix -PublicIpPrefix $expected } "*not found*"

# Create one more time to test deletion with another parameter set
$job = New-AzPublicIpPrefix -ResourceGroupName $rgname -name $rname -location $location -Sku Standard -PrefixLength 30 -AsJob
$job | Wait-Job
$actual = $job | Receive-Job

$job = Remove-AzPublicIpPrefix -ResourceId $actual.Id -PassThru -Force -AsJob
$job | Wait-Job
$delete = $job | Receive-Job
Assert-AreEqual true $delete
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}

<#
.SYNOPSIS
Tests creating new simple publicIpPrefix and allocating Ip from it.
#>
function Test-PublicIpPrefixAllocatePublicIpAddress
{
# Setup
$rgname = Get-ResourceGroupName
$rname = Get-ResourceName
$pipname = $rname+"pip"
$domainNameLabel = Get-ResourceName
$rglocation = Get-ProviderLocation ResourceManagement
$resourceTypeParent = "Microsoft.Network/publicIpPrefixes"
$location = Get-ProviderLocation $resourceTypeParent "West Europe"

try
{
# Create the resource group
$resourceGroup = New-AzResourceGroup -Name $rgname -Location $rglocation -Tags @{ testtag = "testval" }

# Create publicIpPrefix
$job = New-AzPublicIpPrefix -ResourceGroupName $rgname -name $rname -location $location -Sku Standard -PrefixLength 30 -AsJob
$job | Wait-Job
$actual = $job | Receive-Job
$expected = Get-AzPublicIpPrefix -ResourceGroupName $rgname -name $rname
Assert-AreEqual $expected.ResourceGroupName $actual.ResourceGroupName "AreEqual ResourceGroupName"
Assert-AreEqual $expected.Name $actual.Name "AreEqual Name"
Assert-AreEqual $expected.Location $actual.Location "AreEqual Location"
Assert-AreEqual $expected.PrefixLength $actual.PrefixLength "AreEqual PrefixLength"
Assert-NotNull $expected.ResourceGuid "AreEqual ResourceGuid"
Assert-AreEqual "Succeeded" $expected.ProvisioningState "AreEqual ProvisioningState"

# list
$list = Get-AzPublicIpPrefix -ResourceGroupName $rgname
Assert-AreEqual 1 @($list).Count "List PublicIpPrefix AreEqual Count 1"
Assert-AreEqual $list[0].ResourceGroupName $actual.ResourceGroupName "List PublicIpPrefix AreEqual ResourceGroupName"
Assert-AreEqual $list[0].Name $actual.Name "List PublicIpPrefix AreEqual Name"
Assert-AreEqual $list[0].Location $actual.Location "List PublicIpPrefix AreEqual Location"
Assert-AreEqual 30 $list[0].PrefixLength "List PublicIpPrefix AreEqual PrefixLength 30"
Assert-AreEqual "Succeeded" $list[0].ProvisioningState "List PublicIpPrefix ProvisioningState"
Assert-NotNull $list[0].IPPrefix "List PublicIpPrefix NotNull IPPrefix"
$PublicIpPrefix = $list[0]

# Allocate publicIpAddress
$job=New-AzPublicIpAddress -ResourceGroupName $rgname -AllocationMethod Static -IpAddressVersion IPv4 -PublicIpPrefix $PublicIpPrefix -ResourceName $pipname -location $location -Sku Standard -DomainNameLabel $domainNameLabel -AsJob
$job | Wait-Job
$actualIpAddress = $job | Receive-Job
$expected = Get-AzPublicIpAddress -ResourceGroupName $rgname -name $pipname
Assert-AreEqual $expected.ResourceGroupName $actualIpAddress.ResourceGroupName "PublicIpAddress AreEqual ResourceGroupName"
Assert-AreEqual $expected.Name $actualIpAddress.Name "PublicIpAddress AreEqual Name"
Assert-AreEqual $expected.Location $actualIpAddress.Location "PublicIpAddress AreEqual Location"
Assert-AreEqual "Static" $expected.PublicIpAllocationMethod "PublicIpAddress AreEqual PublicIpAllocationMethod Static"
Assert-NotNull $expected.ResourceGuid "PublicIpAddress AreEqual ResourceGuid"
Assert-AreEqual "Succeeded" $expected.ProvisioningState "PublicIpAddress AreEqual ProvisioningState Succeeded"
Assert-AreEqual $domainNameLabel $expected.DnsSettings.DomainNameLabel "PublicIpAddress AreEqual DomainNameLabel"

# list again
$list = Get-AzPublicIpPrefix -ResourceGroupName $rgname
Assert-AreEqual 1 @($list[0].PublicIpAddresses).Count "List2 PublicIpAddresses AreEqual Count 1"

# delete address - should Succeed
$job = Remove-AzPublicIpAddress -ResourceGroupName $actual.ResourceGroupName -name $pipname -PassThru -Force -AsJob
$job | Wait-Job
$delete = $job | Receive-Job
Assert-AreEqual true $delete "Delete PublicIpAddress failed"

# delete prefix - should succeed
$job = Remove-AzPublicIpPrefix -ResourceGroupName $actual.ResourceGroupName -name $rname -PassThru -Force -AsJob
$job | Wait-Job
$delete = $job | Receive-Job
Assert-AreEqual true $delete "Delete PublicIpPrefix failed"

$list = Get-AzPublicIpPrefix -ResourceGroupName $actual.ResourceGroupName
Assert-AreEqual 0 @($list).Count "Hmmmm PublicIpPrefix is still present after delete"
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public PSCustomIpPrefix GetCustomIpPrefix(string resourceGroupName, string name,
var sdkModel = this.CustomIpPrefixClient.Get(resourceGroupName, name, expandResource);

var psModel = ToPsCustomIpPrefix(sdkModel);
//psMasterCustomIpPrefix.ResourceGroupName = resourceGroupName;
psModel.ResourceGroupName = resourceGroupName;

return psModel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,11 @@ private PSCustomIpPrefix CreateCustomIpPrefix()

if (this.ShouldProcess($"Name: {this.Name} ResourceGroup: {this.ResourceGroupName}", "Create new MasterCustomIpPrefix"))
{
var createdSdkModel = this.CustomIpPrefixClient.CreateOrUpdate(this.ResourceGroupName, this.Name, sdkModel);
return this.ToPsCustomIpPrefix(createdSdkModel);
this.CustomIpPrefixClient.CreateOrUpdate(this.ResourceGroupName, this.Name, sdkModel);

var customIpPrefix = this.GetCustomIpPrefix(this.ResourceGroupName, this.Name);

return customIpPrefix;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class SetAzureCustomIpPrefixCommand : CustomIpPrefixBaseCmdlet
Mandatory = true,
ValueFromPipeline = true,
HelpMessage = "The MasterCustomIpPrefix to set.", ParameterSetName = SetByInputObjectParameterSet)]
public PSMasterCustomIpPrefix InputObject { get; set; }
public PSCustomIpPrefix InputObject { get; set; }

[Parameter(
Mandatory = true,
Expand Down Expand Up @@ -134,8 +134,11 @@ public override void Execute()
if (this.ShouldProcess($"Name: {this.Name} ResourceGroup: {this.ResourceGroupName}", "Set existing MasterCustomIpPrefix"))
{
// Execute the PUT MasterCustomIpPrefix Policy call
var modifiedSdkModel = this.CustomIpPrefixClient.CreateOrUpdate(this.ResourceGroupName, this.Name, sdkModel);
WriteObject(this.ToPsCustomIpPrefix(modifiedSdkModel));
this.CustomIpPrefixClient.CreateOrUpdate(this.ResourceGroupName, this.Name, sdkModel);

var customIpPrefix = this.GetCustomIpPrefix(this.ResourceGroupName, this.Name);

WriteObject(customIpPrefix);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public PSMasterCustomIpPrefix GetMasterCustomIpPrefix(string resourceGroupName,
var masterCustomIpPrefix = this.MasterCustomIpPrefixClient.Get(resourceGroupName, name, expandResource);

var psMasterCustomIpPrefix = ToPsMasterCustomIpPrefix(masterCustomIpPrefix);
//psMasterCustomIpPrefix.ResourceGroupName = resourceGroupName;
psMasterCustomIpPrefix.ResourceGroupName = resourceGroupName;

return psMasterCustomIpPrefix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ private PSMasterCustomIpPrefix CreateMasterCustomIpPrefix()
if (this.ShouldProcess($"Name: {this.Name} ResourceGroup: {this.ResourceGroupName}", "Creating a new MasterCustomIpPrefix"))
{
var createdSdkModel = this.MasterCustomIpPrefixClient.CreateOrUpdate(this.ResourceGroupName, this.Name, sdkModel);
return this.ToPsMasterCustomIpPrefix(createdSdkModel);
var createdPsModel = this.ToPsMasterCustomIpPrefix(createdSdkModel);
createdPsModel.ResourceGroupName = NetworkBaseCmdlet.GetResourceGroup(createdPsModel.Id);
return createdPsModel;
}

return null;
Expand Down
Loading

0 comments on commit df074a2

Please sign in to comment.