Skip to content

[Insights] Make sure the diagnostic settings can be added without explicitly specifying the categories. (Issue #7513) #7619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AutoscaleTests\TestGetAzureRmAutoscaleSettingByName.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestSetAzureRmDiagnosticSettingCreate.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestSetAzureRmDiagnosticSettingWithRetention.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -189,7 +192,7 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestSetAzureRmDiagnosticSettingCategoriesOnly.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestSetAzureRmDiagnosticSetting.json">
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestSetAzureRmDiagnosticSettingUpdate.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AzureRmDiagnosticSettingTests\TestGetAzureRmDiagnosticSetting.json">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ public void TestGetAzureRmDiagnosticSetting()

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSetAzureRmDiagnosticSetting()
public void TestSetAzureRmDiagnosticSettingCreate()
{
TestsController.NewInstance.RunPsTest(_logger, "Test-SetAzureRmDiagnosticSetting");
TestsController.NewInstance.RunPsTest(_logger, "Test-SetAzureRmDiagnosticSettingCreate");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSetAzureRmDiagnosticSettingUpdate()
{
TestsController.NewInstance.RunPsTest(_logger, "Test-SetAzureRmDiagnosticSettingUpdate");
}

[Fact] //(Skip = "TODO: fixing this test after introducing Swagger specs")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function Test-GetAzureRmDiagnosticSetting
.SYNOPSIS
Tests setting diagnostics
#>
function Test-SetAzureRmDiagnosticSetting
function Test-SetAzureRmDiagnosticSettingUpdate
{
try
{
Expand All @@ -95,6 +95,33 @@ function Test-SetAzureRmDiagnosticSetting
}
}

<#
.SYNOPSIS
Tests setting diagnostics
#>
function Test-SetAzureRmDiagnosticSettingCreate
{
try
{
# assumes there is no diagnostic settting called service for the given resource
$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

Assert-AreEqual $actual.StorageAccountId "/subscriptions/9cf7cc0a-0ba1-4624-bc82-97e1ee25dc45/resourceGroups/vnet-east-test/providers/Microsoft.Storage/storageAccounts/vnetcnarytestcuseuap2"
Assert-AreEqual 1 $actual.Metrics.Count
Assert-AreEqual $true $actual.Metrics[0].Enabled
Assert-AreEqual "AllMetrics" $actual.Metrics[0].Category
Assert-AreEqual 1 $actual.Logs.Count
Assert-AreEqual $true $actual.Logs[0].Enabled
Assert-AreEqual "AuditEvent" $actual.Logs[0].Category
Assert-AreEqual "service" $actual.Name
}
finally
{
# Cleanup
# No cleanup needed for now
}
}

<#
.SYNOPSIS
Tests setting diagnostics
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
{
"Entries": [
{
"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",
"EncodedRequestUri": "Ly9zdWJzY3JpcHRpb25zLzljZjdjYzBhLTBiYTEtNDYyNC1iYzgyLTk3ZTFlZTI1ZGM0NS9yZXNvdXJjZUdyb3Vwcy9zeXN0ZW0tc2FzLXRlc3QvcHJvdmlkZXJzL01pY3Jvc29mdC5LZXlWYXVsdC92YXVsdHMvbXlDYW5hcnlLZXlWYXVsdENVU0VBVVAvcHJvdmlkZXJzL21pY3Jvc29mdC5pbnNpZ2h0cy9kaWFnbm9zdGljU2V0dGluZ3M/YXBpLXZlcnNpb249MjAxNy0wNS0wMS1wcmV2aWV3",
"RequestMethod": "GET",
"RequestBody": "",
"RequestHeaders": {
"x-ms-client-request-id": [
"a4795193-7f5a-4864-9c8b-406fbf646b34"
],
"accept-language": [
"en-US"
],
"User-Agent": [
"FxVersion/4.7.3163.0",
"OSName/WindowsServer2012R2Standard",
"OSVersion/6.3.9600",
"Microsoft.Azure.Management.Monitor.MonitorManagementClient/0.20.1.0"
]
},
"ResponseBody": "{\r\n \"value\": []\r\n}",
"ResponseHeaders": {
"Content-Length": [
"12"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Expires": [
"-1"
],
"Pragma": [
"no-cache"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains"
],
"x-ms-request-id": [
"a5e02b44-966a-4c43-98af-92c946411d75"
],
"x-ms-ratelimit-remaining-subscription-reads": [
"11997"
],
"x-ms-correlation-request-id": [
"5a786ff5-a52d-4503-b528-807ff6b54ce8"
],
"x-ms-routing-request-id": [
"WESTUS2:20181018T212805Z:5a786ff5-a52d-4503-b528-807ff6b54ce8"
],
"X-Content-Type-Options": [
"nosniff"
],
"Cache-Control": [
"no-cache"
],
"Date": [
"Thu, 18 Oct 2018 21:28:04 GMT"
],
"Server": [
"Microsoft-IIS/10.0"
]
},
"StatusCode": 200
},
{
"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",
"EncodedRequestUri": "Ly9zdWJzY3JpcHRpb25zLzljZjdjYzBhLTBiYTEtNDYyNC1iYzgyLTk3ZTFlZTI1ZGM0NS9yZXNvdXJjZUdyb3Vwcy9zeXN0ZW0tc2FzLXRlc3QvcHJvdmlkZXJzL01pY3Jvc29mdC5LZXlWYXVsdC92YXVsdHMvbXlDYW5hcnlLZXlWYXVsdENVU0VBVVAvcHJvdmlkZXJzL21pY3Jvc29mdC5pbnNpZ2h0cy9kaWFnbm9zdGljU2V0dGluZ3NDYXRlZ29yaWVzP2FwaS12ZXJzaW9uPTIwMTctMDUtMDEtcHJldmlldw==",
"RequestMethod": "GET",
"RequestBody": "",
"RequestHeaders": {
"x-ms-client-request-id": [
"9e3e559b-d580-432d-9177-50ca1a7dddee"
],
"accept-language": [
"en-US"
],
"User-Agent": [
"FxVersion/4.7.3163.0",
"OSName/WindowsServer2012R2Standard",
"OSVersion/6.3.9600",
"Microsoft.Azure.Management.Monitor.MonitorManagementClient/0.20.1.0"
]
},
"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}",
"ResponseHeaders": {
"Content-Length": [
"796"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Expires": [
"-1"
],
"Pragma": [
"no-cache"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains"
],
"x-ms-request-id": [
"7b6b0614-f23e-46ee-bccd-75e415219462"
],
"x-ms-ratelimit-remaining-subscription-reads": [
"11996"
],
"x-ms-correlation-request-id": [
"dba8d674-8fbb-436f-a7a6-ac99832f2247"
],
"x-ms-routing-request-id": [
"WESTUS2:20181018T212805Z:dba8d674-8fbb-436f-a7a6-ac99832f2247"
],
"X-Content-Type-Options": [
"nosniff"
],
"Cache-Control": [
"no-cache"
],
"Date": [
"Thu, 18 Oct 2018 21:28:05 GMT"
],
"Server": [
"Microsoft-IIS/10.0"
]
},
"StatusCode": 200
},
{
"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",
"EncodedRequestUri": "Ly9zdWJzY3JpcHRpb25zLzljZjdjYzBhLTBiYTEtNDYyNC1iYzgyLTk3ZTFlZTI1ZGM0NS9yZXNvdXJjZUdyb3Vwcy9zeXN0ZW0tc2FzLXRlc3QvcHJvdmlkZXJzL01pY3Jvc29mdC5LZXlWYXVsdC92YXVsdHMvbXlDYW5hcnlLZXlWYXVsdENVU0VBVVAvcHJvdmlkZXJzL21pY3Jvc29mdC5pbnNpZ2h0cy9kaWFnbm9zdGljU2V0dGluZ3Mvc2VydmljZT9hcGktdmVyc2lvbj0yMDE3LTA1LTAxLXByZXZpZXc=",
"RequestMethod": "PUT",
"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}",
"RequestHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Content-Length": [
"408"
],
"x-ms-client-request-id": [
"20c00285-ffab-40b6-a6f7-b09548d7412e"
],
"accept-language": [
"en-US"
],
"User-Agent": [
"FxVersion/4.7.3163.0",
"OSName/WindowsServer2012R2Standard",
"OSVersion/6.3.9600",
"Microsoft.Azure.Management.Monitor.MonitorManagementClient/0.20.1.0"
]
},
"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}",
"ResponseHeaders": {
"Content-Length": [
"786"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Expires": [
"-1"
],
"Pragma": [
"no-cache"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains"
],
"x-ms-request-id": [
"16f188d9-1e61-4a5c-8829-4977bdc2dbf1"
],
"x-ms-ratelimit-remaining-subscription-resource-requests": [
"149"
],
"x-ms-correlation-request-id": [
"2157926a-62c6-417d-8220-d76e2f89df6b"
],
"x-ms-routing-request-id": [
"WESTUS2:20181018T212808Z:2157926a-62c6-417d-8220-d76e2f89df6b"
],
"X-Content-Type-Options": [
"nosniff"
],
"Cache-Control": [
"no-cache"
],
"Date": [
"Thu, 18 Oct 2018 21:28:07 GMT"
],
"Server": [
"Microsoft-IIS/10.0"
]
},
"StatusCode": 200
}
],
"Names": {},
"Variables": {
"SubscriptionId": "56bb45c9-5c14-4914-885e-c6fd6f130f7c"
}
}
3 changes: 3 additions & 0 deletions src/ResourceManager/Insights/Commands.Insights/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
-->
## Current Release

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

## Version 5.1.4

* Fixed issues #6833 and #7102 (Diagnostic Settings area)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,41 @@ protected override void ProcessRecordInternal()
{
// Creating a new setting with settingName as name
WriteDebugWithTimestamp(string.Format(CultureInfo.InvariantCulture, "Diagnostic setting named: '{0}' not found in list of {1} settings. Creating a new one.", settingName, listSettings.Count));

properties = new DiagnosticSettingsResource();
properties.Logs = new List<LogSettings>();
properties.Metrics = new List<MetricSettings>();

WriteDebugWithTimestamp(string.Format(CultureInfo.InvariantCulture, "Retrieving supported categories for resource: '{0}'", this.ResourceId));
IList<DiagnosticSettingsCategoryResource> supportedCategories = this.MonitorManagementClient.DiagnosticSettingsCategory.ListAsync(resourceUri: this.ResourceId, cancellationToken: CancellationToken.None).Result.Value;
if (supportedCategories != null)
{
WriteDebugWithTimestamp(string.Format(CultureInfo.InvariantCulture, "Setting supported categories for resource: '{0}'", this.ResourceId));
foreach (var category in supportedCategories)
{
if (category.CategoryType == CategoryType.Metrics)
{
properties.Metrics.Add(
new MetricSettings(
enabled: false,
category: category.Name,
retentionPolicy: null,
timeGrain: null));
}
else
{
properties.Logs.Add(
new LogSettings(
enabled: false,
category: category.Name,
retentionPolicy: null));
}
}
}
else
{
WriteWarningWithTimestamp(string.Format(CultureInfo.InvariantCulture, "Resource: '{0}' does not support any category yet.", this.ResourceId));
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ This command uses the PowerShell pipeline to set (not change made) a diagnostic

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

```yaml
Type: System.Collections.Generic.List`1[System.String]
Expand Down Expand Up @@ -219,7 +219,8 @@ Accept wildcard characters: False
```

### -MetricCategory
The list of metric categories
The list of metric categories.
If no category is specified, this command operates on all supported categories.

```yaml
Type: System.Collections.Generic.List`1[System.String]
Expand Down