Skip to content

Commit fd28f1f

Browse files
authored
Merge pull request #7160 from gucalder/preview
[Insights] Fixing issues #6833 and #7102: failure during creation if categories are included
2 parents 1afb1b0 + 45077aa commit fd28f1f

File tree

3 files changed

+88
-18
lines changed

3 files changed

+88
-18
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,23 @@ public void SetSomeCategories()
203203
expectedSettings.Logs[0].Enabled = false;
204204

205205
VerifyCalledOnce();
206-
VerifySettings(expectedSettings, this.calledSettings);
206+
VerifySettings(expectedSettings, this.calledSettings, suffix: "#1");
207207

208208
// Testing the new categories must be known before the cmdlet can add them
209+
expectedSettings.Logs.Add(
210+
new LogSettings()
211+
{
212+
Category = "TestCategory3",
213+
RetentionPolicy = new RetentionPolicy
214+
{
215+
Days = 0,
216+
Enabled = false
217+
}
218+
});
209219
cmdlet.Categories = new List<string> { "TestCategory3" };
210220
cmdlet.Enabled = false;
211221
cmdlet.MyInvocation.BoundParameters[SetAzureRmDiagnosticSettingCommand.EnabledParamName] = false;
212-
var argException = Assert.Throws<PSInvalidOperationException>(() => cmdlet.ExecuteCmdlet());
213-
Assert.Contains("ArgumentException", argException.ToString());
222+
cmdlet.ExecuteCmdlet();
214223

215224
// Testing the new metric categories must be known before the cmdlet can add them
216225
expectedSettings.Metrics[0].Enabled = false;
@@ -220,14 +229,13 @@ public void SetSomeCategories()
220229
cmdlet.MyInvocation.BoundParameters[SetAzureRmDiagnosticSettingCommand.EnabledParamName] = false;
221230
cmdlet.ExecuteCmdlet();
222231

223-
VerifySettings(expectedSettings, this.calledSettings);
232+
VerifySettings(expectedSettings, this.calledSettings, suffix: "#2");
224233

225234
// Testing the new categories must be known before the cmdlet can add them
226235
cmdlet.MetricCategory = new List<string> { "MetricCat3" };
227236
cmdlet.Enabled = false;
228237
cmdlet.MyInvocation.BoundParameters[SetAzureRmDiagnosticSettingCommand.EnabledParamName] = false;
229-
argException = Assert.Throws<PSInvalidOperationException>(() => cmdlet.ExecuteCmdlet());
230-
Assert.Contains("ArgumentException", argException.ToString());
238+
cmdlet.ExecuteCmdlet();
231239
}
232240

233241
[Fact]
@@ -310,7 +318,8 @@ private void VerifyCalledOnce()
310318

