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 request = self.messages.ComputeInstanceTemplatesInsertRequest( instanceTemplate=self.messages.InstanceTemplate( properties=self.messages.InstanceProperties( machineType=args.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]
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