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

package com.azure.resourcemanager.monitor.generated;

import com.azure.core.util.Context;
import com.azure.resourcemanager.monitor.fluent.models.MetricAlertResourceInner;
import com.azure.resourcemanager.monitor.models.AggregationTypeEnum;
import com.azure.resourcemanager.monitor.models.DynamicMetricCriteria;
import com.azure.resourcemanager.monitor.models.DynamicThresholdFailingPeriods;
import com.azure.resourcemanager.monitor.models.DynamicThresholdOperator;
import com.azure.resourcemanager.monitor.models.DynamicThresholdSensitivity;
import com.azure.resourcemanager.monitor.models.MetricAlertAction;
import com.azure.resourcemanager.monitor.models.MetricAlertMultipleResourceMultipleMetricCriteria;
import com.azure.resourcemanager.monitor.models.MetricAlertSingleResourceMultipleMetricCriteria;
import com.azure.resourcemanager.monitor.models.MetricCriteria;
import com.azure.resourcemanager.monitor.models.MetricDimension;
import com.azure.resourcemanager.monitor.models.Operator;
import com.azure.resourcemanager.monitor.models.WebtestLocationAvailabilityCriteria;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/** Samples for MetricAlerts CreateOrUpdate. */
public final class MetricAlertsCreateOrUpdateSamples {
    /*
     * x-ms-original-file: specification/monitor/resource-manager/Microsoft.Insights/stable/2018-03-01/examples/createOrUpdateMetricAlertSubscription.json
     */
    /**
     * Sample code: Create or update an alert rule on Subscription.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createOrUpdateAnAlertRuleOnSubscription(com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .diagnosticSettings()
            .manager()
            .serviceClient()
            .getMetricAlerts()
            .createOrUpdateWithResponse(
                "gigtest",
                "MetricAlertAtSubscriptionLevel",
                new MetricAlertResourceInner()
                    .withLocation("global")
                    .withTags(mapOf())
                    .withDescription("This is the description of the rule1")
                    .withSeverity(3)
                    .withEnabled(true)
                    .withScopes(Arrays.asList("/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7"))
                    .withEvaluationFrequency(Duration.parse("PT1M"))
                    .withWindowSize(Duration.parse("PT15M"))
                    .withTargetResourceType("Microsoft.Compute/virtualMachines")
                    .withTargetResourceRegion("southcentralus")
                    .withCriteria(
                        new MetricAlertMultipleResourceMultipleMetricCriteria()
                            .withAllOf(
                                Arrays
                                    .asList(
                                        new MetricCriteria()
                                            .withName("High_CPU_80")
                                            .withMetricName("Percentage CPU")
                                            .withMetricNamespace("microsoft.compute/virtualmachines")
                                            .withTimeAggregation(AggregationTypeEnum.AVERAGE)
                                            .withDimensions(Arrays.asList())
                                            .withOperator(Operator.GREATER_THAN)
                                            .withThreshold(80.5))))
                    .withAutoMitigate(true)
                    .withActions(
                        Arrays
                            .asList(
                                new MetricAlertAction()
                                    .withActionGroupId(
                                        "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2")
                                    .withWebhookProperties(mapOf("key11", "value11", "key12", "value12")))),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/monitor/resource-manager/Microsoft.Insights/stable/2018-03-01/examples/createOrUpdateMetricAlertResourceGroup.json
     */
    /**
     * Sample code: Create or update an alert rule on Resource group(s).
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createOrUpdateAnAlertRuleOnResourceGroupS(com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .diagnosticSettings()
            .manager()
            .serviceClient()
            .getMetricAlerts()
            .createOrUpdateWithResponse(
                "gigtest1",
                "MetricAlertAtResourceGroupLevel",
                new MetricAlertResourceInner()
                    .withLocation("global")
                    .withTags(mapOf())
                    .withDescription("This is the description of the rule1")
                    .withSeverity(3)
                    .withEnabled(true)
                    .withScopes(
                        Arrays
                            .asList(
                                "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest1",
                                "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest2"))
                    .withEvaluationFrequency(Duration.parse("PT1M"))
                    .withWindowSize(Duration.parse("PT15M"))
                    .withTargetResourceType("Microsoft.Compute/virtualMachines")
                    .withTargetResourceRegion("southcentralus")
                    .withCriteria(
                        new MetricAlertMultipleResourceMultipleMetricCriteria()
                            .withAllOf(
                                Arrays
                                    .asList(
                                        new MetricCriteria()
                                            .withName("High_CPU_80")
                                            .withMetricName("Percentage CPU")
                                            .withMetricNamespace("microsoft.compute/virtualmachines")
                                            .withTimeAggregation(AggregationTypeEnum.AVERAGE)
                                            .withDimensions(Arrays.asList())
                                            .withOperator(Operator.GREATER_THAN)
                                            .withThreshold(80.5))))
                    .withAutoMitigate(true)
                    .withActions(
                        Arrays
                            .asList(
                                new MetricAlertAction()
                                    .withActionGroupId(
                                        "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2")
                                    .withWebhookProperties(mapOf("key11", "value11", "key12", "value12")))),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/monitor/resource-manager/Microsoft.Insights/stable/2018-03-01/examples/createOrUpdateMetricAlertWithDimensions.json
     */
    /**
     * Sample code: Create or update an alert rules with dimensions.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createOrUpdateAnAlertRulesWithDimensions(com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .diagnosticSettings()
            .manager()
            .serviceClient()
            .getMetricAlerts()
            .createOrUpdateWithResponse(
                "gigtest",
                "MetricAlertOnMultipleDimensions",
                new MetricAlertResourceInner()
                    .withLocation("global")
                    .withTags(mapOf())
                    .withDescription("This is the description of the rule1")
                    .withSeverity(3)
                    .withEnabled(true)
                    .withScopes(
                        Arrays
                            .asList(
                                "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.KeyVault/vaults/keyVaultResource"))
                    .withEvaluationFrequency(Duration.parse("PT1H"))
                    .withWindowSize(Duration.parse("P1D"))
                    .withCriteria(
                        new MetricAlertMultipleResourceMultipleMetricCriteria()
                            .withAllOf(
                                Arrays
                                    .asList(
                                        new MetricCriteria()
                                            .withName("Metric1")
                                            .withMetricName("Availability")
                                            .withMetricNamespace("Microsoft.KeyVault/vaults")
                                            .withTimeAggregation(AggregationTypeEnum.AVERAGE)
                                            .withDimensions(
                                                Arrays
                                                    .asList(
                                                        new MetricDimension()
                                                            .withName("ActivityName")
                                                            .withOperator("Include")
                                                            .withValues(Arrays.asList("*")),
                                                        new MetricDimension()
                                                            .withName("StatusCode")
                                                            .withOperator("Include")
                                                            .withValues(Arrays.asList("200"))))
                                            .withOperator(Operator.GREATER_THAN)
                                            .withThreshold(55.0))))
                    .withAutoMitigate(true)
                    .withActions(
                        Arrays
                            .asList(
                                new MetricAlertAction()
                                    .withActionGroupId(
                                        "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2")
                                    .withWebhookProperties(mapOf("key11", "value11", "key12", "value12")))),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/monitor/resource-manager/Microsoft.Insights/stable/2018-03-01/examples/createOrUpdateWebTestMetricAlert.json
     */
    /**
     * Sample code: Create or update a web test alert rule.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createOrUpdateAWebTestAlertRule(com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .diagnosticSettings()
            .manager()
            .serviceClient()
            .getMetricAlerts()
            .createOrUpdateWithResponse(
                "rg-example",
                "webtest-name-example",
                new MetricAlertResourceInner()
                    .withLocation("global")
                    .withTags(
                        mapOf(
                            "hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example",
                            "Resource",
                            "hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example",
                            "Resource"))
                    .withDescription("Automatically created alert rule for availability test \"component-example\" a")
                    .withSeverity(4)
                    .withEnabled(true)
                    .withScopes(
                        Arrays
                            .asList(
                                "/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example",
                                "/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example"))
                    .withEvaluationFrequency(Duration.parse("PT1M"))
                    .withWindowSize(Duration.parse("PT15M"))
                    .withCriteria(
                        new WebtestLocationAvailabilityCriteria()
                            .withWebTestId(
                                "/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example")
                            .withComponentId(
                                "/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example")
                            .withFailedLocationCount(2f))
                    .withActions(Arrays.asList()),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/monitor/resource-manager/Microsoft.Insights/stable/2018-03-01/examples/createOrUpdateDynamicMetricAlertSingleResource.json
     */
    /**
     * Sample code: Create or update a dynamic alert rule for Single Resource.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createOrUpdateADynamicAlertRuleForSingleResource(
        com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .diagnosticSettings()
            .manager()
            .serviceClient()
            .getMetricAlerts()
            .createOrUpdateWithResponse(
                "gigtest",
                "chiricutin",
                new MetricAlertResourceInner()
                    .withLocation("global")
                    .withTags(mapOf())
                    .withDescription("This is the description of the rule1")
                    .withSeverity(3)
                    .withEnabled(true)
                    .withScopes(
                        Arrays
                            .asList(
                                "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"))
                    .withEvaluationFrequency(Duration.parse("PT1M"))
                    .withWindowSize(Duration.parse("PT15M"))
                    .withCriteria(
                        new MetricAlertMultipleResourceMultipleMetricCriteria()
                            .withAllOf(
                                Arrays
                                    .asList(
                                        new DynamicMetricCriteria()
                                            .withName("High_CPU_80")
                                            .withMetricName("Percentage CPU")
                                            .withMetricNamespace("microsoft.compute/virtualmachines")
                                            .withTimeAggregation(AggregationTypeEnum.AVERAGE)
                                            .withDimensions(Arrays.asList())
                                            .withOperator(DynamicThresholdOperator.GREATER_OR_LESS_THAN)
                                            .withAlertSensitivity(DynamicThresholdSensitivity.MEDIUM)
                                            .withFailingPeriods(
                                                new DynamicThresholdFailingPeriods()
                                                    .withNumberOfEvaluationPeriods(4f)
                                                    .withMinFailingPeriodsToAlert(4f))
                                            .withIgnoreDataBefore(OffsetDateTime.parse("2019-04-04T21:00:00.000Z")))))
                    .withAutoMitigate(true)
                    .withActions(
                        Arrays
                            .asList(
                                new MetricAlertAction()
                                    .withActionGroupId(
                                        "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2")
                                    .withWebhookProperties(mapOf("key11", "value11", "key12", "value12")))),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/monitor/resource-manager/Microsoft.Insights/stable/2018-03-01/examples/createOrUpdateMetricAlertSingleResource.json
     */
    /**
     * Sample code: Create or update an alert rule for Single Resource.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createOrUpdateAnAlertRuleForSingleResource(
        com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .diagnosticSettings()
            .manager()
            .serviceClient()
            .getMetricAlerts()
            .createOrUpdateWithResponse(
                "gigtest",
                "chiricutin",
                new MetricAlertResourceInner()
                    .withLocation("global")
                    .withTags(mapOf())
                    .withDescription("This is the description of the rule1")
                    .withSeverity(3)
                    .withEnabled(true)
                    .withScopes(
                        Arrays
                            .asList(
                                "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"))
                    .withEvaluationFrequency(Duration.parse("Pt1m"))
                    .withWindowSize(Duration.parse("Pt15m"))
                    .withCriteria(
                        new MetricAlertSingleResourceMultipleMetricCriteria()
                            .withAllOf(
                                Arrays
                                    .asList(
                                        new MetricCriteria()
                                            .withName("High_CPU_80")
                                            .withMetricName("\\Processor(_Total)\\% Processor Time")
                                            .withTimeAggregation(AggregationTypeEnum.AVERAGE)
                                            .withDimensions(Arrays.asList())
                                            .withOperator(Operator.GREATER_THAN)
                                            .withThreshold(80.5))))
                    .withAutoMitigate(true)
                    .withActions(
                        Arrays
                            .asList(
                                new MetricAlertAction()
                                    .withActionGroupId(
                                        "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2")
                                    .withWebhookProperties(mapOf("key11", "value11", "key12", "value12")))),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/monitor/resource-manager/Microsoft.Insights/stable/2018-03-01/examples/createOrUpdateMetricAlertMultipleResource.json
     */
    /**
     * Sample code: Create or update an alert rule for Multiple Resource.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createOrUpdateAnAlertRuleForMultipleResource(
        com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .diagnosticSettings()
            .manager()
            .serviceClient()
            .getMetricAlerts()
            .createOrUpdateWithResponse(
                "gigtest",
                "MetricAlertOnMultipleResources",
                new MetricAlertResourceInner()
                    .withLocation("global")
                    .withTags(mapOf())
                    .withDescription("This is the description of the rule1")
                    .withSeverity(3)
                    .withEnabled(true)
                    .withScopes(
                        Arrays
                            .asList(
                                "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1",
                                "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2"))
                    .withEvaluationFrequency(Duration.parse("PT1M"))
                    .withWindowSize(Duration.parse("PT15M"))
                    .withTargetResourceType("Microsoft.Compute/virtualMachines")
                    .withTargetResourceRegion("southcentralus")
                    .withCriteria(
                        new MetricAlertMultipleResourceMultipleMetricCriteria()
                            .withAllOf(
                                Arrays
                                    .asList(
                                        new MetricCriteria()
                                            .withName("High_CPU_80")
                                            .withMetricName("Percentage CPU")
                                            .withMetricNamespace("microsoft.compute/virtualmachines")
                                            .withTimeAggregation(AggregationTypeEnum.AVERAGE)
                                            .withDimensions(Arrays.asList())
                                            .withOperator(Operator.GREATER_THAN)
                                            .withThreshold(80.5))))
                    .withAutoMitigate(true)
                    .withActions(
                        Arrays
                            .asList(
                                new MetricAlertAction()
                                    .withActionGroupId(
                                        "/subscriptions/14ddf0c5-77c5-4b53-84f6-e1fa43ad68f7/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2")
                                    .withWebhookProperties(mapOf("key11", "value11", "key12", "value12")))),
                Context.NONE);
    }

    /*
     * x-ms-original-file: specification/monitor/resource-manager/Microsoft.Insights/stable/2018-03-01/examples/createOrUpdateDynamicMetricAlertMultipleResource.json
     */
    /**
     * Sample code: Create or update a dynamic alert rule for Multiple Resources.
     *
     * @param azure The entry point for accessing resource management APIs in Azure.
     */
    public static void createOrUpdateADynamicAlertRuleForMultipleResources(
        com.azure.resourcemanager.AzureResourceManager azure) {
        azure
            .diagnosticSettings()
            .manager()
            .serviceClient()
            .getMetricAlerts()
            .createOrUpdateWithResponse(
                "gigtest",
                "MetricAlertOnMultipleResources",
                new MetricAlertResourceInner()
                    .withLocation("global")
                    .withTags(mapOf())
                    .withDescription("This is the description of the rule1")
                    .withSeverity(3)
                    .withEnabled(true)
                    .withScopes(
                        Arrays
                            .asList(
                                "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme1",
                                "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme2"))
                    .withEvaluationFrequency(Duration.parse("PT1M"))
                    .withWindowSize(Duration.parse("PT15M"))
                    .withTargetResourceType("Microsoft.Compute/virtualMachines")
                    .withTargetResourceRegion("southcentralus")
                    .withCriteria(
                        new MetricAlertMultipleResourceMultipleMetricCriteria()
                            .withAllOf(
                                Arrays
                                    .asList(
                                        new DynamicMetricCriteria()
                                            .withName("High_CPU_80")
                                            .withMetricName("Percentage CPU")
                                            .withMetricNamespace("microsoft.compute/virtualmachines")
                                            .withTimeAggregation(AggregationTypeEnum.AVERAGE)
                                            .withDimensions(Arrays.asList())
                                            .withOperator(DynamicThresholdOperator.GREATER_OR_LESS_THAN)
                                            .withAlertSensitivity(DynamicThresholdSensitivity.MEDIUM)
                                            .withFailingPeriods(
                                                new DynamicThresholdFailingPeriods()
                                                    .withNumberOfEvaluationPeriods(4f)
                                                    .withMinFailingPeriodsToAlert(4f)))))
                    .withAutoMitigate(true)
                    .withActions(
                        Arrays
                            .asList(
                                new MetricAlertAction()
                                    .withActionGroupId(
                                        "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/gigtest/providers/microsoft.insights/actiongroups/group2")
                                    .withWebhookProperties(mapOf("key11", "value11", "key12", "value12")))),
                Context.NONE);
    }

    @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;
    }
}
