def test_download_stream_optimized_data_1(self):
        context = None

        vm_ref = vim_util.get_moref("vm-232", "VirtualMachine")
        read_handle = rw_handles.VmdkReadHandle(self.session,
                                                self.session._host,
                                                self.session._port,
                                                vm_ref,
                                                None,
                                                3356672*1024)
        res_pool_ref = vim_util.get_moref("resgroup-27", "ResourcePool")
        vm_folder_ref = vim_util.get_moref("group-v22", "Folder")
        vm_name = "%s_%s" % ("OSTACK_IMG", "10003")
        client_factory = self.vim.client.factory
        vm_import_spec = client_factory.create('ns0:VirtualMachineImportSpec')
        vm_import_spec.configSpec = create_spec(client_factory, vm_name, 0, "thin", "datastore1") 
        #print vm_import_spec
        imported_vm_ref = image_transfer.download_stream_optimized_data(
                context,
                600,
                read_handle,
                session = self.session,
                host = self.session._host,
                port = self.session._port,
                image_size = 3356672*1024,
                resource_pool = res_pool_ref,
                vm_folder = vm_folder_ref,
                vm_import_spec = vm_import_spec
                )
        print imported_vm_ref
        #unregister the vm
        self.session.invoke_api(self.vim, "UnregisterVM", imported_vm_ref)
 def test_download_stream_optimized_data(self):
     return
     context = None
     store = test_connection_to_file.store()
     fp = store._query()
     res_pool_ref = vim_util.get_moref("resgroup-27", "ResourcePool")
     vm_folder_ref = vim_util.get_moref("group-v22", "Folder")
     vm_name = "%s_%s" % ("OSTACK_IMG", "10013")
     client_factory = self.vim.client.factory
     vm_import_spec = client_factory.create('ns0:VirtualMachineImportSpec')
     vm_import_spec.configSpec = create_spec(client_factory, vm_name, 0, "thin", "datastore1") 
     #print vm_import_spec
     imported_vm_ref = image_transfer.download_stream_optimized_data(
             context,
             500,
             fp,
             session = self.session,
             host = self.session._host,
             port = self.session._port,
             image_size = 1000000,
             resource_pool = res_pool_ref,
             vm_folder = vm_folder_ref,
             vm_import_spec = vm_import_spec
             )
     print imported_vm_ref
     #unregister the vm
     self.session.invoke_api(self.vim, "UnregisterVM", imported_vm_ref)
    def query_vm_property(self, vm_moid, property_name):
        """Method returns the value of specified property for a VM.

        :param vm_moid: moid of the VM whose property is to be queried
        :param property_name: path of the property
        """
        vm_mobj = vim_util.get_moref(vm_moid, "VirtualMachine")
        session = self._api_session
        return session.invoke_api(vim_util, "get_object_property",
                                  session.vim, vm_mobj, property_name)
    def query_vm_property(self, vm_moid, property_name):
        """Method returns the value of specified property for a VM.

        :param vm_moid: moid of the VM whose property is to be queried
        :param property_name: path of the property
        """
        vm_mobj = vim_util.get_moref(vm_moid, "VirtualMachine")
        session = self._api_session
        return session.invoke_api(vim_util, "get_object_property", session.vim,
                                  vm_mobj, property_name)
