def __init__(self,
              resource_group=None,
              load_balancer_name=None,
              api_version=constants.API_VER_NETWORK,
              logger=None,
              _ctx=ctx):
     resource_group = resource_group or \
         utils.get_resource_group(_ctx=_ctx)
     load_balancer_name = load_balancer_name or \
         utils.get_resource_name_ref(
             constants.REL_CONTAINED_IN_LB,
             'load_balancer_name',
             _ctx=_ctx)
     Resource.__init__(
         self,
         'Backend Address Pool',
         '/{0}/{1}/{2}/{3}'.format(
             'resourceGroups/{0}'.format(resource_group),
             'providers/Microsoft.Network',
             'loadBalancers/{0}'.format(load_balancer_name),
             'backendAddressPools'
         ),
         api_version=api_version,
         logger=logger,
         _ctx=_ctx)
Esempio n. 2
0
 def __init__(self,
              resource_group=None,
              load_balancer_name=None,
              api_version=constants.API_VER_NETWORK,
              logger=None,
              _ctx=ctx):
     resource_group = resource_group or \
         utils.get_resource_group(_ctx=_ctx)
     load_balancer_name = load_balancer_name or \
         utils.get_resource_name_ref(
             constants.REL_CONTAINED_IN_LB,
             'load_balancer_name',
             _ctx=_ctx)
     Resource.__init__(
         self,
         'Backend Address Pool',
         '/{0}/{1}/{2}/{3}'.format(
             'resourceGroups/{0}'.format(resource_group),
             'providers/Microsoft.Network',
             'loadBalancers/{0}'.format(load_balancer_name),
             'backendAddressPools'
         ),
         api_version=api_version,
         logger=logger,
         _ctx=_ctx)
def create_probe(ctx, **_):
    """Uses an existing, or creates a new, Load Balancer Probe"""
    # Check if invalid external resource
    if ctx.node.properties.get('use_external_resource', False) and \
       not ctx.node.properties.get('name'):
        raise cfy_exc.NonRecoverableError(
            '"use_external_resource" specified without a resource "name"')
    # Generate a name if it doesn't exist
    azure_config = ctx.node.properties.get('azure_config')
    if not azure_config.get("subscription_id"):
        azure_config = ctx.node.properties.get('client_config')
    else:
        ctx.logger.warn("azure_config is deprecated please use client_config, "
                        "in later version it will be removed")
    resource_group_name = utils.get_resource_group(ctx)
    load_balancer_name = ctx.node.properties.get('load_balancer_name') or \
        utils.get_resource_name_ref(constants.REL_CONTAINED_IN_LB,
                                    'load_balancer_name',
                                    _ctx=ctx)
    load_balancer = LoadBalancer(azure_config, ctx.logger)
    probe_name = ctx.node.properties.get('name')
    probe_name = \
        get_unique_lb_prop_name(load_balancer, resource_group_name,
                                load_balancer_name, "probes", probe_name)
    ctx.instance.runtime_properties['name'] = probe_name
    # Get an interface to the Load Balancer
    lb_rel = utils.get_relationship_by_type(
        ctx.instance.relationships,
        constants.REL_CONTAINED_IN_LB)
    lb_name = utils.get_resource_name(lb_rel.target)
    # Get the existing probes
    lb_data = load_balancer.get(resource_group_name, lb_name)
    lb_probes = lb_data.get('probes', list())
    lb_probes.append({
        'name': probe_name,
    })
    lb_probes = \
        utils.handle_resource_config_params(lb_probes,
                                            ctx.node.properties.get(
                                                'resource_config', {}))
    # Update the Load Balancer with the new probe
    lb_params = {
        'probes': lb_probes
    }
    # clean empty values from params
    lb_params = \
        utils.cleanup_empty_params(lb_params)
    try:
        result = load_balancer.create_or_update(resource_group_name, lb_name,
                                                lb_params)
        for item in result.get("probes"):
            if item.get("name") == probe_name:
                ctx.instance.runtime_properties['resource_id'] = item.get("id")
                ctx.instance.runtime_properties['resource'] = item
    except CloudError as cr:
        raise cfy_exc.NonRecoverableError(
            "create probe '{0}' "
            "failed with this error : {1}".format(probe_name,
                                                  cr.message)
            )
