public void CreateAndDeleteAlertProcessingRule()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var alertsManagementClient = GetAlertsManagementClient(context, handler);

                string resourceGroupName       = "ActionRules-Powershell-Test";
                string alertProcessingRuleName = "ScenarioTest-AlertProcessingRule";

                // Create Alert Processing Rule
                AlertProcessingRule alertProcessingRule = new AlertProcessingRule(
                    location: "Global",
                    tags: new Dictionary <string, string>(),
                    properties: new AlertProcessingRuleProperties(
                        scopes: new List <string>
                {
                    "/subscriptions/dd91de05-d791-4ceb-b6dc-988682dc7d72/resourceGroups/alertslab",
                    "/subscriptions/dd91de05-d791-4ceb-b6dc-988682dc7d72/resourceGroups/Test-VMs"
                },
                        conditions: new List <Condition>
                {
                    new Condition(
                        field: "Severity",
                        operatorProperty: "Equals",
                        values: new List <string>
                    {
                        "Sev2"
                    })
                },
                        schedule: new Schedule(
                            effectiveFrom: "2019-12-22T11:00:00",
                            effectiveUntil: "2020-09-24T11:00:00",
                            timeZone: "Pacific Standard Time",
                            recurrences: new List <Recurrence>
                {
                    new DailyRecurrence(startTime: "06:00:00", endTime: "14:00:00")
                }
                            ),
                        actions: new List <Action> {
                    new RemoveAllActionGroups()
                },
                        description: "Test Supression Rule",
                        enabled: true
                        )
                    );

                var createdAlertProcessingRule = alertsManagementClient.AlertProcessingRules.CreateOrUpdate(resourceGroupName: resourceGroupName, alertProcessingRuleName: alertProcessingRuleName, alertProcessingRule: alertProcessingRule);

                // Get Alert processing rule by name
                var fetchedAlertProcessingRule = alertsManagementClient.AlertProcessingRules.GetByName(resourceGroupName: resourceGroupName, alertProcessingRuleName: alertProcessingRuleName);

                if (!this.IsRecording)
                {
                    ComparisonUtility.AreEqual(createdAlertProcessingRule, fetchedAlertProcessingRule);
                }

                // Delete created alert processing rule
                alertsManagementClient.AlertProcessingRules.Delete(resourceGroupName: resourceGroupName, alertProcessingRuleName: alertProcessingRuleName);
            }
        }
Example #2
0
        private void CheckAllAlertsContainsUpdatedAlertObject(IPage <Alert> allAlerts, Alert alertPostStateChange)
        {
            bool alertFound = false;

            var enumerator = allAlerts.GetEnumerator();

            while (enumerator.MoveNext())
            {
                Alert current = enumerator.Current;
                if (current.Properties.Essentials.SourceCreatedId == alertPostStateChange.Properties.Essentials.SourceCreatedId)
                {
                    alertFound = true;
                    ComparisonUtility.AreEqual(alertPostStateChange, current);
                    break;
                }
            }

            if (!alertFound)
            {
                throw new Exception("Test Failed : Alert not found in list of alerts.");
            }
        }
        public void CreateAndDeleteActionRule()
        {
            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var alertsManagementClient = GetAlertsManagementClient(context, handler);

                string resourceGroupName = "ActionRules-Powershell-Test";
                string actionRuleName    = "ScenarioTest-ActionRule";

                // Create Action Rule
                ActionRule actionRule = new ActionRule(
                    location: "Global",
                    tags: new Dictionary <string, string>(),
                    properties: new Suppression(
                        scope: new Scope(
                            scopeType: ScopeType.ResourceGroup,
                            values: new List <string>
                {
                    "/subscriptions/dd91de05-d791-4ceb-b6dc-988682dc7d72/resourceGroups/alertslab",
                    "/subscriptions/dd91de05-d791-4ceb-b6dc-988682dc7d72/resourceGroups/Test-VMs"
                }
                            ),
                        conditions: new Conditions(
                            severity: new Condition(
                                operatorProperty: "Equals",
                                values: new List <string>
                {
                    "Sev2"
                })
                            ),
                        suppressionConfig: new SuppressionConfig(
                            recurrenceType: "Once",
                            schedule: new SuppressionSchedule(
                                startDate: "12/09/2018",
                                endDate: "12/18/2018",
                                startTime: "06:00:00",
                                endTime: "14:00:00"
                                )
                            ),
                        description: "Test Supression Rule",
                        status: "Enabled"
                        )
                    );

                var createdActionRule = alertsManagementClient.ActionRules.CreateUpdate(resourceGroupName: resourceGroupName, actionRuleName: actionRuleName, actionRule: actionRule);

                // Get Action rule by name
                var fetchedActionRule = alertsManagementClient.ActionRules.GetByName(resourceGroupName: resourceGroupName, actionRuleName: actionRuleName);

                if (!this.IsRecording)
                {
                    ComparisonUtility.AreEqual(createdActionRule, fetchedActionRule);
                }

                // Delete created action rule
                bool?deleted = alertsManagementClient.ActionRules.Delete(resourceGroupName: resourceGroupName, actionRuleName: actionRuleName);

                if (!this.IsRecording)
                {
                    Assert.True(deleted);
                }
            }
        }