Пример #1
0
 def test_has_fields_of_positive2(self):
     prop = Properties(int_val=10,
                       str_val='STRING_PROP',
                       bool_val=True,
                       opt_int_val=11,
                       list_val=[],
                       nested_val=NestedProperties(int_val=100))
     msg_list = self.has_fields_of_validator.validate(
         prop.get_struct_value(), dynamic_struct_binding_type)
     raise_core_exception(msg_list)
Пример #2
0
 def test_has_fields_of_positive3(self):
     struct_value = StructValue(name='Properties2',
                                values={
                                    'list_val':
                                    ListValue(),
                                    'nested_val':
                                    StructValue('NestedProperties2')
                                })
     msg_list = self.has_fields_of_validator.validate(
         struct_value, dynamic_struct_binding_type2)
     raise_core_exception(msg_list)
Пример #3
0
 def test_has_fields_of_positive2(self):
     prop = Properties(int_val=10,
                       str_val='STRING_PROP',
                       bool_val=True,
                       opt_int_val=11,
                       list_val=[],
                       nested_val=NestedProperties(int_val=100))
     values = {}
     for k, v in prop.get_struct_value().get_fields():
         if k != 'opt_int_val':
             values[k] = v
     msg_list = self.has_fields_of_validator.validate(
         StructValue(name='Properties', values=values),
         dynamic_struct_binding_type)
     raise_core_exception(msg_list)
Пример #4
0
    def validate_struct_value(cls, struct_value):
        """
        Validate if the given struct value satisfies all
        the constraints of this VapiStruct.

        :type  struct_value: :class:`vmware.vapi.data.value.StructValue`
        :param struct_value: StructValue to be validated
        :type  validators: :class:`list` of
            :class:`vmware.vapi.data.validator.Validator`
        :param validators: List of validators
        :raise :class:`vmware.vapi.exception.CoreException` if a constraint is
            not satisfied
        """
        if cls._validator_list:
            for validator in cls._validator_list:
                msg_list = validator.validate(struct_value, None)
                raise_core_exception(msg_list)
Пример #5
0
 def test_union_validator_positive3(self):
     # Test Optional union cases
     union_validator = UnionValidator(
         'test_enum', {
             'LONG': [('long_val', True), ('opt_long_val', False)],
             'STRING': [('string_val', True)],
             'NONE': [],
         })
     msg_list = union_validator.validate(
         StructValue(name='TestStruct',
                     values={
                         'test_enum': StringValue('LONG'),
                         'long_val': IntegerValue(100),
                         'opt_long_val': IntegerValue(120)
                     }))
     raise_core_exception(msg_list)
     msg_list = union_validator.validate(
         StructValue(name='TestStruct',
                     values={
                         'test_enum': StringValue('LONG'),
                         'long_val': IntegerValue(100),
                     }))
     raise_core_exception(msg_list)
     msg_list = union_validator.validate(
         StructValue(name='TestStruct',
                     values={
                         'test_enum': StringValue('NONE'),
                     }))
     raise_core_exception(msg_list)
Пример #6
0
 def test_union_validator_positive2(self):
     # Test Optional union tags
     union_validator = UnionValidator(
         'test_enum', {
             'LONG': [('long_val', True)],
             'STRING': [('string_val', True)],
             'NONE': [],
         })
     msg_list = union_validator.validate(
         StructValue(name='TestStruct',
                     values={
                         'test_enum': OptionalValue(StringValue('LONG')),
                         'long_val': OptionalValue(IntegerValue(100))
                     }))
     raise_core_exception(msg_list)
     msg_list = union_validator.validate(
         StructValue(name='TestStruct',
                     values={
                         'test_enum': OptionalValue(StringValue('NONE')),
                     }))
     raise_core_exception(msg_list)
     msg_list = union_validator.validate(
         StructValue(name='TestStruct',
                     values={
                         'test_enum': OptionalValue(),
                     }))
     raise_core_exception(msg_list)
Пример #7
0
    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)