def test_wait_for_task_result_none(self):
        task.info = Mock(spec=vim.TaskInfo)
        task.info.state = vim.TaskInfo.State.running
        result = 'result'
        task.info.result = None

        waiter = SynchronousTaskWaiter()
        res = waiter.wait_for_task(task=task,
                                   logger=Mock(),
                                   action_name='job',
                                   hide_result=False)

        self.assertIsNone(res, result)
    def test_wait_for_queued_task(self):
        task.info = Mock(spec=vim.TaskInfo)
        task.info.state = vim.TaskInfo.State.queued
        result = 'result'
        task.info.result = result

        waiter = SynchronousTaskWaiter()
        res = waiter.wait_for_task(task=task,
                                   logger=Mock(),
                                   action_name='job',
                                   hide_result=False)

        self.assertEqual(res, result)
    def integration_test_connect_B(self):
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password,
                                      cred.port)
        vm_uuid = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Boris',
                                                   'Boris2-win7').config.uuid

        mapping = VmNetworkMapping()
        mapping.vlan_id = '114'
        # mapping.dv_port_name = 65
        mapping.dv_switch_name = 'dvSwitch'
        mapping.dv_switch_path = 'QualiSB'
        mapping.vlan_spec = 'Trunk'

        vlan_spec = VlanSpecFactory()
        range_fac = VLanIdRangeParser()
        synchronous_task_waiter = SynchronousTaskWaiter()
        name_gen = DvPortGroupNameGenerator()
        mapper = VnicToNetworkMapper(name_gen)
        dv_port_group_creator = DvPortGroupCreator(py_vmomi_service,
                                                   synchronous_task_waiter)
        virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer(
            py_vmomi_service, synchronous_task_waiter, mapper, VNicService())
        connector = VirtualSwitchToMachineConnector(
            dv_port_group_creator, virtual_machine_port_group_configurer)

        command = VirtualSwitchConnectCommand(py_vmomi_service, connector,
                                              name_gen, vlan_spec, range_fac,
                                              Mock())

        command.connect_to_networks(si, vm_uuid, [mapping], 'QualiSB/anetwork',
                                    [])
    def integration_test_connect_A(self):
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password,
                                      cred.port)
        synchronous_task_waiter = SynchronousTaskWaiter()
        mapper = VnicToNetworkMapper(DvPortGroupNameGenerator())
        dv_port_group_creator = DvPortGroupCreator(py_vmomi_service,
                                                   synchronous_task_waiter)
        port_group_name_generator = DvPortGroupNameGenerator()
        virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer(
            py_vmomi_service, synchronous_task_waiter, mapper, VNicService(),
            port_group_name_generator)

        mapping = VmNetworkMapping()
        mapping.vlan_id = [vim.NumericRange(start=65, end=65)]
        mapping.dv_port_name = DvPortGroupNameGenerator(
        ).generate_port_group_name(65, 'Trunk')
        mapping.dv_switch_name = 'dvSwitch'
        mapping.dv_switch_path = 'QualiSB'
        mapping.vlan_spec = vim.dvs.VmwareDistributedVirtualSwitch.TrunkVlanSpec(
        )
        connector = VirtualSwitchToMachineConnector(
            dv_port_group_creator, virtual_machine_port_group_configurer)

        vm = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Raz', '2')

        # Act
        connector.connect_by_mapping(si, vm, [mapping], None, [])

        pass
