Skip to content

Commit 48b26e6

Browse files
authored
Merge pull request #2 from Fryyyyy/gcp-create-disk-dest-project-zone-5322369123445590457
feat(gcp): Add optional destination project and zone to CreateDiskFromSnapshot
2 parents b0ed70b + ad500a2 commit 48b26e6

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

libcloudforensics/providers/gcp/internal/compute.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,9 @@ def CreateDiskFromSnapshot(
525525
snapshot: 'GoogleComputeSnapshot',
526526
disk_name: Optional[str] = None,
527527
disk_name_prefix: Optional[str] = None,
528-
disk_type: str = 'pd-standard') -> 'GoogleComputeDisk':
528+
disk_type: str = 'pd-standard',
529+
dest_project: Optional[str] = None,
530+
dest_zone: Optional[str] = None) -> 'GoogleComputeDisk':
529531
"""Create a new disk based on a Snapshot.
530532
531533
Args:
@@ -536,6 +538,10 @@ def CreateDiskFromSnapshot(
536538
which disk type to use to create the disk. Default is pd-standard. Use
537539
pd-ssd to have a SSD disk. You can list all available disk types by
538540
running the following command: gcloud compute disk-types list
541+
dest_project (str): Optional. The destination project where the disk
542+
should be created. Default is self.project_id.
543+
dest_zone (str): Optional. The destination zone where the disk
544+
should be created. Default is self.default_zone.
539545
540546
Returns:
541547
GoogleComputeDisk: Google Compute Disk.
@@ -547,19 +553,23 @@ def CreateDiskFromSnapshot(
547553

548554
if not disk_name:
549555
disk_name = common.GenerateDiskName(snapshot, disk_name_prefix)
556+
557+
project_id = dest_project or self.project_id
558+
zone = dest_zone or self.default_zone
559+
550560
body = {
551561
'name':
552562
disk_name,
553563
'sourceSnapshot':
554564
snapshot.GetSourceString(),
555565
'type':
556566
'projects/{0:s}/zones/{1:s}/diskTypes/{2:s}'.format(
557-
self.project_id, self.default_zone, disk_type)
567+
project_id, zone, disk_type)
558568
}
559569
try:
560570
gce_disks_client = self.GceApi().disks() # pylint: disable=no-member
561571
request = gce_disks_client.insert(
562-
project=self.project_id, zone=self.default_zone, body=body)
572+
project=project_id, zone=zone, body=body)
563573
response = request.execute()
564574
except HttpError as exception:
565575
if exception.resp.status == 409:
@@ -570,9 +580,9 @@ def CreateDiskFromSnapshot(
570580
'Unknown error occurred when creating disk from Snapshot:'
571581
' {0!s}'.format(exception),
572582
__name__) from exception
573-
self.BlockOperation(response, zone=self.default_zone)
583+
self.BlockOperation(response, zone=zone)
574584
return GoogleComputeDisk(
575-
project_id=self.project_id, zone=self.default_zone, name=disk_name)
585+
project_id=project_id, zone=zone, name=disk_name)
576586

577587
def GetMachineTypes(self, machine_type: str,
578588
zone: Optional[str] = None) -> Dict[str, Any]:

tests/providers/gcp/internal/test_compute.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ def testCreateDiskFromSnapshot(self, mock_gce_api, mock_block_operation):
173173
disk_from_snapshot, compute.GoogleComputeDisk)
174174
self.assertEqual('new-forensics-disk', disk_from_snapshot.name)
175175

176+
# CreateDiskFromSnapshot(Snapshot=gcp_mocks.FAKE_SNAPSHOT,
177+
# dest_project='other-project', dest_zone='other-zone')
178+
disk_from_snapshot = gcp_mocks.FAKE_ANALYSIS_PROJECT.compute.CreateDiskFromSnapshot(
179+
gcp_mocks.FAKE_SNAPSHOT, dest_project='other-project', dest_zone='other-zone')
180+
self.assertIsInstance(
181+
disk_from_snapshot, compute.GoogleComputeDisk)
182+
self.assertEqual('other-project', disk_from_snapshot.project_id)
183+
self.assertEqual('other-zone', disk_from_snapshot.zone)
184+
176185
# CreateDiskFromSnapshot(Snapshot=gcp_mocks.FAKE_SNAPSHOT,
177186
# disk_name='fake-disk') where 'fake-disk' exists already
178187
disks.return_value.insert.return_value.execute.side_effect = HttpError(

0 commit comments

Comments
 (0)