def update_network(username, machine_name, new_network): """Implements the VM network update :param username: The name of the user who owns the virtual machine :type username: String :param machine_name: The name of the virtual machine :type machine_name: String :param new_network: The name of the new network to connect the VM to :type new_network: String """ with vCenter(host=const.INF_VCENTER_SERVER, user=const.INF_VCENTER_USER, \ password=const.INF_VCENTER_PASSWORD) as vcenter: folder = vcenter.get_by_name(name=username, vimtype=vim.Folder) for entity in folder.childEntity: if entity.name == machine_name: info = virtual_machine.get_info(vcenter, entity, username) if info['meta']['component'] == 'Windows': the_vm = entity break else: error = 'No VM named {} found'.format(machine_name) raise ValueError(error) try: network = vcenter.networks[new_network] except KeyError: error = 'No VM named {} found'.format(machine_name) raise ValueError(error) else: virtual_machine.change_network(the_vm, network)
def test_change_network_no_adapter(self, fake_consume_task): """``virtual_machine`` - 'change_network' returns None upon success""" fake_network = MagicMock() fake_nic = MagicMock() fake_nic.deviceInfo.label = 'Network adapter 7' the_vm = MagicMock() the_vm.config.hardware.device = [fake_nic] with self.assertRaises(RuntimeError): virtual_machine.change_network(the_vm, fake_network)
def test_change_network(self, fake_consume_task, fake_VirtualDeviceSpec, fake_PortConnection, fake_DistributedVirtualPortBackingInfo, fake_ConnectInfo, fake_ConfigSpec): """``virtual_machine`` - 'change_network' returns None upon success""" fake_network = MagicMock() fake_nic = MagicMock() fake_nic.deviceInfo.label = 'Network adapter 1' the_vm = MagicMock() the_vm.config.hardware.device = [fake_nic] result = virtual_machine.change_network(the_vm, fake_network) expected = None self.assertTrue(result is None)