Exemple #5
0
    def integrationtest(self):
        resource_connection_details_retriever = Mock()
        credentials = TestCredentials()
        resource_connection_details_retriever.connection_details = Mock(
            return_value=VCenterConnectionDetails(
                credentials.host, credentials.username, credentials.password))
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        synchronous_task_waiter = SynchronousTaskWaiter()
        dv_port_group_creator = DvPortGroupCreator(py_vmomi_service,
                                                   synchronous_task_waiter)

        pg_name_generator = DvPortGroupNameGenerator
        vnic_to_network_mapper = VnicToNetworkMapper(pg_name_generator)
        virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer(
            py_vmomi_service, synchronous_task_waiter, vnic_to_network_mapper,
            VNicService())

        virtual_switch_to_machine_connector = VirtualSwitchToMachineConnector(
            dv_port_group_creator, virtual_machine_port_group_configurer)

        si = py_vmomi_service.connect(credentials.host, credentials.username,
                                      credentials.password, credentials.port)

        virtual_machine_path = 'Boris'
        virtual_machine_name = 'boris1'
        vm = py_vmomi_service.get_obj(si.content, [vim.VirtualMachine],
                                      virtual_machine_name)
        vm_uuid = vm.config.uuid
        #vm_uuid = self.get_vm_uuid(py_vmomi_service, si, virtual_machine_name)
        dv_switch_path = 'QualiSB'
        dv_switch_name = 'dvSwitch'
        dv_port_name = 'boris_group59'
 def __init__(self):
     # self.cs_helper = CloudshellDriverHelper()
     self.model_parser = ResourceModelParser()
     self.ip_manager = VMIPManager()
     self.task_waiter = SynchronousTaskWaiter()
     self.pv_service = pyVmomiService(SmartConnect, Disconnect,
                                      self.task_waiter)
Exemple #7
0
 def __init__(self):
     super(PaloAltoStaticShellDriver, self).__init__()
     self._cli = None
     self.cs_helper = CloudshellDriverHelper()
     self.model_parser = ResourceModelParser()
     self.ip_manager = VMIPManager()
     self.task_waiter = SynchronousTaskWaiter()
     self.pv_service = pyVmomiService(SmartConnect, Disconnect,
                                      self.task_waiter)
    def test_wait_for_task_fail(self):
        task.info = Mock(spec=vim.TaskInfo)
        task.info.error = 'error'
        task.info.state = vim.TaskInfo.State.running
        result = 'result'
        task.info.result = result

        waiter = SynchronousTaskWaiter()

        self.assertRaises(Exception, waiter.wait_for_task, task)
