Skip to content

Added new cmdlets for flowLog resource #11193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,13 @@ public void TestNetworkConfigurationDiagnostic()
{
TestRunner.RunTestScript("Test-NetworkConfigurationDiagnostic");
}

[Fact]
[Trait(Category.RunType, Category.LiveOnly)]
[Trait(Category.Owner, NrpTeamAlias.netanalyticsdev)]
public void TestCRUDFlowLog()
{
TestRunner.RunTestScript("Test-CRUDFlowLog");
}
}
}
82 changes: 82 additions & 0 deletions src/Network/Network.Test/ScenarioTests/NetworkWatcherAPITests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,88 @@ function Test-FlowLog
}
}

<#
.SYNOPSIS
Test Flow log CRUD API.
#>
function Test-CRUDFlowLog
{
# Setup
$resourceGroupName = Get-NrpResourceGroupName
$nwName = Get-NrpResourceName
$nwRgName = Get-NrpResourceGroupName
$flowLogName = Get-NrpResourceName
$domainNameLabel = Get-NrpResourceName
$nsgName = Get-NrpResourceName
$stoname = Get-NrpResourceName
$location = Get-ProviderLocation "Microsoft.Network/networkWatchers" "West Central US"

try
{
# Create Resource group
New-AzResourceGroup -Name $resourceGroupName -Location "$location"

# Create NetworkSecurityGroup
$nsg = New-AzNetworkSecurityGroup -name $nsgName -ResourceGroupName $resourceGroupName -Location $location

# Get NetworkSecurityGroup
$getNsg = Get-AzNetworkSecurityGroup -name $nsgName -ResourceGroupName $resourceGroupName

# Create Resource group for Network Watcher
New-AzResourceGroup -Name $nwRgName -Location "$location"

# Get Network Watcher
$nw = Get-CreateTestNetworkWatcher -location $location -nwName $nwName -nwRgName $nwRgName

# Create storage
$stoname = 'sto' + $stoname
$stotype = 'Standard_GRS'

New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $stoname -Location $location -Type $stotype;
$sto = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $stoname;

# Create flow log
$job = New-AzNetworkWatcherFlowLog -NetworkWatcher $nw Name $flowLogName -TargetResourceId $getNsg.Id -StorageAccountId $sto.Id -Enabled $true
$job | Wait-Job
$config = $job | Receive-Job

# Validation set operation
Assert-AreEqual $config.TargetResourceId $getNsg.Id
Assert-AreEqual $config.StorageId $sto.Id
Assert-AreEqual $config.Enabled $true
Assert-AreEqual $config.Format.Type "JSON"
Assert-AreEqual $config.Format.Version 1

# Get flow log
$flowLog = Get-AzNetworkWatcherFlowLog -NetworkWatcher $nw -Name $flowLogName

# Validation get operation
Assert-AreEqual $flowLog.TargetResourceId $getNsg.Id
Assert-AreEqual $flowLog.StorageId $sto.Id
Assert-AreEqual $flowLog.Enabled $true
Assert-AreEqual $flowLog.Format.Type "JSON"
Assert-AreEqual $flowLog.Format.Version 1

# Set flow log
$flowLog.Format.Version = 2
$flowLog | Set-AzNetworkWatcherFlowLog -Force

# Get updated flowLog
$updatedFlowLog = Get-AzNetworkWatcherFlowLog -NetworkWatcher $nw -Name $flowLogName
Assert-AreEqual $updatedFlowLog.Format.Version 2

# Delete flow log
Remove-AzNetworkWatcherFlowLog -NetworkWatcher $nw -Name $flowLogName
}
finally
{
# Cleanup
Clean-ResourceGroup $resourceGroupName
Clean-ResourceGroup $nwRgName
}
}


