def _override_api_info(self, published_info): """ Override service and operation task info fields """ self.info.service = TypeConverter.convert_to_python( published_info.get_field('service'), StringType()) self.info.operation = TypeConverter.convert_to_python( published_info.get_field('operation'), StringType())
def _initialize_common_info(self): """ Initialize common task info fields """ published_info = self.task_manager.get_info(self.task_id) self._override_api_info(published_info) self.info.cancelable = TypeConverter.convert_to_python( published_info.get_field('cancelable'), BooleanType()) desc = published_info.get_field('description') if desc is not None: self.info.description = TypeConverter.convert_to_python( desc, LocalizableMessage.get_binding_type()) self.info.status = TypeConverter.convert_to_python( published_info.get_field('status'), StringType())
def _verify_correct(self, dt_str, dt): dt_str = StringValue(dt_str) self.assertEqual( TypeConverter.convert_to_python( dt_str, self.typ, rest_converter_mode=RestConverter.SWAGGER_REST), dt)
def test_set_info(self): service_uuid = str(uuid.uuid4()) task_id = self.task_manager.create_task(service_uuid, None, 'test.service', 'op', False) info_val = self.task_manager.get_info(task_id) info = TypeConverter.convert_to_python(info_val, Info.get_binding_type()) info.description = self.description progress = Progress() progress.total = 100 progress.completed = 10 progress.message = self.progress_msg info.progress = progress info.start_time = datetime.utcnow() info.status = Status.RUNNING info.cancelable = True self.task_manager.set_info(task_id, TypeConverter.convert_to_vapi(info, Info.get_binding_type())) result = self.task_manager.get_info(task_id) self.assertEqual(result.get_field('progress'), OptionalValue(TypeConverter.convert_to_vapi(progress, Progress.get_binding_type()))) self.assertEqual(result.get_field('cancelable'), BooleanValue(info.cancelable)) self.assertEqual(result.get_field('status'), StringValue('RUNNING'))
def get_published_info(self): """ Returns the current published task Info """ info = self.task_manager.get_info(self.task_id) converted_info = TypeConverter.convert_to_python( info, self.info_type.get_binding_type()) return converted_info
def get_result(self): """ Returns task result :rtype: :class:`com.vmware.cis.tasks_client.Info` :return: Task Result """ return TypeConverter.convert_to_python(self.get_info().result, self.result_type)
def get_result(self): """ Returns task result :rtype: :class:`com.vmware.cis.tasks_client.Info` :return: Task Result """ info = self.get_info() if hasattr(info, 'result'): return TypeConverter.convert_to_python(info.result, self.result_type) else: return None
def _initialize_common_info(self): """ Initialize common task info fields """ summary = self.task_manager.get_summary(self.task_id) published_info = summary.info self.error_types = summary.errors self._override_api_info(published_info) self.info.cancelable = TypeConverter.convert_to_python( published_info.get_field('cancelable'), BooleanType()) desc = published_info.get_field('description') if desc is not None: self.info.description = TypeConverter.convert_to_python( desc, LocalizableMessage.get_binding_type()) self.info.status = TypeConverter.convert_to_python( published_info.get_field('status'), StringType()) try: user = published_info.get_field('user') self.info.user = TypeConverter.convert_to_python( user, StringType()) except: # pylint: disable=W0702 pass
def test_basic(self): dt = datetime.datetime.utcnow() string_val = TypeConverter.convert_to_vapi(dt, self.typ) self.assertTrue(isinstance(string_val, StringValue)) dt_new = TypeConverter.convert_to_python(string_val, self.typ) self.assertTrue(isinstance(dt_new, datetime.datetime)) self.assertEqual(dt.year, dt_new.year) self.assertEqual(dt.month, dt_new.month) self.assertEqual(dt.day, dt_new.day) self.assertEqual(dt.hour, dt_new.hour) self.assertEqual(dt.minute, dt_new.minute) self.assertEqual(dt.second, dt_new.second) string_val_new = TypeConverter.convert_to_vapi(dt_new, self.typ) self.assertEqual(string_val, string_val_new)
def convert_to(self, cls): """ Convert the underlying StructValue to an instance of the provided class if possible. Conversion will be possible if the StructValue contains all the fields expected by the provided class and the type of the value in each fields matches the type of the field expected by the provided class. :type cls: :class:`vmware.vapi.data.value.StructValue` :param cls: The type to convert to :rtype: :class:'vmware.vapi.bindings.struct.VapiStruct' :return: The converted value """ # Import TypeConverter here since otherwise it causes circular imports from vmware.vapi.bindings.converter import TypeConverter return TypeConverter.convert_to_python( vapi_val=self.get_struct_value(), binding_type=cls.get_binding_type(), rest_converter_mode=self._rest_converter_mode)
def _convert(self, dt, dt_str): dt_str = StringValue(dt_str) self.assertEqual(TypeConverter.convert_to_vapi(dt, self.typ), dt_str) self.assertEqual(TypeConverter.convert_to_python(dt_str, self.typ), dt)
def _convert(self, uri_string): self.assertEqual(TypeConverter.convert_to_vapi(uri_string, self.typ), StringValue(uri_string)) self.assertEqual( TypeConverter.convert_to_python(StringValue(uri_string), self.typ), uri_string)
def native_invoke(self, ctx, method_name, kwargs): """ Invokes the method corresponding to the given method name with the kwargs. In this method, python native values are converted to vAPI runtime values, operation is invoked and the result are converted back to python native values :type ctx: :class:`vmware.vapi.core.ExecutionContext` :param ctx: Execution context for this method :type method_name: :class:`str` :param method_name: Method name :type kwargs: :class:`dict` :param kwargs: arguments to be passed to the method :rtype: :class:`object` :return: Method result """ op_info = self._operations.get(method_name) if op_info is None: raise Exception('Could not find %s method in %s interface' % (method_name, str(self._iface_id))) # Convert input input_type = op_info['input_type'] data_val = TypeConverter.convert_to_vapi(kwargs, input_type, self._rest_converter_mode) # Validate input validators = op_info['input_value_validator_list'] for validator in validators: msg_list = validator.validate(data_val, input_type) raise_core_exception(msg_list) if OPID in ctx.application_context: logger.debug( 'opId: %s invoke: interface_id: %s, operation_name: %s', ctx.application_context[OPID], self._iface_id.get_name(), method_name) else: logger.debug('invoke: interface_id: %s, operation_name: %s', self._iface_id, method_name) # Invoke method_id = MethodIdentifier(self._iface_id, method_name) if self._use_rest: operation_rest_metadata = None if self._rest_metadata: operation_rest_metadata = self._rest_metadata.get(method_name) if operation_rest_metadata is None: msg = message_factory.get_message( 'vapi.bindings.stub.rest_metadata.unavailable') logger.debug(msg) raise CoreException(msg) else: if self._is_vapi_rest is not None and not self._is_vapi_rest: # If the rest format is not vapi, then the server # (OpenAPI based) will not be supporting json-rpc. msg = message_factory.get_message( 'vapi.bindings.stub.jsonrpc.unsupported') logger.debug(msg) raise CoreException(msg) method_result = self.invoke(ctx, method_id, data_val) # Validate output if method_result.success(): validators = op_info['output_validator_list'] output_type = op_info['output_type'] for validator in validators: msg_list = validator.validate(method_result.output, output_type) raise_core_exception(msg_list) # Convert output return TypeConverter.convert_to_python(method_result.output, output_type, self._config.resolver, self._rest_converter_mode) else: # Convert error errors = op_info['errors'] error_type = errors.get(method_result.error.name) if error_type is None: error_type = self._config.resolver.resolve( method_result.error.name) if error_type is None: logger.warning('Unable to convert unexpected vAPI error %s ' + 'to native Python exception', method_result.error.name) vapi_error = UnresolvedError(method_result.error) raise vapi_error raise TypeConverter.convert_to_python(method_result.error, # pylint: disable=E0702 error_type, self._config.resolver, self._rest_converter_mode)
def invoke(self, ctx, method_id, input_value): """ Invokes the specified method using the execution context and the input provided :type ctx: :class:`vmware.vapi.core.ExecutionContext` :param ctx: Execution context for this method :type input_value: :class:`vmware.vapi.data.value.StructValue` :param input_value: Method input parameters :rtype: :class:`vmware.vapi.core.MethodResult` :return: Result of the method invocation """ op_info = self._operations.get(method_id.get_name()) # Set execution context set_context(ctx) try: # input validators validators = op_info['input_value_validator_list'] input_type = op_info['input_type'] for validator in validators: msg_list = validator.validate(input_value, input_type) if msg_list: error_value = make_error_value_from_msgs( self._invalid_argument_def, *msg_list) return MethodResult(error=error_value) error_value = ApiInterfaceSkeleton.check_for_unknown_fields( input_type, input_value) if error_value: return MethodResult(error=error_value) try: meth_args = TypeConverter.convert_to_python( input_value, binding_type=input_type, resolver=self._resolver) except CoreException as e: error_value = make_error_value_from_msgs( self._invalid_argument_def, *e.messages) return MethodResult(error=error_value) method_name = Converter.canonical_to_pep( get_non_task_operation_name(method_id.get_name())) method = getattr(self._impl, method_name) # If it's a task method then create a new thread to run provider # impl and return task id or result depending upon type of # invocation. if op_info['task_type'] != TaskType.NONE: meth_output, task_accepted = self._invoke_task_operation( ctx, method_id, method, meth_args) if isinstance(meth_output, VapiError): raise meth_output # pylint: disable=raising-bad-type # In case of provider not accepting task throw an error. if task_accepted is False: msg_list = [message_factory.get_message( 'vapi.bindings.skeleton.task.invalidstate')] error_value = make_error_value_from_msgs( self._internal_server_error_def, *msg_list) return MethodResult(error=error_value) else: meth_output = method(**meth_args) output_type = op_info['output_type'] # output validators validators = op_info['output_validator_list'] output = TypeConverter.convert_to_vapi(meth_output, output_type) for validator in validators: msg_list = validator.validate(output, output_type) if msg_list: error_value = make_error_value_from_msgs( self._internal_server_error_def, *msg_list) return MethodResult(error=error_value) result = MethodResult(output=output) except CoreException as e: logger.exception("Error in invoking %s - %s", str(method_id), e) error_value = make_error_value_from_msgs( self._internal_server_error_def, *e.messages) result = MethodResult(error=error_value) except VapiError as e: exc_info = sys.exc_info() error_type = e.__class__.get_binding_type() try: error = TypeConverter.convert_to_vapi(e, error_type) except CoreException as ce: logger.error('Failed to convert %s to error type %s because %s', e, error_type.name, ' because '.join((e.def_msg for e in ce.messages)), exc_info=exc_info) raise result = MethodResult(error=error) finally: # Reset the execution context after the operation clear_context() return result