def _CreateLocalSsdMessage(resources, messages, device_name, interface, size_bytes=None, location=None, scope=None, project=None): """Create a message representing a local ssd.""" if location: disk_type_ref = instance_utils.ParseDiskType(resources, 'local-ssd', project, location, scope) disk_type = disk_type_ref.SelfLink() else: disk_type = 'local-ssd' maybe_interface_enum = ( messages.AttachedDisk.InterfaceValueValuesEnum(interface) if interface else None) local_ssd = messages.AttachedDisk( type=messages.AttachedDisk.TypeValueValuesEnum.SCRATCH, autoDelete=True, deviceName=device_name, interface=maybe_interface_enum, mode=messages.AttachedDisk.ModeValueValuesEnum.READ_WRITE, initializeParams=messages.AttachedDiskInitializeParams( diskType=disk_type), ) if size_bytes is not None: local_ssd.diskSizeGb = utils.BytesToGb(size_bytes) return local_ssd
def _CreateLocalNvdimmMessage(resources, messages, size_bytes=None, location=None, scope=None, project=None): """Create a message representing a local NVDIMM.""" if location: disk_type_ref = instance_utils.ParseDiskType(resources, NVDIMM_DISK_TYPE, project, location, scope) disk_type = disk_type_ref.SelfLink() else: disk_type = NVDIMM_DISK_TYPE local_nvdimm = messages.AttachedDisk( type=messages.AttachedDisk.TypeValueValuesEnum.SCRATCH, autoDelete=True, interface=messages.AttachedDisk.InterfaceValueValuesEnum.NVDIMM, mode=messages.AttachedDisk.ModeValueValuesEnum.READ_WRITE, initializeParams=messages.AttachedDiskInitializeParams( diskType=disk_type), ) if size_bytes is not None: local_nvdimm.diskSizeGb = utils.BytesToGb(size_bytes) return local_nvdimm
def CreatePersistentCreateDiskMessages(compute_client, resources, csek_keys, create_disks, project, location, scope, enable_kms=False, enable_snapshots=False, container_mount_disk=None, resource_policy=False, enable_source_snapshot_csek=False, enable_image_csek=False): """Returns a list of AttachedDisk messages for newly creating disks. Args: compute_client: creates resources, resources: parser of resources, csek_keys: customer suplied encryption keys, create_disks: disk objects - contains following properties * name - the name of disk, * description - an optional description for the disk, * mode - 'rw' (R/W), 'ro' (R/O) access mode, * disk-size - the size of the disk, * disk-type - the type of the disk (HDD or SSD), * image - the name of the image to initialize from, * image-csek-required - the name of the CSK protected image, * image-family - the image family name, * image-project - the project name that has the image, * auto-delete - whether disks is deleted when VM is deleted, * device-name - device name on VM, * source-snapshot - the snapshot to initialize from, * source-snapshot-csek-required - CSK protected snapshot, * disk-resource-policy - resource policies applied to disk. * enable_source_snapshot_csek - CSK file for snapshot, * enable_image_csek - CSK file for image project: Project of instance that will own the new disks. location: Location of the instance that will own the new disks. scope: Location type of the instance that will own the new disks. enable_kms: True if KMS keys are supported for the disk. enable_snapshots: True if snapshot initialization is supported for the disk. container_mount_disk: list of disks to be mounted to container, if any. resource_policy: True if resource-policies are enabled enable_source_snapshot_csek: True if snapshot CSK files are enabled enable_image_csek: True if image CSK files are enabled Returns: list of API messages for attached disks """ disks_messages = [] messages = compute_client.messages compute = compute_client.apitools_client for disk in create_disks or []: name = disk.get('name') # Resolves the mode. mode_value = disk.get('mode', 'rw') if mode_value == 'rw': mode = messages.AttachedDisk.ModeValueValuesEnum.READ_WRITE else: mode = messages.AttachedDisk.ModeValueValuesEnum.READ_ONLY auto_delete_value = disk.get('auto-delete', 'yes') auto_delete = auto_delete_value == 'yes' disk_size_gb = utils.BytesToGb(disk.get('size')) disk_type = disk.get('type') if disk_type: disk_type_ref = instance_utils.ParseDiskType(resources, disk_type, project, location, scope) disk_type_uri = disk_type_ref.SelfLink() else: disk_type_uri = None img = disk.get('image') img_family = disk.get('image-family') img_project = disk.get('image-project') image_uri = None if img or img_family: image_expander = image_utils.ImageExpander(compute_client, resources) image_uri, _ = image_expander.ExpandImageFlag( user_project=project, image=img, image_family=img_family, image_project=img_project, return_image_resource=False) image_key = None disk_key = None if csek_keys: image_key = csek_utils.MaybeLookupKeyMessagesByUri( csek_keys, resources, [image_uri], compute) if name: disk_ref = resources.Parse( name, collection='compute.disks', params={'zone': location}) disk_key = csek_utils.MaybeLookupKeyMessage(csek_keys, disk_ref, compute) if enable_kms: disk_key = kms_utils.MaybeGetKmsKeyFromDict(disk, messages, disk_key) initialize_params = messages.AttachedDiskInitializeParams( diskName=name, description=disk.get('description'), sourceImage=image_uri, diskSizeGb=disk_size_gb, diskType=disk_type_uri, sourceImageEncryptionKey=image_key) if enable_snapshots: snapshot_name = disk.get('source-snapshot') attached_snapshot_uri = instance_utils.ResolveSnapshotURI( snapshot=snapshot_name, user_project=project, resource_parser=resources) if attached_snapshot_uri: initialize_params.sourceImage = None initialize_params.sourceSnapshot = attached_snapshot_uri if resource_policy: policies = disk.get('disk-resource-policy') if policies: initialize_params.resourcePolicies = policies if enable_image_csek: image_key_file = disk.get('image_csek') if image_key_file: initialize_params.imageKeyFile = image_key_file if enable_source_snapshot_csek: snapshot_key_file = disk.get('source_snapshot_csek') if snapshot_key_file: initialize_params.snapshotKeyFile = snapshot_key_file device_name = instance_utils.GetDiskDeviceName(disk, name, container_mount_disk) create_disk = messages.AttachedDisk( autoDelete=auto_delete, boot=False, deviceName=device_name, initializeParams=initialize_params, mode=mode, type=messages.AttachedDisk.TypeValueValuesEnum.PERSISTENT, diskEncryptionKey=disk_key) disks_messages.append(create_disk) return disks_messages
def CreateDefaultBootAttachedDiskMessage(compute_client, resources, disk_type, disk_device_name, disk_auto_delete, disk_size_gb, require_csek_key_create, image_uri, instance_name, project, location, scope, csek_keys=None, kms_args=None, enable_kms=False, snapshot_uri=None): """Returns an AttachedDisk message for creating a new boot disk.""" messages = compute_client.messages compute = compute_client.apitools_client if disk_type: disk_type_ref = instance_utils.ParseDiskType(resources, disk_type, project, location, scope) disk_type_uri = disk_type_ref.SelfLink() else: disk_type_uri = None if csek_keys: # If we're going to encrypt the boot disk make sure that we select # a name predictably, instead of letting the API deal with name # conflicts automatically. # # Note that when csek keys are being used we *always* want force this # even if we don't have any encryption key for default disk name. # # Consider the case where the user's key file has a key for disk `foo-1` # and no other disk. Assume she runs # gcloud compute instances create foo --csek-key-file f \ # --no-require-csek-key-create # and gcloud doesn't force the disk name to be `foo`. The API might # select name `foo-1` for the new disk, but has no way of knowing # that the user has a key file mapping for that disk name. That # behavior violates the principle of least surprise. # # Instead it's better for gcloud to force a specific disk name in the # instance create, and fail if that name isn't available. effective_boot_disk_name = (disk_device_name or instance_name) disk_ref = resources.Parse( effective_boot_disk_name, collection='compute.disks', params={ 'project': project, 'zone': location }) disk_key_or_none = csek_utils.MaybeToMessage( csek_keys.LookupKey(disk_ref, require_csek_key_create), compute) [image_key_or_none ] = csek_utils.MaybeLookupKeyMessagesByUri(csek_keys, resources, [image_uri], compute) kwargs_init_parms = {'sourceImageEncryptionKey': image_key_or_none} kwargs_disk = {'diskEncryptionKey': disk_key_or_none} else: kwargs_disk = {} kwargs_init_parms = {} effective_boot_disk_name = disk_device_name if enable_kms: kms_key = kms_utils.MaybeGetKmsKey( kms_args, messages, kwargs_disk.get('diskEncryptionKey', None), boot_disk_prefix=True) if kms_key: kwargs_disk = {'diskEncryptionKey': kms_key} initialize_params = messages.AttachedDiskInitializeParams( sourceImage=image_uri, diskSizeGb=disk_size_gb, diskType=disk_type_uri, **kwargs_init_parms) if snapshot_uri: initialize_params.sourceImage = None initialize_params.sourceSnapshot = snapshot_uri return messages.AttachedDisk( autoDelete=disk_auto_delete, boot=True, deviceName=effective_boot_disk_name, initializeParams=initialize_params, mode=messages.AttachedDisk.ModeValueValuesEnum.READ_WRITE, type=messages.AttachedDisk.TypeValueValuesEnum.PERSISTENT, **kwargs_disk)
def CreatePersistentCreateDiskMessages(compute_client, resources, csek_keys, create_disks, project, location, scope, holder, enable_kms=False, enable_snapshots=False, container_mount_disk=None, enable_source_snapshot_csek=False, enable_image_csek=False, support_replica_zones=False, use_disk_type_uri=True, support_multi_writer=False, support_image_family_scope=False, support_disk_architecture=False): """Returns a list of AttachedDisk messages for newly creating disks. Args: compute_client: creates resources, resources: parser of resources, csek_keys: customer suplied encryption keys, create_disks: disk objects - contains following properties * name - the name of disk, * description - an optional description for the disk, * mode - 'rw' (R/W), 'ro' (R/O) access mode, * disk-size - the size of the disk, * disk-type - the type of the disk (HDD or SSD), * image - the name of the image to initialize from, * image-csek-required - the name of the CSK protected image, * image-family - the image family name, * image-project - the project name that has the image, * auto-delete - whether disks is deleted when VM is deleted, * device-name - device name on VM, * source-snapshot - the snapshot to initialize from, * source-snapshot-csek-required - CSK protected snapshot, * disk-resource-policy - resource policies applied to disk. * enable_source_snapshot_csek - CSK file for snapshot, * enable_image_csek - CSK file for image project: Project of instance that will own the new disks. location: Location of the instance that will own the new disks. scope: Location type of the instance that will own the new disks. holder: Convenience class to hold lazy initialized client and resources. enable_kms: True if KMS keys are supported for the disk. enable_snapshots: True if snapshot initialization is supported for the disk. container_mount_disk: list of disks to be mounted to container, if any. enable_source_snapshot_csek: True if snapshot CSK files are enabled enable_image_csek: True if image CSK files are enabled support_replica_zones: True if we allow creation of regional disks use_disk_type_uri: True to use disk type URI, False if naked type. support_multi_writer: True if we allow multiple instances to write to disk. support_image_family_scope: True if the zonal image views are supported. support_disk_architecture: The machine architecture the created disk is compatible with. Returns: list of API messages for attached disks """ disks_messages = [] messages = compute_client.messages compute = compute_client.apitools_client for disk in create_disks or []: name = disk.get('name') # Resolves the mode. mode_value = disk.get('mode', 'rw') if mode_value == 'rw': mode = messages.AttachedDisk.ModeValueValuesEnum.READ_WRITE else: mode = messages.AttachedDisk.ModeValueValuesEnum.READ_ONLY auto_delete = disk.get('auto-delete', True) disk_size_gb = utils.BytesToGb(disk.get('size')) disk_type = disk.get('type') if disk_type: if use_disk_type_uri: disk_type_ref = instance_utils.ParseDiskType(resources, disk_type, project, location, scope) disk_type = disk_type_ref.SelfLink() else: disk_type = None img = disk.get('image') img_family = disk.get('image-family') img_project = disk.get('image-project') image_family_scope = disk.get('image_family_scope') image_uri = None if img or img_family: image_expander = image_utils.ImageExpander(compute_client, resources) image_uri, _ = image_expander.ExpandImageFlag( user_project=project, image=img, image_family=img_family, image_project=img_project, return_image_resource=False, image_family_scope=image_family_scope, support_image_family_scope=support_image_family_scope) image_key = None disk_key = None if csek_keys: image_key = csek_utils.MaybeLookupKeyMessagesByUri( csek_keys, resources, [image_uri], compute) if name: disk_ref = resources.Parse( name, collection='compute.disks', params={'zone': location}) disk_key = csek_utils.MaybeLookupKeyMessage(csek_keys, disk_ref, compute) if enable_kms: disk_key = kms_utils.MaybeGetKmsKeyFromDict(disk, messages, disk_key) initialize_params = messages.AttachedDiskInitializeParams( diskName=name, description=disk.get('description'), sourceImage=image_uri, diskSizeGb=disk_size_gb, diskType=disk_type, sourceImageEncryptionKey=image_key) replica_zones = disk.get('replica-zones') if support_replica_zones and replica_zones: normalized_zones = [] for zone in replica_zones: zone_ref = holder.resources.Parse( zone, collection='compute.zones', params={'project': project}) normalized_zones.append(zone_ref.SelfLink()) initialize_params.replicaZones = normalized_zones if enable_snapshots: snapshot_name = disk.get('source-snapshot') attached_snapshot_uri = instance_utils.ResolveSnapshotURI( snapshot=snapshot_name, user_project=project, resource_parser=resources) if attached_snapshot_uri: initialize_params.sourceImage = None initialize_params.sourceSnapshot = attached_snapshot_uri policies = disk.get('disk-resource-policy') if policies: initialize_params.resourcePolicies = policies if enable_image_csek: image_key_file = disk.get('image_csek') if image_key_file: initialize_params.imageKeyFile = image_key_file if enable_source_snapshot_csek: snapshot_key_file = disk.get('source_snapshot_csek') if snapshot_key_file: initialize_params.snapshotKeyFile = snapshot_key_file boot = disk.get('boot', False) multi_writer = disk.get('multi-writer') if support_multi_writer and multi_writer: initialize_params.multiWriter = True provisioned_iops = disk.get('provisioned-iops') if provisioned_iops: initialize_params.provisionedIops = provisioned_iops disk_architecture = disk.get('architecture') if support_disk_architecture and disk_architecture: initialize_params.architecture = messages.AttachedDiskInitializeParams.ArchitectureValueValuesEnum( disk_architecture) device_name = instance_utils.GetDiskDeviceName(disk, name, container_mount_disk) create_disk = messages.AttachedDisk( autoDelete=auto_delete, boot=boot, deviceName=device_name, initializeParams=initialize_params, mode=mode, type=messages.AttachedDisk.TypeValueValuesEnum.PERSISTENT, diskEncryptionKey=disk_key) # The boot disk must end up at index 0. if boot: disks_messages = [create_disk] + disks_messages else: disks_messages.append(create_disk) return disks_messages