Esempio n. 1
0
    def new_value(self, value=b''):
        """
        Create a new BlobValue

        :rtype: :class:`vmware.vapi.data.value.BlobValue`
        :return: Newly created BlobValue
        """
        return data_value_factory(self.type, value)
Esempio n. 2
0
    def new_value(self, value=""):
        """
        Create a new SecretValue

        :rtype: :class:`vmware.vapi.data.value.SecretValue`
        :return: Newly created SecretValue
        """
        return data_value_factory(self.type, value)
Esempio n. 3
0
    def new_value(self):
        """
        Create a new ErrorValue

        :rtype: :class:`vmware.vapi.data.value.ErrorValue`
        :return: Newly created ErrorValue
        """
        return data_value_factory(self.type, self.name)
Esempio n. 4
0
    def new_value(self, value=False):
        """
        Create a new BooleanValue

        :rtype: :class:`vmware.vapi.data.value.BooleanValue`
        :return: Newly created BooleanValue
        """
        return data_value_factory(self.type, value)
Esempio n. 5
0
    def new_value(self, value=0.0):
        """
        Create a new DoubleValue

        :rtype: :class:`vmware.vapi.data.value.DoubleValue`
        :return: Newly created DoubleValue
        """
        return data_value_factory(self.type, value)
Esempio n. 6
0
    def new_value(self, value=0):
        """
        Create a new IntegerValue

        :rtype: :class:`vmware.vapi.data.value.IntegerValue`
        :return: Newly created IntegerValue
        """
        return data_value_factory(self.type, value)
Esempio n. 7
0
    def new_value(self):
        """
        Create a new VoidValue

        :rtype: :class:`vmware.vapi.data.value.VoidValue`
        :return: Newly created VoidValue
        """
        return data_value_factory(self.type)
Esempio n. 8
0
    def new_value(self, values=None):
        """
        Create a new ListValue

        :type  values: :class:`list` of :class:`vmware.vapi.data.value.DataValue`
        :param values: List of elements
        :rtype: :class:`vmware.vapi.data.value.ListValue`
        :return: Newly created ListValue
        """
        return data_value_factory(self.type, values)
Esempio n. 9
0
    def new_value(self, value=None):
        """
        Create and return a new :class:`OptionalValue` using this
        optional-definition.

        :type  value: :class:`vmware.vapi.data.value.DataValue`
        :param value: The element value

        :rtype: :class:`vmware.vapi.data.value.OptionalValue`
        :return: A new optional value using the given data-definition
        """
        return data_value_factory(self.type, value)
Esempio n. 10
0
    def visit_builtin(self, type_info, json_value):
        """
        Deserialize a primitive value

        :type  type_info:
            :class:`com.vmware.vapi.metadata.metamodel_client.Type`
        :param type_info: Metamodel type information
        :type  json_value: :class:`object`
        :param json_value: Value to be visited
        :rtype: :class:`vmware.vapi.data.value.DataValue`
        :return: DataValue created using the input
        """
        try:
            native_type = self._builtin_native_type_map[type_info.builtin_type]
            if native_type == six.text_type:
                if type_info.builtin_type == Type.BuiltinType.BINARY:
                    # For Binary types, we need to convert unicode to bytes
                    base64_encoded_value = json_value.encode()
                    native_value = base64.b64decode(base64_encoded_value)
                else:
                    native_value = json_value
            elif native_type == int:
                native_value = int(json_value)
            elif native_type == decimal.Decimal:
                native_value = decimal.Decimal(json_value)
            elif native_type == bool:
                if isinstance(json_value, bool):
                    native_value = json_value
                elif json_value.lower() == 'true':
                    native_value = True
                elif json_value.lower() == 'false':
                    native_value = False
                else:
                    msg = 'Expected boolean value, but got %s' % json_value
                    logger.error(msg)
                    raise werkzeug.exceptions.BadRequest(msg)
            data_type = self._builtin_type_map[type_info.builtin_type]
            return data_value_factory(data_type, native_value)
        except KeyError:
            msg = ('Could not process the request, '
                   'builtin type %s is not supported' % type_info.builtin_type)
            logger.exception(msg)
            raise werkzeug.exceptions.InternalServerError(msg)
Esempio n. 11
0
    def data_value(value):
        """
        get data value from new jsonrpc dict

        # TODO: Structure names and its fields are converted from
        # u'' format to str format. This will break if we allow non
        # ASCII characters in the IDL

        :type  value: :class:`dict`
        :param value: json data value
        :rtype:  subclass of :class:`vmware.vapi.data.value.DataValue`
        :return: subclass of data value
        """
        result = None
        if value is None:
            result = data_value_factory(Type.VOID)
        elif isinstance(value, dict):
            # Optional, Secret, Blob, Error, Structure
            # types are inside object
            if len(value) > 1:
                raise vapi_jsonrpc_error_invalid_params()
            (type_name, json_value) = value.popitem()
            typ = json_to_vapi_map[type_name]
            if typ == Type.SECRET:
                result = data_value_factory(typ, json_value)
            elif typ == Type.BLOB:
                # For Blob types, the string json value will be base64 encoded
                # and UTF-8 decoded
                unicode_json_value = json_value
                base64_encoded_value = unicode_json_value.encode()
                try:
                    bytes_value = base64.b64decode(base64_encoded_value)
                    result = data_value_factory(typ, bytes_value)
                except Exception as err:
                    if six.PY2 and isinstance(err, TypeError):
                        raise vapi_jsonrpc_error_internal_error()
                    elif six.PY3:
                        import binascii
                        if isinstance(err, binascii.Error):
                            raise vapi_jsonrpc_error_internal_error()
                    raise
            elif typ in [Type.ERROR, Type.STRUCTURE]:
                if len(json_value) > 1:
                    raise vapi_jsonrpc_error_invalid_params()
                struct_name, fields = json_value.popitem()
                result = data_value_factory(typ, str(struct_name))
                for field_name, field_value in six.iteritems(fields):
                    field_data_value = JsonRpcDictToVapi.data_value(
                        field_value)
                    result.set_field(str(field_name), field_data_value)
            elif typ == Type.OPTIONAL:
                result = data_value_factory(typ)
                if json_value is not None:
                    result.value = JsonRpcDictToVapi.data_value(json_value)
        elif isinstance(value, list):
            # ListValue is json list
            list_data_values = [
                JsonRpcDictToVapi.data_value(val) for val in value
            ]
            result = data_value_factory(Type.LIST, list_data_values)
        elif isinstance(value, bool):
            result = data_value_factory(Type.BOOLEAN, value)
        elif isinstance(value, six.string_types):
            result = data_value_factory(Type.STRING, value)
        elif isinstance(value, six.integer_types):
            result = data_value_factory(Type.INTEGER, value)
        else:
            # For Double
            result = data_value_factory(Type.DOUBLE, value)
        return result