Beispiel #5
0
    def _query_vm_perf_stats(self, vm_moid, counter_id, device_name, duration):
        """Method queries the real-time stat values for a VM.

        :param vm_moid: moid of the VM for which stats are needed
        :param counter_id: id of the perf counter in VC
        :param device_name: name of the device for which stats are to be
            queried. For aggregate counters pass empty string ("").
            For device counters pass "*", if stats are required over all
            devices.
        :param duration: in seconds from current time,
            over which the stat value was applicable
        :return: a map containing the stat values keyed by the device ID/name
        """

        session = self._api_session
        client_factory = session.vim.client.factory

        # Construct the QuerySpec
        metric_id = client_factory.create('ns0:PerfMetricId')
        metric_id.counterId = counter_id
        metric_id.instance = device_name

        query_spec = client_factory.create('ns0:PerfQuerySpec')
        query_spec.entity = vim_util.get_moref(vm_moid, "VirtualMachine")
        query_spec.metricId = [metric_id]
        query_spec.intervalId = VC_REAL_TIME_SAMPLING_INTERVAL
        # We query all samples which are applicable over the specified duration
        samples_cnt = (int(duration /
                           VC_REAL_TIME_SAMPLING_INTERVAL) if duration
                       and duration >= VC_REAL_TIME_SAMPLING_INTERVAL else 1)
        query_spec.maxSample = samples_cnt

        perf_manager = session.vim.service_content.perfManager
        perf_stats = session.invoke_api(session.vim,
                                        'QueryPerf',
                                        perf_manager,
                                        querySpec=[query_spec])

        stat_values = {}
        if perf_stats:
            entity_metric = perf_stats[0]
            sample_infos = entity_metric.sampleInfo

            if len(sample_infos) > 0:
                for metric_series in entity_metric.value:
                    # Take the average of all samples to improve the accuracy
                    # of the stat value
                    stat_value = float(sum(metric_series.value)) / samples_cnt
                    device_id = metric_series.id.instance
                    stat_values[device_id] = stat_value

        return stat_values
