Skip to content

Commit 7674d70

Browse files
authored
Merge pull request #4009 from EvgenyAgafonchikov/networkusage
Implemeted Usage cmdlet for Network; Added usage tests
2 parents 649965a + fc6f0f8 commit 7674d70

19 files changed

+1383
-6
lines changed

src/ResourceManager/Network/AzureRM.Network.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ CmdletsToExport = 'Add-AzureRmApplicationGatewayAuthenticationCertificate',
290290
'Get-AzureRmVirtualNetwork', 'New-AzureRmVirtualNetwork',
291291
'Get-AzureRmVirtualNetworkGatewayBgpPeerStatus',
292292
'Get-AzureRmVirtualNetworkGatewayAdvertisedRoute',
293-
'Get-AzureRmVirtualNetworkGatewayLearnedRoute'
293+
'Get-AzureRmVirtualNetworkGatewayLearnedRoute',
294+
'Get-AzureRmNetworkUsage'
294295

295296
# Variables to export from this module
296297
# VariablesToExport = @()

src/ResourceManager/Network/Commands.Network.Test/Commands.Network.Test.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
<Compile Include="ScenarioTests\RouteTableTests.cs" />
187187
<Compile Include="ScenarioTests\NetworkSecurityGroupTests.cs" />
188188
<Compile Include="ScenarioTests\TestDnsAvailabilityTest.cs" />
189+
<Compile Include="ScenarioTests\UsageTests.cs" />
189190
<Compile Include="ScenarioTests\VirtualNetworkTests.cs" />
190191
<Compile Include="ScenarioTests\PublicIpAddressTests.cs" />
191192
<Compile Include="ScenarioTests\VirtualNetworkGatewayTests.cs" />
@@ -238,6 +239,9 @@
238239
<None Include="ScenarioTests\TestDnsAvailabilityTest.ps1">
239240
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
240241
</None>
242+
<None Include="ScenarioTests\UsageTests.ps1">
243+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
244+
</None>
241245
<None Include="ScenarioTests\VirtualNetworkTests.ps1">
242246
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
243247
</None>
@@ -457,6 +461,9 @@
457461
<None Include="SessionRecords\Commands.Network.Test.ScenarioTests.LocalNetworkGatewayTests\TestLocalNetworkGatewayCRUD.json">
458462
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
459463
</None>
464+
<None Include="SessionRecords\Commands.Network.Test.ScenarioTests.UsageTests\TestNetworkUsage.json">
465+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
466+
</None>
460467
<None Include="SessionRecords\Commands.Network.Test.ScenarioTests.VirtualNetworkGatewayConnectionTests\TestVirtualNetworkGatewayConnectionCRUD.json">
461468
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
462469
</None>
@@ -517,4 +524,4 @@
517524
</ItemGroup>
518525
<ItemGroup />
519526
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
520-
</Project>
527+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.ServiceManagemenet.Common.Models;
16+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
17+
using Xunit;
18+
using Xunit.Abstractions;
19+
20+
namespace Commands.Network.Test.ScenarioTests
21+
{
22+
public class UsageTests : Microsoft.WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
23+
{
24+
public UsageTests(ITestOutputHelper output)
25+
{
26+
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
27+
}
28+
29+
[Fact]
30+
[Trait(Category.AcceptanceType, Category.CheckIn)]
31+
public void TestNetworkUsage()
32+
{
33+
NetworkResourcesController.NewInstance.RunPsTest("Test-NetworkUsage");
34+
}
35+
}
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 checking usage cmdlet
18+
#>
19+
function Test-NetworkUsage
20+
{
21+
# Setup
22+
$rgname = Get-ResourceGroupName
23+
$vnetName = Get-ResourceName
24+
$subnetName = Get-ResourceName
25+
$rglocation = Get-ProviderLocation ResourceManagement
26+
$resourceTypeParent = "Microsoft.Network/Usages"
27+
$location = Get-ProviderLocation $resourceTypeParent
28+
29+
try
30+
{
31+
$location = $location -replace " ","";
32+
$usage = Get-AzureRMNetworkUsage -Location $location;
33+
$vnetCount = ($usage | Where-Object { $_.name.Value -eq "VirtualNetworks" }).currentValue;
34+
Assert-AreNotEqual 0 $usage.Length "Usage should return non-empty array";
35+
36+
# Create the resource group
37+
$resourceGroup = New-AzureRmResourceGroup -Name $rgname -Location $rglocation
38+
39+
# Create the Virtual Network
40+
New-AzureRmvirtualNetwork -Name $vnetName -ResourceGroupName $rgname -Location $location -AddressPrefix 10.0.0.0/16 -DnsServer 8.8.8.8;
41+
$usage = Get-AzureRMNetworkUsage -Location $location;
42+
$vnetCount2 = ($usage | Where-Object { $_.name.Value -eq "VirtualNetworks" }).currentValue;
43+
44+
Assert-AreEqual ($vnetCount + 1) $vnetCount2 "Virtual Networks usage current value should be increased after Virtual Network was created";
45+
}
46+
finally
47+
{
48+
# Cleanup
49+
Clean-ResourceGroup $rgname
50+
}
51+
}

src/ResourceManager/Network/Commands.Network.Test/SessionRecords/Commands.Network.Test.ScenarioTests.UsageTests/TestNetworkUsage.json

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

src/ResourceManager/Network/Commands.Network/Commands.Network.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@
348348
<Compile Include="Models\PSTroubleshootDetails.cs" />
349349
<Compile Include="Models\PSTroubleshootRecommendedActions.cs" />
350350
<Compile Include="Models\PSTroubleshootResult.cs" />
351+
<Compile Include="Models\PSUsage.cs" />
352+
<Compile Include="Models\PSUsageCustom.cs" />
353+
<Compile Include="Models\PSUsageName.cs" />
351354
<Compile Include="Models\PSViewNsgRules.cs" />
352355
<Compile Include="Models\PSServiceProviderProperties.cs" />
353356
<Compile Include="Models\PSPeering.cs" />
@@ -394,6 +397,7 @@
394397
<Compile Include="NetworkWatcher\PacketCapture\StopAzureNetworkWatcherPacketCaptureCommand.cs" />
395398
<Compile Include="NetworkWatcher\RemoveAzureNetworkWatcherCommand.cs" />
396399
<Compile Include="ProviderWideCmdlets\GetAzureExpressRouteServiceProviderCommand.cs" />
400+
<Compile Include="Usage\GetAzureNetworkUsageCommand.cs" />
397401
<Compile Include="VirtualNetworkGatewayConnection\NewAzureRmIpsecPolicyCommand.cs" />
398402
<Compile Include="RouteFilter\GetAzureRouteFilterCommand.cs" />
399403
<Compile Include="RouteFilter\NewAzureRouteFilterCommand.cs" />

src/ResourceManager/Network/Commands.Network/Common/NetworkResourceManagerProfile.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ protected override void Configure()
137137
Mapper.CreateMap<MNM.NetworkInterfaceDnsSettings, CNM.PSNetworkInterfaceDnsSettings>();
138138
Mapper.CreateMap<MNM.NetworkInterfaceIPConfiguration, CNM.PSNetworkInterfaceIPConfiguration>();
139139

140+
// Usage
141+
Mapper.CreateMap<CNM.PSUsage, MNM.Usage>();
142+
Mapper.CreateMap<MNM.Usage, CNM.PSUsage>();
143+
Mapper.CreateMap<CNM.PSUsageName, MNM.UsageName>();
144+
Mapper.CreateMap<MNM.UsageName, CNM.PSUsageName>();
145+
140146
// NetworkWatcher
141147
// CNM to MNM
142148
Mapper.CreateMap<CNM.PSNetworkWatcher, MNM.NetworkWatcher>();

src/ResourceManager/Network/Commands.Network/Microsoft.Azure.Commands.Network.format.ps1xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<Configuration>
33
<ViewDefinitions>
4+
<View>
5+
<Name>Microsoft.Azure.Commands.Network.Models.PSUsage</Name>
6+
<ViewSelectedBy>
7+
<TypeName>Microsoft.Azure.Commands.Network.Models.PSUsage</TypeName>
8+
</ViewSelectedBy>
9+
<ListControl>
10+
<ListEntries>
11+
<ListEntry>
12+
<ListItems>
13+
<ListItem>
14+
<Label>ResourceType</Label>
15+
<PropertyName>ResourceType</PropertyName>
16+
</ListItem>
17+
<ListItem>
18+
<Label>CurrentValue</Label>
19+
<PropertyName>CurrentValue</PropertyName>
20+
</ListItem>
21+
<ListItem>
22+
<Label>Limit</Label>
23+
<PropertyName>Limit</PropertyName>
24+
</ListItem>
25+
</ListItems>
26+
</ListEntry>
27+
</ListEntries>
28+
</ListControl>
29+
</View>
430
<View>
531
<Name>Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork</Name>
632
<ViewSelectedBy>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright (c) Microsoft and contributors. All rights reserved.
3+
//
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+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
//
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
// Warning: This code was generated by a tool.
18+
//
19+
// Changes to this file may cause incorrect behavior and will be lost if the
20+
// code is regenerated.
21+
22+
using Microsoft.Azure.Management.Network.Models;
23+
using Newtonsoft.Json;
24+
25+
namespace Microsoft.Azure.Commands.Network.Models
26+
{
27+
public partial class PSUsage
28+
{
29+
public long CurrentValue;
30+
public long Limit;
31+
public string Unit;
32+
public PSUsageName Name;
33+
}
34+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Azure.Commands.Network.Models
8+
{
9+
public partial class PSUsage
10+
{
11+
public string ResourceType
12+
{
13+
get { return Name.LocalizedValue; }
14+
}
15+
}
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// Copyright (c) Microsoft and contributors. All rights reserved.
3+
//
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+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
//
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
// Warning: This code was generated by a tool.
18+
//
19+
// Changes to this file may cause incorrect behavior and will be lost if the
20+
// code is regenerated.
21+
22+
using Microsoft.Azure.Management.Network.Models;
23+
using Newtonsoft.Json;
24+
25+
namespace Microsoft.Azure.Commands.Network.Models
26+
{
27+
public partial class PSUsageName
28+
{
29+
public string Value;
30+
public string LocalizedValue;
31+
}
32+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// Copyright (c) Microsoft and contributors. All rights reserved.
3+
//
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+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
//
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
// Warning: This code was generated by a tool.
18+
//
19+
// Changes to this file may cause incorrect behavior and will be lost if the
20+
// code is regenerated.
21+
22+
using Microsoft.Azure.Commands.Network.Models;
23+
using Microsoft.Azure.Management.Network;
24+
using Microsoft.Azure.Management.Network.Models;
25+
using System;
26+
using System.Collections;
27+
using System.Collections.Generic;
28+
using System.Linq;
29+
using System.Management.Automation;
30+
using AutoMapper;
31+
using CNM = Microsoft.Azure.Commands.Network.Models;
32+
33+
namespace Microsoft.Azure.Commands.Network.Automation
34+
{
35+
[Cmdlet(VerbsCommon.Get, "AzureRmNetworkUsage"), OutputType(typeof(PSUsage))]
36+
public partial class GetAzureRmNetworkUsage : NetworkBaseCmdlet
37+
{
38+
[Parameter(
39+
Mandatory = true,
40+
ValueFromPipelineByPropertyName = true,
41+
HelpMessage = "Location.")]
42+
[ValidateNotNullOrEmpty]
43+
public string Location { get; set; }
44+
45+
public override void Execute()
46+
{
47+
base.Execute();
48+
49+
var vUsageList = this.NetworkClient.NetworkManagementClient.Usages.List(Location);
50+
foreach (var vUsage in vUsageList)
51+
{
52+
var vUsageModel = Mapper.Map<CNM.PSUsage>(vUsage);
53+
WriteObject(vUsageModel);
54+
}
55+
}
56+
}
57+
}

src/ResourceManager/Network/Commands.Network/help/Add-AzureRmRouteFilterRuleConfig.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ The Add-AzureRmRouteFilterRuleConfig cmdlet adds a route filter rule to an Azure
2929

3030

3131

32+
33+
34+
35+
36+
37+
3238
```
3339
PS C:\>$RouteFilter = Get-AzureRmRouteFilter -ResourceGroupName "ResourceGroup11" -Name "routefilter01"
3440
PS C:\> Add-AzureRmRouteFilterRuleConfig -Name "rule13" -Access Allow -RouteFilterRuleType Community -RouteFilter $RouteFilter

src/ResourceManager/Network/Commands.Network/help/AzureRM.Network.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ Gets a network security group.
218218
### [Get-AzureRmNetworkSecurityRuleConfig](Get-AzureRmNetworkSecurityRuleConfig.md)
219219
Get a network security rule configuration for a network security group.
220220

221+
### [Get-AzureRmNetworkUsage](Get-AzureRmNetworkUsage.md)
222+
Lists network usages for a subscription
223+
221224
### [Get-AzureRmNetworkWatcher](Get-AzureRmNetworkWatcher.md)
222225
Gets the properties of a Network Watcher
223226

src/ResourceManager/Network/Commands.Network/help/Get-AzureRmApplicationGatewayBackendHealth.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ The Get-AzureRmApplicationGatewayBackendHealth cmdlet gets application gateway b
3131

3232

3333

34+
35+
36+
37+
38+
39+
3440
```
3541
PS C:\>$BackendHealth = Get-AzureRmApplicationGatewayBackendHealth -Name ApplicationGateway01 -ResourceGroupName ResourceGroup01
3642
```
@@ -46,6 +52,12 @@ This command gets the backend health of application gateway named ApplicationGat
4652

4753

4854

55+
56+
57+
58+
59+
60+
4961
```
5062
PS C:\>$BackendHealth = Get-AzureRmApplicationGatewayBackendHealth -Name ApplicationGateway01 -ResourceGroupName ResourceGroup01 -ExpandResource "backendhealth/applicationgatewayresource"
5163
```

0 commit comments

Comments
 (0)