Skip to content

Commit a9c0988

Browse files
authored
Merge pull request Azure#11193 from irrogozh/network-january
Added new cmdlets for flowLog resource
2 parents 4251d26 + bcec121 commit a9c0988

18 files changed

+3311
-19
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/Az.Network.psd1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ CmdletsToExport = 'Add-AzApplicationGatewayAuthenticationCertificate',
247247
'Get-AzNetworkWatcherTroubleshootingResult',
248248
'Get-AzNetworkWatcherFlowLogStatus',
249249
'Set-AzNetworkWatcherConfigFlowLog',
250+
'New-AzNetworkWatcherFlowLog',
251+
'Set-AzNetworkWatcherFlowLog',
252+
'Get-AzNetworkWatcherFlowLog',
253+
'Remove-AzNetworkWatcherFlowLog',
250254
'Test-AzNetworkWatcherConnectivity',
251255
'Get-AzNetworkWatcherReachabilityReport',
252256
'Get-AzNetworkWatcherReachabilityProvidersList',

src/Network/Network/Common/NetworkResourceManagerProfile.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,15 @@ private static void Initialize()
519519
cfg.CreateMap<CNM.PSFlowLogFormatParameters, MNM.FlowLogFormatParameters>();
520520
cfg.CreateMap<CNM.PSTrafficAnalyticsProperties, MNM.TrafficAnalyticsProperties>();
521521
cfg.CreateMap<CNM.PSTrafficAnalyticsConfigurationProperties, MNM.TrafficAnalyticsConfigurationProperties>();
522+
cfg.CreateMap<CNM.PSFlowLogResource, MNM.FlowLog>();
522523

523524
// MNM to CNM
524525
cfg.CreateMap<MNM.FlowLogInformation, CNM.PSFlowLog>();
525526
cfg.CreateMap<MNM.RetentionPolicyParameters, CNM.PSRetentionPolicyParameters>();
526527
cfg.CreateMap<MNM.FlowLogFormatParameters, CNM.PSFlowLogFormatParameters>();
527528
cfg.CreateMap<MNM.TrafficAnalyticsProperties, CNM.PSTrafficAnalyticsProperties>();
528529
cfg.CreateMap<MNM.TrafficAnalyticsConfigurationProperties, CNM.PSTrafficAnalyticsConfigurationProperties>();
530+
cfg.CreateMap<MNM.FlowLog, CNM.PSFlowLogResource>();
529531

530532
// CheckConnectivity
531533
// CNM to MNM
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.Models
16+
{
17+
using Newtonsoft.Json;
18+
using WindowsAzure.Commands.Common.Attributes;
19+
20+
public class PSFlowLogResource : PSTopLevelResource
21+
{
22+
[Ps1Xml(Target = ViewControl.Table)]
23+
public string ProvisioningState { get; set; }
24+
25+
[Ps1Xml(Target = ViewControl.Table)]
26+
public string TargetResourceId { get; set; }
27+
28+
[Ps1Xml(Target = ViewControl.Table)]
29+
public string TargetResourceGuid { get; set; }
30+
31+
[Ps1Xml(Target = ViewControl.Table)]
32+
public string StorageId { get; set; }
33+
34+
[Ps1Xml(Target = ViewControl.Table)]
35+
public bool? Enabled { get; set; }
36+
37+
public PSRetentionPolicyParameters RetentionPolicy { get; set; }
38+
39+
public PSFlowLogFormatParameters Format { get; set; }
40+
41+
public PSTrafficAnalyticsProperties FlowAnalyticsConfiguration { get; set; }
42+
43+
[JsonIgnore]
44+
public string RetentionPolicyText
45+
{
46+
get { return JsonConvert.SerializeObject(this.RetentionPolicy, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); }
47+
}
48+
49+
[JsonIgnore]
50+
public string FormatText
51+
{
52+
get { return JsonConvert.SerializeObject(this.Format, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); }
53+
}
54+
55+
[JsonIgnore]
56+
public string FlowAnalyticsConfigurationText
57+
{
58+
get { return JsonConvert.SerializeObject(this.FlowAnalyticsConfiguration, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); }
59+
}
60+
}
61+
}

