Esempio n. 1
0
def create_metric_alert(client,
                        resource_group_name,
                        rule_name,
                        scopes,
                        condition,
                        disabled=False,
                        description=None,
                        tags=None,
                        actions=None,
                        severity=2,
                        window_size='5m',
                        evaluation_frequency='1m',
                        auto_mitigate=None,
                        target_resource_type=None,
                        target_resource_region=None):
    from azure.mgmt.monitor.models import (
        MetricAlertResource, MetricAlertSingleResourceMultipleMetricCriteria,
        MetricAlertMultipleResourceMultipleMetricCriteria, MetricCriteria)
    from azure.cli.core import CLIError
    # generate names for the conditions
    for i, cond in enumerate(condition):
        cond.name = 'cond{}'.format(i)
    criteria = None
    resource_type, scope_type = _parse_resource_and_scope_type(scopes)
    if scope_type in ['resource_group', 'subscription']:
        if target_resource_type is None or target_resource_region is None:
            raise CLIError(
                '--target-resource-type and --target-resource-region must be provided.'
            )
        criteria = MetricAlertMultipleResourceMultipleMetricCriteria(
            all_of=condition)
    else:
        if len(scopes) == 1 and isinstance(condition, MetricCriteria):
            criteria = MetricAlertSingleResourceMultipleMetricCriteria(
                all_of=condition)
        else:
            criteria = MetricAlertMultipleResourceMultipleMetricCriteria(
                all_of=condition)
            target_resource_type = resource_type
            target_resource_region = target_resource_region if target_resource_region else 'global'

    kwargs = {
        'description': description,
        'severity': severity,
        'enabled': not disabled,
        'scopes': scopes,
        'evaluation_frequency': evaluation_frequency,
        'window_size': window_size,
        'criteria': criteria,
        'target_resource_type': target_resource_type,
        'target_resource_region': target_resource_region,
        'actions': actions,
        'tags': tags,
        'location': 'global',
        'auto_mitigate': auto_mitigate
    }
    return client.create_or_update(resource_group_name, rule_name,
                                   MetricAlertResource(**kwargs))
Esempio n. 2
0
def create_metric_alert(client,
                        resource_group_name,
                        rule_name,
                        scopes,
                        condition,
                        disabled=False,
                        description=None,
                        tags=None,
                        actions=None,
                        severity=2,
                        window_size='5m',
                        evaluation_frequency='1m',
                        auto_mitigate=None):
    from azure.mgmt.monitor.models import (
        MetricAlertResource, MetricAlertSingleResourceMultipleMetricCriteria,
        MetricAlertMultipleResourceMultipleMetricCriteria)
    # generate names for the conditions
    for i, cond in enumerate(condition):
        cond.name = 'cond{}'.format(i)
    criteria = None
    target_resource_type = None
    target_resource_region = None
    if len(scopes) == 1:
        criteria = MetricAlertSingleResourceMultipleMetricCriteria(
            all_of=condition)
    else:
        criteria = MetricAlertMultipleResourceMultipleMetricCriteria(
            all_of=condition)
        target_resource_type = _parse_resource_type(scopes)
        target_resource_region = 'global'
    kwargs = {
        'description': description,
        'severity': severity,
        'enabled': not disabled,
        'scopes': scopes,
        'evaluation_frequency': evaluation_frequency,
        'window_size': window_size,
        'criteria': criteria,
        'target_resource_type': target_resource_type,
        'target_resource_region': target_resource_region,
        'actions': actions,
        'tags': tags,
        'location': 'global',
        'auto_mitigate': auto_mitigate
    }
    return client.create_or_update(resource_group_name, rule_name,
                                   MetricAlertResource(**kwargs))