Skip to content

Commit d1eacad

Browse files
authored
support subscription diagnosticsetting (Azure#14584)
* fix bug for parsing incorrect ResourceID * support subscription diagnosticsettings * add license * update psd1 * generate help * add tests * typo * polish exception message * add default parameterset * suppress breaking change message * fix test * add more example for help markdown * typo in changelog * update exceptions * fix test cases * polish code * fix positional parameter * polish code * update help markdown * polish help markdown
1 parent 589d177 commit d1eacad

21 files changed

+410
-91
lines changed

src/Monitor/Monitor.Test/Diagnostics/SetDiagnosticSettingCommandTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public void DisableWorkspace()
313313
[Trait(Category.AcceptanceType, Category.CheckIn)]
314314
public void InputObjectTest()
315315
{
316-
DiagnosticSettingsResource expectedSettings = GetDefaultSetting(id: "nothing/diagnosticSettings/");
316+
DiagnosticSettingsResource expectedSettings = GetDefaultSetting(id: "nothing/providers/microsoft.insights/diagnosticSettings/");
317317

318318
cmdlet.InputObject =new OutputClasses.PSServiceDiagnosticSettings(expectedSettings);
319319
cmdlet.ResourceId = resourceId;
@@ -322,7 +322,7 @@ public void InputObjectTest()
322322
// VerifyCalledOnce();
323323
VerifySettings(expectedSettings, this.calledSettings);
324324

325-
expectedSettings = GetDefaultSetting(name: "myName", id: "nothing/diagnosticSettings/");
325+
expectedSettings = GetDefaultSetting(name: "myName", id: "nothing/providers/microsoft.insights/diagnosticSettings/");
326326
cmdlet.InputObject = new OutputClasses.PSServiceDiagnosticSettings(expectedSettings);
327327
cmdlet.ResourceId = resourceId;
328328
cmdlet.Name = "myName";

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,12 @@ public void TestGetAzDiagnosticSettingCategory()
9191
{
9292
TestsController.NewInstance.RunPsTest(_logger, "Test-GetAzDiagnosticSettingCategory");
9393
}
94+
95+
[Fact]
96+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
97+
public void TestSubscriptionDiagnosticSetting()
98+
{
99+
TestsController.NewInstance.RunPsTest(_logger, "Test-SubscriptionDiagnosticSetting");
100+
}
94101
}
95102
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,25 @@ function Test-GetAzDiagnosticSettingCategory
331331
}
332332
}
333333

334+
<#
335+
.SYNOPSIS
336+
This is a live only test and example of how it could be tested.
337+
#>
338+
function Test-SubscriptionDiagnosticSetting
339+
{
340+
$list = @()
341+
Get-AzSubscriptionDiagnosticSettingCategory | ForEach-Object {
342+
$list += (New-AzDiagnosticDetailSetting -Log -Category $_.Name -Enabled)
343+
}
344+
345+
$DiagnosticSettingName = 'setting' + (getAssetName)
346+
$SubscriptionId = 'please use your subscription Id here'
347+
$WorkspaceId = 'please use your workspace Id here'
348+
$setting = New-AzDiagnosticSetting -Name $DiagnosticSettingName -SubscriptionId $SubscriptionId -WorkspaceId $WorkspaceId -Setting $list
349+
Set-AzDiagnosticSetting -InputObject $setting
334350

351+
Get-AzDiagnosticSetting -Name $DiagnosticSettingName -SubscriptionId $SubscriptionId
352+
353+
Remove-AzDiagnosticDetailSetting -Name $DiagnosticSettingName -SubscriptionId $SubscriptionId
354+
}
335355
# TODO add more complicated scenarios after we have a definitive subscription

