コード例 #1
0
        protected override void ProcessRecordInternal()
        {
            var putParameters = new ServiceDiagnosticSettingsPutParameters();

            ServiceDiagnosticSettingsGetResponse getResponse = this.InsightsManagementClient.ServiceDiagnosticSettingsOperations.GetAsync(this.ResourceId, CancellationToken.None).Result;

            ServiceDiagnosticSettings properties = getResponse.Properties;

            if (this.Enabled && string.IsNullOrWhiteSpace(this.StorageAccountId))
            {
                throw new ArgumentException("StorageAccountId can't be null when enabling");
            }

            if (!string.IsNullOrWhiteSpace(this.StorageAccountId))
            {
                properties.StorageAccountId = this.StorageAccountId;
            }

            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 for '{1}'", category, this.StorageAccountId));
                        }

                        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 for '{1}'", timegrainString, this.StorageAccountId));
                        }
                        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.Properties = properties;

            this.InsightsManagementClient.ServiceDiagnosticSettingsOperations.PutAsync(this.ResourceId, putParameters, CancellationToken.None).Wait();
            PSServiceDiagnosticSettings psResult = new PSServiceDiagnosticSettings(putParameters.Properties);
            WriteObject(psResult);
        }
コード例 #2
0
        public SetDiagnosticSettingCommandTests()
        {
            insightsDiagnosticsOperationsMock = new Mock<IServiceDiagnosticSettingsOperations>();
            insightsManagementClientMock = new Mock<InsightsManagementClient>();
            commandRuntimeMock = new Mock<ICommandRuntime>();
            cmdlet = new SetAzureRmDiagnosticSettingCommand()
            {
                CommandRuntime = commandRuntimeMock.Object,
                InsightsManagementClient = insightsManagementClientMock.Object
            };

            response = new ServiceDiagnosticSettingsGetResponse
            {
                RequestId = Guid.NewGuid().ToString(),
                StatusCode = HttpStatusCode.OK,
                Properties = new ServiceDiagnosticSettings
                {
                    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>()))
                .Returns(Task.FromResult<ServiceDiagnosticSettingsGetResponse>(response))
                .Callback((string resourceId) =>
                {
                    this.calledResourceId = resourceId;
                });

            insightsDiagnosticsOperationsMock.Setup(f => f.PutAsync(It.IsAny<string>(), It.IsAny<ServiceDiagnosticSettingsPutParameters>()))
                .Returns(Task.FromResult<EmptyResponse>(new EmptyResponse()))
                .Callback((string resourceId, ServiceDiagnosticSettingsPutParameters putParameters) =>
                {
                    this.calledResourceId = resourceId;
                    this.calledPutParameters = putParameters;
                });

            insightsManagementClientMock.SetupGet(f => f.ServiceDiagnosticSettingsOperations).Returns(this.insightsDiagnosticsOperationsMock.Object);
        }
 /// <summary>
 /// Create or update new diagnostic settings for the specified
 /// resource. This operation is long running. Use GetStatus to check
 /// the status of this operation.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Insights.IServiceDiagnosticSettingsOperations.
 /// </param>
 /// <param name='resourceUri'>
 /// Required. The resource identifier of the configuration.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the operation.
 /// </param>
 /// <returns>
 /// Generic empty response. We only pass it to ensure json error
 /// handling
 /// </returns>
 public static Task<EmptyResponse> PutAsync(this IServiceDiagnosticSettingsOperations operations, string resourceUri, ServiceDiagnosticSettingsPutParameters parameters)
 {
     return operations.PutAsync(resourceUri, parameters, CancellationToken.None);
 }
        protected override void ProcessRecordInternal()
        {
            var putParameters = new ServiceDiagnosticSettingsPutParameters();
            
            if (this.Categories == null && this.Timegrains == null && !this.Enabled)
            {
                // This is the only case where no call to get diagnostic settings is necessary. Since we are disabling everything, we just need to request stroage account false.

                putParameters.Properties = new ServiceDiagnosticSettings();
            }
            else
            {
                ServiceDiagnosticSettingsGetResponse getResponse = this.InsightsManagementClient.ServiceDiagnosticSettingsOperations.GetAsync(this.ResourceId, CancellationToken.None).Result;

                ServiceDiagnosticSettings properties = getResponse.Properties;

                if (this.Enabled && string.IsNullOrWhiteSpace(this.StorageAccountId))
                {
                    throw new ArgumentException("StorageAccountId can't be null when enabling");
                }

                if (!string.IsNullOrWhiteSpace(this.StorageAccountId))
                {
                    properties.StorageAccountId = this.StorageAccountId;
                }
                
                if (this.Categories == null)
                {
                    foreach (var log in properties.Logs)
                    {
                        log.Enabled = this.Enabled;
                    }
                }
                else
                {
                    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 for '{1}'", category, this.StorageAccountId));
                        }

                        logSettings.Enabled = this.Enabled;
                    }   
                }

                if (this.Timegrains == null)
                {
                    foreach (var metric in properties.Metrics)
                    {
                        metric.Enabled = this.Enabled;
                    }
                }
                else
                {
                    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 for '{1}'", timegrainString, this.StorageAccountId));
                        }
                        metricSettings.Enabled = this.Enabled;
                    }
                }

                putParameters.Properties = properties;
            }

            this.InsightsManagementClient.ServiceDiagnosticSettingsOperations.PutAsync(this.ResourceId, putParameters, CancellationToken.None).Wait();
            PSServiceDiagnosticSettings psResult = new PSServiceDiagnosticSettings(putParameters.Properties);
            WriteObject(psResult);
        }
 /// <summary>
 /// Create or update new diagnostic settings for the specified
 /// resource. This operation is long running. Use GetStatus to check
 /// the status of this operation.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Insights.IServiceDiagnosticSettingsOperations.
 /// </param>
 /// <param name='resourceUri'>
 /// Required. The resource identifier of the configuration.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the operation.
 /// </param>
 /// <returns>
 /// Generic empty response. We only pass it to ensure json error
 /// handling
 /// </returns>
 public static EmptyResponse Put(this IServiceDiagnosticSettingsOperations operations, string resourceUri, ServiceDiagnosticSettingsPutParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IServiceDiagnosticSettingsOperations)s).PutAsync(resourceUri, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }