def get_nlb_data()

in network-load-balancer-copy-utility/copy_network_load_balancer.py [0:0]


def get_nlb_data(elb_data, region, load_balancer_name, ssl_hc_path):
    """
    Render a dictionary which contains Network Load Balancer attributes
    """
    if debug:
        logger.debug("Building the Network Load Balancer data structure")
    # this is used for building the load balancer spec
    nlb_data = {'VpcId': elb_data['LoadBalancerDescriptions'][0]['VPCId'], 'Region': region,
                'Nlb_name': elb_data['LoadBalancerDescriptions'][0]['LoadBalancerName'],
                'Subnets': elb_data['LoadBalancerDescriptions'][0]['Subnets'],
                'Security_groups': elb_data['LoadBalancerDescriptions'][0]['SecurityGroups'],
                'Scheme': elb_data['LoadBalancerDescriptions'][0]['Scheme'],
                'Tags': elb_data['TagDescriptions'][0]['Tags'],
                'listeners': [],
                'Type': 'network',
                'target_group_attributes': [],
                'target_group_arns': []}

    # this is used for building the listeners specs
    for elb_listener in elb_data['LoadBalancerDescriptions'][0]['ListenerDescriptions']:
        listener = {'Protocol': elb_listener['Listener']['Protocol'],
                    'Port': elb_listener['Listener']['LoadBalancerPort'],
                    'TargetGroup_Port': elb_listener['Listener']['InstancePort'],
                    'TargetGroup_Protocol': elb_listener['Listener']['InstanceProtocol']}
        targetgroup_attribute = {
            'dereg_timeout_seconds_delay': str(elb_data['LoadBalancerAttributes']['ConnectionDraining']['Timeout']),
            'TargetGroup_Port': elb_listener['Listener']['InstancePort']
        }
        nlb_data['listeners'].append(listener)
        nlb_data['target_group_attributes'].append(targetgroup_attribute)

    # this is used for building the target groups
    nlb_data['target_groups'] = []
    target_group = {}
    # Get health check target
    hc_target = elb_data['LoadBalancerDescriptions'][
        0]['HealthCheck']['Target']
    # Set health check interval
    if elb_data['LoadBalancerDescriptions'][0]['HealthCheck']['Interval'] < 15:
        print("The minimal supported health check interval is 10. Setting it to 10 seconds")
        target_group['HealthCheckIntervalSeconds'] = 10
    else:
        print("The health check internal is set to 30 seconds")
        target_group['HealthCheckIntervalSeconds'] = 30
    # Set healthy and unhealthy threshold to the same value which is the
    # healthy threshold of Classic Load Balancer
    target_group['HealthyThresholdCount'] = elb_data['LoadBalancerDescriptions'][0]['HealthCheck'][
        'HealthyThreshold']
    target_group['UnhealthyThresholdCount'] = elb_data['LoadBalancerDescriptions'][0]['HealthCheck'][
        'HealthyThreshold']

    # Set VPC ID
    target_group['VpcId'] = elb_data[
        'LoadBalancerDescriptions'][0]['VPCId']
    # Set health check protocol
    target_group['HealthCheckProtocol'] = hc_target.split(':')[0]
    # If health check protocol is TCP
    if hc_target.split(':')[0] == "TCP":
        target_group['HealthCheckPort'] = hc_target.split(':')[1]
    # If health check protocol is HTTP or HTTPs
    elif hc_target.split(':')[0] == "SSL":
        target_group['HealthCheckProtocol'] = "HTTPS"
        target_group['HealthCheckPort'] = hc_target.split(':')[1]
        target_group['HealthCheckPath'] = ssl_hc_path
    else:
        target_group['HealthCheckPort'] = hc_target.split(':')[1].split('/')[0]
        target_group['HealthCheckPath'] = '/' + hc_target.split('/', 1)[1]

    for listener in nlb_data['listeners']:
        target_group['Protocol'] = listener['TargetGroup_Protocol']
        target_group['Port'] = listener['TargetGroup_Port']
        # target group name comes from the first 18 character of the Classic Load Balancer name, \
        # "-nlb-tg-" and target group port.
        target_group['Name'] = load_balancer_name[: 18] + "-nlb-tg-" + \
            str(listener['TargetGroup_Port'])
        # Only append unique Target Group
        if target_group not in nlb_data['target_groups']:
            nlb_data['target_groups'].append(target_group.copy())
    # Get registered backend instances
    nlb_data['instanceIds'] = []
    for instance in elb_data['LoadBalancerDescriptions'][0]['Instances']:
        nlb_data['instanceIds'].append(instance['InstanceId'])
    if debug:
        logger.debug("nlb_data:")
        logger.debug(nlb_data)
    return nlb_data