src/Monitor/Monitor/Az.Monitor.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ CmdletsToExport = 'Get-AzMetricDefinition', 'Get-AzMetric', 'Remove-AzLogProfile
7878
'Get-AzLogProfile', 'Add-AzLogProfile', 'Get-AzActivityLog',
7979
'New-AzDiagnosticSetting', 'New-AzDiagnosticDetailSetting',
8080
'Set-AzDiagnosticSetting', 'Get-AzDiagnosticSetting',
81-
'Get-AzDiagnosticSettingCategory', 'Remove-AzDiagnosticSetting',
81+
'Get-AzDiagnosticSettingCategory', 'Get-AzSubscriptionDiagnosticSettingCategory',
82+
'Remove-AzDiagnosticSetting',
8283
'New-AzAutoscaleNotification', 'New-AzAutoscaleProfile',
8384
'New-AzAutoscaleRule', 'Add-AzAutoscaleSetting',
8485
'Get-AzAutoscaleHistory', 'Get-AzAutoscaleSetting',

src/Monitor/Monitor/ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Added cmdlet to get diagnostic setting categories for subscription
23+
- `Get-AzSubscriptionDiagnosticSettingCategory`
24+
* Supported subscription diagnostic setting operations with new parameter: SubscriptionId
25+
- 'Get-AzDiagnosticSetting'
26+
- 'New-AzDiagnosticSetting'
27+
- 'Remove-AzDiagnosticSetting'
2228

