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 __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 __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 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 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 make_map_def(key, value): """ Internal function to create map definition from a key and value definitions. For use only by vAPI runtime. :type key: :class:`vmware.vapi.data.type.DataDefinition` :param key: DataDefintion for the map key :type value: :class:`vmware.vapi.data.type.DataDefinition` :param value: DataDefintion for the map value """ return ListDefinition( StructDefinition(MAP_ENTRY, ((_MAP_KEY_FIELD_NAME, key), (_MAP_VALUE_FIELD_NAME, value))))
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_get_error_definition(self): method_def = MethodDefinition( MethodIdentifier(InterfaceIdentifier('interface'), 'method1'), StructDefinition('method1_input', []), VoidDefinition(), [self.error_def1, self.error_def2, self.error_def3]) self.assertEquals(self.error_def1, method_def.get_error_definition(self.ERROR1_NAME)) self.assertEquals(self.error_def2, method_def.get_error_definition(self.ERROR2_NAME)) self.assertEquals(self.error_def3, method_def.get_error_definition(self.ERROR3_NAME)) self.assertEquals( None, method_def.get_error_definition(self.BOGUS_ERROR_NAME))
def test_struct_to_value(self): struct_def = StructDefinition('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('STRUCTURE')) 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 get_method_definition(self, method_id): name = method_id.get_name() if name == 'bogus_method': return None if name == missing_input_definition: input_ = None elif name == invalid_input_definition: input_ = 'not a StructDefinition' elif name == mock_definition: input_ = StructDefinition(OPERATION_INPUT, []) else: input_ = StructDefinition(OPERATION_INPUT, [(param_name, IntegerDefinition())]) if name == missing_output_definition: output = None elif name == invalid_input_definition: output = 'not a DataDefinition' else: output = VoidDefinition() errors = [] if name == report_declared_error: errors.append(not_found_error_def) return MethodDefinition(method_id, input_, output, errors)
def visit_map(self, typ): """ Visit a map value :type typ: :class:`MapType` :param typ: Binding type of the value """ field_defs = [] typ.key_type.accept(self) key_def = self._out_value field_defs.append((MAP_KEY_FIELD, key_def)) typ.value_type.accept(self) value_def = self._out_value field_defs.append((MAP_VALUE_FIELD, value_def)) element_def = StructDefinition(MAP_ENTRY, field_defs) self._out_value = ListDefinition(element_def)
def test_value_to_struct_ref_def(self): struct_value = StructValue(Introspection.DATA_DEFINITION) struct_value.set_field('type', StringValue('STRUCTURE')) struct_value.set_field('name', OptionalValue(StringValue('mock'))) struct_value.set_field('element_definition', OptionalValue()) field_value = self.generate_primitive_value('STRUCTURE_REF') field_value.set_field('name', OptionalValue(StringValue('mock'))) 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) struct_value.set_field('fields', OptionalValue(field_values)) actual_output = convert_data_value_to_data_def(struct_value) expected_output = StructDefinition( 'mock', [('field1', StructRefDefinition('mock'))]) self.assertEqual(actual_output, expected_output) # Check the circular reference as well self.assertEqual( actual_output.get_field('field1').target, expected_output)
def visit_struct(self, typ): """ Visit a struct value :type typ: :class:`StructType` :param typ: Binding type of the value """ if typ.name in self._seen_structures: if self._ctx.is_defined(typ.name): self._out_value = self._ctx.get_definition(typ.name) else: self._out_value = StructRefDefinition(typ.name) self._ctx.add_reference(self._out_value) else: self._seen_structures.append(typ.name) field_defs = [] for field_name in typ.get_field_names(): field_type = typ.get_field(field_name) field_type.accept(self) field_defs.append((field_name, self._out_value)) self._out_value = StructDefinition(typ.name, field_defs) self._ctx.add_definition(self._out_value)
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
return instance 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,
__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): """
# 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])
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)
def get_method_definition(self, method_id): input_ = StructDefinition('mock', []) output = IntegerDefinition() return MethodDefinition(method_id, input_, output, [])
from vmware.vapi.security.oauth import OAUTH_SCHEME_ID, ACCESS_TOKEN from vmware.vapi.security.session import SESSION_SCHEME_ID, SESSION_ID from vmware.vapi.security.sso import SAML_SCHEME_ID, SAML_BEARER_SCHEME_ID, SAML_TOKEN, PRIVATE_KEY from vmware.vapi.security.user_identity import UserIdentity from vmware.vapi.lib.constants import SCHEME_ID from vmware.vapi.settings.config import ProviderConfig from vmware.vapi.provider.local import LocalProvider DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'resources') mock_interface_id = InterfaceIdentifier('mock') mock_method_id = MethodIdentifier(mock_interface_id, 'mock') mock_interface_def = InterfaceDefinition(mock_interface_id, [mock_method_id]) mock_method_def = MethodDefinition( mock_method_id, StructDefinition('input', [('input', VoidDefinition())]), IntegerDefinition(), []) errors = [ 'com.vmware.vapi.std.errors.internal_server_error', 'com.vmware.vapi.std.errors.invalid_argument', 'com.vmware.vapi.std.errors.operation_not_found', 'com.vmware.vapi.std.errors.unauthenticated', ] 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]) logging.basicConfig(level=logging.INFO) class MockupApiInterface(ApiInterface):