Beispiel #6
0
 def test_get_summary(self):
     ds_ref = vim_util.get_moref('ds-0', 'Datastore')
     ds = datastore.Datastore(ds_ref, 'ds-name')
     summary = mock.sentinel.summary
     session = mock.Mock()
     session.invoke_api = mock.Mock()
     session.invoke_api.return_value = summary
     ret = ds.get_summary(session)
     self.assertEqual(summary, ret)
     session.invoke_api.assert_called_once_with(new_vim_util,
                                                'get_object_property',
                                                session.vim, ds.ref,
                                                'summary')
 def test_get_summary(self):
     ds_ref = vim_util.get_moref('ds-0', 'Datastore')
     ds = datastore.Datastore(ds_ref, 'ds-name')
     summary = mock.sentinel.summary
     session = mock.Mock()
     session.invoke_api = mock.Mock()
     session.invoke_api.return_value = summary
     ret = ds.get_summary(session)
     self.assertEqual(summary, ret)
     session.invoke_api.assert_called_once_with(new_vim_util,
                                                'get_object_property',
                                                session.vim,
                                                ds.ref, 'summary')
 def test_copy_stream_optimized_disk(self):
     return
     vm_ref = vim_util.get_moref("vm-232", "VirtualMachine")
     fo = open("res.txt", "w")
     context = None
     image_transfer.copy_stream_optimized_disk(context, 2000, fo,
             session = self.session,
             host = self.session._host,
             port = self.session._port,
             vm = vm_ref,
             vmdk_size = 3335168,#TODO 
             vmdk_file_path = None
             )
    def _query_vm_perf_stats(self, vm_moid, counter_id, device_name, duration):
        """Method queries the real-time stat values for a VM.

        :param vm_moid: moid of the VM for which stats are needed
        :param counter_id: id of the perf counter in VC
        :param device_name: name of the device for which stats are to be
            queried. For aggregate counters pass empty string ("").
            For device counters pass "*", if stats are required over all
            devices.
        :param duration: in seconds from current time,
            over which the stat value was applicable
        :return: a map containing the stat values keyed by the device ID/name
        """

        session = self._api_session
        client_factory = session.vim.client.factory

        # Construct the QuerySpec
        metric_id = client_factory.create('ns0:PerfMetricId')
        metric_id.counterId = counter_id
        metric_id.instance = device_name

        query_spec = client_factory.create('ns0:PerfQuerySpec')
        query_spec.entity = vim_util.get_moref(vm_moid, "VirtualMachine")
        query_spec.metricId = [metric_id]
        query_spec.intervalId = VC_REAL_TIME_SAMPLING_INTERVAL
        # We query all samples which are applicable over the specified duration
        samples_cnt = (int(duration / VC_REAL_TIME_SAMPLING_INTERVAL)
                       if duration and
                       duration >= VC_REAL_TIME_SAMPLING_INTERVAL else 1)
        query_spec.maxSample = samples_cnt

        perf_manager = session.vim.service_content.perfManager
        perf_stats = session.invoke_api(session.vim, 'QueryPerf', perf_manager,
                                        querySpec=[query_spec])

        stat_values = {}
        if perf_stats:
            entity_metric = perf_stats[0]
            sample_infos = entity_metric.sampleInfo

            if len(sample_infos) > 0:
                for metric_series in entity_metric.value:
                    # Take the average of all samples to improve the accuracy
                    # of the stat value
                    stat_value = float(sum(metric_series.value)) / samples_cnt
                    device_id = metric_series.id.instance
                    stat_values[device_id] = stat_value

        return stat_values
    def _query_vm_perf_stats(self, vm_moid, counter_id, device_name):
        """Method queries the real-time stat values for a VM.

        :param vm_moid: moid of the VM for which stats are needed
        :param counter_id: id of the perf counter in VC
        :param device_name: name of the device for which stats are to be
            queried. For aggregate counters pass empty string ("").
            For device counters pass "*", if stats are required over all
            devices.
        :return: a map containing the stat values keyed by the device ID/name
        """

        session = self._api_session
        client_factory = session.vim.client.factory

        # Construct the QuerySpec
        metric_id = client_factory.create('ns0:PerfMetricId')
        metric_id.counterId = counter_id
        metric_id.instance = device_name

        query_spec = client_factory.create('ns0:PerfQuerySpec')
        query_spec.entity = vim_util.get_moref(vm_moid, "VirtualMachine")
        query_spec.metricId = [metric_id]
        query_spec.intervalId = VC_REAL_TIME_SAMPLING_INTERVAL
        # The following setting ensures that we need only one latest sample
        query_spec.maxSample = 1

        perf_manager = session.vim.service_content.perfManager
        perf_stats = session.invoke_api(session.vim, 'QueryPerf', perf_manager,
                                        querySpec=[query_spec])

        stat_values = {}
        if perf_stats:
            entity_metric = perf_stats[0]
            sample_infos = entity_metric.sampleInfo
            samples_count = len(sample_infos)

            if samples_count > 0:
                for metric_series in entity_metric.value:
                    stat_value = float(metric_series.value[samples_count - 1])
                    device_id = metric_series.id.instance
                    stat_values[device_id] = stat_value

        return stat_values
    def test_get_connected_hosts(self):
        session = mock.Mock()
        ds_ref = vim_util.get_moref('ds-0', 'Datastore')
        ds = datastore.Datastore(ds_ref, 'ds-name')
        ds.get_summary = mock.Mock()
        ds.get_summary.return_value.accessible = False
        self.assertEqual([], ds.get_connected_hosts(session))
        ds.get_summary.return_value.accessible = True
        m1 = HostMount("m1", MountInfo('readWrite', True, True))
        m2 = HostMount("m2", MountInfo('read', True, True))
        m3 = HostMount("m3", MountInfo('readWrite', False, True))
        m4 = HostMount("m4", MountInfo('readWrite', True, False))
        ds.get_summary.assert_called_once_with(session)

        class Prop(object):
            DatastoreHostMount = [m1, m2, m3, m4]
        session.invoke_api = mock.Mock()
        session.invoke_api.return_value = Prop()
        hosts = ds.get_connected_hosts(session)
        self.assertEqual(1, len(hosts))
        self.assertEqual("m1", hosts.pop())
Beispiel #12
0
    def test_get_connected_hosts(self):
        session = mock.Mock()
        ds_ref = vim_util.get_moref('ds-0', 'Datastore')
        ds = datastore.Datastore(ds_ref, 'ds-name')
        ds.get_summary = mock.Mock()
        ds.get_summary.return_value.accessible = False
        self.assertEqual([], ds.get_connected_hosts(session))
        ds.get_summary.return_value.accessible = True
        m1 = HostMount("m1", MountInfo('readWrite', True, True))
        m2 = HostMount("m2", MountInfo('read', True, True))
        m3 = HostMount("m3", MountInfo('readWrite', False, True))
        m4 = HostMount("m4", MountInfo('readWrite', True, False))
        ds.get_summary.assert_called_once_with(session)

        class Prop(object):
            DatastoreHostMount = [m1, m2, m3, m4]

        session.invoke_api = mock.Mock()
        session.invoke_api.return_value = Prop()
        hosts = ds.get_connected_hosts(session)
        self.assertEqual(1, len(hosts))
        self.assertEqual("m1", hosts.pop())
