Example #1
0
    def CreateRequests(self, args):
        _ValidateDiskFlags(args)
        instance_utils.ValidateLocalSsdFlags(args)

        # TODO(user) drop test after CSEK goes GA
        if hasattr(args, 'csek_key_file'):
            self.csek_keys = csek_utils.CsekKeyStore.FromArgs(args)
        else:
            self.csek_keys = None

        if args.maintenance_policy:
            on_host_maintenance = (
                self.messages.Scheduling.OnHostMaintenanceValueValuesEnum(
                    args.maintenance_policy))
        else:
            on_host_maintenance = None

        # Note: We always specify automaticRestart=False for preemptible VMs. This
        # makes sense, since no-restart-on-failure is defined as "store-true", and
        # thus can't be given an explicit value. Hence it either has its default
        # value (in which case we override it for convenience's sake to the only
        # setting that makes sense for preemptible VMs), or the user actually
        # specified no-restart-on-failure, the only usable setting.
        if args.preemptible:
            scheduling = self.messages.Scheduling(
                automaticRestart=False,
                onHostMaintenance=on_host_maintenance,
                preemptible=True)
        else:
            scheduling = self.messages.Scheduling(
                automaticRestart=args.restart_on_failure,
                onHostMaintenance=on_host_maintenance)

        service_accounts = self.CreateServiceAccountMessages(args)

        if args.tags:
            tags = self.messages.Tags(items=args.tags)
        else:
            tags = None

        metadata = metadata_utils.ConstructMetadataMessage(
            self.messages,
            metadata=args.metadata,
            metadata_from_file=args.metadata_from_file)

        # If the user already provided an initial Windows password and
        # username through metadata, then there is no need to check
        # whether the image or the boot disk is Windows.

        boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
        utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

        instance_refs = self.CreateZonalReferences(args.names, args.zone)

        # Check if the zone is deprecated or has maintenance coming.
        self.WarnForZonalCreation(instance_refs)

        network_interface = self.CreateNetworkInterfaceMessage(
            args, instance_refs)

        # The element at index i is the machine type URI for instance
        # i. We build this list here because we want to delay work that
        # requires API calls as much as possible. This leads to a better
        # user experience because the tool can fail fast upon a spelling
        # mistake instead of delaying the user by making API calls whose
        # purpose has already been rendered moot by the spelling mistake.
        machine_type_uris = []

        # Setting the machine type
        machine_type_name = instance_utils.InterpretMachineType(args)

        for instance_ref in instance_refs:
            # Check to see if the custom machine type ratio is supported
            instance_utils.CheckCustomCpuRamRatio(self, instance_ref.zone,
                                                  machine_type_name)
            machine_type_uris.append(
                self.CreateZonalReference(
                    machine_type_name,
                    instance_ref.zone,
                    resource_type='machineTypes').SelfLink())

        create_boot_disk = not _UseExistingBootDisk(args)
        if create_boot_disk:
            image_uri, _ = self.ExpandImageFlag(args,
                                                return_image_resource=False)
        else:
            image_uri = None

        # A list of lists where the element at index i contains a list of
        # disk messages that should be set for the instance at index i.
        disks_messages = []

        # A mapping of zone to boot disk references for all existing boot
        # disks that are being attached.
        # TODO(user): Simplify this once resources.Resource becomes
        # hashable.
        existing_boot_disks = {}

        for instance_ref in instance_refs:
            persistent_disks, boot_disk_ref = (
                self.CreatePersistentAttachedDiskMessages(args, instance_ref))
            local_ssds = [
                instance_utils.CreateLocalSsdMessage(self,
                                                     x.get('device-name'),
                                                     x.get('interface'),
                                                     instance_ref.zone)
                for x in args.local_ssd or []
            ]
            if create_boot_disk:
                boot_disk = self.CreateDefaultBootAttachedDiskMessage(
                    args, boot_disk_size_gb, image_uri, instance_ref)
                persistent_disks = [boot_disk] + persistent_disks
            else:
                existing_boot_disks[boot_disk_ref.zone] = boot_disk_ref
            disks_messages.append(persistent_disks + local_ssds)

        requests = []
        for instance_ref, machine_type_uri, disks in zip(
                instance_refs, machine_type_uris, disks_messages):
            requests.append(
                self.messages.ComputeInstancesInsertRequest(
                    instance=self.messages.Instance(
                        canIpForward=args.can_ip_forward,
                        disks=disks,
                        description=args.description,
                        machineType=machine_type_uri,
                        metadata=metadata,
                        name=instance_ref.Name(),
                        networkInterfaces=[network_interface],
                        serviceAccounts=service_accounts,
                        scheduling=scheduling,
                        tags=tags,
                    ),
                    project=self.project,
                    zone=instance_ref.zone))

        return requests
Example #2
0
    def CreateRequests(self, args):
        """Creates and returns an InstanceTemplates.Insert request.

    Args:
      args: the argparse arguments that this command was invoked with.

    Returns:
      request: a ComputeInstanceTemplatesInsertRequest message object
    """
        self.ValidateDiskFlags(args)
        instances_flags.ValidateLocalSsdFlags(args)

        boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
        utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

        instance_template_ref = (
            instance_templates_flags.INSTANCE_TEMPLATE_ARG.ResolveAsResource(
                args, self.resources))

        metadata = metadata_utils.ConstructMetadataMessage(
            self.messages,
            metadata=args.metadata,
            metadata_from_file=args.metadata_from_file)

        network_interface = instance_template_utils.CreateNetworkInterfaceMessage(
            scope_prompter=self,
            messages=self.messages,
            network=args.network,
            region=args.region,
            subnet=args.subnet,
            address=(instance_template_utils.EPHEMERAL_ADDRESS
                     if not args.no_address and not args.address else
                     args.address))

        scheduling = instance_utils.CreateSchedulingMessage(
            messages=self.messages,
            maintenance_policy=args.maintenance_policy,
            preemptible=args.preemptible,
            restart_on_failure=args.restart_on_failure)

        service_accounts = instance_utils.CreateServiceAccountMessages(
            messages=self.messages,
            scopes=([] if args.no_scopes else args.scopes))

        create_boot_disk = not instance_utils.UseExistingBootDisk(args.disk
                                                                  or [])
        if create_boot_disk:
            image_uri, _ = self.ExpandImageFlag(args,
                                                return_image_resource=True)
        else:
            image_uri = None

        if args.tags:
            tags = self.messages.Tags(items=args.tags)
        else:
            tags = None

        persistent_disks = (
            instance_template_utils.CreatePersistentAttachedDiskMessages(
                self.messages, args.disk or []))

        if create_boot_disk:
            boot_disk_list = [
                instance_template_utils.CreateDefaultBootAttachedDiskMessage(
                    messages=self.messages,
                    disk_type=args.boot_disk_type,
                    disk_device_name=args.boot_disk_device_name,
                    disk_auto_delete=args.boot_disk_auto_delete,
                    disk_size_gb=boot_disk_size_gb,
                    image_uri=image_uri)
            ]
        else:
            boot_disk_list = []

        local_ssds = [
            instance_utils.CreateLocalSsdMessage(self, x.get('device-name'),
                                                 x.get('interface'))
            for x in args.local_ssd or []
        ]

        disks = boot_disk_list + persistent_disks + local_ssds

        machine_type = instance_utils.InterpretMachineType(
            machine_type=args.machine_type,
            custom_cpu=args.custom_cpu,
            custom_memory=args.custom_memory)

        request = self.messages.ComputeInstanceTemplatesInsertRequest(
            instanceTemplate=self.messages.InstanceTemplate(
                properties=self.messages.InstanceProperties(
                    machineType=machine_type,
                    disks=disks,
                    canIpForward=args.can_ip_forward,
                    metadata=metadata,
                    networkInterfaces=[network_interface],
                    serviceAccounts=service_accounts,
                    scheduling=scheduling,
                    tags=tags,
                ),
                description=args.description,
                name=instance_template_ref.Name(),
            ),
            project=self.project)

        return [request]
Example #3
0
    def Run(self, args):
        """Issues an InstanceTemplates.Insert request.

    Args:
      args: the argparse arguments that this command was invoked with.

    Returns:
      an InstanceTemplate message object
    """
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        instances_flags.ValidateDockerArgs(args)
        instances_flags.ValidateDiskCommonFlags(args)
        instances_flags.ValidateLocalSsdFlags(args)
        instances_flags.ValidateServiceAccountAndScopeArgs(args)
        if instance_utils.UseExistingBootDisk(args.disk or []):
            raise exceptions.InvalidArgumentException(
                '--disk', 'Boot disk specified for containerized VM.')

        boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
        utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

        instance_template_ref = (
            CreateFromContainer.InstanceTemplateArg.ResolveAsResource(
                args, holder.resources))

        user_metadata = metadata_utils.ConstructMetadataMessage(
            client.messages,
            metadata=args.metadata,
            metadata_from_file=args.metadata_from_file)
        containers_utils.ValidateUserMetadata(user_metadata)

        network_interface = instance_template_utils.CreateNetworkInterfaceMessage(
            resources=holder.resources,
            scope_lister=flags.GetDefaultScopeLister(client),
            messages=client.messages,
            network=args.network,
            region=args.region,
            subnet=args.subnet,
            address=(instance_template_utils.EPHEMERAL_ADDRESS
                     if not args.no_address and not args.address else
                     args.address),
            network_tier=args.network_tier)

        scheduling = instance_utils.CreateSchedulingMessage(
            messages=client.messages,
            maintenance_policy=args.maintenance_policy,
            preemptible=args.preemptible,
            restart_on_failure=args.restart_on_failure)

        if args.no_service_account:
            service_account = None
        else:
            service_account = args.service_account
        service_accounts = instance_utils.CreateServiceAccountMessages(
            messages=client.messages,
            scopes=[] if args.no_scopes else args.scopes,
            service_account=service_account)

        image_uri = containers_utils.ExpandCosImageFlag(client)

        machine_type = instance_utils.InterpretMachineType(
            machine_type=args.machine_type,
            custom_cpu=args.custom_cpu,
            custom_memory=args.custom_memory,
            ext=getattr(args, 'custom_extensions', None))

        metadata = containers_utils.CreateMetadataMessage(
            client.messages, args.run_as_privileged, args.container_manifest,
            args.docker_image, args.port_mappings, args.run_command,
            user_metadata, instance_template_ref.Name())

        request = client.messages.ComputeInstanceTemplatesInsertRequest(
            instanceTemplate=client.messages.InstanceTemplate(
                properties=client.messages.InstanceProperties(
                    machineType=machine_type,
                    disks=self._CreateDiskMessages(
                        holder, args, boot_disk_size_gb, image_uri,
                        instance_template_ref.project),
                    canIpForward=args.can_ip_forward,
                    metadata=metadata,
                    minCpuPlatform=args.min_cpu_platform,
                    networkInterfaces=[network_interface],
                    serviceAccounts=service_accounts,
                    scheduling=scheduling,
                    tags=containers_utils.CreateTagsMessage(
                        client.messages, args.tags),
                ),
                description=args.description,
                name=instance_template_ref.Name(),
            ),
            project=instance_template_ref.project)

        return client.MakeRequests([(client.apitools_client.instanceTemplates,
                                     'Insert', request)])
