Example #1
0
  def testGenerateDiskName(self):
    """Test that the generated disk name is always within GCP boundaries.

    The disk name must comply with the following RegEx:
      - ^(?=.{1,63}$)[a-z]([-a-z0-9]*[a-z0-9])?$

    i.e., it must be between 1 and 63 chars, the first character must be a
    lowercase letter, and all following characters must be a dash, lowercase
    letter, or digit, except the last character, which cannot be a dash.
    """

    disk_name = common.GenerateDiskName(gcp_mocks.FAKE_SNAPSHOT)
    self.assertEqual('fake-snapshot-857c0b16-copy', disk_name)
    self.assertTrue(gcp_mocks.REGEX_DISK_NAME.match(disk_name))

    disk_name = common.GenerateDiskName(gcp_mocks.FAKE_SNAPSHOT_LONG_NAME)
    self.assertEqual(
        'this-is-a-kind-of-long-fake-snapshot-name-and-is--857c0b16-copy',
        disk_name)
    self.assertTrue(gcp_mocks.REGEX_DISK_NAME.match(disk_name))

    disk_name = common.GenerateDiskName(
        gcp_mocks.FAKE_SNAPSHOT, disk_name_prefix='some-not-so-long-disk-name-prefix')
    self.assertEqual(
        'some-not-so-long-disk-name-prefix-fake-snapshot-857c0b16-copy',
        disk_name)
    self.assertTrue(gcp_mocks.REGEX_DISK_NAME.match(disk_name))

    disk_name = common.GenerateDiskName(
        gcp_mocks.FAKE_SNAPSHOT_LONG_NAME,
        disk_name_prefix='some-not-so-long-disk-name-prefix')
    self.assertEqual(
        'some-not-so-long-disk-name-prefix-this-is-a-kind--857c0b16-copy',
        disk_name)
    self.assertTrue(gcp_mocks.REGEX_DISK_NAME.match(disk_name))

    disk_name = common.GenerateDiskName(
        gcp_mocks.FAKE_SNAPSHOT,
        disk_name_prefix='some-really-really-really-really-really-really-long'
        '-disk-name-prefix')
    self.assertEqual(
        'some-really-really-really-really-really-really-lo-857c0b16-copy',
        disk_name)
    self.assertTrue(gcp_mocks.REGEX_DISK_NAME.match(disk_name))

    disk_name = common.GenerateDiskName(
        gcp_mocks.FAKE_SNAPSHOT_LONG_NAME,
        disk_name_prefix='some-really-really-really-really-really-really-long'
        '-disk-name-prefix')
    self.assertEqual(
        'some-really-really-really-really-really-really-lo-857c0b16-copy',
        disk_name)
    self.assertTrue(gcp_mocks.REGEX_DISK_NAME.match(disk_name))

    # Disk prefix cannot start with a capital letter
    with self.assertRaises(errors.InvalidNameError):
      common.GenerateDiskName(
          gcp_mocks.FAKE_SNAPSHOT, 'Some-prefix-that-starts-with-a-capital-letter')
    def CreateDiskFromSnapshot(
            self,
            snapshot: 'GoogleComputeSnapshot',
            disk_name: Optional[str] = None,
            disk_name_prefix: str = '',
            disk_type: str = 'pd-standard') -> 'GoogleComputeDisk':
        """Create a new disk based on a Snapshot.

    Args:
      snapshot (GoogleComputeSnapshot): Snapshot to use.
      disk_name (str): Optional. String to use as new disk name.
      disk_name_prefix (str): Optional. String to prefix the disk name with.
      disk_type (str): Optional. URL of the disk type resource describing
          which disk type to use to create the disk. Default is pd-standard. Use
          pd-ssd to have a SSD disk. You can list all available disk types by
          running the following command: gcloud compute disk-types list

    Returns:
      GoogleComputeDisk: Google Compute Disk.

    Raises:
      RuntimeError: If the disk exists already.
    """

        if not disk_name:
            disk_name = common.GenerateDiskName(snapshot, disk_name_prefix)
        body = {
            'name':
            disk_name,
            'sourceSnapshot':
            snapshot.GetSourceString(),
            'type':
            'projects/{0:s}/zones/{1:s}/diskTypes/{2:s}'.format(
                self.project_id, self.default_zone, disk_type)
        }
        try:
            gce_disks_client = self.GceApi().disks()
            request = gce_disks_client.insert(project=self.project_id,
                                              zone=self.default_zone,
                                              body=body)
            response = request.execute()
        except HttpError as exception:
            if exception.resp.status == 409:
                error_msg = 'Disk {0:s} already exists'.format(disk_name)
                raise RuntimeError(error_msg)
            error_msg = (
                'Unknown error (status: {0:d}) occurred when creating disk '
                'from Snapshot:\n{1!s}').format(exception.resp.status,
                                                exception)
            raise RuntimeError(error_msg)
        self.BlockOperation(response, zone=self.default_zone)
        return GoogleComputeDisk(
            project_id=self.project_id,
            zone=self.default_zone,  # type: ignore
            name=disk_name)