src/Network/Network/Network.format.ps1xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,64 @@
23242324
</ListEntries>
23252325
</ListControl>
23262326
</View>
2327+
<View>
2328+
<Name>Microsoft.Azure.Commands.Network.Models.PSFlowLogResource</Name>
2329+
<ViewSelectedBy>
2330+
<TypeName>Microsoft.Azure.Commands.Network.Models.PSFlowLogResource</TypeName>
2331+
</ViewSelectedBy>
2332+
<ListControl>
2333+
<ListEntries>
2334+
<ListEntry>
2335+
<ListItems>
2336+
<ListItem>
2337+
<Label>Name</Label>
2338+
<PropertyName>Name</PropertyName>
2339+
</ListItem>
2340+
<ListItem>
2341+
<Label>Id</Label>
2342+
<PropertyName>Id</PropertyName>
2343+
</ListItem>
2344+
<ListItem>
2345+
<Label>Etag</Label>
2346+
<PropertyName>Etag</PropertyName>
2347+
</ListItem>
2348+
<ListItem>
2349+
<Label>ProvisioningState</Label>
2350+
<PropertyName>ProvisioningState</PropertyName>
2351+
</ListItem>
2352+
<ListItem>
2353+
<Label>Location</Label>
2354+
<PropertyName>Location</PropertyName>
2355+
</ListItem>
2356+
<ListItem>
2357+
<Label>TargetResourceId</Label>
2358+
<PropertyName>TargetResourceId</PropertyName>
2359+
</ListItem>
2360+
<ListItem>
2361+
<Label>StorageId</Label>
2362+
<PropertyName>StorageId</PropertyName>
2363+
</ListItem>
2364+
<ListItem>
2365+
<Label>Enabled</Label>
2366+
<PropertyName>Enabled</PropertyName>
2367+
</ListItem>
2368+
<ListItem>
2369+
<Label>RetentionPolicy</Label>
2370+
<PropertyName>RetentionPolicyText</PropertyName>
2371+
</ListItem>
2372+
<ListItem>
2373+
<Label>Format</Label>
2374+
<PropertyName>FormatText</PropertyName>
2375+
</ListItem>
2376+
<ListItem>
2377+
<Label>FlowAnalyticsConfiguration</Label>
2378+
<PropertyName>FlowAnalyticsConfigurationText</PropertyName>
2379+
</ListItem>
2380+
</ListItems>
2381+
</ListEntry>
2382+
</ListEntries>
2383+
</ListControl>
2384+
</View>
23272385
<View>
23282386
<Name>Microsoft.Azure.Commands.Network.Models.PSSecurityGroupViewResult</Name>
23292387
<ViewSelectedBy>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.Management.Internal.Resources.Utilities.Models;
16+
using Microsoft.Azure.Management.Network;
17+
using System;
18+
using System.Linq;
19+
using System.Management.Automation;
20+
using System.Net;
21+
using MNM = Microsoft.Azure.Management.Network.Models;
22+
23+
24+
namespace Microsoft.Azure.Commands.Network
25+
{
26+
public abstract class FlowLogBaseCmdlet : NetworkWatcherBaseCmdlet
27+
{
28+
public IFlowLogsOperations FlowLogs
29+
{
30+
get
31+
{
32+
return NetworkClient.NetworkManagementClient.FlowLogs;
33+
}
34+
}
35+
36+
public bool IsFlowLogPresent(string resourceGroupName, string name, string flowLogName)
37+
{
38+
try
39+
{
40+
this.FlowLogs.Get(resourceGroupName, name, flowLogName);
41+
}
42+
catch (MNM.ErrorResponseException exception) when (exception.Response != null && exception.Response.StatusCode == HttpStatusCode.NotFound)
43+
{
44+
return false;
45+
}
46+
47+
return true;
48+
}
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+
}
124+
}
125+
}

0 commit comments

Comments
 (0)