// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.alertsmanagement.generated;

import com.azure.resourcemanager.alertsmanagement.models.AddActionGroups;
import com.azure.resourcemanager.alertsmanagement.models.AlertProcessingRuleProperties;
import com.azure.resourcemanager.alertsmanagement.models.Condition;
import com.azure.resourcemanager.alertsmanagement.models.DailyRecurrence;
import com.azure.resourcemanager.alertsmanagement.models.DaysOfWeek;
import com.azure.resourcemanager.alertsmanagement.models.Field;
import com.azure.resourcemanager.alertsmanagement.models.Operator;
import com.azure.resourcemanager.alertsmanagement.models.RemoveAllActionGroups;
import com.azure.resourcemanager.alertsmanagement.models.Schedule;
import com.azure.resourcemanager.alertsmanagement.models.WeeklyRecurrence;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/** Samples for AlertProcessingRules CreateOrUpdate. */
public final class AlertProcessingRulesCreateOrUpdateSamples {
    /*
     * x-ms-original-file: specification/alertsmanagement/resource-manager/Microsoft.AlertsManagement/stable/2021-08-08/examples/AlertProcessingRules_Create_or_update_remove_all_action_groups_recurring_maintenance_window.json
     */
    /**
     * Sample code: Create or update a rule that removes all action groups from all alerts on any VM in two resource
     * groups during a recurring maintenance window (2200-0400 every Sat and Sun, India Standard Time).
     *
     * @param manager Entry point to AlertsManagementManager.
     */
    public static void
        createOrUpdateARuleThatRemovesAllActionGroupsFromAllAlertsOnAnyVMInTwoResourceGroupsDuringARecurringMaintenanceWindow22000400EverySatAndSunIndiaStandardTime(
            com.azure.resourcemanager.alertsmanagement.AlertsManagementManager manager) {
        manager
            .alertProcessingRules()
            .define("RemoveActionGroupsRecurringMaintenance")
            .withRegion("Global")
            .withExistingResourceGroup("alertscorrelationrg")
            .withTags(mapOf())
            .withProperties(
                new AlertProcessingRuleProperties()
                    .withScopes(
                        Arrays
                            .asList(
                                "/subscriptions/subId1/resourceGroups/RGId1",
                                "/subscriptions/subId1/resourceGroups/RGId2"))
                    .withConditions(
                        Arrays
                            .asList(
                                new Condition()
                                    .withField(Field.TARGET_RESOURCE_TYPE)
                                    .withOperator(Operator.EQUALS)
                                    .withValues(Arrays.asList("microsoft.compute/virtualmachines"))))
                    .withSchedule(
                        new Schedule()
                            .withTimeZone("India Standard Time")
                            .withRecurrences(
                                Arrays
                                    .asList(
                                        new WeeklyRecurrence()
                                            .withStartTime("22:00:00")
                                            .withEndTime("04:00:00")
                                            .withDaysOfWeek(Arrays.asList(DaysOfWeek.SATURDAY, DaysOfWeek.SUNDAY)))))
                    .withActions(Arrays.asList(new RemoveAllActionGroups()))
                    .withDescription(
                        "Remove all ActionGroups from all Vitual machine Alerts during the recurring maintenance")
                    .withEnabled(true))
            .create();
    }