def create_rule(ctx, **_):
    """Uses an existing, or creates a new, Load Balancer Rule"""
    # Check if invalid external resource
    if ctx.node.properties.get('use_external_resource', False) and \
       not ctx.node.properties.get('name'):
        raise cfy_exc.NonRecoverableError(
            '"use_external_resource" specified without a resource "name"')
    # Generate a name if it doesn't exist
    azure_config = ctx.node.properties.get('azure_config')
    if not azure_config.get("subscription_id"):
        azure_config = ctx.node.properties.get('client_config')
    else:
        ctx.logger.warn("azure_config is deprecated please use client_config, "
                        "in later version it will be removed")
    resource_group_name = utils.get_resource_group(ctx)
    load_balancer_name = ctx.node.properties.get('load_balancer_name') or \
        utils.get_resource_name_ref(constants.REL_CONTAINED_IN_LB,
                                    'load_balancer_name',
                                    _ctx=ctx)
    load_balancer = LoadBalancer(azure_config, ctx.logger)
    lb_rule_name = ctx.node.properties.get('name')
    lb_rule_name = \
        get_unique_lb_prop_name(load_balancer, resource_group_name,
                                load_balancer_name, "load_balancing_rules",
                                lb_rule_name)
    ctx.instance.runtime_properties['name'] = lb_rule_name
    # Get an interface to the Load Balancer
    lb_rel = utils.get_relationship_by_type(
        ctx.instance.relationships,
        constants.REL_CONTAINED_IN_LB)
    lb_name = utils.get_resource_name(lb_rel.target)
    load_balancer = LoadBalancer(azure_config, ctx.logger)
    lb_data = load_balancer.get(resource_group_name, lb_name)
    # Get the Load Balancer Backend Pool/ Probe/ Frontend IP Configuration
    lb_be_pool_id = ""
    lb_probe_id = ""
    lb_fe_ipc_id = ""
    rel_pool_type = constants.REL_CONNECTED_TO_LB_BE_POOL
    rel_probe_type = constants.REL_CONNECTED_TO_LB_PROBE
    rel_fe_type = constants.REL_CONNECTED_TO_IPC
    for rel in ctx.instance.relationships:
        if isinstance(rel_pool_type, tuple):
            if any(x in rel.type_hierarchy for x in rel_pool_type):
                lb_be_pool_id = \
                    rel.target.instance.runtime_properties.get('resource_id')
        else:
            if rel_pool_type in rel.type_hierarchy:
                lb_be_pool_id = \
                    rel.target.instance.runtime_properties.get('resource_id')
        if isinstance(rel_probe_type, tuple):
            if any(x in rel.type_hierarchy for x in rel_probe_type):
                lb_probe_id = \
                    rel.target.instance.runtime_properties.get('resource_id')
        else:
            if constants.REL_CONNECTED_TO_LB_PROBE in rel.type_hierarchy:
                lb_probe_id = \
                    rel.target.instance.runtime_properties.get('resource_id')
        if isinstance(rel_fe_type, tuple):
            if any(x in rel.type_hierarchy for x in rel_fe_type):
                lb_fe_ipc_id = \
                    rel.target.instance.runtime_properties.get('resource_id')
        else:
            if constants.REL_CONNECTED_TO_IPC in rel.type_hierarchy:
                lb_fe_ipc_id = \
                    rel.target.instance.runtime_properties.get('resource_id')
    # Get the existing Load Balancer Rules
    lb_rules = lb_data.get('load_balancing_rules', list())
    lb_rules.append({
        'name': lb_rule_name,
        'frontend_ip_configuration': {'id': lb_fe_ipc_id},
        'backend_address_pool': {'id': lb_be_pool_id},
        'probe': {'id': lb_probe_id}
    })
    # Update the Load Balancer with the new rule
    lb_params = {
        'load_balancing_rules': lb_rules
    }
    # clean empty values from params
    lb_params = \
        utils.cleanup_empty_params(lb_params)
    try:
        result = load_balancer.create_or_update(resource_group_name, lb_name,
                                                lb_params)
        for item in result.get("load_balancing_rules"):
            if item.get("name") == lb_rule_name:
                ctx.instance.runtime_properties['resource_id'] = item.get("id")
                ctx.instance.runtime_properties['resource'] = item
    except CloudError as cr:
        raise cfy_exc.NonRecoverableError(
            "create load_balancing_rules '{0}' "
            "failed with this error : {1}".format(lb_rule_name,
                                                  cr.message)
            )