Skip to content

Commit 85782f7

Browse files
committed
Added tests, help files + fixes based on CR
1 parent 7014e6a commit 85782f7

11 files changed

+1945
-215
lines changed

src/Network/Network.Test/ScenarioTests/NetworkWatcherAPITests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,13 @@ public void TestNetworkConfigurationDiagnostic()
121121
{
122122
TestRunner.RunTestScript("Test-NetworkConfigurationDiagnostic");
123123
}
124+
125+
[Fact]
126+
[Trait(Category.RunType, Category.LiveOnly)]
127+
[Trait(Category.Owner, NrpTeamAlias.netanalyticsdev)]
128+
public void TestCRUDFlowLog()
129+
{
130+
TestRunner.RunTestScript("Test-CRUDFlowLog");
131+
}
124132
}
125133
}

src/Network/Network.Test/ScenarioTests/NetworkWatcherAPITests.ps1

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,88 @@ function Test-FlowLog
716716
}
717717
}
718718

719+
<#
720+
.SYNOPSIS
721+
Test Flow log CRUD API.
722+
#>
723+
function Test-CRUDFlowLog
724+
{
725+
# Setup
726+
$resourceGroupName = Get-NrpResourceGroupName
727+
$nwName = Get-NrpResourceName
728+
$nwRgName = Get-NrpResourceGroupName
729+
$flowLogName = Get-NrpResourceName
730+
$domainNameLabel = Get-NrpResourceName
731+
$nsgName = Get-NrpResourceName
732+
$stoname = Get-NrpResourceName
733+
$location = Get-ProviderLocation "Microsoft.Network/networkWatchers" "West Central US"
734+
735+
try
736+
{
737+
# Create Resource group
738+
New-AzResourceGroup -Name $resourceGroupName -Location "$location"
739+
740+
# Create NetworkSecurityGroup
741+
$nsg = New-AzNetworkSecurityGroup -name $nsgName -ResourceGroupName $resourceGroupName -Location $location
742+
743+
# Get NetworkSecurityGroup
744+
$getNsg = Get-AzNetworkSecurityGroup -name $nsgName -ResourceGroupName $resourceGroupName
745+
746+
# Create Resource group for Network Watcher
747+
New-AzResourceGroup -Name $nwRgName -Location "$location"
748+
749+
# Get Network Watcher
750+
$nw = Get-CreateTestNetworkWatcher -location $location -nwName $nwName -nwRgName $nwRgName
751+
752+
# Create storage
753+
$stoname = 'sto' + $stoname
754+
$stotype = 'Standard_GRS'
755+
756+
New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $stoname -Location $location -Type $stotype;
757+
$sto = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $stoname;
758+
759+
# Create flow log
760+
$job = New-AzNetworkWatcherFlowLog -NetworkWatcher $nw Name $flowLogName -TargetResourceId $getNsg.Id -StorageAccountId $sto.Id -Enabled $true
761+
$job | Wait-Job
762+
$config = $job | Receive-Job
763+
764+
# Validation set operation
765+
Assert-AreEqual $config.TargetResourceId $getNsg.Id
766+
Assert-AreEqual $config.StorageId $sto.Id
767+
Assert-AreEqual $config.Enabled $true
768+
Assert-AreEqual $config.Format.Type "JSON"
769+
Assert-AreEqual $config.Format.Version 1
770+
771+
# Get flow log
772+
$flowLog = Get-AzNetworkWatcherFlowLog -NetworkWatcher $nw -Name $flowLogName
773+
774+
# Validation get operation
775+
Assert-AreEqual $flowLog.TargetResourceId $getNsg.Id
776+
Assert-AreEqual $flowLog.StorageId $sto.Id
777+
Assert-AreEqual $flowLog.Enabled $true
778+
Assert-AreEqual $flowLog.Format.Type "JSON"
779+
Assert-AreEqual $flowLog.Format.Version 1
780+
781+
# Set flow log
782+
$flowLog.Format.Version = 2
783+
$flowLog | Set-AzNetworkWatcherFlowLog -Force
784+
785+
# Get updated flowLog
786+
$updatedFlowLog = Get-AzNetworkWatcherFlowLog -NetworkWatcher $nw -Name $flowLogName
787+
Assert-AreEqual $updatedFlowLog.Format.Version 2
788+
789+
# Delete flow log
790+
Remove-AzNetworkWatcherFlowLog -NetworkWatcher $nw -Name $flowLogName
791+
}
792+
finally
793+
{
794+
# Cleanup
795+
Clean-ResourceGroup $resourceGroupName
796+
Clean-ResourceGroup $nwRgName
797+
}
798+
}
799+
800+
719801
<#
720802
.SYNOPSIS
721803
Test ConnectivityCheck NetworkWatcher API.

