def test_vm_create_spec(self): datastore = "ds1" vm_id = str(uuid.uuid4()) metadata = { "configuration": { "guestOS": "otherLinuxGuest" }, "parameters": [{ "name": "key1" }, { "name": "key2" }] } env = { "key1": "value1", "keyUnexpected": "valueNotSet", } cspec = EsxVmConfigSpec(MagicMock()) cspec.init_for_create(vm_id, datastore, 512, 1, metadata, env) spec = cspec.get_spec() assert_that(spec.memoryMB, equal_to(512)) assert_that(spec.numCPUs, equal_to(1)) assert_that(spec.name, equal_to(vm_id)) assert_that(spec.guestId, equal_to("otherLinuxGuest")) expected_metadata = {'guestOS': 'otherLinuxGuest', 'key1': 'value1'} assert_that(cspec._metadata, equal_to(expected_metadata))
def detach_iso(self, vm_id): cfg_spec = EsxVmConfigSpec(self.query_config()) cfg_spec.init_for_update() vm = self.get_vm(vm_id) iso_path = cfg_spec.detach_iso(vm.config) self._reconfigure_vm(vm, cfg_spec.get_spec()) return iso_path
def _create_import_vm_spec(self, vm_id, datastore, vm_path): spec = EsxVmConfigSpec(vm_id, "otherGuest", 32, 1, vm_path, None) # Just specify a tiny capacity in the spec for now; the eventual vm # disk will be based on what is uploaded via the http nfc url. spec = self._vm_manager.create_empty_disk(spec, datastore, None, size_mb=1) import_spec = vim.vm.VmImportSpec(configSpec=spec) return import_spec
def test_vm_create_spec(self): datastore = "ds1" vm_id = str(uuid.uuid4()) metadata = { "configuration": {"guestOS": "otherLinuxGuest"}, "parameters": [{"name": "key1"}, {"name": "key2"}] } env = { "key1": "value1", "keyUnexpected": "valueNotSet", } cspec = EsxVmConfigSpec(MagicMock()) cspec.init_for_create(vm_id, datastore, 512, 1, metadata, env) spec = cspec.get_spec() assert_that(spec.memoryMB, equal_to(512)) assert_that(spec.numCPUs, equal_to(1)) assert_that(spec.name, equal_to(vm_id)) assert_that(spec.guestId, equal_to("otherLinuxGuest")) expected_metadata = {'guestOS': 'otherLinuxGuest', 'key1': 'value1'} assert_that(cspec._metadata, equal_to(expected_metadata))
def get_remove_spec(self, vm_info, disk_path): remove_spec = EsxVmConfigSpec(None) remove_spec.init_for_update() devices = remove_spec._get_devices_by_type(vm_info.config, vim.vm.device.VirtualDisk) found_device = None for device in devices: if device.backing.fileName.endswith(disk_path): found_device = device remove_spec._remove_device(found_device) return remove_spec
def get_update_spec(self, vm_info, disk_path): update_spec = EsxVmConfigSpec(None) update_spec.init_for_update() backing = vim.vm.device.VirtualDisk.FlatVer2BackingInfo( fileName=disk_path, diskMode=vim.vm.device.VirtualDiskOption.DiskMode.persistent) controller = update_spec._find_scsi_controller(vm_info.config) disk = vim.vm.device.VirtualDisk( controllerKey=controller.key, key=-1, unitNumber=-1, backing=backing, capacityInKB=1024, ) update_spec._create_device(disk) return update_spec
def get_create_spec(self, datastore, vm_id, disk_path): create_spec = EsxVmConfigSpec(None) create_spec.init_for_create(vm_id, datastore, 64, 2) create_spec._cfg_spec.files = vim.vm.FileInfo(vmPathName="[%s] /" % datastore) controller = vim.vm.device.VirtualLsiLogicController( key=1, sharedBus=vim.vm.device.VirtualSCSIController.Sharing.noSharing, busNumber=2, unitNumber=-1) create_spec._add_device(controller) backing = vim.vm.device.VirtualDisk.FlatVer2BackingInfo( fileName=disk_path, diskMode=vim.vm.device.VirtualDiskOption.DiskMode.persistent) disk = vim.vm.device.VirtualDisk( controllerKey=1, key=-1, unitNumber=-1, backing=backing, capacityInKB=1024, ) create_spec._create_device(disk) return create_spec
def get_update_spec(self, vm_info, disk_path): update_spec = EsxVmConfigSpec(None) update_spec.init_for_update() backing = vim.vm.device.VirtualDisk.FlatVer2BackingInfo( fileName=disk_path, diskMode=vim.vm.device.VirtualDiskOption.DiskMode.persistent ) controller = update_spec._find_scsi_controller(vm_info.config) disk = vim.vm.device.VirtualDisk( controllerKey=controller.key, key=-1, unitNumber=-1, backing=backing, capacityInKB=1024, ) update_spec._create_device(disk) return update_spec
def get_create_spec(self, datastore, vm_id, disk_path): create_spec = EsxVmConfigSpec(None) create_spec.init_for_create(vm_id, datastore, 64, 2) create_spec._cfg_spec.files = vim.vm.FileInfo(vmPathName="[%s] /" % datastore) controller = vim.vm.device.VirtualLsiLogicController( key=1, sharedBus=vim.vm.device.VirtualSCSIController.Sharing.noSharing, busNumber=2, unitNumber=-1) create_spec._add_device(controller) backing = vim.vm.device.VirtualDisk.FlatVer2BackingInfo( fileName=disk_path, diskMode=vim.vm.device.VirtualDiskOption.DiskMode.persistent ) disk = vim.vm.device.VirtualDisk( controllerKey=1, key=-1, unitNumber=-1, backing=backing, capacityInKB=1024, ) create_spec._create_device(disk) return create_spec
def detach_disk(self, vm_id, disk_id): cfg_spec = EsxVmConfigSpec(self.query_config()) cfg_spec.init_for_update() vm = self.get_vm(vm_id) cfg_spec.detach_disk(vm.config, disk_id) self._reconfigure_vm(vm, cfg_spec.get_spec())
def _update_spec(self): spec = EsxVmConfigSpec(MagicMock()) spec.init_for_update() return spec
def attach_virtual_network(self, vm_id, network_id): cfg_spec = EsxVmConfigSpec(self.query_config()) cfg_spec.init_for_update() vm = self.get_vm(vm_id) cfg_spec.add_virtual_nic(vm.config, network_id) self._reconfigure_vm(vm, cfg_spec.get_spec())
def attach_iso(self, vm_id, iso_file): cfg_spec = EsxVmConfigSpec(self.query_config()) cfg_spec.init_for_update() vm = self.get_vm(vm_id) cfg_spec.attach_iso(vm.config, iso_file) self._reconfigure_vm(vm, cfg_spec.get_spec())
def create_vm_spec(self, vm_id, datastore, memoryMB, cpus, metadata, env): spec = EsxVmConfigSpec(self.query_config()) spec.init_for_create(vm_id, datastore, memoryMB, cpus, metadata, env) return spec