Example #4
0
    def _CreateRequests(self, args):
        instances_flags.ValidateDiskFlags(args)
        instances_flags.ValidateLocalSsdFlags(args)
        instances_flags.ValidateNicFlags(args)
        instances_flags.ValidateServiceAccountAndScopeArgs(args)
        instances_flags.ValidateAcceleratorArgs(args)

        # This feature is only exposed in alpha/beta
        allow_rsa_encrypted = self.ReleaseTrack() in [
            base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA
        ]
        self.csek_keys = csek_utils.CsekKeyStore.FromArgs(
            args, allow_rsa_encrypted)

        scheduling = instance_utils.CreateSchedulingMessage(
            messages=self.messages,
            maintenance_policy=args.maintenance_policy,
            preemptible=args.preemptible,
            restart_on_failure=args.restart_on_failure)

        if args.tags:
            tags = self.messages.Tags(items=args.tags)
        else:
            tags = None

        metadata = metadata_utils.ConstructMetadataMessage(
            self.messages,
            metadata=args.metadata,
            metadata_from_file=args.metadata_from_file)

        # If the user already provided an initial Windows password and
        # username through metadata, then there is no need to check
        # whether the image or the boot disk is Windows.

        boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
        utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

        instance_refs = instances_flags.INSTANCES_ARG.ResolveAsResource(
            args,
            self.resources,
            scope_lister=flags.GetDefaultScopeLister(self.compute_client,
                                                     self.project))

        # Check if the zone is deprecated or has maintenance coming.
        self.WarnForZonalCreation(instance_refs)

        network_interface_arg = getattr(args, 'network_interface', None)
        if network_interface_arg:
            network_interfaces = instance_utils.CreateNetworkInterfaceMessages(
                resources=self.resources,
                compute_client=self.compute_client,
                network_interface_arg=network_interface_arg,
                instance_refs=instance_refs)
        else:
            network_interfaces = [
                instance_utils.CreateNetworkInterfaceMessage(
                    resources=self.resources,
                    compute_client=self.compute_client,
                    network=args.network,
                    subnet=args.subnet,
                    private_network_ip=args.private_network_ip,
                    no_address=args.no_address,
                    address=args.address,
                    instance_refs=instance_refs)
            ]

        machine_type_uris = instance_utils.CreateMachineTypeUris(
            resources=self.resources,
            compute_client=self.compute_client,
            project=self.project,
            machine_type=args.machine_type,
            custom_cpu=args.custom_cpu,
            custom_memory=args.custom_memory,
            ext=getattr(args, 'custom_extensions', None),
            instance_refs=instance_refs)

        create_boot_disk = not instance_utils.UseExistingBootDisk(args.disk
                                                                  or [])
        if create_boot_disk:
            image_uri, _ = self.ExpandImageFlag(
                user_project=self.project,
                image=args.image,
                image_family=args.image_family,
                image_project=args.image_project,
                return_image_resource=False)
        else:
            image_uri = None

        # A list of lists where the element at index i contains a list of
        # disk messages that should be set for the instance at index i.
        disks_messages = []

        # A mapping of zone to boot disk references for all existing boot
        # disks that are being attached.
        # TODO(user): Simplify this once resources.Resource becomes
        # hashable.
        existing_boot_disks = {}

        for instance_ref in instance_refs:
            persistent_disks, boot_disk_ref = (
                instance_utils.CreatePersistentAttachedDiskMessages(
                    self.resources, self.compute_client, self.csek_keys,
                    args.disk or [], instance_ref))
            persistent_create_disks = (
                instance_utils.CreatePersistentCreateDiskMessages(
                    self, self.compute_client, self.resources, self.csek_keys,
                    getattr(args, 'create_disk', []), instance_ref))
            local_ssds = []
            for x in args.local_ssd or []:
                local_ssds.append(
                    instance_utils.CreateLocalSsdMessage(
                        self.resources, self.messages, x.get('device-name'),
                        x.get('interface'), instance_ref.zone))

            if create_boot_disk:
                boot_disk = instance_utils.CreateDefaultBootAttachedDiskMessage(
                    self.compute_client,
                    self.resources,
                    disk_type=args.boot_disk_type,
                    disk_device_name=args.boot_disk_device_name,
                    disk_auto_delete=args.boot_disk_auto_delete,
                    disk_size_gb=boot_disk_size_gb,
                    require_csek_key_create=(args.require_csek_key_create
                                             if self.csek_keys else None),
                    image_uri=image_uri,
                    instance_ref=instance_ref,
                    csek_keys=self.csek_keys)
                persistent_disks = [boot_disk] + persistent_disks
            else:
                existing_boot_disks[boot_disk_ref.zone] = boot_disk_ref
            disks_messages.append(persistent_disks + persistent_create_disks +
                                  local_ssds)

        accelerator_args = getattr(args, 'accelerator', None)

        project_to_sa = {}
        requests = []
        for instance_ref, machine_type_uri, disks in zip(
                instance_refs, machine_type_uris, disks_messages):
            if instance_ref.project not in project_to_sa:
                scopes = None
                if not args.no_scopes and not args.scopes:
                    # User didn't provide any input on scopes. If project has no default
                    # service account then we want to create a VM with no scopes
                    request = (self.compute.projects, 'Get',
                               self.messages.ComputeProjectsGetRequest(
                                   project=instance_ref.project))
                    errors = []
                    result = self.compute_client.MakeRequests([request],
                                                              errors)
                    if not errors:
                        if not result[0].defaultServiceAccount:
                            scopes = []
                            log.status.Print(
                                'There is no default service account for project {}. '
                                'Instance {} will not have scopes.'.format(
                                    instance_ref.project, instance_ref.Name))
                if scopes is None:
                    scopes = [] if args.no_scopes else args.scopes

                if args.no_service_account:
                    service_account = None
                else:
                    service_account = args.service_account
                service_accounts = instance_utils.CreateServiceAccountMessages(
                    messages=self.messages,
                    scopes=scopes,
                    service_account=service_account)
                project_to_sa[instance_ref.project] = service_accounts

            instance = self.messages.Instance(
                canIpForward=args.can_ip_forward,
                disks=disks,
                description=args.description,
                machineType=machine_type_uri,
                metadata=metadata,
                name=instance_ref.Name(),
                networkInterfaces=network_interfaces,
                serviceAccounts=project_to_sa[instance_ref.project],
                scheduling=scheduling,
                tags=tags)
            if accelerator_args:
                accelerator_type_name = accelerator_args['type']
                accelerator_type_ref = self.resources.Parse(
                    accelerator_type_name,
                    collection='compute.acceleratorTypes',
                    params={
                        'project': instance_ref.project,
                        'zone': instance_ref.zone
                    })
                # Accelerator count is default to 1.
                accelerator_count = int(accelerator_args.get('count', 1))
                accelerators = instance_utils.CreateAcceleratorConfigMessages(
                    self.compute_client.messages, accelerator_type_ref,
                    accelerator_count)
                instance.guestAccelerators = accelerators

            request = self.messages.ComputeInstancesInsertRequest(
                instance=instance,
                project=instance_ref.project,
                zone=instance_ref.zone)

            sole_tenancy_host_arg = getattr(args, 'sole_tenancy_host', None)
            if sole_tenancy_host_arg:
                sole_tenancy_host_ref = self.resources.Parse(
                    sole_tenancy_host_arg,
                    collection='compute.hosts',
                    params={'zone': instance_ref.zone})
                request.instance.host = sole_tenancy_host_ref.SelfLink()
            requests.append((self.compute.instances, 'Insert', request))
        return requests