Exemple #9
0
    def test_power_management(self):
        # arrange
        cred = TestCredentials()
        pv_service = pyVmomiService(SmartConnect, Disconnect)
        si = pv_service.connect(cred.host, cred.username, cred.password)

        uuid = pv_service.find_vm_by_name(si, 'QualiSB/Raz', '2').config.uuid
        power_manager = VirtualMachinePowerManagementCommand(
            pv_service, SynchronousTaskWaiter())

        power_manager.power_on(si, uuid)
        power_manager.power_off(si, uuid)
    def setUp(self):
        self._si = None

        self.vm = None

        self.virtual_machine_path = 'SergiiT'
        self.virtual_machine_name = 'test_4f383119'
        self.vm_uuid = None

        self.vcenter_name = "QualiSB"
        self.dv_switch_path = 'QualiSB'
        self.network_path = 'QualiSB'

        #self.dv_switch_name = 'dvSwitch-SergiiT'
        self.dv_switch_name = 'dvSwitch'
        #self.dv_port_group_name = 'aa-dvPortGroup3B'
        self.dv_port_group_name = 'dvPortGroup'
        self.standard_network_name = "Anetwork"

        try:
            self.py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
            self.credentials = TestCredentials()
            self.resource_connection_details_retriever = Mock()
            self.resource_connection_details_retriever.connection_details = Mock(
                return_value=VCenterConnectionDetails(self.credentials.host,
                                                      self.credentials.username,
                                                      self.credentials.password))

            self.synchronous_task_waiter = SynchronousTaskWaiter()
            self.pg_name_generator = DvPortGroupNameGenerator
            self.vnic_to_network_mapper = VnicToNetworkMapper(self.pg_name_generator)

            self.configurer = VirtualMachinePortGroupConfigurer(self.py_vmomi_service,
                                                                self.synchronous_task_waiter,
                                                                self.vnic_to_network_mapper,
                                                                VNicService())
        except:
            print "Infrastructure not available - pretty OK or UnitTesting environment"
    def setUp(self):
        self._si = None

        self.vm = None

        self.virtual_machine_path = 'SergiiT'
        self.virtual_machine_name = 'test_4f383119'
        self.vm_uuid = None

        self.vcenter_name = "QualiSB"
        self.port_group_path = 'QualiSB'
        self.dv_switch_path = 'QualiSB'
        self.network_path = 'QualiSB'

        #self.dv_switch_name = 'dvSwitch-SergiiT'
        self.dv_switch_name = 'dvSwitch'
        #self.dv_port_group_name = 'aa-dvPortGroup3B'
        self.dv_port_group_name = 'dvPortGroup'
        self.standard_network_name = "Anetwork"

        try:
            self.py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
            self.credentials = TestCredentials()
            self.resource_connection_details_retriever = Mock()
            self.resource_connection_details_retriever.connection_details = Mock(
                return_value=VCenterConnectionDetails(self.credentials.host,
                                                      self.credentials.username,
                                                      self.credentials.password))

            self.synchronous_task_waiter = SynchronousTaskWaiter()
            self.pg_name_generator = DvPortGroupNameGenerator
            self.vnic_to_network_mapper = VnicToNetworkMapper(self.pg_name_generator)

            self.configurer = VirtualMachinePortGroupConfigurer(self.py_vmomi_service,
                                                                self.synchronous_task_waiter,
                                                                self.vnic_to_network_mapper,
                                                                VNicService())
        except:
            print "Infrastructure not available - pretty OK or UnitTesting environment"
    def __init__(self):
        """
        Initialize the driver session, this function is called everytime a new instance of the driver is created
        in here the driver is going to be bootstrapped

        :param context: models.QualiDriverModels.InitCommandContext
        """
        self.cs_helper = CloudshellDriverHelper()
        synchronous_task_waiter = SynchronousTaskWaiter()
        pv_service = pyVmomiService(connect=SmartConnect,
                                    disconnect=Disconnect,
                                    task_waiter=synchronous_task_waiter)
        self.resource_model_parser = ResourceModelParser()
        port_group_name_generator = DvPortGroupNameGenerator()

        vnic_to_network_mapper = VnicToNetworkMapper(
            quali_name_generator=port_group_name_generator)
        resource_remover = CloudshellResourceRemover()
        ovf_service = OvfImageDeployerService(self.resource_model_parser)

        self.vm_loader = VMLoader(pv_service)

        vm_deployer = VirtualMachineDeployer(
            pv_service=pv_service,
            name_generator=generate_unique_name,
            ovf_service=ovf_service,
            cs_helper=self.cs_helper,
            resource_model_parser=ResourceModelParser())

        dv_port_group_creator = DvPortGroupCreator(
            pyvmomi_service=pv_service,
            synchronous_task_waiter=synchronous_task_waiter)
        virtual_machine_port_group_configurer = \
            VirtualMachinePortGroupConfigurer(pyvmomi_service=pv_service,
                                              synchronous_task_waiter=synchronous_task_waiter,
                                              vnic_to_network_mapper=vnic_to_network_mapper,
                                              vnic_service=VNicService(),
                                              name_gen=port_group_name_generator)
        virtual_switch_to_machine_connector = VirtualSwitchToMachineConnector(
            dv_port_group_creator, virtual_machine_port_group_configurer)
        # Command Wrapper
        self.command_wrapper = CommandWrapper(
            pv_service=pv_service,
            cloud_shell_helper=self.cs_helper,
            resource_model_parser=self.resource_model_parser,
            context_based_logger_factory=ContextBasedLoggerFactory())
        # Deploy Command
        self.deploy_command = DeployCommand(deployer=vm_deployer)

        # Virtual Switch Revoke
        self.virtual_switch_disconnect_command = \
            VirtualSwitchToMachineDisconnectCommand(
                pyvmomi_service=pv_service,
                port_group_configurer=virtual_machine_port_group_configurer,
                resource_model_parser=self.resource_model_parser)

        # Virtual Switch Connect
        virtual_switch_connect_command = \
            VirtualSwitchConnectCommand(
                pv_service=pv_service,
                virtual_switch_to_machine_connector=virtual_switch_to_machine_connector,
                dv_port_group_name_generator=DvPortGroupNameGenerator(),
                vlan_spec_factory=VlanSpecFactory(),
                vlan_id_range_parser=VLanIdRangeParser())
        self.connection_orchestrator = ConnectionCommandOrchestrator(
            connector=virtual_switch_connect_command,
            disconnector=self.virtual_switch_disconnect_command,
            resource_model_parser=self.resource_model_parser)

        # Destroy VM Command
        self.destroy_virtual_machine_command = \
            DestroyVirtualMachineCommand(pv_service=pv_service,
                                         resource_remover=resource_remover,
                                         disconnector=self.virtual_switch_disconnect_command)
        # Power Command
        self.vm_power_management_command = \
            VirtualMachinePowerManagementCommand(pyvmomi_service=pv_service,
                                                 synchronous_task_waiter=synchronous_task_waiter)

        ip_manager = VMIPManager()

        # Refresh IP command
        self.refresh_ip_command = RefreshIpCommand(
            pyvmomi_service=pv_service,
            resource_model_parser=ResourceModelParser(),
            ip_manager=ip_manager)

        # Save Snapshot
        self.snapshot_saver = SaveSnapshotCommand(
            pyvmomi_service=pv_service, task_waiter=synchronous_task_waiter)

        # Snapshot Restorer
        self.snapshot_restorer = SnapshotRestoreCommand(
            pyvmomi_service=pv_service, task_waiter=synchronous_task_waiter)

        self.snapshots_retriever = RetrieveSnapshotsCommand(
            pyvmomi_service=pv_service)
