示例#1
0
def create_afd_origin_group(
        client: AFDOriginGroupsOperations,
        resource_group_name: str,
        profile_name: str,
        origin_group_name: str,
        load_balancing_sample_size: int,
        load_balancing_successful_samples_required: int,
        load_balancing_additional_latency_in_milliseconds: int,
        probe_request_type: HealthProbeRequestType,
        probe_protocol: str,
        probe_path: str,
        probe_interval_in_seconds: int = 240):

    # Add response error detection support once RP support it.
    health_probe_parameters = HealthProbeParameters(
        probe_path=probe_path,
        probe_request_type=probe_request_type,
        probe_protocol=probe_protocol,
        probe_interval_in_seconds=probe_interval_in_seconds)

    load_balancing_settings_parameters = LoadBalancingSettingsParameters(
        sample_size=load_balancing_sample_size,
        successful_samples_required=load_balancing_successful_samples_required,
        additional_latency_in_milliseconds=
        load_balancing_additional_latency_in_milliseconds)

    afd_origin_group = AFDOriginGroup(
        load_balancing_settings=load_balancing_settings_parameters,
        health_probe_settings=health_probe_parameters)

    return client.begin_create(resource_group_name, profile_name,
                               origin_group_name, afd_origin_group).result()
示例#2
0
def create_origin_group(client: OriginGroupsOperations,
                        resource_group_name: str,
                        profile_name: str,
                        endpoint_name: str,
                        name: str,
                        probe_path: Optional[str] = None,
                        probe_method: str = "HEAD",
                        probe_protocol: str = "HTTP",
                        probe_interval: int = 240,
                        origins: Optional[str] = None):

    # Move these to the parameters list once support is added in RP:
    response_error_detection_error_types: Optional[str] = None
    response_error_detection_failover_threshold: Optional[int] = None
    response_error_detection_status_code_ranges: Optional[str] = None

    from azure.mgmt.cdn.models import (
        OriginGroup, HealthProbeParameters,
        ResponseBasedOriginErrorDetectionParameters, ResourceReference)

    health_probe_settings = HealthProbeParameters(
        probe_path=probe_path,
        probe_request_type=probe_method,
        probe_protocol=probe_protocol,
        probe_interval_in_seconds=probe_interval)

    error_types = None
    if response_error_detection_error_types:
        error_types = response_error_detection_error_types.split(',')

    error_detection_settings = None
    if response_error_detection_error_types or \
       response_error_detection_failover_threshold or \
       response_error_detection_status_code_ranges:
        error_detection_settings = ResponseBasedOriginErrorDetectionParameters(
            response_based_detected_error_types=error_types,
            response_based_failover_threshold_percentage=
            response_error_detection_failover_threshold,
            http_error_ranges=_parse_ranges(
                response_error_detection_status_code_ranges))

    formatted_origins = []
    if origins:
        for origin in origins.split(','):
            # If the origin is not an ID, assume it's a name and format it as an ID.
            if '/' not in origin:
                origin = f'/subscriptions/{client.config.subscription_id}/resourceGroups/{resource_group_name}' \
                         f'/providers/Microsoft.Cdn/profiles/{profile_name}/endpoints/{endpoint_name}' \
                         f'/origins/{origin}'
            formatted_origins.append(ResourceReference(id=origin))

    origin_group = OriginGroup(
        origins=formatted_origins,
        health_probe_settings=health_probe_settings,
        response_based_origin_error_detection_settings=error_detection_settings
    )

    return client.create(resource_group_name, profile_name, endpoint_name,
                         name, origin_group).result()