311319
private void VerifySettings(
312320
DiagnosticSettingsResource expectedSettings,
313-
DiagnosticSettingsResource actualSettings)
321+
DiagnosticSettingsResource actualSettings,
322+
string suffix = "")
314323
{
315324
Assert.Equal(expectedSettings.StorageAccountId, actualSettings.StorageAccountId);
316325
Assert.Equal(expectedSettings.EventHubName, actualSettings.EventHubName);
@@ -321,7 +330,7 @@ private void VerifySettings(
321330
}
322331
else
323332
{
324-
Assert.True(expectedSettings.Logs.Count == actualSettings.Logs.Count, string.Format("Expected: {0}, Actual: {1}, no the same number of Log settings", expectedSettings.Logs.Count, actualSettings.Logs.Count));
333+
Assert.True(expectedSettings.Logs.Count == actualSettings.Logs.Count, string.Format("Expected: {0}, Actual: {1}, no the same number of Log settings {2}", expectedSettings.Logs.Count, actualSettings.Logs.Count, suffix));
325334
for (int i = 0; i < expectedSettings.Logs.Count; i++)
326335
{
327336
var expected = expectedSettings.Logs[i];
@@ -338,7 +347,7 @@ private void VerifySettings(
338347
}
339348
else
340349
{
341-
Assert.True(expectedSettings.Metrics.Count == actualSettings.Metrics.Count, string.Format("Expected: {0}, Actual: {1}, no the same number of Metric settings", expectedSettings.Metrics.Count, actualSettings.Metrics.Count));
350+
Assert.True(expectedSettings.Metrics.Count == actualSettings.Metrics.Count, string.Format("Expected: {0}, Actual: {1}, no the same number of Metric settings {2}", expectedSettings.Metrics.Count, actualSettings.Metrics.Count, suffix));
342351
for (int i = 0; i < expectedSettings.Metrics.Count; i++)
343352
{
344353
var expected = expectedSettings.Metrics[i];

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

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

22+
* Fixed issues #6833 and #7102 (Diagnostic Settings area)
23+
- Issues with the default name, i.e. "service", during creation and listing/getting of diagnostic settings
24+
- Issues creating diagnostic settings with categories
25+
26+
* Added deprecation message for metrics time grains parameters
27+
- Timegrains parameters are still being accepted (this is a non-breaking change,) but they are ignored in the backend since only PT1M is valid
28+
2229
## Version 5.1.3
2330
* Fixed issue with default resource groups not being set.
2431
* Updated common runtime assemblies

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

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,13 @@ private void SetSelectedTimegrains(DiagnosticSettingsResource properties)
364364
throw new ArgumentException("Parameter 'Enabled' is required by 'Timegrains' parameter.");
365365
}
366366

367-
if (this.Timegrains.Count > 0)
367+
if (this.Timegrains != null && this.Timegrains.Count > 0)
368368
{
369+
if (properties.Metrics == null)
370+
{
371+
properties.Metrics = new List<MetricSettings>();
372+
}
373+
369374
WriteWarningWithTimestamp("Deprecation: The timegain argument for metrics will be deprecated since the back end supports only PT1M. Currently it is ignored for backwards compatibility.");
370375
WriteDebugWithTimestamp("Setting Enabled property for metrics since timegrains argument is non-empty");
371376
foreach (MetricSettings metric in properties.Metrics)
@@ -383,16 +388,35 @@ private void SetSelectedCategories(DiagnosticSettingsResource properties)
383388
}
384389

385390
WriteDebugWithTimestamp("Setting log categories, including Enabled property");
391+
if (properties.Logs == null)
392+
{
393+
properties.Logs = new List<LogSettings>();
394+
}
395+
386396
foreach (string category in this.Categories)
387397
{
388398
LogSettings logSettings = properties.Logs.FirstOrDefault(x => string.Equals(x.Category, category, StringComparison.OrdinalIgnoreCase));
389-
390399
if (logSettings == null)
391400
{
392-
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Log category '{0}' is not available", category));
393-
}
401+
// if not there add it
402+
logSettings = new LogSettings()
403+
{
404+
Category = category,
405+
RetentionPolicy = new RetentionPolicy
406+
{
407+
Days = 0,
408+
Enabled = false
409+
},
410+
Enabled = this.Enabled
411+
};
394412

395-
logSettings.Enabled = this.Enabled;
413+
properties.Logs.Add(logSettings);
414+
}
415+
else
416+
{
417+
// else update it
418+
logSettings.Enabled = this.Enabled;
419+
}
396420
}
397421
}
398422

@@ -404,16 +428,37 @@ private void SetSelectedMetricsCategories(DiagnosticSettingsResource properties)
404428
}
405429

406430
WriteDebugWithTimestamp("Setting metric categories, including Enabled property");
431+
if (properties.Metrics == null)
432+
{
433+
properties.Metrics = new List<MetricSettings>();
434+
}
435+
407436
foreach (string category in this.MetricCategory)
408437
{
409438
MetricSettings metricSettings = properties.Metrics.FirstOrDefault(x => string.Equals(x.Category, category, StringComparison.OrdinalIgnoreCase));
410439

411440
if (metricSettings == null)
412441
{
413-
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Metric category '{0}' is not available", category));
414-
}
442+
// If not there add it
443+
metricSettings = new MetricSettings
444+
{
445+
Category = category,
446+
Enabled = this.Enabled,
447+
RetentionPolicy = new RetentionPolicy
448+
{
449+
Days = 0,
450+
Enabled = false
451+
},
452+
TimeGrain = null
453+
};
415454

416-
metricSettings.Enabled = this.Enabled;
455+
properties.Metrics.Add(metricSettings);
456+
}
457+
else
458+
{
459+
// else update it
460+
metricSettings.Enabled = this.Enabled;
461+
}
417462
}
418463
}
419464

@@ -425,12 +470,22 @@ private void SetAllCategoriesAndTimegrains(DiagnosticSettingsResource properties
425470
}
426471

427472
WriteDebugWithTimestamp("Setting Enabled property for logs");
473+
if (properties.Logs == null)
474+
{
475+
properties.Logs = new List<LogSettings>();
476+
}
477+
428478
foreach (var log in properties.Logs)
429479
{
430480
log.Enabled = this.Enabled;
431481
}
432482

433483
WriteDebugWithTimestamp("Setting Enabled property for metrics");
484+
if (properties.Metrics == null)
485+
{
486+
properties.Metrics = new List<MetricSettings>();
487+
}
488+
434489
foreach (var metric in properties.Metrics)
435490
{
436491
metric.Enabled = this.Enabled;
@@ -470,7 +525,6 @@ private void SetEventHubRule(DiagnosticSettingsResource properties)
470525
}
471526
}
472527

473-
474528
private void SetStorage(DiagnosticSettingsResource properties)
475529
{
476530
if (this.isStorageParamPresent)

0 commit comments

Comments
 (0)