Beispiel #13
0
 def test_get_object_properties(self):
     """Get host properties with properties specified """
     test_spec = self.spec.get("test_get_object_properties")
     host_moref = vim_util.get_moref(test_spec.get("host_id"), 'HostSystem')
     objects = self.session.invoke_api(  vim_util, 
                                         'get_object_properties', 
                                         self.vim, 
                                         host_moref, 
                                         ["summary.hardware.numCpuCores", "summary.hardware.numCpuThreads"])   
     self.assertIsNotNone(objects)
     expected_numCpuCores = test_spec.get("numCpuCores")
     expected_numCpuThreads = test_spec.get("numCpuThreads")
     numCpuCores = 0
     numCpuThreads = 0
     if hasattr(objects[0], 'propSet'):
         dynamic_properties = objects[0].propSet
         for prop in dynamic_properties:
             if prop.name == "summary.hardware.numCpuCores":
                 numCpuCores = prop.val
             else:
                 numCpuThreads = prop.val
     self.assertEqual(expected_numCpuCores, numCpuCores)
     self.assertEqual(expected_numCpuThreads, numCpuThreads)
Beispiel #14
0
 def retrieve_service_content(self):
     ref = vim_util.get_moref(service.SERVICE_INSTANCE, SERVICE_TYPE)
     return self.PbmRetrieveServiceContent(ref)
