def prepare_create_instance()

in tools/vm-migrator/src/migrator/instance.py [0:0]


def prepare_create_instance(compute, instance_uri: uri.Instance, network,
                    subnet_uri: uri.Subnet, alias_ip_ranges, node_group,
                    disk_names, ip, target_machine_type_uri: uri.MachineType,
                    image_project, target_service_account, target_scopes) \
        -> object:
    """
    Create Instance method create a new GCP VM from the machine image.
    """
    config = {
        'name': instance_uri.name,
        'networkInterfaces': [{
            'network': network,
            'subnetwork': subnet_uri.uri,
            'networkIP': ip
        }],
        'disks': [{
            'deviceName': device_name,
            'initializeParams': {
                'diskName': disk_name
            }
        } for (device_name, disk_name) in disk_names.items()],
        'sourceMachineImage': 'projects/' + image_project +
                              '/global/machineImages/' + instance_uri.name,
        'machineType': target_machine_type_uri.uri
    }

    # Reserve the static ip before creating the instance
    # If we get ip variable set, we expect to use the same ip in the
    # destination subnet
    if ip:
        logging.info(
            'Trying to create the machine %s while preserving its ips',
            instance_uri.name
        )
        reserve_internal_ip(compute, instance_uri, instance_uri.name,
                            subnet_uri, ip)
    else:
        # Since we cant reserve the same ip address passing
        # None as the ip to reserve random IP
        # The internal ip address is reserved with the same name
        # as machine name
        logging.info('Reserving random ip for machine %s ', instance_uri.name)
        reserve_internal_ip(compute, instance_uri, instance_uri.name,
                            subnet_uri, None)
        config['networkInterfaces'][0]['networkIP'] = \
            get_ip(compute, instance_uri, instance_uri.name)

    if node_group and get_updated_node_group(node_group):
        logging.info('Found a sole tenant maching running on node group %s',
                     node_group)
        config['scheduling'] = get_updated_node_group(node_group)['scheduling']

    if target_service_account and target_scopes:
        config['serviceAccounts'] = [{
            'email': target_service_account,
            'scopes': target_scopes,
        }]

    i = 1
    if len(alias_ip_ranges) > 0:
        logging.info('Found alias ip ranges, reserving it')
        for alias_ip in alias_ip_ranges:
            # If the alias ip is from the primary range then reserve it
            if (not alias_ip.get('subnetworkRangeName')
                    and alias_ip['ipCidrRange'].endswith('/32')):

                # Extract the ip address from something like 10.0.0.2/32
                length_ip = len(alias_ip['ipCidrRange']) - 3

                # Retain existing alias ip or create a random ip and use that
                if ip:
                    actual_ip = alias_ip['ipCidrRange'][0:length_ip]
                else:
                    actual_ip = None

                # Reserve the alias ip

                alias_ip_name = alias_ip.get('aliasIpName')
                if not alias_ip_name:
                    alias_ip_name = instance_uri.name + '-alias-ip-' + str(i)

                # Passing None in actual ip will reserve a
                # random ip for the name
                logging.info('reserving alias ip %s for machine %s', actual_ip,
                             instance_uri.name)
                reserve_internal_ip(compute, instance_uri, alias_ip_name,
                                    subnet_uri, actual_ip)
                # Since we are not retaining the ip we will use the
                # random reserved ip
                if not actual_ip:
                    alias_ip['ipCidrRange'] = get_ip(compute, instance_uri,
                                                     alias_ip_name) + '/32'
                i = i + 1
        config['networkInterfaces'][0]['aliasIpRanges'] = alias_ip_ranges

    return {
        'project': instance_uri.project,
        'zone': instance_uri.zone,
        'body': config
    }