private RuleCondition CreateRuleCondition()
        {
            // Since WindowSize is not mandatory, but it cannot be null (TimeSpan) checking for default value
            if (this.WindowSize == default(TimeSpan))
            {
                this.WindowSize = DefaultWindowSize;
            }

            RuleCondition condition;

            switch (this.RuleType)
            {
            case AlertRuleTypes.Event:
                condition = this.CreateEventRuleCondition();
                break;

            case AlertRuleTypes.Metric:
                condition = this.CreateThresholdRuleCondition();
                break;

            case AlertRuleTypes.Webtest:
                condition = new LocationThresholdRuleCondition()
                {
                    DataSource          = new RuleMetricDataSource(),
                    FailedLocationCount = this.FailedLocationCount,
                    WindowSize          = this.WindowSize,
                };
                break;

            default:
                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, ResourcesForAlertCmdlets.RuleTypeNotSupported, this.RuleType));
            }

            return(condition);
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the PSEventRuleCondition class
        /// </summary>
        /// <param name="ruleCondition">The rule condition</param>
        public PSLocationThresholdRuleCondition(LocationThresholdRuleCondition ruleCondition)
        {
            var dataSource = ruleCondition.DataSource as RuleMetricDataSource;

            if (dataSource != null)
            {
                this.DataSource = dataSource;
            }
            else
            {
                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, ResourcesForAlertCmdlets.RuleDataSourceTypeNotSupported, ruleCondition.DataSource.GetType().Name));
            }

            this.FailedLocationCount = ruleCondition.FailedLocationCount;
            this.WindowSize          = ruleCondition.WindowSize;
        }
        protected override AlertRuleResource CreateSdkCallParameters()
        {
            LocationThresholdRuleCondition condition = this.CreateRuleCondition();

            WriteVerboseWithTimestamp(string.Format("CreateSdkCallParameters: Creating rule object"));
            return(new AlertRuleResource
            {
                Description = this.Description ?? Utilities.GetDefaultDescription("webtest alert rule"),
                Condition = condition,
                Actions = this.Action?.Select(TransitionHelpers.ToMirrorNamespace).ToList(),
                Location = this.Location,
                IsEnabled = !this.DisableRule,
                AlertRuleResourceName = this.Name,

                // DO NOT REMOVE OR CHANGE the following. The two elements in the Tags are required by other services.
                Tags = new Dictionary <string, string>()
                {
                    { "$type", "Microsoft.WindowsAzure.Management.Common.Storage.CasePreservedDictionary,Microsoft.WindowsAzure.Management.Common.Storage" },
                    { "hidden-link:", "Resource" },
                }
            });
        }
示例#4
0
        /// <summary>
        /// Adds properties for web test alert.
        /// </summary>
        /// <param name="parameters"><see cref="RuleCreateOrUpdateParameters"/> instance.</param>
        /// <param name="name">Name of web test.</param>
        /// <param name="element"><see cref="WebTestElement"/> instance from App.config/Web.config.</param>
        /// <param name="webTest"><see cref="ResourceBaseExtended"/> instance representing web test resource.</param>
        /// <param name="insights"><see cref="ResourceBaseExtended"/> instance representing Application Insights resource.</param>
        /// <returns>Returns the <see cref="RuleCreateOrUpdateParameters"/> instance with properties added.</returns>
        public static RuleCreateOrUpdateParameters AddProperties(this RuleCreateOrUpdateParameters parameters, string name, WebTestElement element, ResourceBaseExtended webTest, ResourceBaseExtended insights)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            if (webTest == null)
            {
                throw new ArgumentNullException(nameof(webTest));
            }

            if (insights == null)
            {
                throw new ArgumentNullException(nameof(insights));
            }

            var alertName = $"{name}-{insights.Name}-alert".ToLowerInvariant();

            var action = new RuleEmailAction()
            {
                SendToServiceOwners = element.Alerts.SendAlertToAdmin
            };

            if (!element.Alerts.Recipients.IsNullOrEmpty())
            {
                action.CustomEmails = element.Alerts.Recipients;
            }

            var source = new RuleMetricDataSource()
            {
                MetricName = AlertMetricName, ResourceUri = webTest.Id
            };
            var condition = new LocationThresholdRuleCondition()
            {
                DataSource          = source,
                FailedLocationCount = element.Alerts.AlertLocationThreshold,
                WindowSize          = TimeSpan.FromMinutes((int)element.Alerts.TestAlertFailureTimeWindow),
            };
            var rule = new Rule()
            {
                Name            = alertName,
                Description     = string.Empty,
                IsEnabled       = element.Alerts.IsEnabled,
                LastUpdatedTime = DateTime.UtcNow,
                Actions         = { action },
                Condition       = condition,
            };

            parameters.Properties = rule;

            return(parameters);
        }