Beispiel #15
0
        def request_handler(managed_object, **kwargs):
            """Handler for vSphere API calls.

            Invokes the API and parses the response for fault checking and
            other errors.

            :param managed_object: managed object reference argument of the
                                   API call
            :param kwargs: keyword arguments of the API call
            :returns: response of the API call
            :raises: VimException, VimFaultException, VimAttributeException,
                     VimSessionOverLoadException, VimConnectionException
            """
            try:
                if isinstance(managed_object, str):
                    # For strings, use string value for value and type
                    # of the managed object.
                    managed_object = vim_util.get_moref(managed_object,
                                                        managed_object)
                if managed_object is None:
                    return
                request = getattr(self.client.service, attr_name)
                response = request(managed_object, **kwargs)
                if (attr_name.lower() == 'retrievepropertiesex'):
                    Service._retrieve_properties_ex_fault_checker(response)
                return response
            except exceptions.VimFaultException:
                # Catch the VimFaultException that is raised by the fault
                # check of the SOAP response.
                raise

            except suds.WebFault as excep:
                fault_string = None
                if excep.fault:
                    fault_string = excep.fault.faultstring

                doc = excep.document
                detail = None
                if doc is not None:
                    detail = doc.childAtPath('/detail')
                    if not detail:
                        # NOTE(arnaud): this is needed with VC 5.1
                        detail = doc.childAtPath('/Envelope/Body/Fault/detail')
                fault_list = []
                details = {}
                if detail:
                    for fault in detail.getChildren():
                        fault_list.append(fault.get("type"))
                        for child in fault.getChildren():
                            details[child.name] = child.getText()
                raise exceptions.VimFaultException(fault_list, fault_string,
                                                   excep, details)

            except AttributeError as excep:
                raise exceptions.VimAttributeException(
                    _("No such SOAP method %s.") % attr_name, excep)

            except (httplib.CannotSendRequest,
                    httplib.ResponseNotReady,
                    httplib.CannotSendHeader) as excep:
                raise exceptions.VimSessionOverLoadException(
                    _("httplib error in %s.") % attr_name, excep)

            except requests.RequestException as excep:
                raise exceptions.VimConnectionException(
                    _("requests error in %s.") % attr_name, excep)

            except Exception as excep:
                # TODO(vbala) should catch specific exceptions and raise
                # appropriate VimExceptions.

                # Socket errors which need special handling; some of these
                # might be caused by server API call overload.
                if (six.text_type(excep).find(ADDRESS_IN_USE_ERROR) != -1 or
                        six.text_type(excep).find(CONN_ABORT_ERROR)) != -1:
                    raise exceptions.VimSessionOverLoadException(
                        _("Socket error in %s.") % attr_name, excep)
                # Type error which needs special handling; it might be caused
                # by server API call overload.
                elif six.text_type(excep).find(RESP_NOT_XML_ERROR) != -1:
                    raise exceptions.VimSessionOverLoadException(
                        _("Type error in %s.") % attr_name, excep)
                else:
                    raise exceptions.VimException(
                        _("Exception in %s.") % attr_name, excep)
        def vim_request_handler(managed_object, **kwargs):
            """Handler for VIM API calls.

            Invokes the API and parses the response for fault checking and
            other errors.

            :param managed_object: managed object reference argument of the
                                   API call
            :param kwargs: keyword arguments of the API call
            :returns: response of the API call
            :raises: VimException, VimFaultException, VimAttributeException,
                     VimSessionOverLoadException, VimConnectionException
            """
            try:
                if isinstance(managed_object, str):
                    # For strings, use string value for value and type
                    # of the managed object.
                    managed_object = vim_util.get_moref(managed_object,
                                                        managed_object)
                request = getattr(self.client.service, attr_name)
                LOG.debug(_("Invoking %(attr_name)s on %(moref)s."),
                          {'attr_name': attr_name,
                           'moref': managed_object})
                response = request(managed_object, **kwargs)
                if (attr_name.lower() == 'retrievepropertiesex'):
                    Vim._retrieve_properties_ex_fault_checker(response)
                LOG.debug(_("Invocation of %(attr_name)s on %(moref)s "
                            "completed successfully."),
                          {'attr_name': attr_name,
                           'moref': managed_object})
                return response
            except exceptions.VimFaultException:
                # Catch the VimFaultException that is raised by the fault
                # check of the SOAP response.
                raise

            except suds.WebFault as excep:
                doc = excep.document
                detail = doc.childAtPath('/Envelope/Body/Fault/detail')
                fault_list = []
                if detail:
                    for child in detail.getChildren():
                        fault_list.append(child.get('type'))
                raise exceptions.VimFaultException(
                    fault_list, _("Web fault in %s.") % attr_name, excep)

            except AttributeError as excep:
                raise exceptions.VimAttributeException(
                    _("No such SOAP method %s.") % attr_name, excep)

            except (httplib.CannotSendRequest,
                    httplib.ResponseNotReady,
                    httplib.CannotSendHeader) as excep:
                raise exceptions.VimSessionOverLoadException(
                    _("httplib error in %s.") % attr_name, excep)

            except (urllib2.URLError, urllib2.HTTPError) as excep:
                raise exceptions.VimConnectionException(
                    _("urllib2 error in %s.") % attr_name, excep)

            except Exception as excep:
                # TODO(vbala) should catch specific exceptions and raise
                # appropriate VimExceptions.

                # Socket errors which need special handling; some of these
                # might be caused by server API call overload.
                if (six.text_type(excep).find(ADDRESS_IN_USE_ERROR) != -1 or
                        six.text_type(excep).find(CONN_ABORT_ERROR)) != -1:
                    raise exceptions.VimSessionOverLoadException(
                        _("Socket error in %s.") % attr_name, excep)
                # Type error which needs special handling; it might be caused
                # by server API call overload.
                elif six.text_type(excep).find(RESP_NOT_XML_ERROR) != -1:
                    raise exceptions.VimSessionOverLoadException(
                        _("Type error in %s.") % attr_name, excep)
                else:
                    raise exceptions.VimException(
                        _("Exception in %s.") % attr_name, excep)
Beispiel #17
0
 def test_get_moref(self):
     moref = vim_util.get_moref("vm-0", "VirtualMachine")
     self.assertEqual("vm-0", moref.value)
     self.assertEqual("VirtualMachine", moref._type)
 def _get_volume_ref(self, volume_ref_name):
     """Get the volume moref from the ref name."""
     return vutil.get_moref(volume_ref_name, 'VirtualMachine')
Beispiel #19
0
 def service_content(self):
     if not self._pbm_service_content:
         si_moref = vim_util.get_moref(SERVICE_INSTANCE, SERVICE_TYPE)
         self._pbm_service_content = (
             self._pbm_client.service.PbmRetrieveServiceContent(si_moref))
     return self._pbm_service_content
Beispiel #20
0
 def test_get_moref(self):
     moref = vim_util.get_moref("vm-0", "VirtualMachine")
     self.assertEqual("vm-0", moref.value)
     self.assertEqual("VirtualMachine", moref._type)
