def _parse_instance_bundles()

in ec2instanceconnectcli/input_parser.py [0:0]


def _parse_instance_bundles(instance_bundles):
    """
    Processes instance bundles.  The goal is to establish the final data on instance IDs
    and FQDNs/IPs and any target file to include for sftp
    This includes data validity checks.

    :param instance_bundles: The unprocessed instance bundle objects
    :type instance_bundles: list
    :return: Processed instance bundle objects
    :rtype: list
    """
    for bundle in instance_bundles:
        # We parse target in a specific order based on mode due to how commands prioritize/mark parts optional
        if '@' in bundle['target']:
            if len(bundle['target'].split('@')) > 2:
                # Host details includes an @.  Invalid.
                raise AssertionError('Invalid target')
            # A user was specified
            bundle['username'], bundle['target'] = bundle['target'].split('@')
        if ':' in bundle['target']:
            # May be present for sftp
            bundle['target'], bundle['file'] = bundle['target'].split(':')

        if bundle.get('target', None):
            if INSTANCE_ID_RE.match(bundle['target'].lower()):
                # We might have an instance as the target AND an instance flag and they don't match
                # Since ssh prioritizes user@ over -l login_name, we will prioritize the target over the flag
                # If we don't have the flag, we use the target anyways
                bundle['instance_id'] = bundle['target']
                bundle['target'] = None

        if len(bundle.get('username', '')) == 0:
            bundle['username'] = 'ec2-user'

        # Validate region & zone if present
        if bundle.get('region') and len(bundle.get('region')) > 0:
            if REGION_RE.match(bundle['region']) is None:
                raise AssertionError('{0} is not a valid region'.format(bundle['region']))

        if bundle.get('zone') and len(bundle.get('zone')) > 0:
            if ZONE_RE.match(bundle['zone']) is None:
                raise AssertionError('{0} is not a valid zone'.format(bundle['zone']))

        # Validate username
        if not _is_valid_username(bundle['username']):
            raise AssertionError('{0} is not a valid UNIX username'.format(bundle['username']))

        # Validate instance ID format
        if len(bundle['instance_id']) > 0 and not INSTANCE_ID_RE.match(bundle['instance_id'].lower()):
            raise AssertionError('Invalid instance_id')

        # Validate DNS/IP/hostname/etc
        if bundle.get('target', None):
            if not _is_valid_target(bundle.get('target', '')):
                # It might be an IP
                raise AssertionError('Invalid target')

    return instance_bundles