Skip to content

Commit fcf8ce4

Browse files
authored
Merge branch 'release-2020-12-08' into fix-autoscale-profile
2 parents 100d0d1 + 1b71552 commit fcf8ce4

23 files changed

+2110
-23
lines changed

src/Monitor/Monitor.Test/ScenarioTests/AzureRmDiagnosticSettingTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,12 @@ public void TestSetAzureRmDiagnosticSettingLogAnalytics()
8484
{
8585
TestsController.NewInstance.RunPsTest(_logger, "Test-SetAzureRmDiagnosticSetting-LogAnalytics");
8686
}
87+
88+
[Fact]
89+
[Trait(Category.AcceptanceType, Category.CheckIn)]
90+
public void TestGetAzDiagnosticSettingCategory()
91+
{
92+
TestsController.NewInstance.RunPsTest(_logger, "Test-GetAzDiagnosticSettingCategory");
93+
}
8794
}
8895
}

src/Monitor/Monitor.Test/ScenarioTests/AzureRmDiagnosticSettingTests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,36 @@ function Test-SetAzureRmDiagnosticSetting-LogAnalytics
300300
}
301301
}
302302

303+
<#
304+
.SYNOPSIS
305+
Test Get diagnostic setting supported categories
306+
#>
307+
function Test-GetAzDiagnosticSettingCategory
308+
{
309+
$ResourceGroupName = 'group' + (getAssetName)
310+
$SubnetConfigName = 'config' + (getAssetName)
311+
$VNetName = 'vn' + (getAssetName)
312+
313+
try
314+
{
315+
$rg = New-AzResourceGroup -Name $ResourceGroupName -Location 'eastus'
316+
$subnet = New-AzVirtualNetworkSubnetConfig -Name $SubnetConfigName -AddressPrefix "10.0.1.0/24"
317+
$vn = New-AzVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -Location 'eastus' -AddressPrefix "10.0.0.0/16" -Subnet $subnet
303318

319+
$id = $vn.Id
320+
$log = Get-AzDiagnosticSettingCategory -TargetResourceId $id -Name 'VMProtectionAlerts'
321+
$metric = Get-AzDiagnosticSettingCategory -TargetResourceId $id -Name 'AllMetrics'
322+
323+
Assert-AreEqual 'Logs' $log.CategoryType
324+
Assert-AreEqual 'Metrics' $metric.CategoryType
325+
326+
Remove-AzVirtualNetwork -ResourceGroupName $ResourceGroupName -Name $VNetName -Force
327+
}
328+
finally
329+
{
330+
Remove-AzResourceGroup -Name $ResourceGroupName
331+
}
332+
}
304333

305334

306335
# TODO add more complicated scenarios after we have a definitive subscription

src/Monitor/Monitor.Test/SessionRecords/Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests/TestGetAzDiagnosticSettingCategory.json

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

src/Monitor/Monitor/Az.Monitor.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ FunctionsToExport = @()
7676
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
7777
CmdletsToExport = 'Get-AzMetricDefinition', 'Get-AzMetric', 'Remove-AzLogProfile',
7878
'Get-AzLogProfile', 'Add-AzLogProfile', 'Get-AzActivityLog',
79+
'New-AzDiagnosticSetting',
80+
'New-AzDiagnosticDetailSetting',
7981
'Set-AzDiagnosticSetting', 'Get-AzDiagnosticSetting',
82+
'Get-AzDiagnosticSettingCategory',
8083
'Remove-AzDiagnosticSetting', 'New-AzAutoscaleNotification',
8184
'New-AzAutoscaleProfile', 'New-AzAutoscaleRule',
8285
'Add-AzAutoscaleSetting', 'Get-AzAutoscaleHistory',

