def test_get_dsc_by_name(self, cancel_retrieval, continue_retrieval):
        pod_prop = mock.Mock()
        pod_prop.val = 'ds-cluster'
        pod_ref = vim_util.get_moref('group-p456', 'StoragePod')
        pod = mock.Mock()
        pod.propSet = [pod_prop]
        pod.obj = pod_ref

        retrieve_result = mock.Mock()
        retrieve_result.objects = [pod]

        session = mock.Mock()
        session.invoke_api = mock.Mock()
        session.invoke_api.return_value = retrieve_result
        name = 'ds-cluster'
        dsc_ref, dsc_name = datastore.get_dsc_ref_and_name(session, name)
        self.assertEqual((vim_util.get_moref_value(pod_ref),
                          vim_util.get_moref_type(pod_ref)),
                         (vim_util.get_moref_value(dsc_ref),
                          vim_util.get_moref_type(dsc_ref)))
 def test_get_dsc_with_moid(self):
     session = mock.Mock()
     session.invoke_api = mock.Mock()
     session.invoke_api.return_value = 'ds-cluster'
     dsc_moid = 'group-p123'
     dsc_ref, dsc_name = datastore.get_dsc_ref_and_name(session, dsc_moid)
     self.assertEqual((dsc_moid, 'StoragePod'),
                      (vim_util.get_moref_value(dsc_ref),
                       vim_util.get_moref_type(dsc_ref)))
     self.assertEqual('ds-cluster', dsc_name)
     session.invoke_api.assert_called_once_with(vim_util,
                                                'get_object_property',
                                                session.vim, mock.ANY,
                                                'name')
Exemple #3
0
        def side_effect(mo, **kwargs):
            self.assertEqual(managed_object, vim_util.get_moref_type(mo))
            self.assertEqual(managed_object, vim_util.get_moref_value(mo))
            fault = mock.Mock(faultstring="MyFault")

            fault_children = mock.Mock()
            fault_children.name = "name"
            fault_children.getText.return_value = "value"
            child = mock.Mock()
            child.get.return_value = fault_name
            child.getChildren.return_value = [fault_children]
            detail = mock.Mock()
            detail.getChildren.return_value = [child]
            doc.childAtPath.return_value = detail
            raise suds.WebFault(fault, doc)
Exemple #4
0
        def vim_RetrievePropertiesEx_side_effect(pc, specSet, options,
                                                 skip_op_id=False):
            self.assertTrue(pc is vim.service_content.propertyCollector)
            self.assertEqual(1, options.maxObjects)

            self.assertEqual(1, len(specSet))
            property_filter_spec = specSet[0]

            propSet = property_filter_spec.propSet
            self.assertEqual(1, len(propSet))
            prop_spec = propSet[0]
            self.assertTrue(prop_spec.all)
            self.assertEqual(['name'], prop_spec.pathSet)
            self.assertEqual(vim_util.get_moref_type(moref), prop_spec.type)

            objSet = property_filter_spec.objectSet
            self.assertEqual(1, len(objSet))
            obj_spec = objSet[0]
            self.assertEqual(moref, obj_spec.obj)
            self.assertEqual([], obj_spec.selectSet)
            self.assertFalse(obj_spec.skip)

            return retrieve_result
Exemple #5
0
 def side_effect(mo, **kwargs):
     self.assertEqual(managed_object, vim_util.get_moref_type(mo))
     self.assertEqual(managed_object, vim_util.get_moref_value(mo))
     return resp
Exemple #6
0
 def side_effect(mo, **kwargs):
     self.assertEqual(managed_object, vim_util.get_moref_type(mo))
     self.assertEqual(managed_object, vim_util.get_moref_value(mo))
     raise Exception(message)
Exemple #7
0
 def side_effect(mo, **kwargs):
     self.assertEqual(managed_object, vim_util.get_moref_type(mo))
     self.assertEqual(managed_object, vim_util.get_moref_value(mo))
     raise requests.HTTPError()
Exemple #8
0
 def side_effect(mo, **kwargs):
     self.assertEqual(managed_object, vim_util.get_moref_type(mo))
     self.assertEqual(managed_object, vim_util.get_moref_value(mo))
     raise httplib.CannotSendHeader()
Exemple #9
0
 def side_effect(mo, **kwargs):
     self.assertEqual(managed_object, vim_util.get_moref_type(mo))
     self.assertEqual(managed_object, vim_util.get_moref_value(mo))
     raise httplib.ResponseNotReady()
 def test_get_dvs_moref(self):
     moref = dvs_util.get_dvs_moref('dvs-123')
     self.assertEqual('dvs-123', vim_util.get_moref_value(moref))
     self.assertEqual('VmwareDistributedVirtualSwitch',
                      vim_util.get_moref_type(moref))
Exemple #11
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

                skip_op_id = kwargs.pop('skip_op_id', False)
                op_id = None
                if not skip_op_id:
                    # Generate opID. It will appear in vCenter and ESX logs for
                    # this particular remote call.
                    op_id = '%s-%s' % (self.op_id_prefix,
                                       uuidutils.generate_uuid())
                    LOG.debug('Invoking %s.%s with opID=%s',
                              vim_util.get_moref_type(managed_object),
                              attr_name, op_id)
                self._set_soap_headers(op_id)
                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_type = fault.get('type')
                        # NOTE(vbala): PBM faults use vim25 namespace. Also,
                        # PBM APIs throw NotAuthenticated in vSphere 6.5 for
                        # session expiry.
                        if (fault_type.endswith(exceptions.SECURITY_ERROR)
                                or fault_type.endswith(
                                    exceptions.NOT_AUTHENTICATED)):
                            fault_type = exceptions.NOT_AUTHENTICATED
                        fault_list.append(fault_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 (str(excep).find(ADDRESS_IN_USE_ERROR) != -1
                        or str(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 str(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)
Exemple #12
0
 def test_get_moref(self):
     moref = vim_util.get_moref("vm-0", "VirtualMachine")
     self.assertEqual("vm-0", vim_util.get_moref_value(moref))
     self.assertEqual("VirtualMachine", vim_util.get_moref_type(moref))