|
| 1 | +# ---------------------------------------------------------------------------------- |
| 2 | +# Copyright Microsoft Corporation |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | +# ---------------------------------------------------------------------------------- |
| 13 | + |
| 14 | +<# |
| 15 | +.SYNOPSIS |
| 16 | +DDoS custom policy management operations |
| 17 | +#> |
| 18 | +function Test-DdosCustomPolicyCRUD |
| 19 | +{ |
| 20 | + $rgLocation = Get-ProviderLocation ResourceManagement |
| 21 | + $resourceTypeParent = "Microsoft.Network/DdosCustomPolicies" |
| 22 | + $location = Get-ProviderLocation $resourceTypeParent |
| 23 | + |
| 24 | + $rgName = Get-ResourceGroupName |
| 25 | + $ddosCustomPolicyName = Get-ResourceName |
| 26 | + |
| 27 | + try |
| 28 | + { |
| 29 | + # Create the resource group |
| 30 | + New-AzResourceGroup -Name $rgName -Location $location -Tags @{ testtag = "ddosCustomPolicy tag" } |
| 31 | + |
| 32 | + # Create the DDoS custom policy |
| 33 | + $job = New-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicyName -Location $rgLocation -TrafficType Tcp -PacketsPerSecond 1000000 -AsJob |
| 34 | + $job | Wait-Job |
| 35 | + $ddosCustomPolicyNew = $job | Receive-Job |
| 36 | + |
| 37 | + Assert-AreEqual $rgName $ddosCustomPolicyNew.ResourceGroupName |
| 38 | + Assert-AreEqual $ddosCustomPolicyName $ddosCustomPolicyNew.Name |
| 39 | + Assert-NotNull $ddosCustomPolicyNew.Location |
| 40 | + Assert-NotNull $ddosCustomPolicyNew.Etag |
| 41 | + Assert-NotNull $ddosCustomPolicyNew.ProvisioningState |
| 42 | + |
| 43 | + # Get the DDoS custom policy |
| 44 | + $ddosCustomPolicyGet = Get-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicyName |
| 45 | + Assert-AreEqual $rgName $ddosCustomPolicyGet.ResourceGroupName |
| 46 | + Assert-AreEqual $ddosCustomPolicyName $ddosCustomPolicyGet.Name |
| 47 | + Assert-NotNull $ddosCustomPolicyGet.Location |
| 48 | + Assert-NotNull $ddosCustomPolicyGet.Etag |
| 49 | + |
| 50 | + # Remove the DDoS custom policy |
| 51 | + $ddosCustomPolicyDelete = Remove-AzDdosCustomPolicy -Name $ddosCustomPolicyName -ResourceGroupName $rgName -PassThru |
| 52 | + Assert-AreEqual $true $ddosCustomPolicyDelete |
| 53 | + } |
| 54 | + finally |
| 55 | + { |
| 56 | + # Cleanup |
| 57 | + Clean-ResourceGroup $rgName |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +<# |
| 62 | +.SYNOPSIS |
| 63 | +DDoS custom policy creation with tags |
| 64 | +#> |
| 65 | +function Test-DdosCustomPolicyCRUDWithTags |
| 66 | +{ |
| 67 | + $rgLocation = Get-ProviderLocation ResourceManagement |
| 68 | + $resourceTypeParent = "Microsoft.Network/DdosCustomPolicies" |
| 69 | + $location = Get-ProviderLocation $resourceTypeParent |
| 70 | + |
| 71 | + $rgName = Get-ResourceGroupName |
| 72 | + $ddosCustomPolicyName = Get-ResourceName |
| 73 | + $tags = @{ Environment = "Test"; Project = "DdosCustomPolicy" } |
| 74 | + |
| 75 | + try |
| 76 | + { |
| 77 | + # Create the resource group |
| 78 | + New-AzResourceGroup -Name $rgName -Location $location -Tags @{ testtag = "ddosCustomPolicy tag" } |
| 79 | + |
| 80 | + # Create the DDoS custom policy with tags |
| 81 | + $ddosCustomPolicy = New-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicyName -Location $rgLocation -TrafficType Tcp -PacketsPerSecond 1000000 -Tag $tags |
| 82 | + |
| 83 | + Assert-AreEqual $rgName $ddosCustomPolicy.ResourceGroupName |
| 84 | + Assert-AreEqual $ddosCustomPolicyName $ddosCustomPolicy.Name |
| 85 | + Assert-NotNull $ddosCustomPolicy.Tag |
| 86 | + Assert-AreEqual "Test" $ddosCustomPolicy.Tag["Environment"] |
| 87 | + Assert-AreEqual "DdosCustomPolicy" $ddosCustomPolicy.Tag["Project"] |
| 88 | + |
| 89 | + # Get and verify tags |
| 90 | + $ddosCustomPolicyGet = Get-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicyName |
| 91 | + Assert-AreEqual "Test" $ddosCustomPolicyGet.Tag["Environment"] |
| 92 | + |
| 93 | + # Remove the DDoS custom policy |
| 94 | + Remove-AzDdosCustomPolicy -Name $ddosCustomPolicyName -ResourceGroupName $rgName |
| 95 | + } |
| 96 | + finally |
| 97 | + { |
| 98 | + # Cleanup |
| 99 | + Clean-ResourceGroup $rgName |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +<# |
| 104 | +.SYNOPSIS |
| 105 | +Test DDoS custom policy linkage with Load Balancer Frontend IP Config |
| 106 | +#> |
| 107 | +function Test-LoadBalancerFrontendWithDdosCustomPolicy |
| 108 | +{ |
| 109 | + $resourceTypeParent = "Microsoft.Network/loadBalancers" |
| 110 | + $location = Get-ProviderLocation $resourceTypeParent |
| 111 | + |
| 112 | + $rgName = Get-ResourceGroupName |
| 113 | + $lbName = Get-ResourceName |
| 114 | + $pipName = Get-ResourceName |
| 115 | + $frontendName = Get-ResourceName |
| 116 | + $ddosCustomPolicyName = Get-ResourceName |
| 117 | + |
| 118 | + try |
| 119 | + { |
| 120 | + # Create the resource group |
| 121 | + New-AzResourceGroup -Name $rgName -Location $location |
| 122 | + |
| 123 | + # Create DDoS custom policy |
| 124 | + $ddosCustomPolicy = New-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicyName -Location $location -TrafficType Tcp -PacketsPerSecond 1000000 |
| 125 | + |
| 126 | + # Create Public IP Address |
| 127 | + $publicIp = New-AzPublicIpAddress -ResourceGroupName $rgName -Name $pipName -Location $location -AllocationMethod Static |
| 128 | + |
| 129 | + # Create frontend IP config with DDoS custom policy |
| 130 | + $frontendIpConfig = New-AzLoadBalancerFrontendIpConfig -Name $frontendName -PublicIpAddress $publicIp -DdosCustomPolicyId $ddosCustomPolicy.Id |
| 131 | + |
| 132 | + Assert-NotNull $frontendIpConfig |
| 133 | + Assert-AreEqual $frontendName $frontendIpConfig.Name |
| 134 | + |
| 135 | + # Create Load Balancer backend pool |
| 136 | + $backendPool = New-AzLoadBalancerBackendAddressPoolConfig -Name "BackendPool" |
| 137 | + |
| 138 | + # Create Load Balancer with frontend config |
| 139 | + $loadBalancer = New-AzLoadBalancer -ResourceGroupName $rgName -Name $lbName -Location $location ` |
| 140 | + -FrontendIpConfiguration $frontendIpConfig -BackendAddressPool $backendPool |
| 141 | + |
| 142 | + Assert-NotNull $loadBalancer |
| 143 | + Assert-AreEqual $lbName $loadBalancer.Name |
| 144 | + |
| 145 | + # Verify frontend has DDoS custom policy |
| 146 | + $frontendFromLb = Get-AzLoadBalancerFrontendIpConfig -LoadBalancer $loadBalancer -Name $frontendName |
| 147 | + Assert-NotNull $frontendFromLb |
| 148 | + Assert-NotNull $frontendFromLb.DdosSettings |
| 149 | + Assert-NotNull $frontendFromLb.DdosSettings.DdosCustomPolicy |
| 150 | + Assert-AreEqual $ddosCustomPolicy.Id $frontendFromLb.DdosSettings.DdosCustomPolicy.Id |
| 151 | + |
| 152 | + $ddosCustomPolicyGet = Get-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicyName |
| 153 | + Assert-NotNull $ddosCustomPolicyGet.FrontEndIPConfiguration |
| 154 | + Assert-AreEqual 1 $ddosCustomPolicyGet.FrontEndIPConfiguration.Count |
| 155 | + Assert-AreEqual $frontendFromLb.Id $ddosCustomPolicyGet.FrontEndIPConfiguration[0].Id |
| 156 | + |
| 157 | + # Test Set-AzLoadBalancerFrontendIpConfig with new DDoS policy |
| 158 | + $ddosCustomPolicy2Name = Get-ResourceName |
| 159 | + $ddosCustomPolicy2 = New-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicy2Name -Location $location -TrafficType Tcp -PacketsPerSecond 1000000 |
| 160 | + |
| 161 | + $loadBalancer = Set-AzLoadBalancerFrontendIpConfig -LoadBalancer $loadBalancer -Name $frontendName ` |
| 162 | + -PublicIpAddress $publicIp -DdosCustomPolicyId $ddosCustomPolicy2.Id |
| 163 | + |
| 164 | + Set-AzLoadBalancer -LoadBalancer $loadBalancer | Out-Null |
| 165 | + $loadBalancerUpdated = Get-AzLoadBalancer -ResourceGroupName $rgName -Name $lbName |
| 166 | + $frontendIpConfig2 = Get-AzLoadBalancerFrontendIpConfig -LoadBalancer $loadBalancerUpdated -Name $frontendName |
| 167 | + |
| 168 | + Assert-NotNull $frontendIpConfig2.DdosSettings |
| 169 | + Assert-AreEqual $ddosCustomPolicy2.Id $frontendIpConfig2.DdosSettings.DdosCustomPolicy.Id |
| 170 | + |
| 171 | + # Cleanup |
| 172 | + Remove-AzLoadBalancer -ResourceGroupName $rgName -Name $lbName -Force |
| 173 | + Remove-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicyName |
| 174 | + Remove-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicy2Name |
| 175 | + } |
| 176 | + finally |
| 177 | + { |
| 178 | + # Cleanup |
| 179 | + Clean-ResourceGroup $rgName |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +<# |
| 184 | +.SYNOPSIS |
| 185 | +Test Add-AzLoadBalancerFrontendIpConfig with DDoS custom policy |
| 186 | +#> |
| 187 | +function Test-AddLoadBalancerFrontendWithDdosCustomPolicy |
| 188 | +{ |
| 189 | + $resourceTypeParent = "Microsoft.Network/loadBalancers" |
| 190 | + $location = Get-ProviderLocation $resourceTypeParent |
| 191 | + |
| 192 | + $rgName = Get-ResourceGroupName |
| 193 | + $lbName = Get-ResourceName |
| 194 | + $pipName1 = Get-ResourceName |
| 195 | + $pipName2 = Get-ResourceName |
| 196 | + $frontendName1 = Get-ResourceName |
| 197 | + $frontendName2 = Get-ResourceName |
| 198 | + $ddosCustomPolicyName = Get-ResourceName |
| 199 | + |
| 200 | + try |
| 201 | + { |
| 202 | + # Create the resource group |
| 203 | + New-AzResourceGroup -Name $rgName -Location $location |
| 204 | + |
| 205 | + # Create DDoS custom policy |
| 206 | + $ddosCustomPolicy = New-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicyName -Location $location -TrafficType Tcp -PacketsPerSecond 1000000 |
| 207 | + |
| 208 | + # Create first Public IP Address |
| 209 | + $publicIp1 = New-AzPublicIpAddress -ResourceGroupName $rgName -Name $pipName1 -Location $location -AllocationMethod Static |
| 210 | + |
| 211 | + # Create first frontend IP config |
| 212 | + $frontendIpConfig1 = New-AzLoadBalancerFrontendIpConfig -Name $frontendName1 -PublicIpAddress $publicIp1 |
| 213 | + |
| 214 | + # Create Load Balancer backend pool |
| 215 | + $backendPool = New-AzLoadBalancerBackendAddressPoolConfig -Name "BackendPool" |
| 216 | + |
| 217 | + # Create Load Balancer with first frontend config |
| 218 | + $loadBalancer = New-AzLoadBalancer -ResourceGroupName $rgName -Name $lbName -Location $location ` |
| 219 | + -FrontendIpConfiguration $frontendIpConfig1 -BackendAddressPool $backendPool |
| 220 | + |
| 221 | + # Create second Public IP Address |
| 222 | + $publicIp2 = New-AzPublicIpAddress -ResourceGroupName $rgName -Name $pipName2 -Location $location -AllocationMethod Static |
| 223 | + |
| 224 | + # Add second frontend with DDoS custom policy |
| 225 | + Add-AzLoadBalancerFrontendIpConfig -LoadBalancer $loadBalancer -Name $frontendName2 ` |
| 226 | + -PublicIpAddress $publicIp2 -DdosCustomPolicyId $ddosCustomPolicy.Id |
| 227 | + |
| 228 | + $loadBalancer | Set-AzLoadBalancer |
| 229 | + |
| 230 | + # Verify the LoadBalancer now has two frontends |
| 231 | + $loadBalancerUpdated = Get-AzLoadBalancer -ResourceGroupName $rgName -Name $lbName |
| 232 | + Assert-AreEqual 2 $loadBalancerUpdated.FrontendIpConfigurations.Count |
| 233 | + |
| 234 | + # Verify second frontend has DDoS custom policy |
| 235 | + $frontendFromLb = Get-AzLoadBalancerFrontendIpConfig -LoadBalancer $loadBalancerUpdated -Name $frontendName2 |
| 236 | + Assert-NotNull $frontendFromLb.DdosSettings |
| 237 | + Assert-AreEqual $ddosCustomPolicy.Id $frontendFromLb.DdosSettings.DdosCustomPolicy.Id |
| 238 | + |
| 239 | + $ddosCustomPolicyGet = Get-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicyName |
| 240 | + Assert-NotNull $ddosCustomPolicyGet.FrontEndIPConfiguration |
| 241 | + Assert-AreEqual 1 $ddosCustomPolicyGet.FrontEndIPConfiguration.Count |
| 242 | + Assert-AreEqual $frontendFromLb.Id $ddosCustomPolicyGet.FrontEndIPConfiguration[0].Id |
| 243 | + |
| 244 | + # Cleanup |
| 245 | + Remove-AzLoadBalancer -ResourceGroupName $rgName -Name $lbName -Force |
| 246 | + Remove-AzDdosCustomPolicy -ResourceGroupName $rgName -Name $ddosCustomPolicyName |
| 247 | + } |
| 248 | + finally |
| 249 | + { |
| 250 | + # Cleanup |
| 251 | + Clean-ResourceGroup $rgName |
| 252 | + } |
| 253 | +} |
0 commit comments