src/Monitor/Monitor/ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
## Upcoming Release
2222
* Changed parameter `Rule` of `New-AzAutoscaleProfile` to accept empty list. [#12903]
23+
* Added new cmdlets to support creating diagnostic settings more flexible:
24+
* `Get-AzDiagnosticSettingCategory`
25+
* `New-AzDiagnosticSetting`
26+
* `New-AzDiagnosticDetailSetting`
2327

2428
## Version 2.2.0
2529
* Fixed the bug that warning message cannot be suppressed. [#12889]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Management.Automation;
19+
using Microsoft.Azure.Commands.Insights.OutputClasses;
20+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
21+
using Microsoft.Azure.Management.Monitor;
22+
using Microsoft.Azure.Management.Monitor.Models;
23+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
24+
25+
namespace Microsoft.Azure.Commands.Insights.Diagnostics
26+
{
27+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "DiagnosticSettingCategory"), OutputType(typeof(PSDiagnosticSettingCategory))]
28+
public class GetAzureRmDiagnosticSettingCategoryCommand : ManagementCmdletBase
29+
{
30+
#region Parameters declarations
31+
32+
[Parameter(Mandatory = true, HelpMessage = "Target resource Id")]
33+
[ValidateNotNullOrEmpty]
34+
public string TargetResourceId { get; set; }
35+
36+
[Parameter(Mandatory = false, HelpMessage = "Name of diagnostic setting category")]
37+
[ValidateNotNullOrEmpty]
38+
public string Name { get; set; }
39+
40+
#endregion
41+
42+
protected override void ProcessRecordInternal()
43+
{
44+
if (this.IsParameterBound(c => c.Name))
45+
{
46+
WriteObject(new PSDiagnosticSettingCategory(this.MonitorManagementClient
47+
.DiagnosticSettingsCategory
48+
.Get(TargetResourceId, Name)));
49+
}
50+
else
51+
{
52+
WriteObject(this.MonitorManagementClient
53+
.DiagnosticSettingsCategory
54+
.List(TargetResourceId)
55+
.Value
56+
.Select(x => new PSDiagnosticSettingCategory(x)).ToList(), true);
57+
}
58+
}
59+
}
60+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 System;
16+
using System.Collections.Generic;
17+
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.Insights.OutputClasses;
19+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
20+
using Microsoft.Azure.Management.Monitor;
21+
using Microsoft.Azure.Management.Monitor.Models;
22+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
23+
24+
namespace Microsoft.Azure.Commands.Insights.Diagnostics
25+
{
26+
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "DiagnosticDetailSetting", DefaultParameterSetName = MetricSettingParameterSet), OutputType(typeof(PSDiagnosticDetailSettings))]
27+
public class NewAzureRmDiagnosticDetailSettingCommand : ManagementCmdletBase
28+
{
29+
public const string LogSettingParameterSet = "LogSettingParameterSet";
30+
public const string MetricSettingParameterSet = "MetricSettingParameterSet";
31+
32+
#region Parameters declarations
33+
34+
[Parameter(ParameterSetName = LogSettingParameterSet, Mandatory = true, HelpMessage = "To create log setting")]
35+
public SwitchParameter Log { get; set; }
36+
37+
[Parameter(ParameterSetName = MetricSettingParameterSet, Mandatory = true, HelpMessage = "To create metric setting")]
38+
public SwitchParameter Metric { get; set; }
39+
40+
[Parameter(Mandatory = false, HelpMessage = "Retention days for retention policy")]
41+
public int RetentionInDays { get; set; }
42+
43+
[Parameter(Mandatory = false, HelpMessage = "Enable Retention policy")]
44+
public SwitchParameter RetentionEnabled { get; set; }
45+
46+
[Parameter(Mandatory = true, HelpMessage = "Category of setting")]
47+
public string Category { get; set; }
48+
49+
[Parameter(Mandatory = false, HelpMessage = "Enable the setting")]
50+
public SwitchParameter Enabled { get; set; }
51+
52+
[Parameter(ParameterSetName = MetricSettingParameterSet, Mandatory = false, HelpMessage = "TimeGrain for metric setting")]
53+
public System.TimeSpan? TimeGrain { get; set; }
54+
55+
#endregion
56+
57+
protected override void ProcessRecordInternal()
58+
{
59+
60+
61+
PSRetentionPolicy policy;
62+
if (!this.IsParameterBound(c => c.RetentionInDays) && !this.IsParameterBound(c => c.RetentionEnabled))
63+
{
64+
policy = null;
65+
}
66+
else
67+
{
68+
policy = new PSRetentionPolicy()
69+
{
70+
Days = this.IsParameterBound(c => c.RetentionInDays) ? this.RetentionInDays : 0,
71+
Enabled = this.IsParameterBound(c => RetentionEnabled) ? true : false
72+
};
73+
}
74+
75+
PSDiagnosticDetailSettings setting;
76+
77+
if (this.ParameterSetName.Equals(LogSettingParameterSet))
78+
{
79+
setting = new PSLogSettings()
80+
{
81+
RetentionPolicy = policy,
82+
Category = this.Category,
83+
Enabled = this.IsParameterBound(c => c.Enabled) ? true : false,
84+
CategoryType = PSDiagnosticSettingCategoryType.Logs
85+
};
86+
}
87+
else
88+
{
89+
setting = new PSMetricSettings()
90+
{
91+
RetentionPolicy = policy,
92+
Category = this.Category,
93+
Enabled = this.IsParameterBound(c => c.Enabled) ? true : false,
94+
TimeGrain = this.TimeGrain ?? default(System.TimeSpan),
95+
CategoryType = PSDiagnosticSettingCategoryType.Metrics
96+
};
97+
}
98+
99+
WriteObject(setting);
100+
}
101+
}
102+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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 System;
16+
using System.Collections.Generic;
17+
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.Insights.OutputClasses;
19+
using Microsoft.Azure.Management.Monitor;
20+
using Microsoft.Azure.Management.Monitor.Models;
21+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
22+
23+
namespace Microsoft.Azure.Commands.Insights.Diagnostics
24+
{
25+
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "DiagnosticSetting"), OutputType(typeof(PSServiceDiagnosticSettings))]
26+
public class NewAzureRmDiagnosticSettingCommand : ManagementCmdletBase
27+
{
28+
public const string StorageAccountIdParamName = "StorageAccountId";
29+
public const string ServiceBusRuleIdParamName = "ServiceBusRuleId";
30+
public const string EventHubNameParamName = "EventHubName";
31+
public const string EventHubRuleIdParamName = "EventHubAuthorizationRuleId";
32+
public const string WorkspacetIdParamName = "WorkspaceId";
33+
public const string EnabledParamName = "Enabled";
34+
public const string EnableLogParamName = "EnableLog";
35+
public const string EnableMetricsParamName = "EnableMetrics";
36+
37+
#region Parameters declarations
38+
39+
/// <summary>
40+
/// Gets or sets the resourceId parameter of the cmdlet
41+
/// </summary>
42+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource id")]
43+
[ValidateNotNullOrEmpty]
44+
public string TargetResourceId { get; set; }
45+
46+
/// <summary>
47+
/// Gets or sets the resourceId parameter of the cmdlet
48+
/// </summary>
49+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the diagnostic setting. Defaults to 'service'")]
50+
[ValidateNotNullOrEmpty]
51+
public string Name { get; set; } = "service";
52+
53+
/// <summary>
54+
/// Gets or sets the storage account parameter of the cmdlet
55+
/// </summary>
56+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The storage account id")]
57+
public string StorageAccountId { get; set; }
58+
59+
/// <summary>
60+
/// Gets or sets the service bus rule id parameter of the cmdlet
61+
/// </summary>
62+
[Parameter( Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The service bus rule id")]
63+
public string ServiceBusRuleId { get; set; }
64+
65+
/// <summary>
66+
/// Gets or sets the service bus rule id parameter of the cmdlet
67+
/// </summary>
68+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The service bus rule id")]
69+
public string EventHubName { get; set; }
70+
71+
/// <summary>
72+
/// Gets or sets the event hub authorization rule id parameter of the cmdlet
73+
/// </summary>
74+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The event hub rule id")]
75+
public string EventHubAuthorizationRuleId { get; set; }
76+
77+
/// <summary>
78+
/// Gets or sets the OMS workspace Id
79+
/// </summary>
80+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource Id of the Log Analytics workspace to send logs/metrics to")]
81+
public string WorkspaceId { get; set; }
82+
83+
/// <summary>
84+
/// Gets or sets the retention in days
85+
/// </summary>
86+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The value indicating whether to export (to ODS) to resource-specific (if present) or to AzureDiagnostics (default, not present)")]
87+
public SwitchParameter DedicatedLogAnalyticsDestinationType { get; set; }
88+
89+
/// <summary>
90+
/// Set list of diagnostic settings
91+
/// </summary>
92+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Metric settings or Log settings")]
93+
public PSDiagnosticDetailSettings[] Setting { get; set; }
94+
95+
#endregion
96+
97+
protected override void ProcessRecordInternal()
98+
{
99+
Validate();
100+
101+
IList<MetricSettings> metrics = new List<MetricSettings>();
102+
IList<LogSettings> logs = new List<LogSettings>();
103+
104+
if (this.IsParameterBound(c => c.Setting) && Setting.Length != 0)
105+
{
106+
foreach (PSDiagnosticDetailSettings setting in Setting)
107+
{
108+
switch (setting.CategoryType)
109+
{
110+
case PSDiagnosticSettingCategoryType.Metrics:
111+
metrics.Add(((PSMetricSettings)setting).GetMetricSetting());
112+
break;
113+
case PSDiagnosticSettingCategoryType.Logs:
114+
logs.Add(((PSLogSettings)setting).GetLogSetting());
115+
break;
116+
default:
117+
throw new ArgumentException("Invalid diagnostic setting type");
118+
}
119+
}
120+
}
121+
122+
PSServiceDiagnosticSettings DiagnosticSetting = new PSServiceDiagnosticSettings(id: this.TargetResourceId + "/diagnosticSettings/" + this.Name, name: this.Name)
123+
{
124+
StorageAccountId = this.IsParameterBound(c => c.StorageAccountId) ? this.StorageAccountId : null,
125+
ServiceBusRuleId = this.IsParameterBound(c => c.ServiceBusRuleId) ? this.ServiceBusRuleId : null,
126+
EventHubName = this.IsParameterBound(c => c.EventHubName) ? this.EventHubName : null,
127+
EventHubAuthorizationRuleId = this.IsParameterBound(c => c.EventHubAuthorizationRuleId) ? this.EventHubAuthorizationRuleId : null,
128+
WorkspaceId = this.IsParameterBound(c => c.WorkspaceId) ? this.WorkspaceId : null,
129+
LogAnalyticsDestinationType = this.IsParameterBound(c => c.DedicatedLogAnalyticsDestinationType) ? "Dedicated" : null,
130+
Metrics = metrics,
131+
Logs = logs
132+
};
133+
134+
WriteObject(DiagnosticSetting);
135+
}
136+
137+
protected void Validate()
138+
{
139+
if (!this.IsParameterBound(c => c.StorageAccountId) &&
140+
!this.IsParameterBound(c => c.ServiceBusRuleId) &&
141+
!this.IsParameterBound(c => c.EventHubName) &&
142+
!this.IsParameterBound(c => c.EventHubAuthorizationRuleId) &&
143+
!this.IsParameterBound(c => c.WorkspaceId))
144+
{
145+
throw new ArgumentException("No operation is specified, please specify storage account Id/service bus rule Id/eventhub name/eventhub authorization rule Id/workspace Id");
146+
}
147+
148+
if (!this.IsParameterBound(c => c.WorkspaceId) && this.IsParameterBound(c => c.DedicatedLogAnalyticsDestinationType))
149+
{
150+
throw new ArgumentException("Please provide workspace Id if want to use dedicated log analytics destination type");
151+
}
152+
}
153+
}
154+
}

0 commit comments

Comments
 (0)