Esempio n. 1
0
def get_load_balancer_arns(load_balancer_names: List[str],
                           client: boto3.client) -> Dict[str, List[str]]:
    """
    Returns load balancer arns categorized by the type of load balancer

    return structure:
    {
        'network': ['load balancer arn'],
        'application': ['load balancer arn']
    }
    """
    results = {}
    logger.debug(
        f"Searching for load balancer name(s): {load_balancer_names}.")

    try:
        response = client.describe_load_balancers(Names=load_balancer_names)

        for lb in response["LoadBalancers"]:
            if lb["State"]["Code"] != "active":
                raise FailedActivity("Invalid state for load balancer {}: "
                                     "{} is not active".format(
                                         lb["LoadBalancerName"],
                                         lb["State"]["Code"]))
            results.setdefault(lb["Type"], []).append(lb["LoadBalancerArn"])
            results.setdefault("Names", []).append(lb["LoadBalancerName"])
    except ClientError as e:
        raise FailedActivity(e.response["Error"]["Message"])

    missing_lbs = [
        load_balancer for load_balancer in load_balancer_names
        if load_balancer not in results["Names"]
    ]
    if missing_lbs:
        raise FailedActivity(
            f"Unable to locate load balancer(s): {missing_lbs}")

    if not results:
        raise FailedActivity(
            "Unable to find any load balancer(s) matching name(s): {}".format(
                load_balancer_names))

    return results
Esempio n. 2
0
def get_load_balancer_arns(load_balancer_names: List[str],
                           client: boto3.client) -> Dict[str, List[str]]:
    """
    Returns load balancer arns categorized by the type of load balancer

    return structure:
    {
        'network': ['load balancer arn'],
        'application': ['load balancer arn']
    }
    """
    results = {}
    logger.debug('Searching for load balancer name(s): {}.'.format(
        load_balancer_names))

    try:
        response = client.describe_load_balancers(
            Names=load_balancer_names)

        for lb in response['LoadBalancers']:
            if lb['State']['Code'] != 'active':
                raise FailedActivity(
                    'Invalid state for load balancer {}: '
                    '{} is not active'.format(
                        lb['LoadBalancerName'], lb['State']['Code']))
            results.setdefault(lb['Type'], []).append(
                lb['LoadBalancerArn'])
            results.setdefault('Names', []).append(lb['LoadBalancerName'])
    except ClientError as e:
        raise FailedActivity(e.response['Error']['Message'])

    missing_lbs = [l for l in load_balancer_names if l not in results['Names']]
    if missing_lbs:
        raise FailedActivity(
            'Unable to locate load balancer(s): {}'.format(missing_lbs))

    if not results:
        raise FailedActivity(
            'Unable to find any load balancer(s) matching name(s): {}'.format(
                load_balancer_names))

    return results