public void WhenCreatingContractsAlertThenArmRequestPropertiesAreConvertedCorrectly()
        {
            ContractsAlert contractsAlert = CreateContractsAlert <TestAlert>();

            Assert.AreEqual(1, contractsAlert.AlertProperties.Count);

            AzureResourceManagerRequestAlertProperty armAlertProperty = (AzureResourceManagerRequestAlertProperty)contractsAlert.AlertProperties[0];

            Assert.AreEqual("ArmRequest", armAlertProperty.PropertyName);
            Assert.AreEqual(AlertPropertyType.AzureResourceManagerRequest, armAlertProperty.Type);
            Assert.AreEqual(true, armAlertProperty.IsOptional);
            Assert.AreEqual("/some/query/path", armAlertProperty.AzureResourceManagerRequestUri.ToString());
            Assert.AreEqual(6, armAlertProperty.PropertiesToDisplay.Count);

            int propertyIndex = 0;

            Assert.AreEqual("TextReference", armAlertProperty.PropertiesToDisplay[propertyIndex].PropertyName);
            Assert.AreEqual("Text Reference Display Name", armAlertProperty.PropertiesToDisplay[propertyIndex].DisplayName);
            Assert.AreEqual(AlertPropertyType.Text, armAlertProperty.PropertiesToDisplay[propertyIndex].Type);

            propertyIndex++;
            Assert.AreEqual("LongTextReference", armAlertProperty.PropertiesToDisplay[propertyIndex].PropertyName);
            Assert.AreEqual(AlertPropertyType.LongText, armAlertProperty.PropertiesToDisplay[propertyIndex].Type);

            propertyIndex++;
            Assert.AreEqual("KeyValueReference", armAlertProperty.PropertiesToDisplay[propertyIndex].PropertyName);
            Assert.AreEqual(AlertPropertyType.KeyValue, armAlertProperty.PropertiesToDisplay[propertyIndex].Type);

            propertyIndex++;
            Assert.AreEqual("ChartReference", armAlertProperty.PropertiesToDisplay[propertyIndex].PropertyName);
            Assert.AreEqual(AlertPropertyType.Chart, armAlertProperty.PropertiesToDisplay[propertyIndex].Type);

            propertyIndex++;
            Assert.AreEqual("MultiColumnTableReference", armAlertProperty.PropertiesToDisplay[propertyIndex].PropertyName);
            Assert.AreEqual(AlertPropertyType.Table, armAlertProperty.PropertiesToDisplay[propertyIndex].Type);
            var tableReferenceAlertProperty = armAlertProperty.PropertiesToDisplay[propertyIndex] as TableReferenceAlertProperty;

            Assert.IsNotNull(tableReferenceAlertProperty);
            Assert.AreEqual(true, tableReferenceAlertProperty.IsOptional);
            Assert.AreEqual(true, tableReferenceAlertProperty.IsPropertySerialized);

            propertyIndex++;
            Assert.AreEqual("SingleColumnTableReference", armAlertProperty.PropertiesToDisplay[propertyIndex].PropertyName);
            Assert.AreEqual(AlertPropertyType.Table, armAlertProperty.PropertiesToDisplay[propertyIndex].Type);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Retrieves and composes Alert propeties, for an Arm request Alert property.
        /// </summary>
        /// <param name="armProperty">Alert property of type ARM request to retrieve and compose</param>
        /// <param name="armClient">Support for ARM request</param>
        /// <returns>List of displayable properties from Arm request</returns>
        private async Task <List <DisplayableAlertProperty> > ComposeArmProperties(AzureResourceManagerRequestAlertProperty armProperty, IAzureResourceManagerClient armClient)
        {
            List <DisplayableAlertProperty> displayableArmProperties = new List <DisplayableAlertProperty>();

            try
            {
                List <JObject> response = await armClient.ExecuteArmQueryAsync(armProperty.AzureResourceManagerRequestUri, CancellationToken.None);

                foreach (IReferenceAlertProperty propertyRef in armProperty.PropertiesToDisplay.OfType <IReferenceAlertProperty>())
                {
                    JToken propertyVal = response[0].SelectToken(propertyRef.ReferencePath);

                    if (propertyVal == null)
                    {
                        displayableArmProperties.Add(this.CreateErrorProperty(propertyRef, $"Property {propertyRef.ReferencePath} doesn't exist in Response"));
                    }
                    else
                    {
                        switch (propertyRef)
                        {
                        case TextReferenceAlertProperty textRef:
                            TextAlertProperty displayText = new TextAlertProperty(textRef.PropertyName, textRef.DisplayName, textRef.Order, (string)propertyVal);
                            displayableArmProperties.Add(displayText);
                            break;

                        case LongTextReferenceAlertProperty longTextRef:
                            LongTextAlertProperty displayLongText = new LongTextAlertProperty(longTextRef.PropertyName, longTextRef.DisplayName, longTextRef.Order, (string)propertyVal);
                            displayableArmProperties.Add(displayLongText);
                            break;

                        case KeyValueReferenceAlertProperty keyValueRef:
                            IDictionary <string, string> keyValueField = new Dictionary <string, string> {
                                { keyValueRef.ReferencePath, (string)propertyVal }
                            };
                            KeyValueAlertProperty displayKeyValue = new KeyValueAlertProperty(keyValueRef.PropertyName, keyValueRef.DisplayName, keyValueRef.Order, keyValueField);
                            displayableArmProperties.Add(displayKeyValue);
                            break;

                        case TableReferenceAlertProperty tableRef:
                            JArray tableValue;
                            if (response.Count == 1)
                            {
                                tableValue = response[0].SelectToken(tableRef.ReferencePath) as JArray;
                            }
                            else
                            {
                                tableValue = (new JArray(response)).SelectToken(tableRef.ReferencePath) as JArray;
                            }

                            List <Dictionary <string, JToken> > values = tableValue
                                                                         .OfType <IDictionary <string, JToken> >()
                                                                         .Select(value => value.ToDictionary(item => item.Key, item => item.Value))
                                                                         .ToList();
                            TableAlertProperty <Dictionary <string, JToken> > displayTable = new TableAlertProperty <Dictionary <string, JToken> >(tableRef.PropertyName, tableRef.DisplayName, tableRef.Order, true, tableRef.Columns, values);
                            displayableArmProperties.Add(displayTable);
                            break;
                        }
                    }
                }
            }
            catch (HttpRequestException e)
            {
                string errorValue = $"Failed to get Arm Response, Error: {e.Message}";

                foreach (IReferenceAlertProperty propertyRef in armProperty.PropertiesToDisplay.OfType <IReferenceAlertProperty>())
                {
                    displayableArmProperties.Add(this.CreateErrorProperty(propertyRef, errorValue));
                }
            }

            return(displayableArmProperties);
        }