<#
.SYNOPSIS
Test ConnectivityCheck NetworkWatcher API.
Expand Down
4 changes: 4 additions & 0 deletions src/Network/Network/Az.Network.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ CmdletsToExport = 'Add-AzApplicationGatewayAuthenticationCertificate',
'Get-AzNetworkWatcherTroubleshootingResult',
'Get-AzNetworkWatcherFlowLogStatus',
'Set-AzNetworkWatcherConfigFlowLog',
'New-AzNetworkWatcherFlowLog',
'Set-AzNetworkWatcherFlowLog',
'Get-AzNetworkWatcherFlowLog',
'Remove-AzNetworkWatcherFlowLog',
'Test-AzNetworkWatcherConnectivity',
'Get-AzNetworkWatcherReachabilityReport',
'Get-AzNetworkWatcherReachabilityProvidersList',
Expand Down
2 changes: 2 additions & 0 deletions src/Network/Network/Common/NetworkResourceManagerProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,15 @@ private static void Initialize()
cfg.CreateMap<CNM.PSFlowLogFormatParameters, MNM.FlowLogFormatParameters>();
cfg.CreateMap<CNM.PSTrafficAnalyticsProperties, MNM.TrafficAnalyticsProperties>();
cfg.CreateMap<CNM.PSTrafficAnalyticsConfigurationProperties, MNM.TrafficAnalyticsConfigurationProperties>();
cfg.CreateMap<CNM.PSFlowLogResource, MNM.FlowLog>();

// MNM to CNM
cfg.CreateMap<MNM.FlowLogInformation, CNM.PSFlowLog>();
cfg.CreateMap<MNM.RetentionPolicyParameters, CNM.PSRetentionPolicyParameters>();
cfg.CreateMap<MNM.FlowLogFormatParameters, CNM.PSFlowLogFormatParameters>();
cfg.CreateMap<MNM.TrafficAnalyticsProperties, CNM.PSTrafficAnalyticsProperties>();
cfg.CreateMap<MNM.TrafficAnalyticsConfigurationProperties, CNM.PSTrafficAnalyticsConfigurationProperties>();
cfg.CreateMap<MNM.FlowLog, CNM.PSFlowLogResource>();

// CheckConnectivity
// CNM to MNM
Expand Down
61 changes: 61 additions & 0 deletions src/Network/Network/Models/PSFlowLogResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// ----------------------------------------------------------------------------------
//
// 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.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Network.Models
{
using Newtonsoft.Json;
using WindowsAzure.Commands.Common.Attributes;

public class PSFlowLogResource : PSTopLevelResource
{
[Ps1Xml(Target = ViewControl.Table)]
public string ProvisioningState { get; set; }

[Ps1Xml(Target = ViewControl.Table)]
public string TargetResourceId { get; set; }

[Ps1Xml(Target = ViewControl.Table)]
public string TargetResourceGuid { get; set; }

[Ps1Xml(Target = ViewControl.Table)]
public string StorageId { get; set; }

[Ps1Xml(Target = ViewControl.Table)]
public bool? Enabled { get; set; }

public PSRetentionPolicyParameters RetentionPolicy { get; set; }

public PSFlowLogFormatParameters Format { get; set; }

public PSTrafficAnalyticsProperties FlowAnalyticsConfiguration { get; set; }

[JsonIgnore]
public string RetentionPolicyText
{
get { return JsonConvert.SerializeObject(this.RetentionPolicy, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); }
}

[JsonIgnore]
public string FormatText
{
get { return JsonConvert.SerializeObject(this.Format, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); }
}

[JsonIgnore]
public string FlowAnalyticsConfigurationText
{
get { return JsonConvert.SerializeObject(this.FlowAnalyticsConfiguration, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); }
}
}
}
58 changes: 58 additions & 0 deletions src/Network/Network/Network.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -2324,6 +2324,64 @@
</ListEntries>
</ListControl>
</View>
<View>
<Name>Microsoft.Azure.Commands.Network.Models.PSFlowLogResource</Name>
<ViewSelectedBy>
<TypeName>Microsoft.Azure.Commands.Network.Models.PSFlowLogResource</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<Label>Name</Label>
<PropertyName>Name</PropertyName>
</ListItem>
<ListItem>
<Label>Id</Label>
<PropertyName>Id</PropertyName>
</ListItem>
<ListItem>
<Label>Etag</Label>
<PropertyName>Etag</PropertyName>
</ListItem>
<ListItem>
<Label>ProvisioningState</Label>
<PropertyName>ProvisioningState</PropertyName>
</ListItem>
<ListItem>
<Label>Location</Label>
<PropertyName>Location</PropertyName>
</ListItem>
<ListItem>
<Label>TargetResourceId</Label>
<PropertyName>TargetResourceId</PropertyName>
</ListItem>
<ListItem>
<Label>StorageId</Label>
<PropertyName>StorageId</PropertyName>
</ListItem>
<ListItem>
<Label>Enabled</Label>
<PropertyName>Enabled</PropertyName>
</ListItem>
<ListItem>
<Label>RetentionPolicy</Label>
<PropertyName>RetentionPolicyText</PropertyName>
</ListItem>
<ListItem>
<Label>Format</Label>
<PropertyName>FormatText</PropertyName>
</ListItem>
<ListItem>
<Label>FlowAnalyticsConfiguration</Label>
<PropertyName>FlowAnalyticsConfigurationText</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
<View>
<Name>Microsoft.Azure.Commands.Network.Models.PSSecurityGroupViewResult</Name>
<ViewSelectedBy>
Expand Down
125 changes: 125 additions & 0 deletions src/Network/Network/NetworkWatcher/FlowLog/FlowLogBaseCmdlet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// ----------------------------------------------------------------------------------
//
// 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.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using Microsoft.Azure.Management.Network;
using System;
using System.Linq;
using System.Management.Automation;
using System.Net;
using MNM = Microsoft.Azure.Management.Network.Models;


