Skip to content

Commit 397ea87

Browse files
committed
[Insights] Use explicit null value to disable data sink in diagnosticsettings
1 parent 333d27c commit 397ea87

File tree

4 files changed

+30
-166
lines changed

4 files changed

+30
-166
lines changed

src/ResourceManager/Insights/Commands.Insights.Test/Diagnostics/SetDiagnosticSettingCommandTests.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public SetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper out
5151
CommandRuntime = commandRuntimeMock.Object,
5252
InsightsManagementClient = insightsManagementClientMock.Object
5353
};
54-
54+
5555
this.ExistingSetting = GetDefaultSetting();
5656

5757
insightsDiagnosticsOperationsMock.Setup(f => f.GetWithHttpMessagesAsync(
@@ -86,7 +86,7 @@ public SetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper out
8686
[Trait(Category.AcceptanceType, Category.CheckIn)]
8787
public void DisableStorage()
8888
{
89-
cmdlet.DisableStorage = new SwitchParameter(true);
89+
cmdlet.MyInvocation.BoundParameters["StorageAccountId"] = null;
9090
cmdlet.ExecuteCmdlet();
9191

9292
ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting();
@@ -102,6 +102,7 @@ public void SetStorage()
102102
{
103103
string newStorageId = "otherstorage";
104104
cmdlet.StorageAccountId = newStorageId;
105+
cmdlet.MyInvocation.BoundParameters[SetAzureRmDiagnosticSettingCommand.StorageAccountIdParamName] = newStorageId;
105106
cmdlet.ExecuteCmdlet();
106107

107108
ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting();
@@ -117,6 +118,7 @@ public void SetServiceBus()
117118
{
118119
string newServiceBusId = "otherservicebus";
119120
cmdlet.ServiceBusRuleId = newServiceBusId;
121+
cmdlet.MyInvocation.BoundParameters[SetAzureRmDiagnosticSettingCommand.ServiceBusRuleIdParamName] = newServiceBusId;
120122
cmdlet.ExecuteCmdlet();
121123

122124
ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting();
@@ -132,6 +134,7 @@ public void SetWorkspace()
132134
{
133135
string newWorkspaceId = "otherworkspace";
134136
cmdlet.WorkspaceId = newWorkspaceId;
137+
cmdlet.MyInvocation.BoundParameters[SetAzureRmDiagnosticSettingCommand.WorkspacetIdParamName] = newWorkspaceId;
135138
cmdlet.ExecuteCmdlet();
136139

137140
ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting();
@@ -175,7 +178,7 @@ public void SetSomeTimeGrains()
175178
[Trait(Category.AcceptanceType, Category.CheckIn)]
176179
public void DisableEventHub()
177180
{
178-
cmdlet.DisableServiceBus = new SwitchParameter(true);
181+
cmdlet.MyInvocation.BoundParameters[SetAzureRmDiagnosticSettingCommand.ServiceBusRuleIdParamName] = null;
179182
cmdlet.ExecuteCmdlet();
180183

181184
ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting();
@@ -189,7 +192,7 @@ public void DisableEventHub()
189192
[Trait(Category.AcceptanceType, Category.CheckIn)]
190193
public void DisableWorkspace()
191194
{
192-
cmdlet.DisableWorkspace = new SwitchParameter(true);
195+
cmdlet.MyInvocation.BoundParameters["WorkspaceId"] = null;
193196
cmdlet.ExecuteCmdlet();
194197

195198
ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting();

src/ResourceManager/Insights/Commands.Insights/Diagnostics/SetAzureRmDiagnosticSettingCommand.cs

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Microsoft.Azure.Commands.Insights.OutputClasses;
2323
using Microsoft.Azure.Management.Insights;
2424
using Microsoft.Azure.Management.Insights.Models;
25+
using Newtonsoft.Json;
2526

2627
namespace Microsoft.Azure.Commands.Insights.Diagnostics
2728
{
@@ -31,6 +32,10 @@ namespace Microsoft.Azure.Commands.Insights.Diagnostics
3132
[Cmdlet(VerbsCommon.Set, "AzureRmDiagnosticSetting"), OutputType(typeof(PSServiceDiagnosticSettings))]
3233
public class SetAzureRmDiagnosticSettingCommand : ManagementCmdletBase
3334
{
35+
public const string StorageAccountIdParamName = "StorageAccountId";
36+
public const string ServiceBusRuleIdParamName = "ServiceBusRuleId";
37+
public const string WorkspacetIdParamName = "WorkspaceId";
38+
3439
#region Parameters declarations
3540

3641
/// <summary>
@@ -40,39 +45,16 @@ public class SetAzureRmDiagnosticSettingCommand : ManagementCmdletBase
4045
[ValidateNotNullOrEmpty]
4146
public string ResourceId { get; set; }
4247

43-
/// <summary>
44-
/// Gets or sets the switch for storage parameter of the cmdlet
45-
/// </summary>
46-
[Parameter(Mandatory = false, HelpMessage = "Disable storage")]
47-
[ValidateNotNullOrEmpty]
48-
public SwitchParameter DisableStorage { get; set; }
49-
50-
/// <summary>
51-
/// Gets or sets the switch for eventhub parameter of the cmdlet
52-
/// </summary>
53-
[Parameter(Mandatory = false, HelpMessage = "Disable service bus")]
54-
[ValidateNotNullOrEmpty]
55-
public SwitchParameter DisableServiceBus { get; set; }
56-
57-
/// <summary>
58-
/// Gets or sets the switch for workspace parameter of the cmdlet
59-
/// </summary>
60-
[Parameter(Mandatory = false, HelpMessage = "Disable workspace")]
61-
[ValidateNotNullOrEmpty]
62-
public SwitchParameter DisableWorkspace { get; set; }
63-
6448
/// <summary>
6549
/// Gets or sets the storage account parameter of the cmdlet
6650
/// </summary>
6751
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The storage account id")]
68-
[ValidateNotNullOrEmpty]
6952
public string StorageAccountId { get; set; }
7053

7154
/// <summary>
7255
/// Gets or sets the service bus rule id parameter of the cmdlet
7356
/// </summary>
7457
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The service bus rule id")]
75-
[ValidateNotNullOrEmpty]
7658
public string ServiceBusRuleId { get; set; }
7759

7860
/// <summary>
@@ -107,7 +89,6 @@ public class SetAzureRmDiagnosticSettingCommand : ManagementCmdletBase
10789
/// Gets or sets the OMS workspace Id
10890
/// </summary>
10991
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource Id of the Log Analytics workspace to send logs/metrics to")]
110-
[ValidateNotNullOrEmpty]
11192
public string WorkspaceId { get; set; }
11293

11394
/// <summary>
@@ -118,14 +99,23 @@ public class SetAzureRmDiagnosticSettingCommand : ManagementCmdletBase
11899

119100
#endregion
120101

102+
private bool isStorageParamPresent;
103+
104+
private bool isServiceBusParamPresent;
105+
106+
private bool isWorkspaceParamPresent;
107+
121108
protected override void ProcessRecordInternal()
122109
{
123-
if (!DisableStorage &&
124-
!DisableServiceBus &&
125-
!DisableWorkspace &&
126-
string.IsNullOrWhiteSpace(this.StorageAccountId) &&
127-
string.IsNullOrWhiteSpace(this.ServiceBusRuleId) &&
128-
string.IsNullOrWhiteSpace(this.WorkspaceId) &&
110+
HashSet<string> usedParams = new HashSet<string>(this.MyInvocation.BoundParameters.Keys, StringComparer.OrdinalIgnoreCase);
111+
112+
this.isStorageParamPresent = usedParams.Contains(StorageAccountIdParamName);
113+
this.isServiceBusParamPresent = usedParams.Contains(ServiceBusRuleIdParamName);
114+
this.isWorkspaceParamPresent = usedParams.Contains(WorkspacetIdParamName);
115+
116+
if (!this.isStorageParamPresent &&
117+
!this.isServiceBusParamPresent &&
118+
!this.isWorkspaceParamPresent &&
129119
!this.Enabled.HasValue)
130120
{
131121
throw new ArgumentException("No operation is specified");
@@ -265,53 +255,26 @@ private void SetAllCategoriesAndTimegrains(ServiceDiagnosticSettingsResource pro
265255

266256
private void SetWorkspace(ServiceDiagnosticSettingsResource properties)
267257
{
268-
if (!string.IsNullOrWhiteSpace(this.WorkspaceId))
258+
if (this.isWorkspaceParamPresent)
269259
{
270-
if (this.DisableWorkspace)
271-
{
272-
throw new ArgumentException("WorkspaceId and DisableWorkspace cannot be both present.");
273-
}
274-
275260
properties.WorkspaceId = this.WorkspaceId;
276261
}
277-
else if (this.DisableWorkspace)
278-
{
279-
properties.WorkspaceId = null;
280-
}
281262
}
282263

283264
private void SetServiceBus(ServiceDiagnosticSettingsResource properties)
284265
{
285-
if (!string.IsNullOrWhiteSpace(this.ServiceBusRuleId))
266+
if (this.isServiceBusParamPresent)
286267
{
287-
if (this.DisableServiceBus)
288-
{
289-
throw new ArgumentException("ServiceBusId and DisableServiceBus cannot be both present.");
290-
}
291-
292268
properties.ServiceBusRuleId = this.ServiceBusRuleId;
293269
}
294-
else if (this.DisableServiceBus)
295-
{
296-
properties.ServiceBusRuleId = null;
297-
}
298270
}
299271

300272
private void SetStorage(ServiceDiagnosticSettingsResource properties)
301273
{
302-
if (!string.IsNullOrWhiteSpace(this.StorageAccountId))
274+
if (this.isStorageParamPresent)
303275
{
304-
if (this.DisableStorage)
305-
{
306-
throw new ArgumentException("StorageAccountId and DisableStorage cannot be both present.");
307-
}
308-
309276
properties.StorageAccountId = this.StorageAccountId;
310277
}
311-
else if (this.DisableStorage)
312-
{
313-
properties.StorageAccountId = null;
314-
}
315278
}
316279
}
317280
}

src/ResourceManager/Insights/Commands.Insights/Microsoft.Azure.Commands.Insights.dll-Help.xml

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7002,27 +7002,6 @@ $rule2 = New-AzureRmAutoscaleRule -MetricName Requests -MetricResourceId &quot;/
70027002
</maml:description>
70037003
<command:parameterValue required="true" variableLength="false">String</command:parameterValue>
70047004
</command:parameter>
7005-
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false" position="named">
7006-
<maml:name>DisableStorage</maml:name>
7007-
<maml:description>
7008-
<maml:para>Disable storage in the diagnostic settings.</maml:para>
7009-
</maml:description>
7010-
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
7011-
</command:parameter>
7012-
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false" position="named">
7013-
<maml:name>DisableServiceBus</maml:name>
7014-
<maml:description>
7015-
<maml:para>Disable service bus in the diagnostic settings.</maml:para>
7016-
</maml:description>
7017-
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
7018-
</command:parameter>
7019-
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false" position="named">
7020-
<maml:name>DisableWorkspace</maml:name>
7021-
<maml:description>
7022-
<maml:para>Disable workspace in the diagnostic settings.</maml:para>
7023-
</maml:description>
7024-
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
7025-
</command:parameter>
70267005
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="true (ByPropertyName)" position="named">
70277006
<maml:name>StorageAccountId</maml:name>
70287007
<maml:description>
@@ -7108,42 +7087,6 @@ $rule2 = New-AzureRmAutoscaleRule -MetricName Requests -MetricResourceId &quot;/
71087087
</dev:type>
71097088
<dev:defaultValue></dev:defaultValue>
71107089
</command:parameter>
7111-
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="true (ByPropertyName)" position="named">
7112-
<maml:name>DisableStorage</maml:name>
7113-
<maml:description>
7114-
<maml:para>Disable storage in the diagnostic settings.</maml:para>
7115-
</maml:description>
7116-
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
7117-
<dev:type>
7118-
<maml:name>SwitchParameter</maml:name>
7119-
<maml:uri/>
7120-
</dev:type>
7121-
<dev:defaultValue></dev:defaultValue>
7122-
</command:parameter>
7123-
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="true (ByPropertyName)" position="named">
7124-
<maml:name>DisableServiceBus</maml:name>
7125-
<maml:description>
7126-
<maml:para>Disable service bus in the diagnostic settings.</maml:para>
7127-
</maml:description>
7128-
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
7129-
<dev:type>
7130-
<maml:name>SwitchParameter</maml:name>
7131-
<maml:uri/>
7132-
</dev:type>
7133-
<dev:defaultValue></dev:defaultValue>
7134-
</command:parameter>
7135-
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="true (ByPropertyName)" position="named">
7136-
<maml:name>DisableWorkspace</maml:name>
7137-
<maml:description>
7138-
<maml:para>Disable workspace in the diagnostic settings.</maml:para>
7139-
</maml:description>
7140-
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
7141-
<dev:type>
7142-
<maml:name>SwitchParameter</maml:name>
7143-
<maml:uri/>
7144-
</dev:type>
7145-
<dev:defaultValue></dev:defaultValue>
7146-
</command:parameter>
71477090
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="true (ByPropertyName)" position="named">
71487091
<maml:name>StorageAccountId</maml:name>
71497092
<maml:description>

src/ResourceManager/Insights/Commands.Insights/help/Set-AzureRmDiagnosticSetting.md

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -89,51 +89,6 @@ Accept pipeline input: True (ByPropertyName)
8989
Accept wildcard characters: False
9090
```
9191
92-
### -DisableStorage
93-
Disable storage in the diagnostic settings.
94-
95-
```yaml
96-
Type: SwitchParameter
97-
Parameter Sets: (All)
98-
Aliases:
99-
100-
Required: True
101-
Position: Named
102-
Default value: None
103-
Accept pipeline input: False
104-
Accept wildcard characters: False
105-
```
106-
107-
### -DisableServiceBus
108-
Disable service bus in the diagnostic settings.
109-
110-
```yaml
111-
Type: SwitchParameter
112-
Parameter Sets: (All)
113-
Aliases:
114-
115-
Required: True
116-
Position: Named
117-
Default value: None
118-
Accept pipeline input: False
119-
Accept wildcard characters: False
120-
```
121-
122-
### -DisableWorkspace
123-
Disable workspace in the diagnostic settings.
124-
125-
```yaml
126-
Type: SwitchParameter
127-
Parameter Sets: (All)
128-
Aliases:
129-
130-
Required: True
131-
Position: Named
132-
Default value: None
133-
Accept pipeline input: False
134-
Accept wildcard characters: False
135-
```
136-
13792
### -StorageAccountId
13893
Specifies the ID of the Storage account in which to save the data.
13994

0 commit comments

Comments
 (0)