def __init__(self, name, introspection_adapter): """ Initialize OperationApiInterface :type name: :class:`str` :param name: Name of the provider :type introspection_adapter: :class:`vmware.vapi.provider.introspection.ApiProviderIntrospector` :param introspection_adapter: Adapter for fetching introspection information """ self._adapter = introspection_adapter iface_id = InterfaceIdentifier( 'com.vmware.vapi.std.introspection.operation') method_defs = {} methods = {} # list method list_method_id = MethodIdentifier(iface_id, 'list') output_def = ListDefinition(StringDefinition()) method_defs[list_method_id] = MethodDefinition( list_method_id, StructDefinition(OPERATION_INPUT, [('service_id', StringDefinition())]), output_def, [not_found_def]) methods[list_method_id] = self._list # get method get_method_id = MethodIdentifier(iface_id, 'get') data_ref_def = StructRefDefinition( 'com.vmware.vapi.std.introspection.operation.data_definition') field_def = StructDefinition(MAP_ENTRY, [('key', StringDefinition()), ('value', data_ref_def)]) data_def = StructDefinition( 'com.vmware.vapi.std.introspection.operation.data_definition', [('type', StringDefinition()), ('element_definition', OptionalDefinition(data_ref_def)), ('name', OptionalDefinition(StringDefinition())), ('fields', OptionalDefinition(ListDefinition(field_def)))]) data_ref_def.target = data_def output_def = StructDefinition( 'com.vmware.vapi.std.introspection.operation.info', [('input_definition', data_def), ('output_definition', data_def), ('error_definitions', ListDefinition(data_def))]) method_defs[get_method_id] = MethodDefinition( get_method_id, StructDefinition(OPERATION_INPUT, [('service_id', StringDefinition()), ('operation_id', StringDefinition())]), output_def, [not_found_def]) methods[get_method_id] = self._get IntrospectionBaseApiInterface.__init__(self, iface_id, method_defs, methods)
def get_data_definition(self): # Get a cover all data definitions data_defs = [ VoidDefinition(), IntegerDefinition(), DoubleDefinition(), StringDefinition(), SecretDefinition(), BooleanDefinition(), BlobDefinition(), OpaqueDefinition(), OptionalDefinition(IntegerDefinition()), ListDefinition(IntegerDefinition()), StructDefinition('struct', [ ('int', IntegerDefinition()), ('str', StringDefinition()), ('struct', StructDefinition('struct1', [('double', DoubleDefinition()), ('secret', SecretDefinition())])), ('list', ListDefinition(StructDefinition('struct1', [('double', DoubleDefinition()), ('secret', SecretDefinition())]))), ('optional', ListDefinition(StructDefinition('struct1', [('double', DoubleDefinition()), ('secret', SecretDefinition())]))) ]), ErrorDefinition('error', [ ('int', IntegerDefinition()), ('str', StringDefinition()) ]), ] return data_defs
def visit_optional(self, typ): """ Visit an optional value :type typ: :class:`OptionalType` :param typ: Binding type of the value """ typ.element_type.accept(self) element_def = self._out_value self._out_value = OptionalDefinition(element_def)
def test_struct_type(self): fields = { 'name': StringType(), 'age': IntegerType(), 'address': OptionalType(StringType()), } struct_type = StructType('test', fields) fields_def = [ ('name', StringDefinition()), ('age', IntegerDefinition()), ('address', OptionalDefinition(StringDefinition())), ] struct_def = StructDefinition('test', fields_def) self.assertNotEqual(struct_type, None) self.assertEqual(struct_type.definition, struct_def) self.assertEqual(struct_type.name, 'test') self.assertEqual(sorted(struct_type.get_field_names()), sorted(struct_def.get_field_names())) for field in struct_type.get_field_names(): self.assertEqual( struct_type.get_field(field).definition, struct_def.get_field(field))
_ID_FIELD_NAME = 'id' _DEFAULT_MESSAGE_FIELD_NAME = 'default_message' _ARGS_FIELD_NAME = 'args' localizable_message_def = StructDefinition( 'com.vmware.vapi.std.localizable_message', ((_ID_FIELD_NAME, StringDefinition()), (_DEFAULT_MESSAGE_FIELD_NAME, StringDefinition()), (_ARGS_FIELD_NAME, ListDefinition(StringDefinition())))) _MESSAGES_FIELD_NAME = 'messages' messages_list_def = ListDefinition(localizable_message_def) _DATA_FIELD_NAME = 'data' data_optional_dynamicstructure_def = OptionalDefinition( DynamicStructDefinition()) _ERROR_DEF_FIELDS = [(_MESSAGES_FIELD_NAME, messages_list_def), (_DATA_FIELD_NAME, data_optional_dynamicstructure_def)] def make_std_error_def(name): """ Internal function to create a "standard" ErrorDefinition for use only by the vAPI runtime. :type name: :class:`str` :param args: Fully qualified name of the standard error type :rtype: :class:`vmware.vapi.data.definition.ErrorDefinition` :return: ErrorDefinition containing a single message field """ return ErrorDefinition(name, _ERROR_DEF_FIELDS)
def build_data_value(py_value, data_def): """ Converts a native python value to data value using the provided data definition :type py_value: :class:`object` :param py_value: Python native value :type data_def: :class:`vmware.vapi.data.definition.DataDefinition` :param data_def: Data definition :rtype: :class:`vmware.vapi.data.value.DataValue` :return: Data value """ output_val = None if data_def.type == Type.OPTIONAL: output_val = data_def.new_value() if py_value is not None: output_val.value = build_data_value(py_value, data_def.element_type) elif data_def.type == Type.LIST: output_val = data_def.new_value() if py_value: for output in py_value: output_val.add(build_data_value(output, data_def.element_type)) elif data_def.type in (Type.STRUCTURE, Type.ERROR): output_val = data_def.new_value() for field in data_def.get_field_names(): field_def = data_def.get_field(field) if py_value is None: if field_def.type == Type.OPTIONAL: output_val.set_field(field, OptionalValue()) else: msg = message_factory.get_message( 'vapi.data.structure.field.missing', data_def.name, field) raise CoreException(msg) else: if isinstance(py_value, dict): value = py_value.get(field) elif isinstance(py_value, VapiStruct): # If Class used in python bindings is the struct value = py_value.get_field(field) else: msg = message_factory.get_message( 'vapi.data.structure.field.invalid', field) raise CoreException(msg) output_val.set_field( field, build_data_value(value, data_def.get_field(field))) elif data_def.type == Type.DYNAMIC_STRUCTURE: output_val = StructValue(DYNAMIC_STRUCTURE) if isinstance(py_value, dict): for key, val in six.iteritems(py_value): if val is None: field_data_def = OptionalDefinition(StringDefinition()) elif isinstance(val, list): if len(val) == 0: # empty list field_data_def = ListDefinition(StringDefinition()) else: # at least one element field_data_def = ListDefinition( _py_to_data_def_map.get(type(val[0]))()) elif isinstance(val, dict): field_data_def = DynamicStructDefinition() else: try: # Field is probably a primitive/atomic type field_data_def_type = _py_to_data_def_map[type(val)] field_data_def = field_data_def_type() except KeyError: msg = message_factory.get_message( 'vapi.data.serializers.python.unsupported.' 'python.type', type(val), key) raise CoreException(msg) output_val.set_field(key, build_data_value(val, field_data_def)) elif data_def.type == Type.VOID: output_val = data_def.new_value() elif data_def.type == Type.OPAQUE: output_val = py_value # Primitive type Integer/Double/String/Boolean/Secret else: output_val = data_def.new_value(py_value) return output_val
def test_optional_type(self): opt_type = OptionalType(IntegerType()) self.assertNotEqual(opt_type, None) self.assertEqual(opt_type.definition, OptionalDefinition(IntegerDefinition())) self.assertEqual(opt_type.element_type.definition, IntegerDefinition())
localization_param_ref = \ StructRefDefinition('com.vmware.vapi.std.localization_param') _ID_FIELD_NAME = 'id' _DEFAULT_MESSAGE_FIELD_NAME = 'default_message' _ARGS_FIELD_NAME = 'args' _LOCALIZED_FIELD_NAME = 'localized' _PARAMS_FIELD_NAME = 'params' nested_localizable_message_def = StructDefinition( NESTED_LOCALIZABLE_MESSAGE, ((_ID_FIELD_NAME, StringDefinition()), (_PARAMS_FIELD_NAME, OptionalDefinition( make_map_def(StringDefinition(), localization_param_ref))))) localization_param_def = StructDefinition( LOCALIZATION_PARAM, (("s", OptionalDefinition(StringDefinition())), ("dt", OptionalDefinition(StringDefinition())), ("i", OptionalDefinition(IntegerDefinition())), ("d", OptionalDefinition(DoubleDefinition())), ("l", OptionalDefinition(nested_localizable_message_def)), ("format", OptionalDefinition(StringDefinition())), ("precision", OptionalDefinition(IntegerDefinition())))) localization_param_ref.target = localization_param_def localizable_message_def = StructDefinition( LOCALIZABLE_MESSAGE,