def GetNetworkInterfacesAlpha(args, client, holder, project, location, scope, skip_defaults): if (skip_defaults and not instance_utils.IsAnySpecified( args, 'network', 'subnet', 'private_network_ip', 'no_address', 'address', 'network_tier', 'no_public_dns', 'public_dns', 'no_public_ptr', 'public_ptr', 'no_public_ptr_domain', 'public_ptr_domain')): return [] return [ 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, project=project, location=location, scope=scope, network_tier=getattr(args, 'network_tier', None), 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)) ]
def GetBulkNetworkInterfaces(args, resource_parser, compute_client, holder, project, location, scope, skip_defaults): if (skip_defaults and not instance_utils.IsAnySpecified( args, 'network_interface', 'network', 'network_tier', 'subnet', 'no_address')): return [] elif args.network_interface: return CreateNetworkInterfaceMessages( resources=resource_parser, compute_client=compute_client, network_interface_arg=args.network_interface, project=project, location=location, scope=scope) else: return [ CreateNetworkInterfaceMessage( resources=holder.resources, compute_client=compute_client, network=args.network, subnet=args.subnet, no_address=args.no_address, project=project, location=location, scope=scope, network_tier=getattr(args, 'network_tier', None), ) ]
def CheckSpecifiedDiskArgs(args, skip_defaults=False, support_kms=False, support_nvdimm=False): """Checks if relevant disk arguments have been specified.""" flags_to_check = [ 'disk', 'local_ssd', 'boot_disk_type', 'boot_disk_device_name', 'boot_disk_auto_delete', 'require_csek_key_create', ] if support_kms: flags_to_check.extend([ 'create_disk', 'boot_disk_kms_key', 'boot_disk_kms_project', 'boot_disk_kms_location', 'boot_disk_kms_keyring', ]) if support_nvdimm: flags_to_check.extend(['local_nvdimm']) if (skip_defaults and not instance_utils.IsAnySpecified(args, *flags_to_check)): return False return True
def _GetUpdateInstanceSchedulingRef(self, instance_ref, args, holder): client = holder.client.apitools_client messages = holder.client.messages if instance_utils.IsAnySpecified(args, 'node', 'node_affinity_file', 'node_group'): affinities = sole_tenancy_util.GetSchedulingNodeAffinityListFromArgs( args, messages) elif args.IsSpecified('clear_node_affinities'): affinities = [] else: # No relevant args were specified. We shouldn't have called this function. return None instance = client.instances.Get( messages.ComputeInstancesGetRequest(**instance_ref.AsDict())) instance.scheduling.nodeAffinities = affinities request = messages.ComputeInstancesUpdateRequest( instance=instance_ref.Name(), project=instance_ref.project, zone=instance_ref.zone, instanceResource=instance, minimalAction=messages.ComputeInstancesUpdateRequest. MinimalActionValueValuesEnum.NO_EFFECT, mostDisruptiveAllowedAction=messages.ComputeInstancesUpdateRequest. MostDisruptiveAllowedActionValueValuesEnum.REFRESH) operation = client.instances.Update(request) return holder.resources.Parse(operation.selfLink, collection='compute.zoneOperations')
def CheckDiskMessageArgs(self, args, skip_defaults): """Creates API messages with disks attached to VM instance.""" flags_to_check = [ 'create_disk', 'local_ssd', 'boot_disk_type', 'boot_disk_device_name', 'boot_disk_auto_delete' ] if hasattr(args, 'local_nvdimm'): flags_to_check.append('local_nvdimm') if (skip_defaults and not args.IsSpecified('disk') and not instance_utils.IsAnySpecified(args, *flags_to_check)): return False return True
def _Run(self, args): """Issues request necessary for setting scheduling options.""" holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) client = holder.client instance_ref = flags.INSTANCE_ARG.ResolveAsResource( args, holder.resources, scope_lister=flags.GetInstanceZoneScopeLister(client)) scheduling_options = client.messages.Scheduling() scheduling_options.automaticRestart = args.restart_on_failure if args.IsSpecified('preemptible'): scheduling_options.preemptible = args.preemptible cleared_fields = [] if args.IsSpecified('min_node_cpu'): scheduling_options.minNodeCpus = int(args.min_node_cpu) elif args.IsSpecified('clear_min_node_cpu'): scheduling_options.minNodeCpus = None cleared_fields.append('minNodeCpus') if args.IsSpecified('maintenance_policy'): scheduling_options.onHostMaintenance = ( client.messages.Scheduling.OnHostMaintenanceValueValuesEnum( args.maintenance_policy)) if instance_utils.IsAnySpecified(args, 'node', 'node_affinity_file', 'node_group'): affinities = sole_tenancy_util.GetSchedulingNodeAffinityListFromArgs( args, client.messages) scheduling_options.nodeAffinities = affinities elif args.IsSpecified('clear_node_affinities'): scheduling_options.nodeAffinities = [] cleared_fields.append('nodeAffinities') with holder.client.apitools_client.IncludeFields(cleared_fields): request = client.messages.ComputeInstancesSetSchedulingRequest( instance=instance_ref.Name(), project=instance_ref.project, scheduling=scheduling_options, zone=instance_ref.zone) return client.MakeRequests([(client.apitools_client.instances, 'SetScheduling', request)])
def _BuildCpuUtilization(args, messages, predictive=False): """Builds the CPU Utilization message given relevant arguments.""" flags_to_check = ['target_cpu_utilization', 'scale_based_on_cpu'] if predictive: flags_to_check.append('cpu_utilization_predictive_method') if instance_utils.IsAnySpecified(args, *flags_to_check): cpu_message = messages.AutoscalingPolicyCpuUtilization() if args.target_cpu_utilization: cpu_message.utilizationTarget = args.target_cpu_utilization if predictive and args.cpu_utilization_predictive_method: cpu_predictive_enum = messages.AutoscalingPolicyCpuUtilization.PredictiveMethodValueValuesEnum cpu_message.predictiveMethod = arg_utils.ChoiceToEnum( args.cpu_utilization_predictive_method, cpu_predictive_enum) return cpu_message return None
def GetNetworkInterfaces(args, client, holder, project, location, scope, skip_defaults): """Get network interfaces.""" network_interface_args = filter(lambda flag: hasattr(args, flag), [ 'address', 'ipv6_network_tier', 'ipv6_public_ptr_domain', 'network', 'network_tier', 'no_address', 'no_public_ptr', 'no_public_ptr_domain', 'private_network_ip', 'public_ptr', 'public_ptr_domain', 'stack_type', 'subnet', ]) if (skip_defaults and not instance_utils.IsAnySpecified(args, *network_interface_args)): return [] return [ CreateNetworkInterfaceMessage( resources=holder.resources, compute_client=client, network=args.network, subnet=args.subnet, no_address=args.no_address, address=args.address, project=project, location=location, scope=scope, no_public_ptr=args.no_public_ptr, public_ptr=args.public_ptr, no_public_ptr_domain=args.no_public_ptr_domain, public_ptr_domain=args.public_ptr_domain, private_network_ip=getattr(args, 'private_network_ip', None), network_tier=getattr(args, 'network_tier', None), ipv6_public_ptr_domain=getattr(args, 'ipv6_public_ptr_domain', None), stack_type=getattr(args, 'stack_type', None), ipv6_network_tier=getattr(args, 'ipv6_network_tier', None), ) ]
def _GetDiskMessages(self, args, skip_defaults, instance_refs, compute_client, resource_parser, create_boot_disk, boot_disk_size_gb, image_uri, csek_keys): flags_to_check = [ 'disk', 'local_ssd', 'boot_disk_type', 'boot_disk_device_name', 'boot_disk_auto_delete', 'require_csek_key_create', ] if self._support_kms: flags_to_check.extend([ 'create_disk', 'boot_disk_kms_key', 'boot_disk_kms_project', 'boot_disk_kms_location', 'boot_disk_kms_keyring', ]) if self._support_nvdimm: flags_to_check.extend(['local_nvdimm']) if (skip_defaults and not instance_utils.IsAnySpecified(args, *flags_to_check)): return [[] for _ in instance_refs] # 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. for instance_ref in instance_refs: disks_messages.append( self._CreateDiskMessage( args, skip_defaults, instance_ref.Name(), instance_ref.project, instance_ref.zone, compute_client, resource_parser, create_boot_disk, boot_disk_size_gb, image_uri, csek_keys)) return disks_messages
def _GetDiskMessagess( self, args, skip_defaults, instance_refs, compute_client, resource_parser, create_boot_disk, boot_disk_size_gb, image_uri, csek_keys): flags_to_check = [ 'disk', 'local_ssd', 'boot_disk_type', 'boot_disk_device_name', 'boot_disk_auto_delete', 'require_csek_key_create', ] if self._support_kms: flags_to_check.extend([ 'create_disk', 'boot_disk_kms_key', 'boot_disk_kms_project', 'boot_disk_kms_location', 'boot_disk_kms_keyring', ]) if (skip_defaults and not instance_utils.IsAnySpecified(args, *flags_to_check)): return [[] for _ in instance_refs] # 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 = {} for instance_ref in instance_refs: persistent_disks, boot_disk_ref = ( instance_utils.CreatePersistentAttachedDiskMessages( resource_parser, compute_client, csek_keys, args.disk or [], instance_ref)) persistent_create_disks = ( instance_utils.CreatePersistentCreateDiskMessages( compute_client, resource_parser, 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 csek_keys else None), image_uri=image_uri, instance_ref=instance_ref, csek_keys=csek_keys, kms_args=args) 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) return disks_messages
def _Run(self, args): holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) client = holder.client.apitools_client messages = holder.client.messages instance_ref = flags.INSTANCE_ARG.ResolveAsResource( args, holder.resources, scope_lister=flags.GetInstanceZoneScopeLister(holder.client)) result = None labels_operation_ref = None min_cpu_platform_operation_ref = None deletion_protection_operation_ref = None shielded_instance_config_ref = None display_device_ref = None labels_diff = labels_util.Diff.FromUpdateArgs(args) if labels_diff.MayHaveUpdates(): instance = client.instances.Get( messages.ComputeInstancesGetRequest(**instance_ref.AsDict())) result = instance labels_operation_ref = self._GetLabelsOperationRef( labels_diff, instance, instance_ref, holder) if hasattr(args, 'min_cpu_platform') and args.min_cpu_platform is not None: min_cpu_platform_operation_ref = self._GetMinCpuPlatformOperationRef( args.min_cpu_platform, instance_ref, holder) if args.deletion_protection is not None: deletion_protection_operation_ref = ( self._GetDeletionProtectionOperationRef( args.deletion_protection, instance_ref, holder)) operation_poller = poller.Poller(client.instances) result = self._WaitForResult(operation_poller, labels_operation_ref, 'Updating labels of instance [{0}]', instance_ref.Name()) or result result = self._WaitForResult( operation_poller, min_cpu_platform_operation_ref, 'Changing minimum CPU platform of instance [{0}]', instance_ref.Name()) or result result = self._WaitForResult( operation_poller, deletion_protection_operation_ref, 'Setting deletion protection of instance [{0}] to [{1}]', instance_ref.Name(), args.deletion_protection) or result if (args.IsSpecified('shielded_vm_secure_boot') or args.IsSpecified('shielded_vm_vtpm') or args.IsSpecified('shielded_vm_integrity_monitoring')): shielded_instance_config_ref = self._GetShieldedInstanceConfigRef( instance_ref, args, holder) result = self._WaitForResult( operation_poller, shielded_instance_config_ref, 'Setting shieldedInstanceConfig of instance [{0}]', instance_ref.Name()) or result if args.IsSpecified('shielded_vm_learn_integrity_policy'): shielded_instance_integrity_policy_ref = ( self._GetShieldedInstanceIntegrityPolicyRef( instance_ref, holder)) result = self._WaitForResult( operation_poller, shielded_instance_integrity_policy_ref, 'Setting shieldedInstanceIntegrityPolicy of instance [{0}]', instance_ref.Name()) or result if args.IsSpecified('enable_display_device'): display_device_ref = self._GetDisplayDeviceOperationRef( args.enable_display_device, instance_ref, holder) result = self._WaitForResult( operation_poller, display_device_ref, 'Updating display device of instance [{0}]', instance_ref.Name()) or result if instance_utils.IsAnySpecified(args, 'node', 'node_affinity_file', 'node_group', 'clear_node_affinities'): update_scheduling_ref = self._GetUpdateInstanceSchedulingRef( instance_ref, args, holder) result = self._WaitForResult( operation_poller, update_scheduling_ref, 'Updating the scheduling of instance [{0}]', instance_ref.Name()) or result return result