Beispiel #21
0
 def test_poweroff_instance(self):
     vm_name = self.spec.get("test_poweroff_instance").get("vm_name")
     vm_ref = vim_util.get_moref(vm_name, "VirtualMachine")
     poweroff_task = self.session.invoke_api(self.vim, "PowerOffVM_Task", vm_ref)
     task_info = self.session.wait_for_task(poweroff_task)
     self.assertEqual(task_info.state, "success")
Beispiel #22
0
 def retrieve_service_content(self):
     ref = vim_util.get_moref(service.SERVICE_INSTANCE, SERVICE_TYPE)
     return self.PbmRetrieveServiceContent(ref)
Beispiel #23
0
def get_moref(value, type):
    return vutil.get_moref(value, type)
        def vim_request_handler(managed_object, **kwargs):
            """Handler for VIM API calls.

            Invokes the API and parses the response for fault checking and
            other errors.

            :param managed_object: managed object reference argument of the
                                   API call
            :param kwargs: keyword arguments of the API call
            :returns: response of the API call
            :raises: VimException, VimFaultException, VimAttributeException,
                     VimSessionOverLoadException, VimConnectionException
            """
            try:
                if isinstance(managed_object, str):
                    # For strings, use string value for value and type
                    # of the managed object.
                    managed_object = vim_util.get_moref(
                        managed_object, managed_object)
                request = getattr(self.client.service, attr_name)
                LOG.debug(_("Invoking %(attr_name)s on %(moref)s."), {
                    'attr_name': attr_name,
                    'moref': managed_object
                })
                response = request(managed_object, **kwargs)
                if (attr_name.lower() == 'retrievepropertiesex'):
                    Vim._retrieve_properties_ex_fault_checker(response)
                LOG.debug(
                    _("Invocation of %(attr_name)s on %(moref)s "
                      "completed successfully."), {
                          'attr_name': attr_name,
                          'moref': managed_object
                      })
                return response
            except exceptions.VimFaultException:
                # Catch the VimFaultException that is raised by the fault
                # check of the SOAP response.
                raise

            except suds.WebFault as excep:
                doc = excep.document
                detail = doc.childAtPath('/Envelope/Body/Fault/detail')
                fault_list = []
                if detail:
                    for child in detail.getChildren():
                        fault_list.append(child.get('type'))
                raise exceptions.VimFaultException(
                    fault_list,
                    _("Web fault in %s.") % attr_name, excep)

            except AttributeError as excep:
                raise exceptions.VimAttributeException(
                    _("No such SOAP method %s.") % attr_name, excep)

            except (httplib.CannotSendRequest, httplib.ResponseNotReady,
                    httplib.CannotSendHeader) as excep:
                raise exceptions.VimSessionOverLoadException(
                    _("httplib error in %s.") % attr_name, excep)

            except (urllib2.URLError, urllib2.HTTPError) as excep:
                raise exceptions.VimConnectionException(
                    _("urllib2 error in %s.") % attr_name, excep)

            except Exception as excep:
                # TODO(vbala) should catch specific exceptions and raise
                # appropriate VimExceptions.

                # Socket errors which need special handling; some of these
                # might be caused by server API call overload.
                if (six.text_type(excep).find(ADDRESS_IN_USE_ERROR) != -1
                        or six.text_type(excep).find(CONN_ABORT_ERROR)) != -1:
                    raise exceptions.VimSessionOverLoadException(
                        _("Socket error in %s.") % attr_name, excep)
                # Type error which needs special handling; it might be caused
                # by server API call overload.
                elif six.text_type(excep).find(RESP_NOT_XML_ERROR) != -1:
                    raise exceptions.VimSessionOverLoadException(
                        _("Type error in %s.") % attr_name, excep)
                else:
                    raise exceptions.VimException(
                        _("Exception in %s.") % attr_name, excep)
Beispiel #25
0
 def _get_volume_ref(self, volume_ref_name):
     """Get the volume moref from the ref name."""
     return vutil.get_moref(volume_ref_name, 'VirtualMachine')
Beispiel #26
0
def get_moref(value, type):
    return vutil.get_moref(value, type)