Skip to content

Commit 7014e6a

Browse files
committed
Added new cmdlets for flowLog resource
1 parent f913ab5 commit 7014e6a

12 files changed

+1581
-19
lines changed

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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.Network;
16+
using System.Net;
17+
using MNM = Microsoft.Azure.Management.Network.Models;
18+
19+
20+
namespace Microsoft.Azure.Commands.Network
21+
{
22+
public abstract class FlowLogBaseCmdlet : NetworkWatcherBaseCmdlet
23+
{
24+
public IFlowLogsOperations FlowLogs
25+
{
26+
get
27+
{
28+
return NetworkClient.NetworkManagementClient.FlowLogs;
29+
}
30+
}
31+
32+
public bool IsFlowLogPresent(string resourceGroupName, string name, string flowLogName)
33+
{
34+
try
35+
{
36+
this.FlowLogs.Get(resourceGroupName, name, flowLogName);
37+
}
38+
catch (MNM.ErrorResponseException exception) when (exception.Response != null && exception.Response.StatusCode == HttpStatusCode.NotFound)
39+
{
40+
return false;
41+
}
42+
43+
return true;
44+
}
45+
}
46+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 AutoMapper;
16+
using Microsoft.Azure.Commands.Network.Models;
17+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
18+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
19+
using Microsoft.Azure.Management.Network;
20+
using System;
21+
using System.Collections.Generic;
22+
using System.Linq;
23+
using System.Management.Automation;
24+
using MNM = Microsoft.Azure.Management.Network.Models;
25+
26+
namespace Microsoft.Azure.Commands.Network
27+
{
28+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "NetworkWatcherFlowLog", DefaultParameterSetName = "SetByName"), OutputType(typeof(PSFlowLogResource))]
29+
30+
public class GetAzNetworkWatcherFlowLogCommand : FlowLogBaseCmdlet
31+
{
32+
[Parameter(
33+
Mandatory = true,
34+
ValueFromPipeline = true,
35+
HelpMessage = "The network watcher resource.",
36+
ParameterSetName = "SetByResource")]
37+
[ValidateNotNull]
38+
public PSNetworkWatcher NetworkWatcher { get; set; }
39+
40+
[Parameter(
41+
Mandatory = true,
42+
HelpMessage = "The name of network watcher.",
43+
ParameterSetName = "SetByName")]
44+
[ResourceNameCompleter("Microsoft.Network/networkWatchers", "ResourceGroupName")]
45+
[ValidateNotNullOrEmpty]
46+
public string NetworkWatcherName { get; set; }
47+
48+
[Parameter(
49+
Mandatory = true,
50+
HelpMessage = "The name of the network watcher resource group.",
51+
ParameterSetName = "SetByName")]
52+
[ResourceGroupCompleter]
53+
[ValidateNotNullOrEmpty]
54+
public string ResourceGroupName { get; set; }
55+
56+
[Parameter(
57+
Mandatory = true,
58+
HelpMessage = "Location of the network watcher.",
59+
ParameterSetName = "SetByLocation")]
60+
[LocationCompleter("Microsoft.Network/networkWatchers")]
61+
[ValidateNotNull]
62+
public string Location { get; set; }
63+
64+
[Parameter(
65+
Mandatory = true,
66+
ValueFromPipelineByPropertyName = true,
67+
HelpMessage = "Resource ID.",
68+
ParameterSetName = "SetByResourceId")]
69+
[ValidateNotNull]
70+
public string ResourceId { get; set; }
71+
72+
[Alias("FlowLogName")]
73+
[Parameter(
74+
Mandatory = false,
75+
HelpMessage = "The flow log name.",
76+
ParameterSetName = "SetByName")]
77+
[Parameter(
78+
Mandatory = false,
79+
HelpMessage = "The flow log name.",
80+
ParameterSetName = "SetByResource")]
81+
[Parameter(
82+
Mandatory = false,
83+
HelpMessage = "The flow log name.",
84+
ParameterSetName = "SetByLocation")]
85+
[ValidateNotNullOrEmpty]
86+
[SupportsWildcards]
87+
public string Name { get; set; }
88+
89+
public override void Execute()
90+
{
91+
base.Execute();
92+
93+
if (ParameterSetName.Contains("SetByResourceId"))
94+
{
95+
ResourceIdentifier flowLogInfo = new ResourceIdentifier(this.ResourceId);
96+
97+
this.Name = flowLogInfo.ResourceName;
98+
this.ResourceGroupName = flowLogInfo.ResourceGroupName;
99+
100+
string parent = flowLogInfo.ParentResource;
101+
string[] tokens = parent.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
102+
103+
this.NetworkWatcherName = tokens[1];
104+
}
105+
else if (ParameterSetName.Contains("SetByResource"))
106+
{
107+
this.ResourceGroupName = this.NetworkWatcher.ResourceGroupName;
108+
this.NetworkWatcherName = this.NetworkWatcher.Name;
109+
}
110+
else if (ParameterSetName.Contains("SetByLocation"))
111+
{
112+
var networkWatcher = this.GetNetworkWatcherByLocation(this.Location);
113+
114+
if (networkWatcher == null)
115+
{
116+
throw new PSArgumentException(Properties.Resources.NoNetworkWatcherFound);
117+
}
118+
119+
this.ResourceGroupName = NetworkBaseCmdlet.GetResourceGroup(networkWatcher.Id);
120+
this.NetworkWatcherName = networkWatcher.Name;
121+
}
122+
123+
if (ShouldGetByName(this.ResourceGroupName, this.Name))
124+
{
125+
MNM.FlowLog flowLogResult = this.FlowLogs.Get(this.ResourceGroupName, this.NetworkWatcherName, this.Name);
126+
WriteObject(NetworkResourceManagerProfile.Mapper.Map<PSFlowLogResource>(flowLogResult));
127+
}
128+
else
129+
{
130+
var flowLogResults = this.FlowLogs.List(this.ResourceGroupName, this.NetworkWatcherName);
131+
132+
List<PSFlowLogResource> flowLogs = new List<PSFlowLogResource>();
133+
foreach (var flowLogResult in flowLogResults)
134+
{
135+
flowLogs.Add(NetworkResourceManagerProfile.Mapper.Map<PSFlowLogResource>(flowLogResult));
136+
}
137+
138+
WriteObject(SubResourceWildcardFilter(this.Name, flowLogs), true);
139+
}
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)