Example #5
0
def _RunCreate(compute_api,
               args,
               support_network_tier=False,
               support_labels=False):
    """Common routine for creating instance template.

  This is shared between various release tracks.

  Args:
      compute_api: The compute api.
      args: argparse.Namespace, An object that contains the values for the
          arguments specified in the .Args() method.
      support_network_tier: Indicates whether network tier is supported or not.
      support_labels: Indicates whether user labels are supported or not.

  Returns:
      A resource object dispatched by display.Displayer().
  """
    _ValidateInstancesFlags(args)
    if support_network_tier:
        instances_flags.ValidateNetworkTierArgs(args)

    client = compute_api.client

    boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
    utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

    instance_template_ref = (Create.InstanceTemplateArg.ResolveAsResource(
        args, compute_api.resources))

    metadata = metadata_utils.ConstructMetadataMessage(
        client.messages,
        metadata=args.metadata,
        metadata_from_file=args.metadata_from_file)

    if hasattr(args, 'network_interface') and args.network_interface:
        network_interfaces = (
            instance_template_utils.CreateNetworkInterfaceMessages)(
                resources=compute_api.resources,
                scope_lister=flags.GetDefaultScopeLister(client),
                messages=client.messages,
                network_interface_arg=args.network_interface,
                region=args.region,
                support_network_tier=support_network_tier)
    else:
        network_tier = getattr(args, 'network_tier', None)
        network_interfaces = [
            instance_template_utils.CreateNetworkInterfaceMessage(
                resources=compute_api.resources,
                scope_lister=flags.GetDefaultScopeLister(client),
                messages=client.messages,
                network=args.network,
                region=args.region,
                subnet=args.subnet,
                address=(instance_template_utils.EPHEMERAL_ADDRESS
                         if not args.no_address and not args.address else
                         args.address),
                network_tier=network_tier)
        ]

    scheduling = instance_utils.CreateSchedulingMessage(
        messages=client.messages,
        maintenance_policy=args.maintenance_policy,
        preemptible=args.preemptible,
        restart_on_failure=args.restart_on_failure)

    if args.no_service_account:
        service_account = None
    else:
        service_account = args.service_account
    service_accounts = instance_utils.CreateServiceAccountMessages(
        messages=client.messages,
        scopes=[] if args.no_scopes else args.scopes,
        service_account=service_account)

    create_boot_disk = not instance_utils.UseExistingBootDisk(args.disk or [])
    if create_boot_disk:
        image_expander = image_utils.ImageExpander(client,
                                                   compute_api.resources)
        try:
            image_uri, _ = image_expander.ExpandImageFlag(
                user_project=instance_template_ref.project,
                image=args.image,
                image_family=args.image_family,
                image_project=args.image_project,
                return_image_resource=True)
        except utils.ImageNotFoundError as e:
            if args.IsSpecified('image_project'):
                raise e
            image_uri, _ = image_expander.ExpandImageFlag(
                user_project=instance_template_ref.project,
                image=args.image,
                image_family=args.image_family,
                image_project=args.image_project,
                return_image_resource=False)
            raise utils.ImageNotFoundError(
                'The resource [{}] was not found. Is the image located in another '
                'project? Use the --image-project flag to specify the '
                'project where the image is located.'.format(image_uri))
    else:
        image_uri = None

    if args.tags:
        tags = client.messages.Tags(items=args.tags)
    else:
        tags = None

    persistent_disks = (
        instance_template_utils.CreatePersistentAttachedDiskMessages(
            client.messages, args.disk or []))

    persistent_create_disks = (
        instance_template_utils.CreatePersistentCreateDiskMessages(
            client, compute_api.resources, instance_template_ref.project,
            getattr(args, 'create_disk', [])))

    if create_boot_disk:
        boot_disk_list = [
            instance_template_utils.CreateDefaultBootAttachedDiskMessage(
                messages=client.messages,
                disk_type=args.boot_disk_type,
                disk_device_name=args.boot_disk_device_name,
                disk_auto_delete=args.boot_disk_auto_delete,
                disk_size_gb=boot_disk_size_gb,
                image_uri=image_uri)
        ]
    else:
        boot_disk_list = []

    local_ssds = []
    for x in args.local_ssd or []:
        local_ssd = instance_utils.CreateLocalSsdMessage(
            compute_api.resources, client.messages, x.get('device-name'),
            x.get('interface'), x.get('size'))
        local_ssds.append(local_ssd)

    disks = (boot_disk_list + persistent_disks + persistent_create_disks +
             local_ssds)

    machine_type = instance_utils.InterpretMachineType(
        machine_type=args.machine_type,
        custom_cpu=args.custom_cpu,
        custom_memory=args.custom_memory,
        ext=getattr(args, 'custom_extensions', None))

    guest_accelerators = (
        instance_template_utils.CreateAcceleratorConfigMessages(
            client.messages, getattr(args, 'accelerator', None)))

    request = client.messages.ComputeInstanceTemplatesInsertRequest(
        instanceTemplate=client.messages.InstanceTemplate(
            properties=client.messages.InstanceProperties(
                machineType=machine_type,
                disks=disks,
                canIpForward=args.can_ip_forward,
                metadata=metadata,
                minCpuPlatform=args.min_cpu_platform,
                networkInterfaces=network_interfaces,
                serviceAccounts=service_accounts,
                scheduling=scheduling,
                tags=tags,
                guestAccelerators=guest_accelerators,
            ),
            description=args.description,
            name=instance_template_ref.Name(),
        ),
        project=instance_template_ref.project)

    if support_labels and args.labels:
        labels = client.messages.InstanceProperties.LabelsValue(
            additionalProperties=[
                client.messages.InstanceProperties.LabelsValue.
                AdditionalProperty(key=key, value=value)
                for key, value in sorted(args.labels.iteritems())
            ])
        request.instanceTemplate.properties.labels = labels

    return client.MakeRequests([(client.apitools_client.instanceTemplates,
                                 'Insert', request)])
Example #6
0
    def Run(self, args):
        compute_holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = compute_holder.client

        self.Validate(args)

        size_gb = utils.BytesToGb(args.size)

        from_image = args.image or args.image_family
        if not size_gb and not args.source_snapshot and not from_image:
            if args.type and 'pd-ssd' in args.type:
                size_gb = constants.DEFAULT_SSD_DISK_SIZE_GB
            else:
                size_gb = constants.DEFAULT_STANDARD_DISK_SIZE_GB

        utils.WarnIfDiskSizeIsTooSmall(size_gb, args.type)

        requests = []
        disk_refs = Create.disks_arg.ResolveAsResource(
            args,
            compute_holder.resources,
            scope_lister=flags.GetDefaultScopeLister(client))

        # Check if the zone is deprecated or has maintenance coming.
        zone_resource_fetcher = zone_utils.ZoneResourceFetcher(client)
        zone_resource_fetcher.WarnForZonalCreation(
            (ref for ref in disk_refs if ref.Collection() == 'compute.disks'))
        # Check if the region is deprecated or has maintenance coming.
        region_resource_fetcher = region_utils.RegionResourceFetcher(client)
        region_resource_fetcher.WarnForRegionalCreation(
            (ref for ref in disk_refs
             if ref.Collection() == 'compute.regionDisks'))

        project_to_source_image = {}

        image_expander = image_utils.ImageExpander(client,
                                                   compute_holder.resources)

        for disk_ref in disk_refs:
            if from_image:
                if disk_ref.project not in project_to_source_image:
                    source_image_uri, _ = image_expander.ExpandImageFlag(
                        user_project=disk_ref.project,
                        image=args.image,
                        image_family=args.image_family,
                        image_project=args.image_project,
                        return_image_resource=False)
                    project_to_source_image[
                        disk_ref.project] = argparse.Namespace()
                    project_to_source_image[
                        disk_ref.project].uri = source_image_uri
            else:
                project_to_source_image[
                    disk_ref.project] = argparse.Namespace()
                project_to_source_image[disk_ref.project].uri = None

        snapshot_ref = disks_flags.SOURCE_SNAPSHOT_ARG.ResolveAsResource(
            args, compute_holder.resources)
        if snapshot_ref:
            snapshot_uri = snapshot_ref.SelfLink()
        else:
            snapshot_uri = None

        # This feature is only exposed in alpha/beta
        allow_rsa_encrypted = self.ReleaseTrack() in [
            base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA
        ]
        csek_keys = csek_utils.CsekKeyStore.FromArgs(args, allow_rsa_encrypted)

        for project in project_to_source_image:
            source_image_uri = project_to_source_image[project].uri
            project_to_source_image[project].keys = (
                csek_utils.MaybeLookupKeyMessagesByUri(
                    csek_keys, compute_holder.resources,
                    [source_image_uri, snapshot_uri], client.apitools_client))

        for disk_ref in disk_refs:
            if args.type:
                if disk_ref.Collection() == 'compute.disks':
                    type_ref = compute_holder.resources.Parse(
                        args.type,
                        collection='compute.diskTypes',
                        params={'zone': disk_ref.zone})
                elif disk_ref.Collection() == 'compute.regionDisks':
                    type_ref = compute_holder.resources.Parse(
                        args.type,
                        collection='compute.regionDiskTypes',
                        params={'region': disk_ref.region})
                type_uri = type_ref.SelfLink()
            else:
                type_uri = None

            if csek_keys:
                disk_key_or_none = csek_keys.LookupKey(
                    disk_ref, args.require_csek_key_create)
                disk_key_message_or_none = csek_utils.MaybeToMessage(
                    disk_key_or_none, client.apitools_client)
                kwargs = {
                    'diskEncryptionKey':
                    disk_key_message_or_none,
                    'sourceImageEncryptionKey':
                    project_to_source_image[disk_ref.project].keys[0],
                    'sourceSnapshotEncryptionKey':
                    project_to_source_image[disk_ref.project].keys[1]
                }
            else:
                kwargs = {}

            if disk_ref.Collection() == 'compute.disks':
                request = client.messages.ComputeDisksInsertRequest(
                    disk=client.messages.Disk(name=disk_ref.Name(),
                                              description=args.description,
                                              sizeGb=size_gb,
                                              sourceSnapshot=snapshot_uri,
                                              type=type_uri,
                                              **kwargs),
                    project=disk_ref.project,
                    sourceImage=project_to_source_image[disk_ref.project].uri,
                    zone=disk_ref.zone)
                request = (client.apitools_client.disks, 'Insert', request)
            elif disk_ref.Collection() == 'compute.regionDisks':

                def SelfLink(zone, disk_ref):
                    return compute_holder.resources.Parse(
                        zone,
                        collection='compute.zones',
                        params={
                            'project': disk_ref.project
                        }).SelfLink()

                zones = [
                    SelfLink(zone, disk_ref) for zone in args.replica_zones
                ]
                request = client.messages.ComputeRegionDisksInsertRequest(
                    disk=client.messages.Disk(name=disk_ref.Name(),
                                              description=args.description,
                                              sizeGb=size_gb,
                                              sourceSnapshot=snapshot_uri,
                                              type=type_uri,
                                              replicaZones=zones,
                                              **kwargs),
                    project=disk_ref.project,
                    sourceImage=project_to_source_image[disk_ref.project].uri,
                    region=disk_ref.region)
                request = (client.apitools_client.regionDisks, 'Insert',
                           request)
            requests.append(request)

        return client.MakeRequests(requests)
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        instances_flags.ValidateDockerArgs(args)
        instances_flags.ValidateDiskCommonFlags(args)
        instances_flags.ValidateLocalSsdFlags(args)
        instances_flags.ValidateServiceAccountAndScopeArgs(args)
        if instance_utils.UseExistingBootDisk(args.disk or []):
            raise exceptions.InvalidArgumentException(
                '--disk', 'Boot disk specified for containerized VM.')

        self.WarnForSourceInstanceTemplateLimitations(args)

        scheduling = instance_utils.CreateSchedulingMessage(
            messages=client.messages,
            maintenance_policy=args.maintenance_policy,
            preemptible=args.preemptible,
            restart_on_failure=args.restart_on_failure)

        if args.no_service_account:
            service_account = None
        else:
            service_account = args.service_account
        service_accounts = instance_utils.CreateServiceAccountMessages(
            messages=client.messages,
            scopes=[] if args.no_scopes else args.scopes,
            service_account=service_account)

        user_metadata = metadata_utils.ConstructMetadataMessage(
            client.messages,
            metadata=args.metadata,
            metadata_from_file=args.metadata_from_file)
        containers_utils.ValidateUserMetadata(user_metadata)

        boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
        utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

        instance_refs = instances_flags.INSTANCES_ARG.ResolveAsResource(
            args,
            holder.resources,
            scope_lister=flags.GetDefaultScopeLister(client))

        # Check if the zone is deprecated or has maintenance coming.
        zone_resource_fetcher = zone_utils.ZoneResourceFetcher(client)
        zone_resource_fetcher.WarnForZonalCreation(instance_refs)

        instances_flags.ValidatePublicDnsFlags(args)

        network_interface = instance_utils.CreateNetworkInterfaceMessage(
            resources=holder.resources,
            compute_client=client,
            network=args.network,
            subnet=args.subnet,
            private_network_ip=args.private_network_ip,
            no_address=args.no_address,
            address=args.address,
            instance_refs=instance_refs,
            network_tier=args.network_tier,
            no_public_dns=getattr(args, 'no_public_dns', None),
            public_dns=getattr(args, 'public_dns', None),
            no_public_ptr=getattr(args, 'no_public_ptr', None),
            public_ptr=getattr(args, 'public_ptr', None),
            no_public_ptr_domain=getattr(args, 'no_public_ptr_domain', None),
            public_ptr_domain=getattr(args, 'public_ptr_domain', None))

        machine_type_uris = instance_utils.CreateMachineTypeUris(
            resources=holder.resources,
            compute_client=client,
            machine_type=args.machine_type,
            custom_cpu=args.custom_cpu,
            custom_memory=args.custom_memory,
            ext=getattr(args, 'custom_extensions', None),
            instance_refs=instance_refs)

        image_uri = containers_utils.ExpandCosImageFlag(client)

        args_labels = getattr(args, 'labels', None)
        labels = None
        if args_labels:
            labels = client.messages.Instance.LabelsValue(
                additionalProperties=[
                    client.messages.Instance.LabelsValue.AdditionalProperty(
                        key=key, value=value)
                    for key, value in sorted(args.labels.iteritems())
                ])

        requests = []
        for instance_ref, machine_type_uri in zip(instance_refs,
                                                  machine_type_uris):
            metadata = containers_utils.CreateMetadataMessage(
                client.messages, args.run_as_privileged,
                args.container_manifest, args.docker_image, args.port_mappings,
                args.run_command, user_metadata, instance_ref.Name())
            request = client.messages.ComputeInstancesInsertRequest(
                instance=client.messages.Instance(
                    canIpForward=args.can_ip_forward,
                    disks=(self._CreateDiskMessages(holder, args,
                                                    boot_disk_size_gb,
                                                    image_uri, instance_ref)),
                    description=args.description,
                    machineType=machine_type_uri,
                    metadata=metadata,
                    minCpuPlatform=args.min_cpu_platform,
                    name=instance_ref.Name(),
                    networkInterfaces=[network_interface],
                    serviceAccounts=service_accounts,
                    scheduling=scheduling,
                    tags=containers_utils.CreateTagsMessage(
                        client.messages, args.tags)),
                project=instance_ref.project,
                zone=instance_ref.zone)
            if labels:
                request.instance.labels = labels
            source_instance_template = self.GetSourceInstanceTemplate(
                args, holder.resources)
            if source_instance_template:
                request.sourceInstanceTemplate = source_instance_template

                # Labels and MachineType are currently overridable.
                # If no custom value was specified, default to None. Otherwise gcloud
                # auto-default value will be considered as an override by Arcus.
                if (not args.IsSpecified('machine_type')
                        and not args.IsSpecified('custom_cpu')
                        and not args.IsSpecified('custom_memory')):
                    request.instance.machineType = None
                if not args.IsSpecified('labels'):
                    request.instance.labels = None

            requests.append(
                (client.apitools_client.instances, 'Insert', request))

        return client.MakeRequests(requests)
