Beispiel #1
0
        /// <summary>
        /// Creates one or more <see cref="AlertProperty"/> objects based on an alert presentation property
        /// </summary>
        /// <param name="propertyOwner">The object that has the property</param>
        /// <param name="property">The property info of the property to create</param>
        /// <param name="presentationAttribute">The attribute defining the presentation of the alert property</param>
        /// <param name="propertyValue">The property value</param>
        /// <param name="order">The order to use</param>
        /// <param name="parentPropertyName">The parent property name</param>
        /// <returns>The <see cref="AlertProperty"/> objects</returns>
        private static IEnumerable <AlertProperty> CreateAlertProperties(object propertyOwner, PropertyInfo property, AlertPresentationPropertyAttribute presentationAttribute, object propertyValue, Order order, string parentPropertyName)
        {
            // Get the attribute display name
            string displayName = presentationAttribute.DisplayName.EvaluateInterpolatedString(propertyOwner);

            // Get the property name (and add the parent property name as prefix, if provided)
            string propertyName = string.IsNullOrWhiteSpace(presentationAttribute.PropertyName) ? property.Name : presentationAttribute.PropertyName;

            propertyName = CombinePropertyNames(parentPropertyName, propertyName);

            // Try converting the property value to PropertyReference
            var propertyReferenceValue = propertyValue as PropertyReference;

            // Return the presentation property according to the property type
            switch (presentationAttribute)
            {
            case ChartPropertyAttribute chartAttribute:
                yield return(CreateChartAlertProperty(propertyValue, propertyName, displayName, chartAttribute, order));

                break;

            case MetricChartPropertyAttribute _:
                if (!(propertyValue is MetricChart metricChart))
                {
                    throw new ArgumentException($"A {nameof(MetricChartPropertyAttribute)} can only be applied to properties of type {nameof(MetricChart)}");
                }

                yield return(CreateMetricChartAlertProperty(propertyName, displayName, order, metricChart));

                break;

            case LongTextPropertyAttribute longTextPropertyAttribute:
                if (propertyReferenceValue != null)
                {
                    if (!string.IsNullOrEmpty(longTextPropertyAttribute.FormatString))
                    {
                        throw new ArgumentException($"A {nameof(LongTextPropertyAttribute)} applied to properties of type {nameof(PropertyReference)} cannot have format string");
                    }

                    yield return(new LongTextReferenceAlertProperty(propertyName, displayName, order.Next(), propertyReferenceValue.ReferencePath, propertyReferenceValue.IsOptional, propertyReferenceValue.IsPropertySerialized));
                }
                else
                {
                    yield return(new LongTextAlertProperty(propertyName, displayName, order.Next(), PropertyValueToString(propertyOwner, property, propertyValue, longTextPropertyAttribute.FormatString)));
                }

                break;

            case TextPropertyAttribute textPropertyAttribute:
                if (propertyReferenceValue != null)
                {
                    if (!string.IsNullOrEmpty(textPropertyAttribute.FormatString))
                    {
                        throw new ArgumentException($"A {nameof(TextPropertyAttribute)} applied to properties of type {nameof(PropertyReference)} cannot have format string");
                    }

                    yield return(new TextReferenceAlertProperty(propertyName, displayName, order.Next(), propertyReferenceValue.ReferencePath, propertyReferenceValue.IsOptional, propertyReferenceValue.IsPropertySerialized));
                }
                else
                {
                    yield return(new TextAlertProperty(propertyName, displayName, order.Next(), PropertyValueToString(propertyOwner, property, propertyValue, textPropertyAttribute.FormatString)));
                }

                break;

            case ListPropertyAttribute _:
                if (!(propertyValue is IList list))
                {
                    throw new ArgumentException($"A {nameof(ListPropertyAttribute)} can only be applied to properties of type IList");
                }

                foreach (AlertProperty p in CreateAlertPropertiesFromList(propertyName, list, order))
                {
                    yield return(p);
                }

                break;

            case KeyValuePropertyAttribute keyValueAttribute:
                yield return(CreateKeyValueAlertProperty(propertyOwner, propertyValue, propertyName, displayName, keyValueAttribute, order));

                break;

            case TablePropertyAttribute tableAttribute:
                yield return(CreateTableAlertProperty(propertyValue, propertyName, displayName, tableAttribute, order));

                break;

            case AzureResourceManagerRequestPropertyAttribute armRequestAttribute:
                if (!(propertyValue is AzureResourceManagerRequest armRequest))
                {
                    throw new ArgumentException($"A {nameof(AzureResourceManagerRequestPropertyAttribute)} can only be applied to properties of type {nameof(AzureResourceManagerRequest)}");
                }

                List <AlertProperty> properties = armRequest.ExtractProperties(AzureResourceManagerRequestBaseClassPropertiesNames);
                if (properties.Any(prop => !(prop is IReferenceAlertProperty || prop is RawAlertProperty)))
                {
                    // We support raw properties because they can contain values used in interpolated display name string
                    throw new ArgumentException($"An {nameof(AzureResourceManagerRequest)} can only have reference and raw alert properties");
                }

                List <AlertProperty> propertiesToDisplay = properties.Where(prop => prop is IReferenceAlertProperty).ToList();

                yield return(new AzureResourceManagerRequestAlertProperty(propertyName, order.Next(), armRequest.RequestUri, propertiesToDisplay.Cast <DisplayableAlertProperty>().ToList(), armRequest.IsOptional));

                break;

            default:
                throw new InvalidEnumArgumentException($"Unable to handle presentation attribute of type {presentationAttribute.GetType().Name}");
            }
        }