示例#3
0
def update_afd_origin_group(
        client: AFDOriginGroupsOperations,
        resource_group_name: str,
        profile_name: str,
        origin_group_name: str,
        load_balancing_sample_size: int = None,
        load_balancing_successful_samples_required: int = None,
        load_balancing_additional_latency_in_milliseconds: int = None,
        probe_request_type: HealthProbeRequestType = None,
        probe_protocol: str = None,
        probe_path: str = None,
        probe_interval_in_seconds: int = None):

    # Move these to the parameters list once support is added in RP.
    existing = client.get(resource_group_name, profile_name, origin_group_name)

    health_probe_parameters = HealthProbeParameters(
        probe_path=probe_path,
        probe_request_type=probe_request_type,
        probe_protocol=probe_protocol,
        probe_interval_in_seconds=probe_interval_in_seconds)

    _update_mapper(existing.health_probe_settings, health_probe_parameters, [
        "probe_path", "probe_request_type", "probe_protocol",
        "probe_interval_in_seconds"
    ])

    load_balancing_settings_parameters = LoadBalancingSettingsParameters(
        sample_size=load_balancing_sample_size,
        successful_samples_required=load_balancing_successful_samples_required,
        additional_latency_in_milliseconds=
        load_balancing_additional_latency_in_milliseconds)
    _update_mapper(existing.load_balancing_settings,
                   load_balancing_settings_parameters, [
                       "sample_size", "successful_samples_required",
                       "additional_latency_in_milliseconds"
                   ])

    afd_origin_group = AFDOriginGroup(
        load_balancing_settings=load_balancing_settings_parameters,
        health_probe_settings=health_probe_parameters)

    return client.begin_create(resource_group_name, profile_name,
                               origin_group_name, afd_origin_group).result()
示例#4
0
def update_origin_group(client: OriginGroupsOperations,
                        resource_group_name: str,
                        profile_name: str,
                        endpoint_name: str,
                        name: str,
                        probe_path: str = None,
                        probe_method: str = None,
                        probe_protocol: str = None,
                        probe_interval: int = None,
                        origins: str = None):

    # Move these to the parameters list once support is added in RP:
    error_types: Optional[str] = None
    failover_threshold: Optional[int] = None
    status_code_ranges: Optional[str] = None

    from azure.mgmt.cdn.models import (
        OriginGroupUpdateParameters, HealthProbeParameters,
        ResponseBasedOriginErrorDetectionParameters, ResourceReference)

    # Get existing health probe settings:
    existing = client.get(resource_group_name, profile_name, endpoint_name,
                          name)
    # Allow removing properties explicitly by specifying as empty string, or
    # update without modifying by not specifying (value is None).
    if probe_path == '':
        probe_path = None
    elif probe_path is None:
        probe_path = existing.health_probe_settings.probe_path
    if probe_method == '':
        probe_method = None
    elif probe_method is None:
        probe_method = existing.health_probe_settings.probe_request_type
    if probe_protocol == '':
        probe_protocol = None
    elif probe_protocol is None:
        probe_protocol = existing.health_probe_settings.probe_protocol
    if probe_interval == '':
        probe_interval = None
    elif probe_interval is None:
        probe_interval = existing.health_probe_settings.probe_interval_in_seconds
    origins = origins or existing.origins

    health_probe_settings = HealthProbeParameters(
        probe_path=probe_path,
        probe_request_type=probe_method,
        probe_protocol=probe_protocol,
        probe_interval_in_seconds=probe_interval)

    if error_types is not None:
        error_types = error_types.split(',')
    if status_code_ranges is not None:
        status_code_ranges = _parse_ranges(status_code_ranges)

    error_detection_settings = None
    if error_types or \
       failover_threshold or \
       status_code_ranges:
        error_detection_settings = ResponseBasedOriginErrorDetectionParameters(
            response_based_detected_error_types=error_types,
            response_based_failover_threshold_percentage=failover_threshold,
            http_error_ranges=status_code_ranges)

    formatted_origins = []
    for origin in origins.split(','):
        # If the origin is not an ID, assume it's a name and format it as an ID.
        if '/' not in origin:
            origin = f'/subscriptions/{client.config.subscription_id}/resourceGroups/{resource_group_name}' \
                     f'/providers/Microsoft.Cdn/profiles/{profile_name}/endpoints/{endpoint_name}' \
                     f'/origins/{origin}'
        formatted_origins.append(ResourceReference(id=origin))

    origin_group = OriginGroupUpdateParameters(
        origins=formatted_origins,
        health_probe_settings=health_probe_settings,
        response_based_origin_error_detection_settings=error_detection_settings
    )

    # client.create isn't really a create, it's a PUT which is create or update,
    # client.update doesn't allow unsetting fields.
    return client.create(resource_group_name, profile_name, endpoint_name,
                         name, origin_group)