Example #8
0
    def CreateRequests(self, args):
        """Returns a list of requests necessary for adding disks."""
        size_gb = utils.BytesToGb(args.size)

        from_image = args.image or args.image_family
        if not size_gb and not args.source_snapshot and not from_image:
            if args.type and 'pd-ssd' in args.type:
                size_gb = constants.DEFAULT_SSD_DISK_SIZE_GB
            else:
                size_gb = constants.DEFAULT_STANDARD_DISK_SIZE_GB

        utils.WarnIfDiskSizeIsTooSmall(size_gb, args.type)

        requests = []

        disk_refs = self.CreateZonalReferences(args.names, args.zone)

        # Check if the zone is deprecated or has maintenance coming.
        self.WarnForZonalCreation(disk_refs)

        if from_image:
            source_image_uri, _ = self.ExpandImageFlag(
                args, return_image_resource=False)
        else:
            source_image_uri = None

        if args.source_snapshot:
            snapshot_ref = self.CreateGlobalReference(
                args.source_snapshot, resource_type='snapshots')
            snapshot_uri = snapshot_ref.SelfLink()
        else:
            snapshot_uri = None

        if hasattr(args, 'csek_key_file'):
            csek_keys = csek_utils.CsekKeyStore.FromArgs(args)
        else:
            csek_keys = None

        image_key_message_or_none, snapshot_key_message_or_none = (
            csek_utils.MaybeLookupKeyMessagesByUri(
                csek_keys, self.resources, [source_image_uri, snapshot_uri],
                self.compute))

        for disk_ref in disk_refs:
            if args.type:
                type_ref = self.CreateZonalReference(args.type,
                                                     disk_ref.zone,
                                                     resource_type='diskTypes')
                type_uri = type_ref.SelfLink()
            else:
                type_uri = None

            if csek_keys:
                disk_key_or_none = csek_keys.LookupKey(
                    disk_ref, args.require_csek_key_create)
                disk_key_message_or_none = csek_utils.MaybeToMessage(
                    disk_key_or_none, self.compute)
                kwargs = {
                    'diskEncryptionKey': disk_key_message_or_none,
                    'sourceImageEncryptionKey': image_key_message_or_none,
                    'sourceSnapshotEncryptionKey': snapshot_key_message_or_none
                }
            else:
                kwargs = {}

            request = self.messages.ComputeDisksInsertRequest(
                disk=self.messages.Disk(name=disk_ref.Name(),
                                        description=args.description,
                                        sizeGb=size_gb,
                                        sourceSnapshot=snapshot_uri,
                                        type=type_uri,
                                        **kwargs),
                project=self.project,
                sourceImage=source_image_uri,
                zone=disk_ref.zone)
            requests.append(request)

        return requests
Example #9
0
    def _CreateRequests(self, args, instance_refs, compute_client,
                        resource_parser):
        # gcloud creates default values for some fields in Instance resource
        # when no value was specified on command line.
        # When --source-instance-template was specified, defaults are taken from
        # Instance Template and gcloud flags are used to override them - by default
        # fields should not be initialized.
        source_instance_template = self.GetSourceInstanceTemplate(
            args, resource_parser)
        skip_defaults = source_instance_template is not None

        # This feature is only exposed in alpha/beta
        allow_rsa_encrypted = self.ReleaseTrack() in [
            base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA
        ]
        self.csek_keys = csek_utils.CsekKeyStore.FromArgs(
            args, allow_rsa_encrypted)

        if (skip_defaults and not args.IsSpecified('maintenance_policy')
                and not args.IsSpecified('preemptible')
                and not args.IsSpecified('restart_on_failure')):
            scheduling = None
        else:
            scheduling = instance_utils.CreateSchedulingMessage(
                messages=compute_client.messages,
                maintenance_policy=args.maintenance_policy,
                preemptible=args.preemptible,
                restart_on_failure=args.restart_on_failure)

        if args.tags:
            tags = compute_client.messages.Tags(items=args.tags)
        else:
            tags = None

        labels = None
        args_labels = getattr(args, 'labels', None)
        if args_labels:
            labels = compute_client.messages.Instance.LabelsValue(
                additionalProperties=[
                    compute_client.messages.Instance.LabelsValue.
                    AdditionalProperty(key=key, value=value)
                    for key, value in sorted(args.labels.iteritems())
                ])

        if (skip_defaults and not args.IsSpecified('metadata')
                and not args.IsSpecified('metadata_from_file')):
            metadata = None
        else:
            metadata = metadata_utils.ConstructMetadataMessage(
                compute_client.messages,
                metadata=args.metadata,
                metadata_from_file=args.metadata_from_file)

        # If the user already provided an initial Windows password and
        # username through metadata, then there is no need to check
        # whether the image or the boot disk is Windows.

        boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
        utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

        # Check if the zone is deprecated or has maintenance coming.
        zone_resource_fetcher = zone_utils.ZoneResourceFetcher(compute_client)
        zone_resource_fetcher.WarnForZonalCreation(instance_refs)

        network_interface_arg = getattr(args, 'network_interface', None)
        if network_interface_arg:
            network_interfaces = instance_utils.CreateNetworkInterfaceMessages(
                resources=resource_parser,
                compute_client=compute_client,
                network_interface_arg=network_interface_arg,
                instance_refs=instance_refs,
                support_network_tier=self._support_network_tier)
        else:
            if self._support_public_dns is True:
                instances_flags.ValidatePublicDnsFlags(args)

            if (skip_defaults and not args.IsSpecified('network')
                    and not args.IsSpecified('subnet')
                    and not args.IsSpecified('private_network_ip')
                    and not args.IsSpecified('no_address')
                    and not args.IsSpecified('address')
                    and not args.IsSpecified('network_tier')
                    and not args.IsSpecified('no_public_dns')
                    and not args.IsSpecified('public_dns')
                    and not args.IsSpecified('no_public_ptr')
                    and not args.IsSpecified('public_ptr')
                    and not args.IsSpecified('no_public_ptr_domain')
                    and not args.IsSpecified('public_ptr_domain')):
                network_interfaces = []
            else:
                network_tier = getattr(args, 'network_tier', None)

                network_interfaces = [
                    instance_utils.CreateNetworkInterfaceMessage(
                        resources=resource_parser,
                        compute_client=compute_client,
                        network=args.network,
                        subnet=args.subnet,
                        private_network_ip=args.private_network_ip,
                        no_address=args.no_address,
                        address=args.address,
                        instance_refs=instance_refs,
                        network_tier=network_tier,
                        no_public_dns=getattr(args, 'no_public_dns', None),
                        public_dns=getattr(args, 'public_dns', None),
                        no_public_ptr=getattr(args, 'no_public_ptr', None),
                        public_ptr=getattr(args, 'public_ptr', None),
                        no_public_ptr_domain=getattr(args,
                                                     'no_public_ptr_domain',
                                                     None),
                        public_ptr_domain=getattr(args, 'public_ptr_domain',
                                                  None))
                ]

        if (skip_defaults and not args.IsSpecified('machine_type')
                and not args.IsSpecified('custom_cpu')
                and not args.IsSpecified('custom_memory')):
            machine_type_uris = [None for _ in instance_refs]
        else:
            machine_type_uris = instance_utils.CreateMachineTypeUris(
                resources=resource_parser,
                compute_client=compute_client,
                machine_type=args.machine_type,
                custom_cpu=args.custom_cpu,
                custom_memory=args.custom_memory,
                ext=getattr(args, 'custom_extensions', None),
                instance_refs=instance_refs)

        create_boot_disk = not instance_utils.UseExistingBootDisk(args.disk
                                                                  or [])
        if create_boot_disk:
            image_expander = image_utils.ImageExpander(compute_client,
                                                       resource_parser)
            image_uri, _ = image_expander.ExpandImageFlag(
                user_project=instance_refs[0].project,
                image=args.image,
                image_family=args.image_family,
                image_project=args.image_project,
                return_image_resource=False)
        else:
            image_uri = None

        # A list of lists where the element at index i contains a list of
        # disk messages that should be set for the instance at index i.
        disks_messages = []

        # A mapping of zone to boot disk references for all existing boot
        # disks that are being attached.
        # TODO(b/36050875): Simplify since resources.Resource is now hashable.
        existing_boot_disks = {}

        if (skip_defaults and not args.IsSpecified('disk')
                and not args.IsSpecified('create_disk')
                and not args.IsSpecified('local_ssd')
                and not args.IsSpecified('boot_disk_type')
                and not args.IsSpecified('boot_disk_device_name')
                and not args.IsSpecified('boot_disk_auto_delete')
                and not args.IsSpecified('require_csek_key_create')):
            disks_messages = [[] for _ in instance_refs]
        else:
            for instance_ref in instance_refs:
                persistent_disks, boot_disk_ref = (
                    instance_utils.CreatePersistentAttachedDiskMessages(
                        resource_parser, compute_client, self.csek_keys,
                        args.disk or [], instance_ref))
                persistent_create_disks = (
                    instance_utils.CreatePersistentCreateDiskMessages(
                        compute_client, resource_parser, self.csek_keys,
                        getattr(args, 'create_disk', []), instance_ref))
                local_ssds = []
                for x in args.local_ssd or []:
                    local_ssds.append(
                        instance_utils.CreateLocalSsdMessage(
                            resource_parser, compute_client.messages,
                            x.get('device-name'), x.get('interface'),
                            x.get('size'), instance_ref.zone,
                            instance_ref.project))

                if create_boot_disk:
                    boot_disk = instance_utils.CreateDefaultBootAttachedDiskMessage(
                        compute_client,
                        resource_parser,
                        disk_type=args.boot_disk_type,
                        disk_device_name=args.boot_disk_device_name,
                        disk_auto_delete=args.boot_disk_auto_delete,
                        disk_size_gb=boot_disk_size_gb,
                        require_csek_key_create=(args.require_csek_key_create
                                                 if self.csek_keys else None),
                        image_uri=image_uri,
                        instance_ref=instance_ref,
                        csek_keys=self.csek_keys)
                    persistent_disks = [boot_disk] + persistent_disks
                else:
                    existing_boot_disks[boot_disk_ref.zone] = boot_disk_ref
                disks_messages.append(persistent_disks +
                                      persistent_create_disks + local_ssds)

        accelerator_args = getattr(args, 'accelerator', None)

        project_to_sa = {}
        requests = []
        for instance_ref, machine_type_uri, disks in zip(
                instance_refs, machine_type_uris, disks_messages):
            if instance_ref.project not in project_to_sa:
                scopes = None
                if not args.no_scopes and not args.scopes:
                    # User didn't provide any input on scopes. If project has no default
                    # service account then we want to create a VM with no scopes
                    request = (
                        compute_client.apitools_client.projects, 'Get',
                        compute_client.messages.ComputeProjectsGetRequest(
                            project=instance_ref.project))
                    errors = []
                    result = compute_client.MakeRequests([request], errors)
                    if not errors:
                        if not result[0].defaultServiceAccount:
                            scopes = []
                            log.status.Print(
                                'There is no default service account for project {}. '
                                'Instance {} will not have scopes.'.format(
                                    instance_ref.project, instance_ref.Name))
                if scopes is None:
                    scopes = [] if args.no_scopes else args.scopes

                if args.no_service_account:
                    service_account = None
                else:
                    service_account = args.service_account
                if (skip_defaults and not args.IsSpecified('scopes')
                        and not args.IsSpecified('no_scopes')
                        and not args.IsSpecified('service_account')
                        and not args.IsSpecified('no_service_account')):
                    service_accounts = []
                else:
                    service_accounts = instance_utils.CreateServiceAccountMessages(
                        messages=compute_client.messages,
                        scopes=scopes,
                        service_account=service_account)
                project_to_sa[instance_ref.project] = service_accounts

            if skip_defaults and not args.IsSpecified('can_ip_forward'):
                can_ip_forward = None
            else:
                can_ip_forward = args.can_ip_forward

            instance = compute_client.messages.Instance(
                canIpForward=can_ip_forward,
                disks=disks,
                description=args.description,
                machineType=machine_type_uri,
                metadata=metadata,
                name=instance_ref.Name(),
                networkInterfaces=network_interfaces,
                serviceAccounts=project_to_sa[instance_ref.project],
                scheduling=scheduling,
                tags=tags)
            if getattr(args, 'min_cpu_platform', None):
                instance.minCpuPlatform = args.min_cpu_platform
            if labels:
                instance.labels = labels
            if accelerator_args:
                accelerator_type_name = accelerator_args['type']
                accelerator_type_ref = resource_parser.Parse(
                    accelerator_type_name,
                    collection='compute.acceleratorTypes',
                    params={
                        'project': instance_ref.project,
                        'zone': instance_ref.zone
                    })
                # Accelerator count is default to 1.
                accelerator_count = int(accelerator_args.get('count', 1))
                accelerators = instance_utils.CreateAcceleratorConfigMessages(
                    compute_client.messages, accelerator_type_ref,
                    accelerator_count)
                instance.guestAccelerators = accelerators

            request = compute_client.messages.ComputeInstancesInsertRequest(
                instance=instance,
                project=instance_ref.project,
                zone=instance_ref.zone)

            if source_instance_template:
                request.sourceInstanceTemplate = source_instance_template

            sole_tenancy_host_arg = getattr(args, 'sole_tenancy_host', None)
            if sole_tenancy_host_arg:
                sole_tenancy_host_ref = resource_parser.Parse(
                    sole_tenancy_host_arg,
                    collection='compute.hosts',
                    params={
                        'project': instance_ref.project,
                        'zone': instance_ref.zone
                    })
                request.instance.host = sole_tenancy_host_ref.SelfLink()
            requests.append(
                (compute_client.apitools_client.instances, 'Insert', request))
        return requests