namespace Microsoft.Azure.Commands.Network
{
public abstract class FlowLogBaseCmdlet : NetworkWatcherBaseCmdlet
{
public IFlowLogsOperations FlowLogs
{
get
{
return NetworkClient.NetworkManagementClient.FlowLogs;
}
}

public bool IsFlowLogPresent(string resourceGroupName, string name, string flowLogName)
{
try
{
this.FlowLogs.Get(resourceGroupName, name, flowLogName);
}
catch (MNM.ErrorResponseException exception) when (exception.Response != null && exception.Response.StatusCode == HttpStatusCode.NotFound)
{
return false;
}

return true;
}

public bool IsValidResourceId(ResourceIdentifier id, string expectedResourceType, bool validateParent = false, string expectedParentType = null)
{
if (id == null || string.IsNullOrEmpty(id.ResourceName) || string.IsNullOrEmpty(id.ResourceGroupName) || string.IsNullOrEmpty(id.Subscription)
|| !string.Equals(id.ResourceType, expectedResourceType))
{
return false;
}

if (validateParent)
{
if (string.IsNullOrEmpty(id.ParentResource))
{
return false;
}

string[] tokens = id.ParentResource.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Count() != 2 || (!string.IsNullOrEmpty(expectedParentType) && !string.Equals(tokens[0], expectedParentType)))
{
return false;
}
}

return true;
}

public void ValidateFlowLogParameters(string targetResourceId, string storageId, int? formatVersion, string formatType,
bool enableTrafficAnalytics, string trafficAnalyticsWorkspaceId, int? trafficAnalyticsInterval, int? retentionPolicyDays)
{
ResourceIdentifier targetResourceInfo = new ResourceIdentifier(targetResourceId);
if (!this.IsValidResourceId(targetResourceInfo, "Microsoft.Network/networkSecurityGroups"))
{
throw new PSArgumentException(Properties.Resources.InvalidTargetResourceId);
}

ResourceIdentifier storageAccountInfo = new ResourceIdentifier(storageId);
if (!this.IsValidResourceId(storageAccountInfo, "Microsoft.Storage/storageAccounts"))
{
throw new PSArgumentException(Properties.Resources.InvalidStorageId);
}

if (formatVersion != null && (formatVersion < 0 || formatVersion > 2))
{
throw new PSArgumentException(Properties.Resources.InvalidFlowLogFormatVersion);
}

if (!string.IsNullOrEmpty(formatType) && !string.Equals(formatType, "JSON", StringComparison.OrdinalIgnoreCase))
{
throw new PSArgumentException(Properties.Resources.InvalidFlowLogFormatVersion);
}

if (enableTrafficAnalytics && string.IsNullOrEmpty(trafficAnalyticsWorkspaceId))
{
throw new PSArgumentException(Properties.Resources.TrafficAnalyticsWorkspaceResourceIdIsMissing);
}

if (trafficAnalyticsInterval != null && trafficAnalyticsInterval != 10 && trafficAnalyticsInterval != 60)
{
throw new PSArgumentException(Properties.Resources.InvalidTrafficAnalyticsInterval);
}

if (!string.IsNullOrEmpty(trafficAnalyticsWorkspaceId))
{
ResourceIdentifier workspaceInfo = new ResourceIdentifier(trafficAnalyticsWorkspaceId);
if (!this.IsValidResourceId(workspaceInfo, "Microsoft.OperationalInsights/workspaces"))
{
throw new PSArgumentException(Properties.Resources.InvalidWorkspaceResourceId);
}
}

if (retentionPolicyDays != null && retentionPolicyDays < 0)
{
throw new PSArgumentException(Properties.Resources.InvalidTrafficAnalyticsInterval);
}
}
}
}
Loading