/// <summary>
 /// Initializes a new instance of the LogProfileResource class.
 /// </summary>
 /// <param name="location">Resource location</param>
 /// <param name="locations">List of regions for which Activity Log
 /// events should be stored or streamed. It is a comma separated list
 /// of valid ARM locations including the 'global' location.</param>
 /// <param name="id">Azure resource Id</param>
 /// <param name="name">Azure resource name</param>
 /// <param name="type">Azure resource type</param>
 /// <param name="tags">Resource tags</param>
 /// <param name="storageAccountId">the resource id of the storage
 /// account to which you would like to send the Activity Log.</param>
 /// <param name="serviceBusRuleId">The service bus rule ID of the
 /// service bus namespace in which you would like to have Event Hubs
 /// created for streaming the Activity Log. The rule ID is of the
 /// format: '{service bus resource ID}/authorizationrules/{key
 /// name}'.</param>
 /// <param name="categories">the categories of the logs. These
 /// categories are created as is convenient to the user. Some values
 /// are: 'Write', 'Delete', and/or 'Action.'</param>
 /// <param name="retentionPolicy">the retention policy for the events
 /// in the log.</param>
 public LogProfileResource(string location, System.Collections.Generic.IList <string> locations, string id = default(string), string name = default(string), string type = default(string), System.Collections.Generic.IDictionary <string, string> tags = default(System.Collections.Generic.IDictionary <string, string>), string storageAccountId = default(string), string serviceBusRuleId = default(string), System.Collections.Generic.IList <string> categories = default(System.Collections.Generic.IList <string>), RetentionPolicy retentionPolicy = default(RetentionPolicy))
     : base(location, id, name, type, tags)
 {
     StorageAccountId = storageAccountId;
     ServiceBusRuleId = serviceBusRuleId;
     Locations        = locations;
     Categories       = categories;
     RetentionPolicy  = retentionPolicy;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="PSLogProfile"/> class.
        /// </summary>
        public PSLogProfile(string id, string name, LogProfile logProfile)
        {
            this.Id = id;

            this.Name = name;

            this.Categories = logProfile.Categories.Select(x => x).ToList();
            this.Locations = logProfile.Locations.Select(x => x).ToList();
            this.RetentionPolicy = new RetentionPolicy()
            {
                Days = logProfile.RetentionPolicy.Days,
                Enabled = logProfile.RetentionPolicy.Enabled
            };
            this.ServiceBusRuleId = logProfile.ServiceBusRuleId;
            this.StorageAccountId = logProfile.StorageAccountId;
        }
        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);
        }
 /// <summary>
 /// Initializes a new instance of the PSRetentionPolicy class.
 /// </summary>
 public PSRetentionPolicy(RetentionPolicy retentionPolicy)
 {
     this.Enabled = retentionPolicy.Enabled;
     this.Days = retentionPolicy.Days;
 }
        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);
        }
 /// <summary>
 /// Initializes a new instance of the LogSettings class.
 /// </summary>
 /// <param name="enabled">a value indicating whether this log is
 /// enabled.</param>
 /// <param name="category">Name of a Diagnostic Log category for a
 /// resource type this setting is applied to. To obtain the list of
 /// Diagnostic Log categories for a resource, first perform a GET
 /// diagnostic settings operation.</param>
 /// <param name="retentionPolicy">the retention policy for this
 /// log.</param>
 public LogSettings(bool enabled, string category = default(string), RetentionPolicy retentionPolicy = default(RetentionPolicy))
 {
     Category        = category;
     Enabled         = enabled;
     RetentionPolicy = retentionPolicy;
 }
 /// <summary>
 /// Initializes a new instance of the MetricSettings class.
 /// </summary>
 /// <param name="timeGrain">the timegrain of the metric in ISO8601
 /// format.</param>
 /// <param name="enabled">a value indicating whether this timegrain is
 /// enabled.</param>
 /// <param name="retentionPolicy">the retention policy for this
 /// timegrain.</param>
 public MetricSettings(System.TimeSpan timeGrain, bool enabled, RetentionPolicy retentionPolicy = default(RetentionPolicy))
 {
     TimeGrain       = timeGrain;
     Enabled         = enabled;
     RetentionPolicy = retentionPolicy;
 }