class TestVirtualSwitchToMachineConnector(TestCase):

    @property
    def si(self):
        if not self._si:
            credentials = self.credentials
            self._si = self.py_vmomi_service.connect(credentials.host,
                                                     credentials.username,
                                                     credentials.password,
                                                     credentials.port)
        return self._si

    def setUp(self):
        self._si = None

        self.vm = None

        self.virtual_machine_path = 'SergiiT'
        self.virtual_machine_name = 'test_4f383119'
        self.vm_uuid = None

        self.vcenter_name = "QualiSB"
        self.dv_switch_path = 'QualiSB'
        self.network_path = 'QualiSB'

        #self.dv_switch_name = 'dvSwitch-SergiiT'
        self.dv_switch_name = 'dvSwitch'
        #self.dv_port_group_name = 'aa-dvPortGroup3B'
        self.dv_port_group_name = 'dvPortGroup'
        self.standard_network_name = "Anetwork"

        try:
            self.py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
            self.credentials = TestCredentials()
            self.resource_connection_details_retriever = Mock()
            self.resource_connection_details_retriever.connection_details = Mock(
                return_value=VCenterConnectionDetails(self.credentials.host,
                                                      self.credentials.username,
                                                      self.credentials.password))

            self.synchronous_task_waiter = SynchronousTaskWaiter()
            self.pg_name_generator = DvPortGroupNameGenerator
            self.vnic_to_network_mapper = VnicToNetworkMapper(self.pg_name_generator)

            self.configurer = VirtualMachinePortGroupConfigurer(self.py_vmomi_service,
                                                                self.synchronous_task_waiter,
                                                                self.vnic_to_network_mapper,
                                                                VNicService())
        except:
            print "Infrastructure not available - pretty OK or UnitTesting environment"

    def get_vm(self, py_vmomi_service, virtual_machine_name, si=None):
        if not self.vm:
            if not si:
                si = self.si
            self.vm = py_vmomi_service.get_obj(si.content, [vim.VirtualMachine], virtual_machine_name)
        return self.vm

    def get_vm_uuid(self, py_vmomi_service, virtual_machine_name, si=None):
        if not self.vm_uuid:
            self.vm_uuid = self.get_vm(py_vmomi_service, virtual_machine_name, si).config.uuid
        return self.vm_uuid

    def integrationtest_disconnect_all(self):
        default_network = None
        connector = VirtualSwitchToMachineDisconnectCommand(self.py_vmomi_service,
                                                            self.resource_connection_details_retriever,
                                                            self.configurer,
                                                            default_network)

        self.vm_uuid = self.vm_uuid or self.get_vm_uuid(self.py_vmomi_service, self.virtual_machine_name)
        print "DISCONNECT. Machine: '{}' UUID: [{}]".format(self.virtual_machine_name, self.vm_uuid)

        result = connector.disconnect_all(self.vcenter_name, self.vm_uuid)

        print result

    def test_integrationtest_disconnect(self):
        default_network = None
        connector = VirtualSwitchToMachineDisconnectCommand(self.py_vmomi_service,
                                                            self.configurer,
                                                            default_network)

        self.vm_uuid = self.vm_uuid or self.get_vm_uuid(self.py_vmomi_service, self.virtual_machine_name)
        print "DISCONNECT. Machine: '{}' UUID: [{}]".format(self.virtual_machine_name, self.vm_uuid)

        #result = connector.disconnect(self.vcenter_name, self.vm_uuid, self.dv_port_group_name)
        result = connector.disconnect(self.vcenter_name, self.vm_uuid, self.standard_network_name)

        print result


    def integrationtest_all_disconnect(self):
        default_network = None
        connector = VirtualSwitchToMachineDisconnectCommand(self.py_vmomi_service,
                                                            self.resource_connection_details_retriever,
                                                            self.configurer,
                                                            default_network)

        self.vm_uuid = self.vm_uuid or self.get_vm_uuid(self.py_vmomi_service, self.virtual_machine_name)
        print "DISCONNECT ALL. Machine: '{}' UUID: [{}]".format(self.virtual_machine_name, self.vm_uuid)
        result = connector.disconnect_all(self.vcenter_name, self.vm_uuid)
        print result


    def integrationtest_remove_interface(self):
        default_network = None
        connector = VirtualSwitchToMachineDisconnectCommand(self.py_vmomi_service,
                                                            self.resource_connection_details_retriever,
                                                            self.configurer,
                                                            default_network)

        vm = self.get_vm(self.py_vmomi_service, self.virtual_machine_name)
        print "Remove vNIC. Machine: '{}' UUID: [{}]".format(self.virtual_machine_name, self.vm_uuid)
        task = connector.remove_interfaces_from_vm_task(vm)
        self.synchronous_task_waiter.wait_for_task(task, Mock())


    def integrationtest_attach_vnic(self, network):
        nicspes = VNicService.vnic_new_attached_to_network(network)
        path = "{}/{}".format(self.vcenter_name, self.virtual_machine_path)
        vm = self.py_vmomi_service.find_vm_by_name(self.si, path, self.virtual_machine_name)
        if not vm:
            print "No VM named '{}/{}'".format(path, self.virtual_machine_name)
            return
        print "VM found. \n{}".format(vm)

        task = VNicService.vnic_add_to_vm_task(nicspes, vm)
        self.synchronous_task_waiter.wait_for_task(task, Mock())

    def integrationtest_attach_vnic_standard(self):
        network = self.py_vmomi_service.find_network_by_name(self.si, self.network_path, self.standard_network_name)
        return self.integrationtest_attach_vnic(network)

    def integrationtest_attach_vnic_portgroup(self):
        network = self.py_vmomi_service.find_network_by_name(self.si,
                                                             self.port_group_path,
                                                             self.dv_port_group_name)
        if not network:
            print "No Network '{}'".format(network)
            return
        return self.integrationtest_attach_vnic(network)

    def port_group_destroy(self):
        port_group = self.py_vmomi_service.find_network_by_name(self.si, self.port_group_path, self.dv_port_group_name)

        task = DvPortGroupCreator.dv_port_group_destroy_task(port_group)
        print task
        # print_attributes(task)
        try:
            self.synchronous_task_waiter.wait_for_task(task, Mock())
        except vim.fault.ResourceInUse, e:
            print "IT USED NOW"
        pass
Exemple #14
0
 def __init__(self):
     self.model_parser = ResourceModelParser()
     self.ip_manager = VMIPManager()
     self.task_waiter = SynchronousTaskWaiter()
     self.logger = get_qs_logger('VM AutoLoad')
     self.pv_service = pyVmomiService(SmartConnect, Disconnect, self.task_waiter)
 def __init__(self):
     self.parser = ResourceModelParser()
     self.pv_service = pyVmomiService(SmartConnect, Disconnect,
                                      SynchronousTaskWaiter())
     self.context_based_logger_factory = ContextBasedLoggerFactory()