Skip to content

Commit 7dab610

Browse files
authored
Merge pull request #7619 from gucalder/preview
[Insights] Make sure the diagnostic settings can be added without explicitly specifying the categories. (Issue #7513)
2 parents 04b17b6 + 193c925 commit 7dab610

File tree

8 files changed

+282
-7
lines changed

8 files changed

+282
-7
lines changed

src/ResourceManager/Insights/Commands.Insights.Test/Commands.Insights.Test.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@
156156
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AutoscaleTests\TestGetAzureRmAutoscaleSettingByName.json">
157157
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
158158
</None>
159+
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestSetAzureRmDiagnosticSettingCreate.json">
160+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
161+
</None>
159162
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestSetAzureRmDiagnosticSettingWithRetention.json">
160163
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
161164
</None>
@@ -189,7 +192,7 @@
189192
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestSetAzureRmDiagnosticSettingCategoriesOnly.json">
190193
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
191194
</None>
192-
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestSetAzureRmDiagnosticSetting.json">
195+
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestSetAzureRmDiagnosticSettingUpdate.json">
193196
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
194197
</None>
195198
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestGetAzureRmDiagnosticSetting.json">

src/ResourceManager/Insights/Commands.Insights.Test/ScenarioTests/AzureRmDiagnosticSettingTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ public void TestGetAzureRmDiagnosticSetting()
3838

3939
[Fact]
4040
[Trait(Category.AcceptanceType, Category.CheckIn)]
41-
public void TestSetAzureRmDiagnosticSetting()
41+
public void TestSetAzureRmDiagnosticSettingCreate()
4242
{
43-
TestsController.NewInstance.RunPsTest(_logger, "Test-SetAzureRmDiagnosticSetting");
43+
TestsController.NewInstance.RunPsTest(_logger, "Test-SetAzureRmDiagnosticSettingCreate");
44+
}
45+
46+
[Fact]
47+
[Trait(Category.AcceptanceType, Category.CheckIn)]
48+
public void TestSetAzureRmDiagnosticSettingUpdate()
49+
{
50+
TestsController.NewInstance.RunPsTest(_logger, "Test-SetAzureRmDiagnosticSettingUpdate");
4451
}
4552

4653
[Fact] //(Skip = "TODO: fixing this test after introducing Swagger specs")]

src/ResourceManager/Insights/Commands.Insights.Test/ScenarioTests/AzureRmDiagnosticSettingTests.ps1

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function Test-GetAzureRmDiagnosticSetting
6969
.SYNOPSIS
7070
Tests setting diagnostics
7171
#>
72-
function Test-SetAzureRmDiagnosticSetting
72+
function Test-SetAzureRmDiagnosticSettingUpdate
7373
{
7474
try
7575
{
@@ -95,6 +95,33 @@ function Test-SetAzureRmDiagnosticSetting
9595
}
9696
}
9797

98+
<#
99+
.SYNOPSIS
100+
Tests setting diagnostics
101+
#>
102+
function Test-SetAzureRmDiagnosticSettingCreate
103+
{
104+
try
105+
{
106+
# assumes there is no diagnostic settting called service for the given resource
107+
$actual = Set-AzureRmDiagnosticSetting -ResourceId /subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourceGroups/system-sas-test/providers/Microsoft.KeyVault/vaults/myCanaryKeyVaultCUSEAUP -StorageAccountId /subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourceGroups/vnet-east-test/providers/Microsoft.Storage/storageAccounts/vnetcnarytestcuseuap2 -Enabled $true
108+
109+
Assert-AreEqual $actual.StorageAccountId "/subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourceGroups/vnet-east-test/providers/Microsoft.Storage/storageAccounts/vnetcnarytestcuseuap2"
110+
Assert-AreEqual 1 $actual.Metrics.Count
111+
Assert-AreEqual $true $actual.Metrics[0].Enabled
112+
Assert-AreEqual "AllMetrics" $actual.Metrics[0].Category
113+
Assert-AreEqual 1 $actual.Logs.Count
114+
Assert-AreEqual $true $actual.Logs[0].Enabled
115+
Assert-AreEqual "AuditEvent" $actual.Logs[0].Category
116+
Assert-AreEqual "service" $actual.Name
117+
}
118+
finally
119+
{
120+
# Cleanup
121+
# No cleanup needed for now
122+
}
123+
}
124+
98125
<#
99126
.SYNOPSIS
100127
Tests setting diagnostics
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
{
2+
"Entries": [
3+
{
4+
"RequestUri": "//subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourceGroups/system-sas-test/providers/Microsoft.KeyVault/vaults/myCanaryKeyVaultCUSEAUP/providers/microsoft.insights/diagnosticSettings?api-version=2017-05-01-preview",
5+
"EncodedRequestUri": "Ly9zdWJzY3JpcHRpb25zLzljZjdjYzBhLTBiYTEtNDYyNC1iYzgyLTk3ZTFlZTI1ZGM0NS9yZXNvdXJjZUdyb3Vwcy9zeXN0ZW0tc2FzLXRlc3QvcHJvdmlkZXJzL01pY3Jvc29mdC5LZXlWYXVsdC92YXVsdHMvbXlDYW5hcnlLZXlWYXVsdENVU0VBVVAvcHJvdmlkZXJzL21pY3Jvc29mdC5pbnNpZ2h0cy9kaWFnbm9zdGljU2V0dGluZ3M/YXBpLXZlcnNpb249MjAxNy0wNS0wMS1wcmV2aWV3",
6+
"RequestMethod": "GET",
7+
"RequestBody": "",
8+
"RequestHeaders": {
9+
"x-ms-client-request-id": [
10+
"a4795193-7f5a-4864-9c8b-406fbf646b34"
11+
],
12+
"accept-language": [
13+
"en-US"
14+
],
15+
"User-Agent": [
16+
"FxVersion/4.7.3163.0",
17+
"OSName/WindowsServer2012R2Standard",
18+
"OSVersion/6.3.9600",
19+
"Microsoft.Azure.Management.Monitor.MonitorManagementClient/0.20.1.0"
20+
]
21+
},
22+
"ResponseBody": "{\r\n \"value\": []\r\n}",
23+
"ResponseHeaders": {
24+
"Content-Length": [
25+
"12"
26+
],
27+
"Content-Type": [
28+
"application/json; charset=utf-8"
29+
],
30+
"Expires": [
31+
"-1"
32+
],
33+
"Pragma": [
34+
"no-cache"
35+
],
36+
"Strict-Transport-Security": [
37+
"max-age=31536000; includeSubDomains"
38+
],
39+
"x-ms-request-id": [
40+
"a5e02b44-966a-4c43-98af-92c946411d75"
41+
],
42+
"x-ms-ratelimit-remaining-subscription-reads": [
43+
"11997"
44+
],
45+
"x-ms-correlation-request-id": [
46+
"5a786ff5-a52d-4503-b528-807ff6b54ce8"
47+
],
48+
"x-ms-routing-request-id": [
49+
"WESTUS2:20181018T212805Z:5a786ff5-a52d-4503-b528-807ff6b54ce8"
50+
],
51+
"X-Content-Type-Options": [
52+
"nosniff"
53+
],
54+
"Cache-Control": [
55+
"no-cache"
56+
],
57+
"Date": [
58+
"Thu, 18 Oct 2018 21:28:04 GMT"
59+
],
60+
"Server": [
61+
"Microsoft-IIS/10.0"
62+
]
63+
},
64+
"StatusCode": 200
65+
},
66+
{
67+
"RequestUri": "//subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourceGroups/system-sas-test/providers/Microsoft.KeyVault/vaults/myCanaryKeyVaultCUSEAUP/providers/microsoft.insights/diagnosticSettingsCategories?api-version=2017-05-01-preview",
68+
"EncodedRequestUri": "Ly9zdWJzY3JpcHRpb25zLzljZjdjYzBhLTBiYTEtNDYyNC1iYzgyLTk3ZTFlZTI1ZGM0NS9yZXNvdXJjZUdyb3Vwcy9zeXN0ZW0tc2FzLXRlc3QvcHJvdmlkZXJzL01pY3Jvc29mdC5LZXlWYXVsdC92YXVsdHMvbXlDYW5hcnlLZXlWYXVsdENVU0VBVVAvcHJvdmlkZXJzL21pY3Jvc29mdC5pbnNpZ2h0cy9kaWFnbm9zdGljU2V0dGluZ3NDYXRlZ29yaWVzP2FwaS12ZXJzaW9uPTIwMTctMDUtMDEtcHJldmlldw==",
69+
"RequestMethod": "GET",
70+
"RequestBody": "",
71+
"RequestHeaders": {
72+
"x-ms-client-request-id": [
73+
"9e3e559b-d580-432d-9177-50ca1a7dddee"
74+
],
75+
"accept-language": [
76+
"en-US"
77+
],
78+
"User-Agent": [
79+
"FxVersion/4.7.3163.0",
80+
"OSName/WindowsServer2012R2Standard",
81+
"OSVersion/6.3.9600",
82+
"Microsoft.Azure.Management.Monitor.MonitorManagementClient/0.20.1.0"
83+
]
84+
},
85+
"ResponseBody": "{\r\n \"value\": [\r\n {\r\n \"id\": \"/subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourcegroups/system-sas-test/providers/microsoft.keyvault/vaults/mycanarykeyvaultcuseaup/providers/microsoft.insights/diagnosticSettingsCategories/AuditEvent\",\r\n \"type\": \"microsoft.insights/diagnosticSettingsCategories\",\r\n \"name\": \"AuditEvent\",\r\n \"location\": null,\r\n \"kind\": null,\r\n \"tags\": null,\r\n \"properties\": {\r\n \"categoryType\": \"Logs\"\r\n },\r\n \"identity\": null\r\n },\r\n {\r\n \"id\": \"/subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourcegroups/system-sas-test/providers/microsoft.keyvault/vaults/mycanarykeyvaultcuseaup/providers/microsoft.insights/diagnosticSettingsCategories/AllMetrics\",\r\n \"type\": \"microsoft.insights/diagnosticSettingsCategories\",\r\n \"name\": \"AllMetrics\",\r\n \"location\": null,\r\n \"kind\": null,\r\n \"tags\": null,\r\n \"properties\": {\r\n \"categoryType\": \"Metrics\"\r\n },\r\n \"identity\": null\r\n }\r\n ]\r\n}",
86+
"ResponseHeaders": {
87+
"Content-Length": [
88+
"796"
89+
],
90+
"Content-Type": [
91+
"application/json; charset=utf-8"
92+
],
93+
"Expires": [
94+
"-1"
95+
],
96+
"Pragma": [
97+
"no-cache"
98+
],
99+
"Strict-Transport-Security": [
100+
"max-age=31536000; includeSubDomains"
101+
],
102+
"x-ms-request-id": [
103+
"7b6b0614-f23e-46ee-bccd-75e415219462"
104+
],
105+
"x-ms-ratelimit-remaining-subscription-reads": [
106+
"11996"
107+
],
108+
"x-ms-correlation-request-id": [
109+
"dba8d674-8fbb-436f-a7a6-ac99832f2247"
110+
],
111+
"x-ms-routing-request-id": [
112+
"WESTUS2:20181018T212805Z:dba8d674-8fbb-436f-a7a6-ac99832f2247"
113+
],
114+
"X-Content-Type-Options": [
115+
"nosniff"
116+
],
117+
"Cache-Control": [
118+
"no-cache"
119+
],
120+
"Date": [
121+
"Thu, 18 Oct 2018 21:28:05 GMT"
122+
],
123+
"Server": [
124+
"Microsoft-IIS/10.0"
125+
]
126+
},
127+
"StatusCode": 200
128+
},
129+
{
130+
"RequestUri": "//subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourceGroups/system-sas-test/providers/Microsoft.KeyVault/vaults/myCanaryKeyVaultCUSEAUP/providers/microsoft.insights/diagnosticSettings/service?api-version=2017-05-01-preview",
131+
"EncodedRequestUri": "Ly9zdWJzY3JpcHRpb25zLzljZjdjYzBhLTBiYTEtNDYyNC1iYzgyLTk3ZTFlZTI1ZGM0NS9yZXNvdXJjZUdyb3Vwcy9zeXN0ZW0tc2FzLXRlc3QvcHJvdmlkZXJzL01pY3Jvc29mdC5LZXlWYXVsdC92YXVsdHMvbXlDYW5hcnlLZXlWYXVsdENVU0VBVVAvcHJvdmlkZXJzL21pY3Jvc29mdC5pbnNpZ2h0cy9kaWFnbm9zdGljU2V0dGluZ3Mvc2VydmljZT9hcGktdmVyc2lvbj0yMDE3LTA1LTAxLXByZXZpZXc=",
132+
"RequestMethod": "PUT",
133+
"RequestBody": "{\r\n \"properties\": {\r\n \"storageAccountId\": \"/subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourceGroups/vnet-east-test/providers/Microsoft.Storage/storageAccounts/vnetcnarytestcuseuap2\",\r\n \"metrics\": [\r\n {\r\n \"category\": \"AllMetrics\",\r\n \"enabled\": true\r\n }\r\n ],\r\n \"logs\": [\r\n {\r\n \"category\": \"AuditEvent\",\r\n \"enabled\": true\r\n }\r\n ]\r\n }\r\n}",
134+
"RequestHeaders": {
135+
"Content-Type": [
136+
"application/json; charset=utf-8"
137+
],
138+
"Content-Length": [
139+
"408"
140+
],
141+
"x-ms-client-request-id": [
142+
"20c00285-ffab-40b6-a6f7-b09548d7412e"
143+
],
144+
"accept-language": [
145+
"en-US"
146+
],
147+
"User-Agent": [
148+
"FxVersion/4.7.3163.0",
149+
"OSName/WindowsServer2012R2Standard",
150+
"OSVersion/6.3.9600",
151+
"Microsoft.Azure.Management.Monitor.MonitorManagementClient/0.20.1.0"
152+
]
153+
},
154+
"ResponseBody": "{\r\n \"id\": \"/subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourcegroups/system-sas-test/providers/microsoft.keyvault/vaults/mycanarykeyvaultcuseaup/providers/microsoft.insights/diagnosticSettings/service\",\r\n \"type\": null,\r\n \"name\": \"service\",\r\n \"location\": null,\r\n \"kind\": null,\r\n \"tags\": null,\r\n \"properties\": {\r\n \"storageAccountId\": \"/subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourceGroups/vnet-east-test/providers/Microsoft.Storage/storageAccounts/vnetcnarytestcuseuap2\",\r\n \"serviceBusRuleId\": null,\r\n \"workspaceId\": null,\r\n \"eventHubAuthorizationRuleId\": null,\r\n \"eventHubName\": null,\r\n \"metrics\": [\r\n {\r\n \"timeGrain\": \"PT1M\",\r\n \"category\": \"AllMetrics\",\r\n \"enabled\": true,\r\n \"retentionPolicy\": {\r\n \"enabled\": false,\r\n \"days\": 0\r\n }\r\n }\r\n ],\r\n \"logs\": [\r\n {\r\n \"category\": \"AuditEvent\",\r\n \"enabled\": true,\r\n \"retentionPolicy\": {\r\n \"enabled\": false,\r\n \"days\": 0\r\n }\r\n }\r\n ]\r\n },\r\n \"identity\": null\r\n}",
155+
"ResponseHeaders": {
156+
"Content-Length": [
157+
"786"
158+
],
159+
"Content-Type": [
160+
"application/json; charset=utf-8"
161+
],
162+
"Expires": [
163+
"-1"
164+
],
165+
"Pragma": [
166+
"no-cache"
167+
],
168+
"Strict-Transport-Security": [
169+
"max-age=31536000; includeSubDomains"
170+
],
171+
"x-ms-request-id": [
172+
"16f188d9-1e61-4a5c-8829-4977bdc2dbf1"
173+
],
174+
"x-ms-ratelimit-remaining-subscription-resource-requests": [
175+
"149"
176+
],
177+
"x-ms-correlation-request-id": [
178+
"2157926a-62c6-417d-8220-d76e2f89df6b"
179+
],
180+
"x-ms-routing-request-id": [
181+
"WESTUS2:20181018T212808Z:2157926a-62c6-417d-8220-d76e2f89df6b"
182+
],
183+
"X-Content-Type-Options": [
184+
"nosniff"
185+
],
186+
"Cache-Control": [
187+
"no-cache"
188+
],
189+
"Date": [
190+
"Thu, 18 Oct 2018 21:28:07 GMT"
191+
],
192+
"Server": [
193+
"Microsoft-IIS/10.0"
194+
]
195+
},
196+
"StatusCode": 200
197+
}
198+
],
199+
"Names": {},
200+
"Variables": {
201+
"SubscriptionId": "56bb45c9-5c14-4914-885e-c6fd6f130f7c"
202+
}
203+
}

src/ResourceManager/Insights/Commands.Insights/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
-->
2020
## Current Release
2121

22+
* Fixed issue #7513 [Insights] Set-AzureRMDiagnosticSetting requires explicit specification of categories during creation of setting
23+
- Now the cmdlet does not require explicit indication of the categories to enable during creation, i.e. it works as it is documented
24+
2225
## Version 5.1.4
2326

2427
* Fixed issues #6833 and #7102 (Diagnostic Settings area)

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,41 @@ protected override void ProcessRecordInternal()
200200
{
201201
// Creating a new setting with settingName as name
202202
WriteDebugWithTimestamp(string.Format(CultureInfo.InvariantCulture, "Diagnostic setting named: '{0}' not found in list of {1} settings. Creating a new one.", settingName, listSettings.Count));
203-
203+
204204
properties = new DiagnosticSettingsResource();
205205
properties.Logs = new List<LogSettings>();
206206
properties.Metrics = new List<MetricSettings>();
207+
208+
WriteDebugWithTimestamp(string.Format(CultureInfo.InvariantCulture, "Retrieving supported categories for resource: '{0}'", this.ResourceId));
209+
IList<DiagnosticSettingsCategoryResource> supportedCategories = this.MonitorManagementClient.DiagnosticSettingsCategory.ListAsync(resourceUri: this.ResourceId, cancellationToken: CancellationToken.None).Result.Value;
210+
if (supportedCategories != null)
211+
{
212+
WriteDebugWithTimestamp(string.Format(CultureInfo.InvariantCulture, "Setting supported categories for resource: '{0}'", this.ResourceId));
213+
foreach (var category in supportedCategories)
214+
{
215+
if (category.CategoryType == CategoryType.Metrics)
216+
{
217+
properties.Metrics.Add(
218+
new MetricSettings(
219+
enabled: false,
220+
category: category.Name,
221+
retentionPolicy: null,
222+
timeGrain: null));
223+
}
224+
else
225+
{
226+
properties.Logs.Add(
227+
new LogSettings(
228+
enabled: false,
229+
category: category.Name,
230+
retentionPolicy: null));
231+
}
232+
}
233+
}
234+
else
235+
{
236+
WriteWarningWithTimestamp(string.Format(CultureInfo.InvariantCulture, "Resource: '{0}' does not support any category yet.", this.ResourceId));
237+
}
207238
}
208239
else
209240
{

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ This command uses the PowerShell pipeline to set (not change made) a diagnostic
128128

129129
### -Categories
130130
Specifies the list of log categories to enable or disable, according to the value of *Enabled*.
131-
If you do not specify a category, this command operates on all categories.
131+
If no category is specified, this command operates on all supported categories.
132132

133133
```yaml
134134
Type: System.Collections.Generic.List`1[System.String]
@@ -219,7 +219,8 @@ Accept wildcard characters: False
219219
```
220220
221221
### -MetricCategory
222-
The list of metric categories
222+
The list of metric categories.
223+
If no category is specified, this command operates on all supported categories.
223224
224225
```yaml
225226
Type: System.Collections.Generic.List`1[System.String]

0 commit comments

Comments
 (0)