Example #10
0
    def CreateRequests(self, args):
        instances_flags.ValidateDiskFlags(args)
        instances_flags.ValidateLocalSsdFlags(args)
        instances_flags.ValidateNicFlags(args)

        # This feature is only exposed in alpha/beta
        allow_rsa_encrypted = self.ReleaseTrack() in [
            base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA
        ]
        self.csek_keys = csek_utils.CsekKeyStore.FromArgs(
            args, allow_rsa_encrypted)

        scheduling = instance_utils.CreateSchedulingMessage(
            messages=self.messages,
            maintenance_policy=args.maintenance_policy,
            preemptible=args.preemptible,
            restart_on_failure=args.restart_on_failure)

        service_accounts = instance_utils.CreateServiceAccountMessages(
            messages=self.messages,
            scopes=([] if args.no_scopes else args.scopes))

        if args.tags:
            tags = self.messages.Tags(items=args.tags)
        else:
            tags = None

        metadata = metadata_utils.ConstructMetadataMessage(
            self.messages,
            metadata=args.metadata,
            metadata_from_file=args.metadata_from_file)

        # If the user already provided an initial Windows password and
        # username through metadata, then there is no need to check
        # whether the image or the boot disk is Windows.

        boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
        utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

        instance_refs = instances_flags.INSTANCES_ARG.ResolveAsResource(
            args,
            self.resources,
            scope_lister=flags.GetDefaultScopeLister(self.compute_client,
                                                     self.project))

        # Check if the zone is deprecated or has maintenance coming.
        self.WarnForZonalCreation(instance_refs)

        network_interface_arg = getattr(args, 'network_interface', None)
        if network_interface_arg:
            network_interfaces = instance_utils.CreateNetworkInterfaceMessages(
                resources=self.resources,
                compute_client=self.compute_client,
                network_interface_arg=network_interface_arg,
                instance_refs=instance_refs)
        else:
            network_interfaces = [
                instance_utils.CreateNetworkInterfaceMessage(
                    resources=self.resources,
                    compute_client=self.compute_client,
                    network=args.network,
                    subnet=args.subnet,
                    private_network_ip=args.private_network_ip,
                    no_address=args.no_address,
                    address=args.address,
                    instance_refs=instance_refs)
            ]

        machine_type_uris = instance_utils.CreateMachineTypeUris(
            resources=self.resources,
            compute_client=self.compute_client,
            project=self.project,
            machine_type=args.machine_type,
            custom_cpu=args.custom_cpu,
            custom_memory=args.custom_memory,
            instance_refs=instance_refs)

        create_boot_disk = not instance_utils.UseExistingBootDisk(args.disk
                                                                  or [])
        if create_boot_disk:
            image_uri, _ = self.ExpandImageFlag(
                image=args.image,
                image_family=args.image_family,
                image_project=args.image_project,
                return_image_resource=False)
        else:
            image_uri = None

        # A list of lists where the element at index i contains a list of
        # disk messages that should be set for the instance at index i.
        disks_messages = []

        # A mapping of zone to boot disk references for all existing boot
        # disks that are being attached.
        # TODO(user): Simplify this once resources.Resource becomes
        # hashable.
        existing_boot_disks = {}

        for instance_ref in instance_refs:
            persistent_disks, boot_disk_ref = (
                instance_utils.CreatePersistentAttachedDiskMessages(
                    self.resources, self.compute_client, self.csek_keys,
                    args.disk or [], instance_ref))
            persistent_create_disks = (
                instance_utils.CreatePersistentCreateDiskMessages(
                    self, self.compute_client, self.resources, self.csek_keys,
                    getattr(args, 'create_disk', []), instance_ref))
            local_ssds = []
            for x in args.local_ssd or []:
                local_ssds.append(
                    instance_utils.CreateLocalSsdMessage(
                        self.resources, self.messages, x.get('device-name'),
                        x.get('interface'), instance_ref.zone))

            if create_boot_disk:
                boot_disk = instance_utils.CreateDefaultBootAttachedDiskMessage(
                    self.compute_client,
                    self.resources,
                    disk_type=args.boot_disk_type,
                    disk_device_name=args.boot_disk_device_name,
                    disk_auto_delete=args.boot_disk_auto_delete,
                    disk_size_gb=boot_disk_size_gb,
                    require_csek_key_create=(args.require_csek_key_create
                                             if self.csek_keys else None),
                    image_uri=image_uri,
                    instance_ref=instance_ref,
                    csek_keys=self.csek_keys)
                persistent_disks = [boot_disk] + persistent_disks
            else:
                existing_boot_disks[boot_disk_ref.zone] = boot_disk_ref
            disks_messages.append(persistent_disks + persistent_create_disks +
                                  local_ssds)

        requests = []
        for instance_ref, machine_type_uri, disks in zip(
                instance_refs, machine_type_uris, disks_messages):
            requests.append(
                self.messages.ComputeInstancesInsertRequest(
                    instance=self.messages.Instance(
                        canIpForward=args.can_ip_forward,
                        disks=disks,
                        description=args.description,
                        machineType=machine_type_uri,
                        metadata=metadata,
                        name=instance_ref.Name(),
                        networkInterfaces=network_interfaces,
                        serviceAccounts=service_accounts,
                        scheduling=scheduling,
                        tags=tags,
                    ),
                    project=self.project,
                    zone=instance_ref.zone))

        return requests