2329
## Version 2.4.0
2430
* Added cmdlets for data collection rules:
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.Management.Automation;
16+
using Microsoft.Azure.Commands.Common.Exceptions;
17+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
18+
19+
namespace Microsoft.Azure.Commands.Insights.Diagnostics
20+
{
21+
public abstract class DiagnosticSettingCommandBase : ManagementCmdletBase
22+
{
23+
protected const string ResourceIdParameterSet = "ResourceIdParameterSet";
24+
protected const string SubscriptionIdParameterSet = "SubscriptionIdParameterSet";
25+
26+
[Parameter(ParameterSetName = ResourceIdParameterSet, Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource id")]
27+
[ValidateNotNullOrEmpty]
28+
[Alias("TargetResourceId")]
29+
public string ResourceId { get; set; }
30+
31+
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription id")]
32+
[ValidateNotNullOrEmpty]
33+
public string SubscriptionId { get; set; }
34+
35+
protected virtual void Validate()
36+
{
37+
if (this.IsParameterBound(c => c.SubscriptionId) && !Utilities.IsGuid(this.SubscriptionId))
38+
{
39+
throw new AzPSArgumentException("Please specify a valid subscription Id", "SubscriptionId");
40+
}
41+
42+
if (this.IsParameterBound(c => c.ResourceId) && Utilities.IsGuid(this.ResourceId))
43+
{
44+
throw new AzPSArgumentException("Please use parameter 'SubscriptionId' instead of 'ResourceId'", "ResourceId");
45+
}
46+
}
47+
48+
private string GetSubscription()
49+
{
50+
return string.Format("/subscriptions/{0}", this.SubscriptionId);
51+
}
52+
53+
protected string GetTargetUri()
54+
{
55+
return this.IsParameterBound(c => c.SubscriptionId) ? GetSubscription() : this.ResourceId;
56+
}
57+
}
58+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.Collections.Generic;
16+
using System.Linq;
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+
22+
namespace Microsoft.Azure.Commands.Insights.Diagnostics
23+
{
24+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SubscriptionDiagnosticSettingCategory"), OutputType(typeof(PSSubscriptionDiagnosticSettingCategory))]
25+
public class GetAzureRmSubscriptionDiagnosticSettingCategoryCommand : ManagementCmdletBase
26+
{
27+
protected override void ProcessRecordInternal()
28+
{
29+
IEnumerable<LocalizableString> rawData = this.MonitorManagementClient.EventCategories.List();
30+
WriteObject(rawData.Select(value => new PSSubscriptionDiagnosticSettingCategory(value.Value, PSDiagnosticSettingCategoryType.Logs)).ToList(), true); ;
31+
}
32+
}
33+
}

src/Monitor/Monitor/Diagnostics/GetAzureRmDiagnosticSettingCategoryCommand.cs

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

15-
using System;
16-
using System.Collections.Generic;
1715
using System.Linq;
1816
using System.Management.Automation;
1917
using Microsoft.Azure.Commands.Insights.OutputClasses;
20-
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
2118
using Microsoft.Azure.Management.Monitor;
22-
using Microsoft.Azure.Management.Monitor.Models;
2319
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2420

2521
namespace Microsoft.Azure.Commands.Insights.Diagnostics

src/Monitor/Monitor/Diagnostics/GetAzureRmDiagnosticSettingCommand.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.Azure.Commands.Insights.OutputClasses;
1616
using Microsoft.Azure.Management.Monitor;
1717
using Microsoft.Azure.Management.Monitor.Models;
18+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1819
using System.Collections.Generic;
1920
using System.Linq;
2021
using System.Management.Automation;
@@ -24,19 +25,12 @@ namespace Microsoft.Azure.Commands.Insights.Diagnostics
2425
/// <summary>
2526
/// Gets the logs and metrics for the resource.
2627
/// </summary>
27-
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "DiagnosticSetting"), OutputType(typeof(PSServiceDiagnosticSettings))]
28-
public class GetAzureRmDiagnosticSettingCommand : ManagementCmdletBase
28+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "DiagnosticSetting", DefaultParameterSetName = ResourceIdParameterSet), OutputType(typeof(PSServiceDiagnosticSettings))]
29+
public class GetAzureRmDiagnosticSettingCommand : DiagnosticSettingCommandBase
2930
{
3031

3132
#region Parameters declarations
3233

33-
/// <summary>
34-
/// Gets or sets the resourceId parameter of the cmdlet
35-
/// </summary>
36-
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The ResourceId")]
37-
[ValidateNotNullOrEmpty]
38-
public string ResourceId { get; set; }
39-
4034
/// <summary>
4135
/// Gets or sets the diagnostics setting name parameter of the cmdlet
4236
/// </summary>
@@ -48,17 +42,19 @@ public class GetAzureRmDiagnosticSettingCommand : ManagementCmdletBase
4842

4943
protected override void ProcessRecordInternal()
5044
{
45+
Validate();
46+
5147
IList<PSServiceDiagnosticSettings> output;
5248
if (string.IsNullOrWhiteSpace(this.Name))
5349
{
5450
// Temporary service name constant provided for backwards compatibility
55-
IList<DiagnosticSettingsResource> results = this.MonitorManagementClient.DiagnosticSettings.List(resourceUri: this.ResourceId).Value;
51+
IList<DiagnosticSettingsResource> results = this.MonitorManagementClient.DiagnosticSettings.List(resourceUri: GetTargetUri()).Value;
5652
output = results.Select(e => new PSServiceDiagnosticSettings(e)).ToList();
5753
}
5854
else
5955
{
6056
// Temporary service name constant provided for backwards compatibility
61-
DiagnosticSettingsResource result = this.MonitorManagementClient.DiagnosticSettings.Get(resourceUri: this.ResourceId, name: this.Name);
57+
DiagnosticSettingsResource result = this.MonitorManagementClient.DiagnosticSettings.Get(resourceUri: GetTargetUri(), name: this.Name);
6258
output = new List<PSServiceDiagnosticSettings> { new PSServiceDiagnosticSettings(result) };
6359
}
6460
WriteObject(sendToPipeline: output, enumerateCollection: true);

src/Monitor/Monitor/Diagnostics/NewAzureRmDiagnosticSettingCommand.cs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,18 @@
1515
using System;
1616
using System.Collections.Generic;
1717
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.Common.Exceptions;
1819
using Microsoft.Azure.Commands.Insights.OutputClasses;
19-
using Microsoft.Azure.Management.Monitor;
2020
using Microsoft.Azure.Management.Monitor.Models;
2121
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2222

2323
namespace Microsoft.Azure.Commands.Insights.Diagnostics
2424
{
25-
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "DiagnosticSetting"), OutputType(typeof(PSServiceDiagnosticSettings))]
26-
public class NewAzureRmDiagnosticSettingCommand : ManagementCmdletBase
25+
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "DiagnosticSetting", DefaultParameterSetName = ResourceIdParameterSet), OutputType(typeof(PSServiceDiagnosticSettings))]
26+
public class NewAzureRmDiagnosticSettingCommand : DiagnosticSettingCommandBase
2727
{
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-
3728
#region Parameters declarations
3829

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>
4930
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the diagnostic setting. Defaults to 'service'")]
5031
[ValidateNotNullOrEmpty]
5132
public string Name { get; set; } = "service";
@@ -83,7 +64,7 @@ public class NewAzureRmDiagnosticSettingCommand : ManagementCmdletBase
8364
/// <summary>
8465
/// Gets or sets the retention in days
8566
/// </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)")]
67+
[Parameter(ParameterSetName = ResourceIdParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The value indicating whether to export (to ODS) to resource-specific (if present) or to AzureDiagnostics (default, not present)")]
8768
public SwitchParameter DedicatedLogAnalyticsDestinationType { get; set; }
8869

8970
/// <summary>
@@ -98,8 +79,8 @@ protected override void ProcessRecordInternal()
9879
{
9980
Validate();
10081

101-
IList<MetricSettings> metrics = new List<MetricSettings>();
102-
IList<LogSettings> logs = new List<LogSettings>();
82+
IList<MetricSettings> metrics = null;
83+
IList<LogSettings> logs = null;
10384

10485
if (this.IsParameterBound(c => c.Setting) && Setting.Length != 0)
10586
{
@@ -108,9 +89,17 @@ protected override void ProcessRecordInternal()
10889
switch (setting.CategoryType)
10990
{
11091
case PSDiagnosticSettingCategoryType.Metrics:
92+
if (metrics == null)
93+
{
94+
metrics = new List<MetricSettings>();
95+
}
11196
metrics.Add(((PSMetricSettings)setting).GetMetricSetting());
11297
break;
11398
case PSDiagnosticSettingCategoryType.Logs:
99+
if (logs == null)
100+
{
101+
logs = new List<LogSettings>();
102+
}
114103
logs.Add(((PSLogSettings)setting).GetLogSetting());
115104
break;
116105
default:
@@ -119,7 +108,7 @@ protected override void ProcessRecordInternal()
119108
}
120109
}
121110

122-
PSServiceDiagnosticSettings DiagnosticSetting = new PSServiceDiagnosticSettings(id: this.TargetResourceId + "/diagnosticSettings/" + this.Name, name: this.Name)
111+
PSServiceDiagnosticSettings DiagnosticSetting = new PSServiceDiagnosticSettings(id: GetTargetUri() + "/providers/microsoft.insights/diagnosticSettings/" + this.Name, name: this.Name)
123112
{
124113
StorageAccountId = this.IsParameterBound(c => c.StorageAccountId) ? this.StorageAccountId : null,
125114
ServiceBusRuleId = this.IsParameterBound(c => c.ServiceBusRuleId) ? this.ServiceBusRuleId : null,
@@ -134,20 +123,22 @@ protected override void ProcessRecordInternal()
134123
WriteObject(DiagnosticSetting);
135124
}
136125

137-
protected void Validate()
126+
protected override void Validate()
138127
{
128+
base.Validate();
129+
139130
if (!this.IsParameterBound(c => c.StorageAccountId) &&
140131
!this.IsParameterBound(c => c.ServiceBusRuleId) &&
141132
!this.IsParameterBound(c => c.EventHubName) &&
142133
!this.IsParameterBound(c => c.EventHubAuthorizationRuleId) &&
143134
!this.IsParameterBound(c => c.WorkspaceId))
144135
{
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");
136+
throw new AzPSInvalidOperationException("No operation is specified, please specify storage account Id/service bus rule Id/eventhub name/eventhub authorization rule Id/workspace Id");
146137
}
147138

148139
if (!this.IsParameterBound(c => c.WorkspaceId) && this.IsParameterBound(c => c.DedicatedLogAnalyticsDestinationType))
149140
{
150-
throw new ArgumentException("Please provide workspace Id if want to use dedicated log analytics destination type");
141+
throw new AzPSInvalidOperationException("Please provide workspace Id if want to use dedicated log analytics destination type");
151142
}
152143
}
153144
}

0 commit comments

Comments
 (0)