def __init__(self, name, introspection_adapter): """ Initialize ProviderApiInterface :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._name = name self._adapter = introspection_adapter iface_id = InterfaceIdentifier( 'com.vmware.vapi.std.introspection.provider') method_defs = {} # get method get_method_id = MethodIdentifier(iface_id, 'get') output_def = StructDefinition( 'com.vmware.vapi.std.introspection.provider.info', [('id', StringDefinition()), ('checksum', StringDefinition())]) method_defs[get_method_id] = MethodDefinition( get_method_id, StructDefinition(OPERATION_INPUT, []), output_def, []) methods = {} 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 test_map_type(self): map_type = MapType(StringType(), StringType()) self.assertNotEqual(map_type, None) self.assertEqual( map_type.definition, ListDefinition( StructDefinition(MAP_ENTRY, [('key', StringDefinition()), ('value', StringDefinition())]))) self.assertRaises(TypeError, MapType, 'bogus', StringType()) self.assertRaises(TypeError, MapType, StringType(), 'bogus')
def visit_id(self, typ): """ Visit a ID value :type typ: :class:`IdType` :param typ: Binding type of the value """ self._out_value = StringDefinition()
def visit_string(self, typ): """ Visit a string value :type typ: :class:`StringType` :param typ: Binding type of the value """ self._out_value = StringDefinition()
def visit_uri(self, typ): """ Visit an URI value :type typ: :class:`URIType` :param typ: Binding type of the value """ self._out_value = StringDefinition()
def visit_date_time(self, typ): """ Visit a datetime value :type typ: :class:`DateTimeType` :param typ: Binding type of the value """ self._out_value = StringDefinition()
def visit_enum(self, typ): """ Visit a enum value :type typ: :class:`EnumType` :param typ: Binding type of the value """ self._out_value = StringDefinition()
def test_multiple_struct_references(self): top_level_struct_type = TopLevelStruct.get_binding_type() multiple_struct_references_type = MultipleStructReferences.get_binding_type( ) expected_definition = StructDefinition( 'multiple_struct_references', [('string_field', StringDefinition()), ('structure_field1', top_level_struct_type.definition), ('structure_field2', top_level_struct_type.definition)]) self.assertEqual(multiple_struct_references_type.definition, expected_definition)
def test_generic_def_to_value(self): for typ_def, typ in [(OptionalDefinition, 'OPTIONAL'), (ListDefinition, 'LIST')]: typ_def = typ_def(StringDefinition()) actual_output = convert_data_def_to_data_value(typ_def) expected_output = StructValue(Introspection.DATA_DEFINITION) element_value = self.generate_primitive_value('STRING') expected_output.set_field('type', StringValue(typ)) expected_output.set_field('fields', OptionalValue()) expected_output.set_field('name', OptionalValue()) expected_output.set_field('element_definition', OptionalValue(element_value)) self.assertEqual(actual_output, expected_output)
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 __init__(self, name, introspection_adapter): """ Initialize ServiceApiInterface :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.service') 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, []), output_def, []) methods[list_method_id] = self._list # get method get_method_id = MethodIdentifier(iface_id, 'get') output_def = StructDefinition( 'com.vmware.vapi.std.introspection.service.info', [('operations', ListDefinition(StringDefinition()))]) method_defs[get_method_id] = MethodDefinition( get_method_id, StructDefinition(OPERATION_INPUT, [('id', StringDefinition())]), output_def, [not_found_def]) methods[get_method_id] = self._get IntrospectionBaseApiInterface.__init__(self, iface_id, method_defs, methods)
def test_error_to_value(self): struct_def = ErrorDefinition('mock', [('field1', StringDefinition())]) actual_output = convert_data_def_to_data_value(struct_def) expected_output = StructValue(Introspection.DATA_DEFINITION) expected_output.set_field('type', StringValue('ERROR')) expected_output.set_field('name', OptionalValue(StringValue('mock'))) expected_output.set_field('element_definition', OptionalValue()) field_value = self.generate_primitive_value('STRING') field_entry = StructValue(MAP_ENTRY) field_entry.set_field('key', StringValue('field1')) field_entry.set_field('value', field_value) field_values = ListValue() field_values.add(field_entry) expected_output.set_field('fields', OptionalValue(field_values)) self.assertEqual(actual_output, expected_output)
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))
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_recursive_structure(self): linked_list_type = ReferenceLocalRecursive.get_binding_type() expected_definition = StructDefinition( 'linked_list', [('data', StringDefinition()), ('next_node', StructRefDefinition('linked_list'))]) self.assertEqual(linked_list_type.definition, expected_definition)
fake_iface_id = 'vmware.test.fake.iface' null_method_id = 'Null' null_method_def = MethodDefinition(MethodIdentifier( InterfaceIdentifier(fake_iface_id), null_method_id), VoidDefinition(), VoidDefinition(), [make_std_error_def('error1'), make_std_error_def('error2')] ) echo_method_id = 'Echo' echo_method_def = MethodDefinition(MethodIdentifier( InterfaceIdentifier(fake_iface_id), echo_method_id), StructDefinition('struct', [('int', IntegerDefinition()), ('str', StringDefinition())] ), StructDefinition('struct', [('int', IntegerDefinition()), ('str', StringDefinition())] ), [make_std_error_def('e1'), make_std_error_def('e2'), make_std_error_def('e3')]) class FakeInterface(ApiInterface): def __init__(self): self.if_id = InterfaceIdentifier(fake_iface_id) self.operations = { null_method_id: null_method_def,
def test_enum_type(self): enum_name = 'test_enum' enum_type = TestEnum.get_binding_type() self.assertNotEqual(enum_type, None) self.assertEqual(enum_type.name, enum_name) self.assertEqual(enum_type.definition, StringDefinition())
def test_uri_type(self): uri_type = URIType() self.assertNotEqual(uri_type, None) self.assertEqual(uri_type.definition, StringDefinition())
def test_datetime_type(self): dt_type = DateTimeType() self.assertNotEqual(dt_type, None) self.assertEqual(dt_type.definition, StringDefinition())
def test_invalid_struct_type(self): self.assertRaises(TypeError, StructType, 'test', []) self.assertRaises(TypeError, StructType, 10, {}) self.assertRaises(TypeError, StructType, 'test', {'name': StringDefinition()})
# Method identifiers mock_method_id = MethodIdentifier(interface_id, mock_method_name) pre_method_id = MethodIdentifier(interface_id, pre_method_name) post_method_id = MethodIdentifier(interface_id, post_method_name) veto_method_id = MethodIdentifier(interface_id, veto_method_name) method_id_dict = { mock_method_name: mock_method_id, pre_method_name: pre_method_id, post_method_name: post_method_id, veto_method_name: veto_method_id, } input_def = StructDefinition('input', [ ('service_name', StringDefinition()), ('operation_name', StringDefinition()), ('operation_input', OpaqueDefinition()), ]) mock_input_def = StructDefinition('input', [('raise', BooleanDefinition())]) not_found_error_def = make_std_error_def( 'com.vmware.vapi.std.errors.not_found') errors = [ 'com.vmware.vapi.std.errors.not_found', 'com.vmware.vapi.std.errors.internal_server_error', 'com.vmware.vapi.std.errors.invalid_argument', 'com.vmware.vapi.std.errors.operation_not_found', ] error_defs = [make_std_error_def(error) for error in errors] error_values = ListValue( [convert_data_def_to_data_value(error_def) for error_def in error_defs])
__author__ = 'VMware, Inc.' __copyright__ = 'Copyright 2015 VMware, Inc. All rights reserved. -- VMware Confidential' # pylint: disable=line-too-long from vmware.vapi.data.definition import ( DynamicStructDefinition, ErrorDefinition, ListDefinition, OptionalDefinition, StringDefinition, StructDefinition) from vmware.vapi.l10n.runtime import message_factory _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): """
def test_string_type(self): s_type = StringType() self.assertNotEqual(s_type, None) self.assertEqual(s_type.definition, StringDefinition())
def test_set_type(self): set_type = SetType(StringType()) self.assertNotEqual(set_type, None) self.assertEqual(set_type.definition, ListDefinition(StringDefinition())) self.assertRaises(TypeError, SetType, 'bogus')
StructDefinition(MAP_ENTRY, ((_MAP_KEY_FIELD_NAME, key), (_MAP_VALUE_FIELD_NAME, value)))) 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