Example #11
0
  def CreateRequests(self, args):
    """Creates and returns an InstanceTemplates.Insert request.

    Args:
      args: the argparse arguments that this command was invoked with.

    Returns:
      request: a ComputeInstanceTemplatesInsertRequest message object
    """
    instances_flags.ValidateDockerArgs(args)
    instances_flags.ValidateDiskCommonFlags(args)
    instances_flags.ValidateLocalSsdFlags(args)
    if instance_utils.UseExistingBootDisk(args.disk or []):
      raise exceptions.InvalidArgumentException(
          '--disk',
          'Boot disk specified for containerized VM.')

    boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
    utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

    instance_template_ref = (
        instance_templates_flags.INSTANCE_TEMPLATE_ARG.ResolveAsResource(
            args, self.resources))

    user_metadata = metadata_utils.ConstructMetadataMessage(
        self.messages,
        metadata=args.metadata,
        metadata_from_file=args.metadata_from_file)
    containers_utils.ValidateUserMetadata(user_metadata)

    network_interface = instance_template_utils.CreateNetworkInterfaceMessage(
        scope_prompter=self,
        messages=self.messages,
        network=args.network,
        region=args.region,
        subnet=args.subnet,
        address=(instance_template_utils.EPHEMERAL_ADDRESS
                 if not args.no_address and not args.address
                 else args.address))

    scheduling = instance_utils.CreateSchedulingMessage(
        messages=self.messages,
        maintenance_policy=args.maintenance_policy,
        preemptible=args.preemptible,
        restart_on_failure=args.restart_on_failure)

    service_accounts = instance_utils.CreateServiceAccountMessages(
        messages=self.messages,
        scopes=([] if args.no_scopes else args.scopes))

    image_uri = containers_utils.ExpandGciImageFlag(self.compute_client)

    machine_type = instance_utils.InterpretMachineType(
        machine_type=args.machine_type,
        custom_cpu=args.custom_cpu,
        custom_memory=args.custom_memory)

    metadata = containers_utils.CreateMetadataMessage(
        self.messages, args.run_as_privileged, args.container_manifest,
        args.docker_image, args.port_mappings, args.run_command,
        user_metadata, instance_template_ref.Name())

    request = self.messages.ComputeInstanceTemplatesInsertRequest(
        instanceTemplate=self.messages.InstanceTemplate(
            properties=self.messages.InstanceProperties(
                machineType=machine_type,
                disks=self._CreateDiskMessages(args, boot_disk_size_gb,
                                               image_uri),
                canIpForward=args.can_ip_forward,
                metadata=metadata,
                networkInterfaces=[network_interface],
                serviceAccounts=service_accounts,
                scheduling=scheduling,
                tags=containers_utils.CreateTagsMessage(
                    self.messages, args.tags),
            ),
            description=args.description,
            name=instance_template_ref.Name(),
        ),
        project=self.project)

    return [request]
Example #12
0
    def CreateRequests(self, args):
        """Returns a list of requests necessary for adding disks."""
        size_gb = utils.BytesToGb(args.size)

        from_image = args.image or args.image_family
        if not size_gb and not args.source_snapshot and not from_image:
            if args.type and 'pd-ssd' in args.type:
                size_gb = constants.DEFAULT_SSD_DISK_SIZE_GB
            else:
                size_gb = constants.DEFAULT_STANDARD_DISK_SIZE_GB

        utils.WarnIfDiskSizeIsTooSmall(size_gb, args.type)

        requests = []
        disk_refs = disks_flags.DISKS_ARG.ResolveAsResource(
            args,
            self.resources,
            scope_lister=flags.GetDefaultScopeLister(self.compute_client,
                                                     self.project))

        # Check if the zone is deprecated or has maintenance coming.
        self.WarnForZonalCreation(disk_refs)

        if from_image:
            source_image_uri, _ = self.ExpandImageFlag(
                image=args.image,
                image_family=args.image_family,
                image_project=args.image_project,
                return_image_resource=False)
        else:
            source_image_uri = None

        snapshot_ref = disks_flags.SOURCE_SNAPSHOT_ARG.ResolveAsResource(
            args, self.resources)
        if snapshot_ref:
            snapshot_uri = snapshot_ref.SelfLink()
        else:
            snapshot_uri = None

        # This feature is only exposed in alpha/beta
        allow_rsa_encrypted = self.ReleaseTrack() in [
            base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA
        ]
        csek_keys = csek_utils.CsekKeyStore.FromArgs(args, allow_rsa_encrypted)

        image_key_message_or_none, snapshot_key_message_or_none = (
            csek_utils.MaybeLookupKeyMessagesByUri(
                csek_keys, self.resources, [source_image_uri, snapshot_uri],
                self.compute))

        for disk_ref in disk_refs:
            if args.type:
                type_ref = self.resources.Parse(args.type,
                                                collection='compute.diskTypes',
                                                params={'zone': disk_ref.zone})
                type_uri = type_ref.SelfLink()
            else:
                type_uri = None

            if csek_keys:
                disk_key_or_none = csek_keys.LookupKey(
                    disk_ref, args.require_csek_key_create)
                disk_key_message_or_none = csek_utils.MaybeToMessage(
                    disk_key_or_none, self.compute)
                kwargs = {
                    'diskEncryptionKey': disk_key_message_or_none,
                    'sourceImageEncryptionKey': image_key_message_or_none,
                    'sourceSnapshotEncryptionKey': snapshot_key_message_or_none
                }
            else:
                kwargs = {}

            request = self.messages.ComputeDisksInsertRequest(
                disk=self.messages.Disk(name=disk_ref.Name(),
                                        description=args.description,
                                        sizeGb=size_gb,
                                        sourceSnapshot=snapshot_uri,
                                        type=type_uri,
                                        **kwargs),
                project=self.project,
                sourceImage=source_image_uri,
                zone=disk_ref.zone)
            requests.append(request)

        return requests
Example #13
0
    def CreateRequests(self, args):
        """Creates and returns an InstanceTemplates.Insert request.

    Args:
      args: the argparse arguments that this command was invoked with.

    Returns:
      request: a ComputeInstanceTemplatesInsertRequest message object
    """
        self.ValidateDiskFlags(args)
        instance_utils.ValidateLocalSsdFlags(args)

        boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
        utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

        instance_template_ref = self.CreateGlobalReference(args.name)

        metadata = metadata_utils.ConstructMetadataMessage(
            self.messages,
            metadata=args.metadata,
            metadata_from_file=args.metadata_from_file)

        network_interface = self.CreateNetworkInterfaceMessage(args)

        if args.maintenance_policy:
            on_host_maintenance = (
                self.messages.Scheduling.OnHostMaintenanceValueValuesEnum(
                    args.maintenance_policy))
        else:
            on_host_maintenance = None

        # Note: We always specify automaticRestart=False for preemptible VMs. This
        # makes sense, since no-restart-on-failure is defined as "store-true", and
        # thus can't be given an explicit value. Hence it either has its default
        # value (in which case we override it for convenience's sake to the only
        # setting that makes sense for preemptible VMs), or the user actually
        # specified no-restart-on-failure, the only usable setting.
        if args.preemptible:
            scheduling = self.messages.Scheduling(
                automaticRestart=False,
                onHostMaintenance=on_host_maintenance,
                preemptible=True)
        else:
            scheduling = self.messages.Scheduling(
                automaticRestart=args.restart_on_failure,
                onHostMaintenance=on_host_maintenance)

        service_accounts = self.CreateServiceAccountMessages(args)

        create_boot_disk = not self.UseExistingBootDisk(args)
        if create_boot_disk:
            image_uri, _ = self.ExpandImageFlag(args,
                                                return_image_resource=True)
        else:
            image_uri = None

        if args.tags:
            tags = self.messages.Tags(items=args.tags)
        else:
            tags = None

        persistent_disks = self.CreateAttachedPersistentDiskMessages(args)
        if create_boot_disk:
            boot_disk_list = [
                self.CreateDefaultBootAttachedDiskMessage(
                    args, boot_disk_size_gb, image_uri)
            ]
        else:
            boot_disk_list = []

        local_ssds = [
            instance_utils.CreateLocalSsdMessage(self, x.get('device-name'),
                                                 x.get('interface'))
            for x in args.local_ssd or []
        ]

        disks = boot_disk_list + persistent_disks + local_ssds

        machine_type = instance_utils.InterpretMachineType(args)

        request = self.messages.ComputeInstanceTemplatesInsertRequest(
            instanceTemplate=self.messages.InstanceTemplate(
                properties=self.messages.InstanceProperties(
                    machineType=machine_type,
                    disks=disks,
                    canIpForward=args.can_ip_forward,
                    metadata=metadata,
                    networkInterfaces=[network_interface],
                    serviceAccounts=service_accounts,
                    scheduling=scheduling,
                    tags=tags,
                ),
                description=args.description,
                name=instance_template_ref.Name(),
            ),
            project=self.context['project'])

        return [request]
