Beispiel #1
0
 def ParseDiskRef(self, resources, args, instance_ref):
   if args.disk_scope == 'regional':
     scope = compute_scopes.ScopeEnum.REGION
   else:
     scope = compute_scopes.ScopeEnum.ZONE
   return instance_utils.ParseDiskResource(
       resources, args.disk, instance_ref.project, instance_ref.zone, scope)
Beispiel #2
0
def CreatePersistentAttachedDiskMessages(resources,
                                         compute_client,
                                         csek_keys,
                                         disks,
                                         project,
                                         location,
                                         scope,
                                         container_mount_disk=None):
  """Returns a list of AttachedDisk messages and the boot disk's reference."""
  disks_messages = []

  messages = compute_client.messages
  compute = compute_client.apitools_client
  for disk in disks:
    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

    boot = disk.get('boot') == 'yes'
    auto_delete = disk.get('auto-delete') == 'yes'

    if 'scope' in disk and disk['scope'] == 'regional':
      scope = compute_scopes.ScopeEnum.REGION
    else:
      scope = compute_scopes.ScopeEnum.ZONE
    disk_ref = instance_utils.ParseDiskResource(resources, name, project,
                                                location, scope)

    # TODO(b/36051031) drop test after CSEK goes GA
    if csek_keys:
      disk_key_or_none = csek_utils.MaybeLookupKeyMessage(
          csek_keys, disk_ref, compute)
      kwargs = {'diskEncryptionKey': disk_key_or_none}
    else:
      kwargs = {}

    device_name = instance_utils.GetDiskDeviceName(disk, name,
                                                   container_mount_disk)

    attached_disk = messages.AttachedDisk(
        autoDelete=auto_delete,
        boot=boot,
        deviceName=device_name,
        mode=mode,
        source=disk_ref.SelfLink(),
        type=messages.AttachedDisk.TypeValueValuesEnum.PERSISTENT,
        **kwargs)

    # The boot disk must end up at index 0.
    if boot:
      disks_messages = [attached_disk] + disks_messages
    else:
      disks_messages.append(attached_disk)

  return disks_messages
Beispiel #3
0
 def ParseDiskRef(self, args, instance_ref):
     if args.disk_scope == 'regional':
         return instance_utils.ParseDiskResource(
             self.resources, args.disk, instance_ref.zone,
             compute_scopes.ScopeEnum.REGION)
     else:
         return super(AttachDiskAlpha,
                      self).ParseDiskRef(args, instance_ref)
    def _GetPossibleDisks(self, resources, name, instance_ref):
        """Gets the possible disks that match the provided disk name.

    First, we attempt to parse the provided disk name as a regional and as a
    zonal disk. Next, we iterate over the attached disks to find the ones that
    match the parsed regional and zonal disks.

    If the disk can match either a zonal or regional disk, we prompt the user to
    choose one.

    Args:
      resources: resources.Registry, The resource registry
      name: str, name of the disk.
      instance_ref: Reference of the instance instance.

    Returns:
      List of possible disks references that possibly match the provided disk
          name.
    """
        possible_disks = []
        try:
            regional_disk = instance_utils.ParseDiskResource(
                resources, name, instance_ref.project, instance_ref.zone,
                compute_scopes.ScopeEnum.REGION)
            possible_disks.append(regional_disk)
        except cloud_resources.WrongResourceCollectionException:
            pass
        try:
            zonal_disk = instance_utils.ParseDiskResource(
                resources, name, instance_ref.project, instance_ref.zone,
                compute_scopes.ScopeEnum.ZONE)
            possible_disks.append(zonal_disk)
        except cloud_resources.WrongResourceCollectionException:
            pass

        return possible_disks
Beispiel #5
0
    def ParseDiskRef(self, resources, args, instance_ref):
        """Parses disk reference.

    Could be overridden by subclasses to customize disk resource parsing as
    necessary for alpha release track.

    Args:
      resources: resources.Registry, The resource registry.
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.
      instance_ref: resources.Resource, The instance reference.

    Returns:
      Disk reference.
    """
        return instance_utils.ParseDiskResource(resources, args.disk,
                                                instance_ref.project,
                                                instance_ref.zone,
                                                compute_scopes.ScopeEnum.ZONE)
 def ParseDiskRef(self, resources, args, instance_ref):
     return instance_utils.ParseDiskResource(resources, args.disk,
                                             instance_ref.project,
                                             instance_ref.zone,
                                             compute_scopes.ScopeEnum.ZONE)
 def testParseDiskResourceRaisesException(self, name, disk_type):
     with self.assertRaises(
             cloud_resources.WrongResourceCollectionException):
         instance_utils.ParseDiskResource(self.resources, name, 'project',
                                          'us-central1-a', disk_type)
 def testParseDiskResource(self, name, disk_type, expected_rel_name):
     parsed = instance_utils.ParseDiskResource(self.resources, name,
                                               'project', 'us-central1-a',
                                               disk_type)
     self.assertEqual(parsed.RelativeName(), expected_rel_name)