public SetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper output) { //ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output)); insightsDiagnosticsOperationsMock = new Mock <IServiceDiagnosticSettingsOperations>(); insightsManagementClientMock = new Mock <InsightsManagementClient>(); commandRuntimeMock = new Mock <ICommandRuntime>(); cmdlet = new SetAzureRmDiagnosticSettingCommand() { CommandRuntime = commandRuntimeMock.Object, InsightsManagementClient = insightsManagementClientMock.Object }; request = new ServiceDiagnosticSettingsResource { StorageAccountId = storageAccountId, Logs = new List <LogSettings> { new LogSettings { Category = "TestCategory1", Enabled = true }, new LogSettings { Category = "TestCategory2", Enabled = false } }, Metrics = new List <MetricSettings> { new MetricSettings { TimeGrain = TimeSpan.FromMinutes(1), Enabled = false }, new MetricSettings { TimeGrain = TimeSpan.FromHours(1), Enabled = true } } }; insightsDiagnosticsOperationsMock.Setup(f => f.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult <ServiceDiagnosticSettingsResource>(request)) .Callback((string resourceId) => { this.calledResourceId = resourceId; }); Rest.Azure.AzureOperationResponse <ServiceDiagnosticSettingsResource> response = new AzureOperationResponse <ServiceDiagnosticSettingsResource> { Body = request }; insightsDiagnosticsOperationsMock.Setup(f => f.CreateOrUpdateWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <ServiceDiagnosticSettingsCreateOrUpdateParameters>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult <Rest.Azure.AzureOperationResponse <ServiceDiagnosticSettingsResource> >(response)) .Callback((string resourceId, ServiceDiagnosticSettingsCreateOrUpdateParameters putParameters, Dictionary <string, List <string> > headers) => { this.calledResourceId = resourceId; this.calledPutParameters = putParameters; }); insightsManagementClientMock.SetupGet(f => f.ServiceDiagnosticSettings).Returns(this.insightsDiagnosticsOperationsMock.Object); }
public SetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper output) { //ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output)); insightsDiagnosticsOperationsMock = new Mock<IServiceDiagnosticSettingsOperations>(); insightsManagementClientMock = new Mock<InsightsManagementClient>(); commandRuntimeMock = new Mock<ICommandRuntime>(); cmdlet = new SetAzureRmDiagnosticSettingCommand() { CommandRuntime = commandRuntimeMock.Object, InsightsManagementClient = insightsManagementClientMock.Object }; request = new ServiceDiagnosticSettingsResource { StorageAccountId = storageAccountId, Logs = new List<LogSettings> { new LogSettings { Category = "TestCategory1", Enabled = true }, new LogSettings { Category = "TestCategory2", Enabled = false } }, Metrics = new List<MetricSettings> { new MetricSettings { TimeGrain = TimeSpan.FromMinutes(1), Enabled = false }, new MetricSettings { TimeGrain = TimeSpan.FromHours(1), Enabled = true } } }; insightsDiagnosticsOperationsMock.Setup(f => f.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())) .Returns(Task.FromResult<ServiceDiagnosticSettingsResource>(request)) .Callback((string resourceId) => { this.calledResourceId = resourceId; }); Rest.Azure.AzureOperationResponse<ServiceDiagnosticSettingsResource> response = new AzureOperationResponse<ServiceDiagnosticSettingsResource> { Body = request }; insightsDiagnosticsOperationsMock.Setup(f => f.CreateOrUpdateWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<ServiceDiagnosticSettingsCreateOrUpdateParameters>(), It.IsAny<Dictionary<string,List<string>>>(), It.IsAny<CancellationToken>())) .Returns(Task.FromResult<Rest.Azure.AzureOperationResponse<ServiceDiagnosticSettingsResource>>(response)) .Callback((string resourceId, ServiceDiagnosticSettingsCreateOrUpdateParameters putParameters, Dictionary<string,List<string>> headers) => { this.calledResourceId = resourceId; this.calledPutParameters = putParameters; }); insightsManagementClientMock.SetupGet(f => f.ServiceDiagnosticSettings).Returns(this.insightsDiagnosticsOperationsMock.Object); }
protected override void ProcessRecordInternal() { var putParameters = new ServiceDiagnosticSettingsCreateOrUpdateParameters(); ServiceDiagnosticSettingsResource getResponse = this.InsightsManagementClient.ServiceDiagnosticSettings.GetAsync(resourceUri: this.ResourceId, cancellationToken: CancellationToken.None).Result; ServiceDiagnosticSettingsResource properties = getResponse; if (!string.IsNullOrWhiteSpace(this.StorageAccountId)) { properties.StorageAccountId = this.StorageAccountId; } if (!string.IsNullOrWhiteSpace(this.ServiceBusRuleId)) { properties.ServiceBusRuleId = this.ServiceBusRuleId; } if (!string.IsNullOrWhiteSpace(this.WorkspaceId)) { properties.WorkspaceId = this.WorkspaceId; } if (this.Categories == null && this.Timegrains == null) { foreach (var log in properties.Logs) { log.Enabled = this.Enabled; } foreach (var metric in properties.Metrics) { metric.Enabled = this.Enabled; } } else { if (this.Categories != null) { foreach (string category in this.Categories) { LogSettings logSettings = properties.Logs.FirstOrDefault(x => string.Equals(x.Category, category, StringComparison.OrdinalIgnoreCase)); if (logSettings == null) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Log category '{0}' is not available", category)); } logSettings.Enabled = this.Enabled; } } if (this.Timegrains != null) { foreach (string timegrainString in this.Timegrains) { TimeSpan timegrain = XmlConvert.ToTimeSpan(timegrainString); MetricSettings metricSettings = properties.Metrics.FirstOrDefault(x => TimeSpan.Equals(x.TimeGrain, timegrain)); if (metricSettings == null) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Metric timegrain '{0}' is not available", timegrainString)); } metricSettings.Enabled = this.Enabled; } } } if (this.RetentionEnabled.HasValue) { var retentionPolicy = new RetentionPolicy { Enabled = this.RetentionEnabled.Value, Days = this.RetentionInDays.Value }; if (properties.Logs != null) { foreach (LogSettings logSettings in properties.Logs) { logSettings.RetentionPolicy = retentionPolicy; } } if (properties.Metrics != null) { foreach (MetricSettings metricSettings in properties.Metrics) { metricSettings.RetentionPolicy = retentionPolicy; } } } putParameters.Logs = properties.Logs; putParameters.Metrics = properties.Metrics; putParameters.ServiceBusRuleId = properties.ServiceBusRuleId; putParameters.StorageAccountId = properties.StorageAccountId; putParameters.WorkspaceId = properties.WorkspaceId; ServiceDiagnosticSettingsResource result = this.InsightsManagementClient.ServiceDiagnosticSettings.CreateOrUpdateAsync(resourceUri: this.ResourceId, parameters: putParameters, cancellationToken: CancellationToken.None).Result; WriteObject(result); }