Example #14
0
def _RunCreate(compute_api,
               args,
               support_source_instance,
               support_kms=False,
               support_min_node_cpus=False,
               support_confidential_compute=False):
    """Common routine for creating instance template.

  This is shared between various release tracks.

  Args:
      compute_api: The compute api.
      args: argparse.Namespace, An object that contains the values for the
        arguments specified in the .Args() method.
      support_source_instance: indicates whether source instance is supported.
      support_kms: Indicate whether KMS is integrated or not.
      support_min_node_cpus: Indicate whether the --min-node-cpus flag for
        sole tenancy overcommit is supported.
      support_confidential_compute: Indicate whether confidential compute is
        supported.

  Returns:
      A resource object dispatched by display.Displayer().
  """
    _ValidateInstancesFlags(args, support_kms=support_kms)
    instances_flags.ValidateNetworkTierArgs(args)

    client = compute_api.client

    boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
    utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

    instance_template_ref = (Create.InstanceTemplateArg.ResolveAsResource(
        args, compute_api.resources))

    AddMeshArgsToMetadata(args)

    metadata = metadata_utils.ConstructMetadataMessage(
        client.messages,
        metadata=args.metadata,
        metadata_from_file=args.metadata_from_file)

    if hasattr(args, 'network_interface') and args.network_interface:
        network_interfaces = (
            instance_template_utils.CreateNetworkInterfaceMessages)(
                resources=compute_api.resources,
                scope_lister=flags.GetDefaultScopeLister(client),
                messages=client.messages,
                network_interface_arg=args.network_interface,
                region=args.region)
    else:
        network_tier = getattr(args, 'network_tier', None)
        network_interfaces = [
            instance_template_utils.CreateNetworkInterfaceMessage(
                resources=compute_api.resources,
                scope_lister=flags.GetDefaultScopeLister(client),
                messages=client.messages,
                network=args.network,
                private_ip=args.private_network_ip,
                region=args.region,
                subnet=args.subnet,
                address=(instance_template_utils.EPHEMERAL_ADDRESS
                         if not args.no_address and not args.address else
                         args.address),
                network_tier=network_tier)
        ]

    # Compute the shieldedInstanceConfig message.
    shieldedinstance_config_message = BuildShieldedInstanceConfigMessage(
        messages=client.messages, args=args)

    if support_confidential_compute:
        confidential_instance_config_message = (
            BuildConfidentialInstanceConfigMessage(messages=client.messages,
                                                   args=args))

    node_affinities = sole_tenancy_util.GetSchedulingNodeAffinityListFromArgs(
        args, client.messages)

    min_node_cpus = None
    if support_min_node_cpus and args.IsSpecified('min_node_cpus'):
        min_node_cpus = args.min_node_cpus

    scheduling = instance_utils.CreateSchedulingMessage(
        messages=client.messages,
        maintenance_policy=args.maintenance_policy,
        preemptible=args.preemptible,
        restart_on_failure=args.restart_on_failure,
        node_affinities=node_affinities,
        min_node_cpus=min_node_cpus)

    if args.no_service_account:
        service_account = None
    else:
        service_account = args.service_account
    service_accounts = instance_utils.CreateServiceAccountMessages(
        messages=client.messages,
        scopes=[] if args.no_scopes else args.scopes,
        service_account=service_account)

    create_boot_disk = not instance_utils.UseExistingBootDisk(args.disk or [])
    if create_boot_disk:
        image_expander = image_utils.ImageExpander(client,
                                                   compute_api.resources)
        try:
            image_uri, _ = image_expander.ExpandImageFlag(
                user_project=instance_template_ref.project,
                image=args.image,
                image_family=args.image_family,
                image_project=args.image_project,
                return_image_resource=True)
        except utils.ImageNotFoundError as e:
            if args.IsSpecified('image_project'):
                raise e
            image_uri, _ = image_expander.ExpandImageFlag(
                user_project=instance_template_ref.project,
                image=args.image,
                image_family=args.image_family,
                image_project=args.image_project,
                return_image_resource=False)
            raise utils.ImageNotFoundError(
                'The resource [{}] was not found. Is the image located in another '
                'project? Use the --image-project flag to specify the '
                'project where the image is located.'.format(image_uri))
    else:
        image_uri = None

    if args.tags:
        tags = client.messages.Tags(items=args.tags)
    else:
        tags = None

    persistent_disks = (
        instance_template_utils.CreatePersistentAttachedDiskMessages(
            client.messages, args.disk or []))

    persistent_create_disks = (
        instance_template_utils.CreatePersistentCreateDiskMessages(
            client,
            compute_api.resources,
            instance_template_ref.project,
            getattr(args, 'create_disk', []),
            support_kms=support_kms))

    if create_boot_disk:
        boot_disk_list = [
            instance_template_utils.CreateDefaultBootAttachedDiskMessage(
                messages=client.messages,
                disk_type=args.boot_disk_type,
                disk_device_name=args.boot_disk_device_name,
                disk_auto_delete=args.boot_disk_auto_delete,
                disk_size_gb=boot_disk_size_gb,
                image_uri=image_uri,
                kms_args=args,
                support_kms=support_kms)
        ]
    else:
        boot_disk_list = []

    local_nvdimms = instance_utils.CreateLocalNvdimmMessages(
        args,
        compute_api.resources,
        client.messages,
    )

    local_ssds = instance_utils.CreateLocalSsdMessages(
        args,
        compute_api.resources,
        client.messages,
    )

    disks = (boot_disk_list + persistent_disks + persistent_create_disks +
             local_nvdimms + local_ssds)

    machine_type = instance_utils.InterpretMachineType(
        machine_type=args.machine_type,
        custom_cpu=args.custom_cpu,
        custom_memory=args.custom_memory,
        ext=getattr(args, 'custom_extensions', None),
        vm_type=getattr(args, 'custom_vm_type', None))

    guest_accelerators = (
        instance_template_utils.CreateAcceleratorConfigMessages(
            client.messages, getattr(args, 'accelerator', None)))

    instance_template = client.messages.InstanceTemplate(
        properties=client.messages.InstanceProperties(
            machineType=machine_type,
            disks=disks,
            canIpForward=args.can_ip_forward,
            metadata=metadata,
            minCpuPlatform=args.min_cpu_platform,
            networkInterfaces=network_interfaces,
            serviceAccounts=service_accounts,
            scheduling=scheduling,
            tags=tags,
            guestAccelerators=guest_accelerators,
        ),
        description=args.description,
        name=instance_template_ref.Name(),
    )

    instance_template.properties.shieldedInstanceConfig = shieldedinstance_config_message

    instance_template.properties.reservationAffinity = instance_utils.GetReservationAffinity(
        args, client)

    if support_confidential_compute:
        instance_template.properties.confidentialInstanceConfig = (
            confidential_instance_config_message)

    request = client.messages.ComputeInstanceTemplatesInsertRequest(
        instanceTemplate=instance_template,
        project=instance_template_ref.project)

    request.instanceTemplate.properties.labels = labels_util.ParseCreateArgs(
        args, client.messages.InstanceProperties.LabelsValue)

    _AddSourceInstanceToTemplate(compute_api, args, instance_template,
                                 support_source_instance)

    return client.MakeRequests([(client.apitools_client.instanceTemplates,
                                 'Insert', request)])
Example #15
0
def GetBootDiskSizeGb(args):
    boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
    utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)
    return boot_disk_size_gb
Example #16
0
    def CreateRequests(self, args):
        instances_flags.ValidateDockerArgs(args)
        instances_flags.ValidateDiskCommonFlags(args)
        instances_flags.ValidateLocalSsdFlags(args)
        instances_flags.ValidateServiceAccountAndScopeArgs(args)
        if instance_utils.UseExistingBootDisk(args.disk or []):
            raise exceptions.InvalidArgumentException(
                '--disk', 'Boot disk specified for containerized VM.')

        scheduling = instance_utils.CreateSchedulingMessage(
            messages=self.messages,
            maintenance_policy=args.maintenance_policy,
            preemptible=args.preemptible,
            restart_on_failure=args.restart_on_failure)

        if args.no_service_account:
            service_account = None
        else:
            service_account = args.service_account
        service_accounts = instance_utils.CreateServiceAccountMessages(
            messages=self.messages,
            scopes=[] if args.no_scopes else args.scopes,
            service_account=service_account)

        user_metadata = metadata_utils.ConstructMetadataMessage(
            self.messages,
            metadata=args.metadata,
            metadata_from_file=args.metadata_from_file)
        containers_utils.ValidateUserMetadata(user_metadata)

        boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
        utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

        instance_refs = instances_flags.INSTANCES_ARG.ResolveAsResource(
            args,
            self.resources,
            scope_lister=flags.GetDefaultScopeLister(self.compute_client,
                                                     self.project))

        # Check if the zone is deprecated or has maintenance coming.
        zone_resource_fetcher = zone_utils.ZoneResourceFetcher(
            self.compute_client)
        zone_resource_fetcher.WarnForZonalCreation(instance_refs)

        instances_flags.ValidatePublicDnsFlags(args)

        network_interface = instance_utils.CreateNetworkInterfaceMessage(
            resources=self.resources,
            compute_client=self.compute_client,
            network=args.network,
            subnet=args.subnet,
            private_network_ip=args.private_network_ip,
            no_address=args.no_address,
            address=args.address,
            instance_refs=instance_refs,
            network_tier=args.network_tier,
            no_public_dns=getattr(args, 'no_public_dns', None),
            public_dns=getattr(args, 'public_dns', None),
            no_public_ptr=getattr(args, 'no_public_ptr', None),
            public_ptr=getattr(args, 'public_ptr', None),
            no_public_ptr_domain=getattr(args, 'no_public_ptr_domain', None),
            public_ptr_domain=getattr(args, 'public_ptr_domain', None))

        machine_type_uris = instance_utils.CreateMachineTypeUris(
            resources=self.resources,
            compute_client=self.compute_client,
            project=self.project,
            machine_type=args.machine_type,
            custom_cpu=args.custom_cpu,
            custom_memory=args.custom_memory,
            ext=getattr(args, 'custom_extensions', None),
            instance_refs=instance_refs)

        image_uri = containers_utils.ExpandCosImageFlag(self.compute_client)
        requests = []
        for instance_ref, machine_type_uri in zip(instance_refs,
                                                  machine_type_uris):
            metadata = containers_utils.CreateMetadataMessage(
                self.messages, args.run_as_privileged, args.container_manifest,
                args.docker_image, args.port_mappings, args.run_command,
                user_metadata, instance_ref.Name())
            requests.append(
                self.messages.ComputeInstancesInsertRequest(
                    instance=self.messages.Instance(
                        canIpForward=args.can_ip_forward,
                        disks=(self._CreateDiskMessages(
                            args, boot_disk_size_gb, image_uri, instance_ref)),
                        description=args.description,
                        machineType=machine_type_uri,
                        metadata=metadata,
                        minCpuPlatform=args.min_cpu_platform,
                        name=instance_ref.Name(),
                        networkInterfaces=[network_interface],
                        serviceAccounts=service_accounts,
                        scheduling=scheduling,
                        tags=containers_utils.CreateTagsMessage(
                            self.messages, args.tags),
                    ),
                    project=self.project,
                    zone=instance_ref.zone))
        return requests