    /*
     * x-ms-original-file: specification/alertsmanagement/resource-manager/Microsoft.AlertsManagement/stable/2021-08-08/examples/AlertProcessingRules_Create_or_update_add_two_action_groups_all_Sev0_Sev1_two_resource_groups.json
     */
    /**
     * Sample code: Create or update a rule that adds two action groups to all Sev0 and Sev1 alerts in two resource
     * groups.
     *
     * @param manager Entry point to AlertsManagementManager.
     */
    public static void createOrUpdateARuleThatAddsTwoActionGroupsToAllSev0AndSev1AlertsInTwoResourceGroups(
        com.azure.resourcemanager.alertsmanagement.AlertsManagementManager manager) {
        manager
            .alertProcessingRules()
            .define("AddActionGroupsBySeverity")
            .withRegion("Global")
            .withExistingResourceGroup("alertscorrelationrg")
            .withTags(mapOf())
            .withProperties(
                new AlertProcessingRuleProperties()
                    .withScopes(
                        Arrays
                            .asList(
                                "/subscriptions/subId1/resourceGroups/RGId1",
                                "/subscriptions/subId1/resourceGroups/RGId2"))
                    .withConditions(
                        Arrays
                            .asList(
                                new Condition()
                                    .withField(Field.SEVERITY)
                                    .withOperator(Operator.EQUALS)
                                    .withValues(Arrays.asList("sev0", "sev1"))))
                    .withActions(
                        Arrays
                            .asList(
                                new AddActionGroups()
                                    .withActionGroupIds(
                                        Arrays
                                            .asList(
                                                "/subscriptions/subId1/resourcegroups/RGId1/providers/microsoft.insights/actiongroups/AGId1",
                                                "/subscriptions/subId1/resourcegroups/RGId1/providers/microsoft.insights/actiongroups/AGId2"))))
                    .withDescription("Add AGId1 and AGId2 to all Sev0 and Sev1 alerts in these resourceGroups")
                    .withEnabled(true))
            .create();
    }

    /*
     * x-ms-original-file: specification/alertsmanagement/resource-manager/Microsoft.AlertsManagement/stable/2021-08-08/examples/AlertProcessingRules_Create_or_update_add_action_group_all_alerts_in_subscription.json
     */
    /**
     * Sample code: Create or update a rule that adds an action group to all alerts in a subscription.
     *
     * @param manager Entry point to AlertsManagementManager.
     */
    public static void createOrUpdateARuleThatAddsAnActionGroupToAllAlertsInASubscription(
        com.azure.resourcemanager.alertsmanagement.AlertsManagementManager manager) {
        manager
            .alertProcessingRules()
            .define("AddActionGroupToSubscription")
            .withRegion("Global")
            .withExistingResourceGroup("alertscorrelationrg")
            .withTags(mapOf())
            .withProperties(
                new AlertProcessingRuleProperties()
                    .withScopes(Arrays.asList("/subscriptions/subId1"))
                    .withActions(
                        Arrays
                            .asList(
                                new AddActionGroups()
                                    .withActionGroupIds(
                                        Arrays
                                            .asList(
                                                "/subscriptions/subId1/resourcegroups/RGId1/providers/microsoft.insights/actiongroups/ActionGroup1"))))
                    .withDescription("Add ActionGroup1 to all alerts in the subscription")
                    .withEnabled(true))
            .create();
    }

    /*
     * x-ms-original-file: specification/alertsmanagement/resource-manager/Microsoft.AlertsManagement/stable/2021-08-08/examples/AlertProcessingRules_Create_or_update_remove_all_action_groups_specific_VM_one-off_maintenance_window.json
     */
    /**
     * Sample code: Create or update a rule that removes all action groups from alerts on a specific VM during a one-off
     * maintenance window (1800-2000 at a specific date, Pacific Standard Time).
     *
     * @param manager Entry point to AlertsManagementManager.
     */
    public static void
        createOrUpdateARuleThatRemovesAllActionGroupsFromAlertsOnASpecificVMDuringAOneOffMaintenanceWindow18002000AtASpecificDatePacificStandardTime(
            com.azure.resourcemanager.alertsmanagement.AlertsManagementManager manager) {
        manager
            .alertProcessingRules()
            .define("RemoveActionGroupsMaintenanceWindow")
            .withRegion("Global")
            .withExistingResourceGroup("alertscorrelationrg")
            .withTags(mapOf())
            .withProperties(
                new AlertProcessingRuleProperties()
                    .withScopes(
                        Arrays
                            .asList(
                                "/subscriptions/subId1/resourceGroups/RGId1/providers/Microsoft.Compute/virtualMachines/VMName"))
                    .withSchedule(
                        new Schedule()
                            .withEffectiveFrom("2021-04-15T18:00:00")
                            .withEffectiveUntil("2021-04-15T20:00:00")
                            .withTimeZone("Pacific Standard Time"))
                    .withActions(Arrays.asList(new RemoveAllActionGroups()))
                    .withDescription("Removes all ActionGroups from all Alerts on VMName during the maintenance window")
                    .withEnabled(true))
            .create();
    }

