Skip to content

Commit 81321b5

Browse files
committed
Add machine_type param to StartAnalysisVm and pass it through to GetOrCreateAnalysisVm
1 parent 987a049 commit 81321b5

3 files changed

Lines changed: 49 additions & 20 deletions

File tree

libcloudforensics/providers/gcp/forensics.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ def StartAnalysisVm(
137137
boot_disk_size: int = 10,
138138
boot_disk_type: str = 'pd-standard',
139139
cpu_cores: int = 4,
140+
machine_type: Optional[str] = None,
140141
attach_disks: Optional[List[str]] = None,
141142
image_project: str = 'ubuntu-os-cloud',
142143
image_family: str = 'ubuntu-2204-lts',
143-
packages: Optional[List[str]] = None
144+
packages: Optional[List[str]] = None,
144145
) -> Tuple['compute.GoogleComputeInstance', bool]:
145146
"""Start a virtual machine for analysis purposes.
146147
@@ -149,18 +150,18 @@ def StartAnalysisVm(
149150
vm_name: The name of the virtual machine.
150151
zone: Zone for the virtual machine.
151152
boot_disk_size: The size of the analysis VM boot disk (in GB).
152-
boot_disk_type: URL of the disk type resource describing
153-
which disk type to use to create the disk. Use pd-standard for a
154-
standard disk and pd-ssd for a SSD disk.
153+
boot_disk_type: URL of the disk type resource describing which disk type to
154+
use to create the disk. Use pd-standard for a standard disk and pd-ssd for
155+
a SSD disk.
155156
cpu_cores: The number of CPU cores to create the machine with.
156-
attach_disks: List of disk names to attach. Default
157-
behaviour is to search in zonal disks then regional disks, when using
158-
regional disks CreateInstanceFromArguments from GoogleCloudCompute is
159-
recommended to avoid name colisions with zonal disks.
160-
image_project: Name of the project where the analysis VM
161-
image is hosted.
162-
image_family: Name of the image to use to create the
163-
analysis VM.
157+
machine_type: Machine type for the virtual machine. If specified, cpu_cores
158+
will be ignored.
159+
attach_disks: List of disk names to attach. Default behaviour is to search
160+
in zonal disks then regional disks, when using regional disks
161+
CreateInstanceFromArguments from GoogleCloudCompute is recommended to
162+
avoid name colisions with zonal disks.
163+
image_project: Name of the project where the analysis VM image is hosted.
164+
image_family: Name of the image to use to create the analysis VM.
164165
packages: List of extra packages to install in the VM.
165166
166167
Returns:
@@ -178,10 +179,17 @@ def StartAnalysisVm(
178179
disk = proj.compute.GetRegionDisk(disk_name)
179180
data_disks.append(disk)
180181
analysis_vm, created = proj.compute.GetOrCreateAnalysisVm(
181-
vm_name, boot_disk_size, disk_type=boot_disk_type, cpu_cores=cpu_cores,
182-
image_project=image_project, image_family=image_family,
182+
vm_name,
183+
boot_disk_size,
184+
disk_type=boot_disk_type,
185+
cpu_cores=cpu_cores,
186+
machine_type=machine_type,
187+
image_project=image_project,
188+
image_family=image_family,
183189
packages=packages,
184-
data_disks=data_disks, zone=zone)
190+
data_disks=data_disks,
191+
zone=zone,
192+
)
185193
logger.info('VM started.')
186194
return analysis_vm, created
187195

libcloudforensics/providers/gcp/internal/compute.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,7 @@ def GetOrCreateAnalysisVm(
961961
boot_disk_size: int = 10,
962962
disk_type: str = 'pd-standard',
963963
cpu_cores: int = 4,
964+
machine_type: Optional[str] = None,
964965
image_project: str = 'ubuntu-os-cloud',
965966
image_family: str = 'ubuntu-2204-lts',
966967
packages: Optional[List[str]] = None,
@@ -984,6 +985,8 @@ def GetOrCreateAnalysisVm(
984985
which disk type to use to create the disk. Default is pd-standard. Use
985986
pd-ssd to have a SSD disk.
986987
cpu_cores: Number of CPU cores for the virtual machine.
988+
machine_type: Machine type for the virtual machine. If specified,
989+
cpu_cores will be ignored.
987990
image_project: Name of the project where the analysis VM
988991
image is hosted.
989992
image_family: Name of the image to use to create the
@@ -1022,11 +1025,13 @@ def GetOrCreateAnalysisVm(
10221025
pass
10231026
compute_zone = zone if zone else self.default_zone
10241027

1025-
if cpu_cores not in E2_STANDARD_CPU_CORES:
1026-
raise ValueError(
1027-
'Number of requested CPU cores ({0:d}) not available for machine type'
1028-
' {1:s}'.format(cpu_cores, DEFAULT_MACHINE_TYPE))
1029-
machine_type = '{0:s}-{1:d}'.format(DEFAULT_MACHINE_TYPE, cpu_cores)
1028+
if machine_type is None:
1029+
if cpu_cores not in E2_STANDARD_CPU_CORES:
1030+
raise ValueError(
1031+
'Number of requested CPU cores ({0:d}) '
1032+
'not available for machine type'
1033+
' {1:s}'.format(cpu_cores, DEFAULT_MACHINE_TYPE))
1034+
machine_type = '{0:s}-{1:d}'.format(DEFAULT_MACHINE_TYPE, cpu_cores)
10301035
startup_script = utils.ReadStartupScript(utils.FORENSICS_STARTUP_SCRIPT_GCP)
10311036

10321037
if packages:

tests/providers/gcp/internal/test_compute.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,22 @@ def testGetOrCreateAnalysisVmExist(self, mock_create_from_args, mock_get_instanc
429429
self.assertFalse(created)
430430
mock_create_from_args.assert_not_called()
431431

432+
@typing.no_type_check
433+
@mock.patch('libcloudforensics.providers.gcp.internal.compute.GoogleCloudCompute.GetInstance')
434+
@mock.patch('libcloudforensics.providers.gcp.internal.compute.GoogleCloudCompute.CreateInstanceFromArguments')
435+
def testGetOrCreateAnalysisVmWithMachineType(self, mock_create_from_args, mock_get_instance):
436+
"""Test creating analysis VM with specific machine type."""
437+
mock_get_instance.side_effect = errors.ResourceNotFoundError('', __name__)
438+
mock_create_from_args.return_value = gcp_mocks.FAKE_ANALYSIS_VM
439+
440+
vm, created = gcp_mocks.FAKE_ANALYSIS_PROJECT.compute.GetOrCreateAnalysisVm(
441+
gcp_mocks.FAKE_ANALYSIS_VM.name, machine_type='custom-machine-type')
442+
443+
self.assertTrue(created)
444+
mock_create_from_args.assert_called_once()
445+
args, _ = mock_create_from_args.call_args
446+
self.assertEqual('custom-machine-type', args[1])
447+
432448
@typing.no_type_check
433449
@mock.patch('libcloudforensics.providers.gcp.internal.compute.GoogleCloudCompute.ListInstanceByLabels')
434450
@mock.patch('libcloudforensics.providers.gcp.internal.common.GoogleCloudComputeClient.GceApi')

0 commit comments

Comments
 (0)