src/Network/Network/NetworkWatcher/FlowLog/FlowLogBaseCmdlet.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
1516
using Microsoft.Azure.Management.Network;
17+
using System;
18+
using System.Linq;
19+
using System.Management.Automation;
1620
using System.Net;
1721
using MNM = Microsoft.Azure.Management.Network.Models;
1822

@@ -42,5 +46,80 @@ public bool IsFlowLogPresent(string resourceGroupName, string name, string flowL
4246

4347
return true;
4448
}
49+
50+
public bool IsValidResourceId(ResourceIdentifier id, string expectedResourceType, bool validateParent = false, string expectedParentType = null)
51+
{
52+
if (id == null || string.IsNullOrEmpty(id.ResourceName) || string.IsNullOrEmpty(id.ResourceGroupName) || string.IsNullOrEmpty(id.Subscription)
53+
|| !string.Equals(id.ResourceType, expectedResourceType))
54+
{
55+
return false;
56+
}
57+
58+
if (validateParent)
59+
{
60+
if (string.IsNullOrEmpty(id.ParentResource))
61+
{
62+
return false;
63+
}
64+
65+
string[] tokens = id.ParentResource.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
66+
if (tokens.Count() != 2 || (!string.IsNullOrEmpty(expectedParentType) && !string.Equals(tokens[0], expectedParentType)))
67+
{
68+
return false;
69+
}
70+
}
71+
72+
return true;
73+
}
74+
75+
public void ValidateFlowLogParameters(string targetResourceId, string storageId, int? formatVersion, string formatType,
76+
bool enableTrafficAnalytics, string trafficAnalyticsWorkspaceId, int? trafficAnalyticsInterval, int? retentionPolicyDays)
77+
{
78+
ResourceIdentifier targetResourceInfo = new ResourceIdentifier(targetResourceId);
79+
if (!this.IsValidResourceId(targetResourceInfo, "Microsoft.Network/networkSecurityGroups"))
80+
{
81+
throw new PSArgumentException(Properties.Resources.InvalidTargetResourceId);
82+
}
83+
84+
ResourceIdentifier storageAccountInfo = new ResourceIdentifier(storageId);
85+
if (!this.IsValidResourceId(storageAccountInfo, "Microsoft.Storage/storageAccounts"))
86+
{
87+
throw new PSArgumentException(Properties.Resources.InvalidStorageId);
88+
}
89+
90+
if (formatVersion != null && (formatVersion < 0 || formatVersion > 2))
91+
{
92+
throw new PSArgumentException(Properties.Resources.InvalidFlowLogFormatVersion);
93+
}
94+
95+
if (!string.IsNullOrEmpty(formatType) && !string.Equals(formatType, "JSON", StringComparison.OrdinalIgnoreCase))
96+
{
97+
throw new PSArgumentException(Properties.Resources.InvalidFlowLogFormatVersion);
98+
}
99+
100+
if (enableTrafficAnalytics && string.IsNullOrEmpty(trafficAnalyticsWorkspaceId))
101+
{
102+
throw new PSArgumentException(Properties.Resources.TrafficAnalyticsWorkspaceResourceIdIsMissing);
103+
}
104+
105+
if (trafficAnalyticsInterval != null && trafficAnalyticsInterval != 10 && trafficAnalyticsInterval != 60)
106+
{
107+
throw new PSArgumentException(Properties.Resources.InvalidTrafficAnalyticsInterval);
108+
}
109+
110+
if (!string.IsNullOrEmpty(trafficAnalyticsWorkspaceId))
111+
{
112+
ResourceIdentifier workspaceInfo = new ResourceIdentifier(trafficAnalyticsWorkspaceId);
113+
if (!this.IsValidResourceId(workspaceInfo, "Microsoft.OperationalInsights/workspaces"))
114+
{
115+
throw new PSArgumentException(Properties.Resources.InvalidWorkspaceResourceId);
116+
}
117+
}
118+
119+
if (retentionPolicyDays != null && retentionPolicyDays < 0)
120+
{
121+
throw new PSArgumentException(Properties.Resources.InvalidTrafficAnalyticsInterval);
122+
}
123+
}
45124
}
46125
}

0 commit comments

Comments
 (0)