    /*
     * x-ms-original-file: specification/alertsmanagement/resource-manager/Microsoft.AlertsManagement/stable/2021-08-08/examples/AlertProcessingRules_Create_or_update_remove_all_action_groups_from_specific_alert_rule.json
     */
    /**
     * Sample code: Create or update a rule that removes all action groups from all alerts in a subscription coming from
     * a specific alert rule.
     *
     * @param manager Entry point to AlertsManagementManager.
     */
    public static void
        createOrUpdateARuleThatRemovesAllActionGroupsFromAllAlertsInASubscriptionComingFromASpecificAlertRule(
            com.azure.resourcemanager.alertsmanagement.AlertsManagementManager manager) {
        manager
            .alertProcessingRules()
            .define("RemoveActionGroupsSpecificAlertRule")
            .withRegion("Global")
            .withExistingResourceGroup("alertscorrelationrg")
            .withTags(mapOf())
            .withProperties(
                new AlertProcessingRuleProperties()
                    .withScopes(Arrays.asList("/subscriptions/subId1"))
                    .withConditions(
                        Arrays
                            .asList(
                                new Condition()
                                    .withField(Field.ALERT_RULE_ID)
                                    .withOperator(Operator.EQUALS)
                                    .withValues(
                                        Arrays
                                            .asList(
                                                "/subscriptions/suubId1/resourceGroups/Rgid2/providers/microsoft.insights/activityLogAlerts/RuleName"))))
                    .withActions(Arrays.asList(new RemoveAllActionGroups()))
                    .withDescription("Removes all ActionGroups from all Alerts that fire on above AlertRule")
                    .withEnabled(true))
            .create();
    }

    /*
     * x-ms-original-file: specification/alertsmanagement/resource-manager/Microsoft.AlertsManagement/stable/2021-08-08/examples/AlertProcessingRules_Create_or_update_remove_all_action_groups_outside_business_hours.json
     */
    /**
     * Sample code: Create or update a rule that removes all action groups outside business hours (Mon-Fri 09:00-17:00,
     * Eastern Standard Time).
     *
     * @param manager Entry point to AlertsManagementManager.
     */
    public static void
        createOrUpdateARuleThatRemovesAllActionGroupsOutsideBusinessHoursMonFri09001700EasternStandardTime(
            com.azure.resourcemanager.alertsmanagement.AlertsManagementManager manager) {
        manager
            .alertProcessingRules()
            .define("RemoveActionGroupsOutsideBusinessHours")
            .withRegion("Global")
            .withExistingResourceGroup("alertscorrelationrg")
            .withTags(mapOf())
            .withProperties(
                new AlertProcessingRuleProperties()
                    .withScopes(Arrays.asList("/subscriptions/subId1"))
                    .withSchedule(
                        new Schedule()
                            .withTimeZone("Eastern Standard Time")
                            .withRecurrences(
                                Arrays
                                    .asList(
                                        new DailyRecurrence().withStartTime("17:00:00").withEndTime("09:00:00"),
                                        new WeeklyRecurrence()
                                            .withDaysOfWeek(Arrays.asList(DaysOfWeek.SATURDAY, DaysOfWeek.SUNDAY)))))
                    .withActions(Arrays.asList(new RemoveAllActionGroups()))
                    .withDescription("Remove all ActionGroups outside business hours")
                    .withEnabled(true))
            .create();
    }

    @SuppressWarnings("unchecked")
    private static <T> Map<String, T> mapOf(Object... inputs) {
        Map<String, T> map = new HashMap<>();
        for (int i = 0; i < inputs.length; i += 2) {
            String key = (String) inputs[i];
            T value = (T) inputs[i + 1];
            map.put(key, value);
        }
        return map;
    }
}