def _RunCreate(compute_api,
               args,
               support_source_instance,
               support_kms=False,
               support_post_key_revocation_action_type=False,
               support_multi_writer=False,
               support_mesh=False,
               support_host_error_timeout_seconds=False,
               support_numa_node_count=False,
               support_visible_core_count=False,
               support_disk_architecture=False,
               support_key_revocation_action_type=False,
               support_max_run_duration=False):
  """Common routine for creating instance template.

  This is shared between various release tracks.

  Args:
      compute_api: The compute api.
      args: argparse.Namespace, An object that contains the values for the
        arguments specified in the .Args() method.
      support_source_instance: indicates whether source instance is supported.
      support_kms: Indicate whether KMS is integrated or not.
      support_post_key_revocation_action_type: Indicate whether
        post_key_revocation_action_type is supported.
      support_multi_writer: Indicates whether a disk can have multiple writers.
      support_mesh: Indicates whether adding VM to a Anthos Service Mesh is
        supported.
      support_host_error_timeout_seconds: Indicate the timeout in seconds for
        host error detection.
      support_numa_node_count: Indicates whether setting NUMA node count is
        supported.
      support_visible_core_count: Indicates whether setting a custom visible
      support_disk_architecture: Storage resources can be used to create boot
        disks compatible with ARM64 or X86_64 machine architectures. If this
        field is not specified, the default is ARCHITECTURE_UNSPECIFIED.
      support_key_revocation_action_type: Indicate whether
        key_revocation_action_type is supported.
      support_max_run_duration: Indicate whether max-run-duration or
        termination-time issupported.

  Returns:
      A resource object dispatched by display.Displayer().
  """
  _ValidateInstancesFlags(
      args,
      support_kms=support_kms,
      support_max_run_duration=support_max_run_duration)
  instances_flags.ValidateNetworkTierArgs(args)

  instance_templates_flags.ValidateServiceProxyFlags(args)
  if support_mesh:
    instance_templates_flags.ValidateMeshFlag(args)

  client = compute_api.client

  boot_disk_size_gb = utils.BytesToGb(args.boot_disk_size)
  utils.WarnIfDiskSizeIsTooSmall(boot_disk_size_gb, args.boot_disk_type)

  instance_template_ref = (
      Create.InstanceTemplateArg.ResolveAsResource(args, compute_api.resources))

  AddScopesForServiceProxy(args)
  AddServiceProxyArgsToMetadata(args)

  if hasattr(args, 'network_interface') and args.network_interface:
    network_interfaces = (
        instance_template_utils.CreateNetworkInterfaceMessages)(
            resources=compute_api.resources,
            scope_lister=flags.GetDefaultScopeLister(client),
            messages=client.messages,
            network_interface_arg=args.network_interface,
            region=args.region)
  else:
    network_tier = getattr(args, 'network_tier', None)
    stack_type = getattr(args, 'stack_type', None)
    ipv6_network_tier = getattr(args, 'ipv6_network_tier', None)
    network_interfaces = [
        instance_template_utils.CreateNetworkInterfaceMessage(
            resources=compute_api.resources,
            scope_lister=flags.GetDefaultScopeLister(client),
            messages=client.messages,
            network=args.network,
            private_ip=args.private_network_ip,
            region=args.region,
            subnet=args.subnet,
            address=(instance_template_utils.EPHEMERAL_ADDRESS
                     if not args.no_address and not args.address else
                     args.address),
            network_tier=network_tier,
            stack_type=stack_type,
            ipv6_network_tier=ipv6_network_tier)
    ]

  if support_mesh:
    ConfigureMeshTemplate(args, instance_template_ref, network_interfaces)

  metadata = metadata_utils.ConstructMetadataMessage(
      client.messages,
      metadata=args.metadata,
      metadata_from_file=args.metadata_from_file)

  # Compute the shieldedInstanceConfig message.
  shieldedinstance_config_message = BuildShieldedInstanceConfigMessage(
      messages=client.messages, args=args)

  confidential_instance_config_message = (
      BuildConfidentialInstanceConfigMessage(
          messages=client.messages, args=args))

  node_affinities = sole_tenancy_util.GetSchedulingNodeAffinityListFromArgs(
      args, client.messages)

  location_hint = None
  if args.IsSpecified('location_hint'):
    location_hint = args.location_hint

  provisioning_model = None
  if (hasattr(args, 'provisioning_model') and
      args.IsSpecified('provisioning_model')):
    provisioning_model = args.provisioning_model

  termination_action = None
  if (hasattr(args, 'instance_termination_action') and
      args.IsSpecified('instance_termination_action')):
    termination_action = args.instance_termination_action

  max_run_duration = None
  if (hasattr(args, 'max_run_duration') and
      args.IsSpecified('max_run_duration')):
    max_run_duration = args.max_run_duration

  termination_time = None
  if (hasattr(args, 'termination_time') and
      args.IsSpecified('termination_time')):
    termination_time = args.termination_time

  host_error_timeout_seconds = None
  if support_host_error_timeout_seconds and args.IsSpecified(
      'host_error_timeout_seconds'):
    host_error_timeout_seconds = args.host_error_timeout_seconds
  scheduling = instance_utils.CreateSchedulingMessage(
      messages=client.messages,
      maintenance_policy=args.maintenance_policy,
      preemptible=args.preemptible,
      restart_on_failure=args.restart_on_failure,
      node_affinities=node_affinities,
      min_node_cpu=args.min_node_cpu,
      location_hint=location_hint,
      provisioning_model=provisioning_model,
      instance_termination_action=termination_action,
      host_error_timeout_seconds=host_error_timeout_seconds,
      max_run_duration=max_run_duration,
      termination_time=termination_time)

  if args.no_service_account:
    service_account = None
  else:
    service_account = args.service_account
  service_accounts = instance_utils.CreateServiceAccountMessages(
      messages=client.messages,
      scopes=[] if args.no_scopes else args.scopes,
      service_account=service_account)

  create_boot_disk = not (
      instance_utils.UseExistingBootDisk((args.disk or []) +
                                         (args.create_disk or [])))
  if create_boot_disk:
    image_expander = image_utils.ImageExpander(client, compute_api.resources)
    try:
      image_uri, _ = image_expander.ExpandImageFlag(
          user_project=instance_template_ref.project,
          image=args.image,
          image_family=args.image_family,
          image_project=args.image_project,
          return_image_resource=True)
    except utils.ImageNotFoundError as e:
      if args.IsSpecified('image_project'):
        raise e
      image_uri, _ = image_expander.ExpandImageFlag(
          user_project=instance_template_ref.project,
          image=args.image,
          image_family=args.image_family,
          image_project=args.image_project,
          return_image_resource=False)
      raise utils.ImageNotFoundError(
          'The resource [{}] was not found. Is the image located in another '
          'project? Use the --image-project flag to specify the '
          'project where the image is located.'.format(image_uri))
  else:
    image_uri = None

  if args.tags:
    tags = client.messages.Tags(items=args.tags)
  else:
    tags = None

  persistent_disks = (
      instance_template_utils.CreatePersistentAttachedDiskMessages(
          client.messages, args.disk or []))

  persistent_create_disks = (
      instance_template_utils.CreatePersistentCreateDiskMessages(
          client,
          compute_api.resources,
          instance_template_ref.project,
          getattr(args, 'create_disk', []),
          support_kms=support_kms,
          support_multi_writer=support_multi_writer,
          support_disk_architecture=support_disk_architecture))

  if create_boot_disk:
    boot_disk_list = [
        instance_template_utils.CreateDefaultBootAttachedDiskMessage(
            messages=client.messages,
            disk_type=args.boot_disk_type,
            disk_device_name=args.boot_disk_device_name,
            disk_auto_delete=args.boot_disk_auto_delete,
            disk_size_gb=boot_disk_size_gb,
            image_uri=image_uri,
            kms_args=args,
            support_kms=support_kms,
            disk_provisioned_iops=args.boot_disk_provisioned_iops)
    ]
  else:
    boot_disk_list = []

  local_nvdimms = create_utils.CreateLocalNvdimmMessages(
      args,
      compute_api.resources,
      client.messages,
  )

  local_ssds = create_utils.CreateLocalSsdMessages(
      args,
      compute_api.resources,
      client.messages,
  )

  disks = (
      boot_disk_list + persistent_disks + persistent_create_disks +
      local_nvdimms + local_ssds)

  machine_type = instance_utils.InterpretMachineType(
      machine_type=args.machine_type,
      custom_cpu=args.custom_cpu,
      custom_memory=args.custom_memory,
      ext=getattr(args, 'custom_extensions', None),
      vm_type=getattr(args, 'custom_vm_type', None))

  guest_accelerators = (
      instance_template_utils.CreateAcceleratorConfigMessages(
          client.messages, getattr(args, 'accelerator', None)))

  instance_template = client.messages.InstanceTemplate(
      properties=client.messages.InstanceProperties(
          machineType=machine_type,
          disks=disks,
          canIpForward=args.can_ip_forward,
          metadata=metadata,
          minCpuPlatform=args.min_cpu_platform,
          networkInterfaces=network_interfaces,
          serviceAccounts=service_accounts,
          scheduling=scheduling,
          tags=tags,
          guestAccelerators=guest_accelerators,
      ),
      description=args.description,
      name=instance_template_ref.Name(),
  )

  instance_template.properties.shieldedInstanceConfig = shieldedinstance_config_message

  instance_template.properties.reservationAffinity = instance_utils.GetReservationAffinity(
      args, client)

  instance_template.properties.confidentialInstanceConfig = (
      confidential_instance_config_message)

  if args.IsSpecified('network_performance_configs'):
    instance_template.properties.networkPerformanceConfig = (
        instance_utils.GetNetworkPerformanceConfig(args, client))

  if args.IsSpecified('resource_policies'):
    instance_template.properties.resourcePolicies = getattr(
        args, 'resource_policies', [])

  if support_post_key_revocation_action_type and args.IsSpecified(
      'post_key_revocation_action_type'):
    instance_template.properties.postKeyRevocationActionType = arg_utils.ChoiceToEnum(
        args.post_key_revocation_action_type, client.messages.InstanceProperties
        .PostKeyRevocationActionTypeValueValuesEnum)

  if support_key_revocation_action_type and args.IsSpecified(
      'key_revocation_action_type'):
    instance_template.properties.keyRevocationActionType = arg_utils.ChoiceToEnum(
        args.key_revocation_action_type, client.messages.InstanceProperties
        .KeyRevocationActionTypeValueValuesEnum)

  if args.private_ipv6_google_access_type is not None:
    instance_template.properties.privateIpv6GoogleAccess = (
        instances_flags.GetPrivateIpv6GoogleAccessTypeFlagMapperForTemplate(
            client.messages).GetEnumForChoice(
                args.private_ipv6_google_access_type))

  # Create an AdvancedMachineFeatures message if any of the features requiring
  # one have been specified.
  has_visible_core_count = (
      support_visible_core_count and args.visible_core_count is not None)
  if (args.enable_nested_virtualization is not None or
      args.threads_per_core is not None or
      (support_numa_node_count and args.numa_node_count is not None) or
      has_visible_core_count or args.enable_uefi_networking is not None):

    visible_core_count = args.visible_core_count if has_visible_core_count else None
    instance_template.properties.advancedMachineFeatures = (
        instance_utils.CreateAdvancedMachineFeaturesMessage(
            client.messages, args.enable_nested_virtualization,
            args.threads_per_core,
            args.numa_node_count if support_numa_node_count else None,
            visible_core_count, args.enable_uefi_networking))

  if args.resource_manager_tags:
    ret_resource_manager_tags = resource_manager_tags_utils.GetResourceManagerTags(
        args.resource_manager_tags)
    if ret_resource_manager_tags is not None:
      properties = client.messages.InstanceProperties
      instance_template.properties.resourceManagerTags = properties.ResourceManagerTagsValue(
          additionalProperties=[
              properties.ResourceManagerTagsValue.AdditionalProperty(
                  key=key, value=value) for key, value in sorted(
                      six.iteritems(ret_resource_manager_tags))
          ])

  request = client.messages.ComputeInstanceTemplatesInsertRequest(
      instanceTemplate=instance_template, project=instance_template_ref.project)

  request.instanceTemplate.properties.labels = ParseCreateArgsWithServiceProxy(
      args, client.messages.InstanceProperties.LabelsValue)

  _AddSourceInstanceToTemplate(compute_api, args, instance_template,
                               support_source_instance)

  return client.MakeRequests([(client.apitools_client.instanceTemplates,
                               'Insert', request)])