Exemplo n.º 1
0
 def __init__(self, config):
     operations = {
         'echo': {
             'input_type': StructType('echoInput',
                                      {'message': StringType()}),
             'output_type': StringType(),
             'errors': {},
             'input_value_validator_list': [],
             'output_validator_list': [],
             'task_type': TaskType.NONE,
         },
         'invalid': {
             'input_type': StructType('invalidInput', {}),
             'output_type': StringType(),
             'errors': {},
             'input_value_validator_list': [],
             'output_validator_list': [],
             'task_type': TaskType.NONE,
         }
     }
     rest_metadata = {
         'echo':
         OperationRestMetadata(http_method='POST',
                               url_template='/test/echo?action=echo'),
         'invalid':
         OperationRestMetadata(http_method='POST',
                               url_template='/test/echo?action=invalid')
     }
     ApiInterfaceStub.__init__(self,
                               iface_name=self.interface_id,
                               config=config,
                               operations=operations,
                               rest_metadata=rest_metadata,
                               is_vapi_rest=False)
Exemplo n.º 2
0
 def _override_api_info(self, published_info):
     """
     Override service and operation task info fields
     """
     self.info.service = TypeConverter.convert_to_python(
         published_info.get_field('service'), StringType())
     self.info.operation = TypeConverter.convert_to_python(
         published_info.get_field('operation'), StringType())
Exemplo n.º 3
0
 def test_check_for_unknown_fields_invalid_3(self):
     list_info_type = ListType(StructType(
         'info',
         {
             'basic': StructType(
                 'basic',
                 {
                     'name': StringType()
                 }
             ),
             'address': StructType(
                 'address',
                 {
                     'city': StringType()
                 }
             ),
         }
     ))
     list_info_value = [
         StructValue(
             name='info',
             values={
                 'basic': StructValue(
                     name='basic',
                     values={
                         'name': 'foo',
                     }
                 ),
                 'address': StructValue(
                     name='address',
                     values={
                         'city': 'foo'
                     }
                 )
             }
         ),
         StructValue(
             name='info',
             values={
                 'basic': StructValue(
                     name='basic',
                     values={
                         'name': 'foo',
                         'extra': 'bar' # extra field in 2nd element of the list
                     }
                 ),
                 'address': StructValue(
                     name='address',
                     values={
                         'city': 'foo'
                     }
                 )
             }
         )
     ]
     self.assertIsNotNone(
         ApiInterfaceSkeleton.check_for_unknown_fields(
             list_info_type, list_info_value))
Exemplo n.º 4
0
 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')
Exemplo n.º 5
0
 def __init__(self, impl, error_types):
     operations = {}
     input_type = StructType(OPERATION_INPUT, {'message': StringType(),
                   'throw': BooleanType()})
     localizable_message_type = StructType(
         'com.vmware.vapi.std.localizable_message',
         {
             'id': StringType(),
             'default_message': StringType(),
             'args': ListType(StringType())
         })
     messages_list_type = ListType(localizable_message_type)
     data_optional_type = OptionalType(
         DynamicStructType('vmware.vapi.dynamic_struct', {}, VapiStruct))
     error = ErrorType('com.vmware.vapi.std.errors.not_found',
                       {
                           'messages': messages_list_type,
                           'data': data_optional_type
                       })
     operations[method_name] = {
         'input_type': input_type,
         'output_type': StringType(),
         'errors': [error],
         'input_value_validator_list': [],
         'output_validator_list': [],
         'task_type': TaskType.NONE,
     }
     operations[task_method_name] = {
         'input_type': input_type,
         'output_type': StringType(),
         'errors': [error],
         'input_value_validator_list': [],
         'output_validator_list': [],
         'task_type': TaskType.TASK,
     }
     operations['%s$task' % task_method_name] = {
         'input_type': input_type,
         'output_type': IdType(),
         'errors': [error],
         'input_value_validator_list': [],
         'output_validator_list': [],
         'task_type': TaskType.TASK,
     }
     operations[application_context_method_name] = {
         'input_type': StructType(OPERATION_INPUT, {}),
         'output_type': StringType(),
         'errors': [error],
         'input_value_validator_list': [],
         'output_validator_list': [],
         'task_type': TaskType.NONE,
     }
     ApiInterfaceSkeleton.__init__(self,
                                   iface_name=interface_name,
                                   impl=impl,
                                   operations=operations,
                                   error_types=error_types)
Exemplo n.º 6
0
 def test_check_for_unknown_fields_invalid_2(self):
     info_type = StructType(
         'info',
         {
             'basic': StructType(
                 'basic',
                 {
                     'name': StringType()
                 }
             ),
             'address': StructType(
                 'address',
                 {
                     'city': StringType()
                 }
             ),
         }
     )
     info_value = StructValue(
         name='info',
         values={
             'basic': StructValue(
                 name='basic',
                 values={
                     'name': 'foo',
                     'extra': 'bar' # extra field
                 }
             ),
             'address': StructValue(
                 name='address',
                 values={
                     'city': 'foo'
                 }
             )
         }
     )
     self.assertIsNotNone(
         ApiInterfaceSkeleton.check_for_unknown_fields(
             info_type, info_value))
Exemplo n.º 7
0
 def _initialize_common_info(self):
     """
     Initialize common task info fields
     """
     published_info = self.task_manager.get_info(self.task_id)
     self._override_api_info(published_info)
     self.info.cancelable = TypeConverter.convert_to_python(
         published_info.get_field('cancelable'), BooleanType())
     desc = published_info.get_field('description')
     if desc is not None:
         self.info.description = TypeConverter.convert_to_python(
             desc, LocalizableMessage.get_binding_type())
     self.info.status = TypeConverter.convert_to_python(
         published_info.get_field('status'), StringType())
Exemplo n.º 8
0
    def _initialize_common_info(self):
        """
        Initialize common task info fields
        """
        summary = self.task_manager.get_summary(self.task_id)
        published_info = summary.info
        self.error_types = summary.errors
        self._override_api_info(published_info)
        self.info.cancelable = TypeConverter.convert_to_python(
            published_info.get_field('cancelable'), BooleanType())
        desc = published_info.get_field('description')
        if desc is not None:
            self.info.description = TypeConverter.convert_to_python(
                desc, LocalizableMessage.get_binding_type())
        self.info.status = TypeConverter.convert_to_python(
            published_info.get_field('status'), StringType())

        try:
            user = published_info.get_field('user')
            self.info.user = TypeConverter.convert_to_python(
                user, StringType())
        except:  # pylint: disable=W0702
            pass
Exemplo n.º 9
0
class TopLevelStruct(VapiStruct):
    def __init__(self, **kwargs):
        self.dbl1 = kwargs.get('dbl1')
        VapiStruct.__init__(self)

    class NestedStruct(VapiStruct):
        def __init__(self, **kwargs):
            self.str1 = kwargs.get('str1')
            VapiStruct.__init__(self)

    NestedStruct._set_binding_type(
        StructType('nested_struct', {
            'str1': StringType(),
        }, NestedStruct))
Exemplo n.º 10
0
    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))
Exemplo n.º 11
0
 def test_check_for_unknown_fields(self):
     struct_type = StructType(
         'test',
         {
             'name': StringType()
         }
     )
     struct_value = StructValue(
         name='test',
         values={
             'name': StringValue('hello')
         }
     )
     self.assertIsNone(
         ApiInterfaceSkeleton.check_for_unknown_fields(
             struct_type, struct_value))
Exemplo n.º 12
0
 def test_check_for_unknown_fields_2(self):
     optional_struct_type = OptionalType(StructType(
         'test',
         {
             'name': StringType()
         }
     ))
     optional_struct_value = OptionalValue(StructValue(
         name='test',
         values={
             'name': StringValue('hello')
         }
     ))
     self.assertIsNone(
         ApiInterfaceSkeleton.check_for_unknown_fields(
             optional_struct_type,
             optional_struct_value))
Exemplo n.º 13
0
 def setUpClass(cls):
     cls.task_manager = get_task_manager()
     msg_factory = MessageFactory(cls.msgs)
     cls.description = LocalizableMessage(id='msg1',
                                          default_message='task1',
                                          args=[])
     cls.progress_msg = LocalizableMessage(id='prog1',
                                           default_message='progress',
                                           args=[])
     cls.task_id = cls.task_manager.create_task(str(uuid.uuid4()),
                                                cls.description,
                                                'test.service', 'op1', True)
     app_ctx = create_default_application_context()
     app_ctx[TASK_ID] = cls.task_id
     sec_ctx = SecurityContext()
     TLS.ctx = ExecutionContext(app_ctx, sec_ctx)
     cls.task_handle = get_task_handle(Info, StringType())
Exemplo n.º 14
0
 def __init__(self, config):
     operations = {
         'echo': {
             'input_type': StructType('echoInput',
                                      {'message': StringType()}),
             'output_type': StringType(),
             'errors': {},
             'input_value_validator_list': [],
             'output_validator_list': [],
             'task_type': TaskType.NONE,
         },
         'echo_long_running': {
             'input_type': StructType('echoInput',
                                      {'message': StringType()}),
             'output_type': StringType(),
             'errors': {},
             'input_value_validator_list': [],
             'output_validator_list': [],
             'task_type': TaskType.TASK,
         },
         'echo_long_running$task': {
             'input_type': StructType('echoInput',
                                      {'message': StringType()}),
             'output_type': IdType(resource_types='com.vmware.cis.TASK'),
             'errors': {},
             'input_value_validator_list': [],
             'output_validator_list': [],
             'task_type': TaskType.TASK,
         },
         'invalid': {
             'input_type': StructType('invalidInput', {}),
             'output_type': StringType(),
             'errors': {},
             'input_value_validator_list': [],
             'output_validator_list': [],
             'task_type': TaskType.NONE,
         }
     }
     ApiInterfaceStub.__init__(self,
                               iface_name=self.interface_id,
                               config=config,
                               operations=operations)
Exemplo n.º 15
0

class ReferenceLocalRecursive(VapiStruct):
    # class LinkedList {
    #     String data;
    #     Optional<LinkedList> nextNode;
    # }
    def __init__(self, **kwargs):
        self.data = kwargs.get('data')
        self.next_node = kwargs.get('next_node')


ReferenceLocalRecursive._set_binding_type(
    StructType(
        'linked_list', {
            'data': StringType(),
            'next_node': ReferenceType(__name__, 'ReferenceLocalRecursive')
        }))


class MultipleStructReferences(VapiStruct):
    # class MultipleStructReferences {
    #     String string_field;
    #     TopLevelStruct structure_field1;
    #      TopLevelStruct structure_field2;
    # }
    def __init__(self, **kwargs):
        self.name = kwargs.get('string_field')
        self.structure_field1 = kwargs.get('structure_field1')
        self.structure_field2 = kwargs.get('structure_field2')
Exemplo n.º 16
0
 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')
Exemplo n.º 17
0
 def test_string_type(self):
     s_type = StringType()
     self.assertNotEqual(s_type, None)
     self.assertEqual(s_type.definition, StringDefinition())
Exemplo n.º 18
0
                 list_val=None,
                 nested_val=None):
        self.int_val = int_val
        self.str_val = str_val
        self.bool_val = bool_val
        self.opt_val = opt_val
        self.list_val = list_val
        self.nested_val = nested_val
        VapiStruct.__init__(self)


Properties._set_binding_type(
    StructType(
        'properties', {
            'int_val': IntegerType(),
            'str_val': StringType(),
            'bool_val': BooleanType(),
            'opt_val': OptionalType(IntegerType()),
            'list_val': ListType(IntegerType()),
            'nested_val': ReferenceType(__name__, 'NestedProperties')
        }, Properties))


class TestVapiStruct(unittest.TestCase):
    def setUp(self):
        logging.basicConfig(level=logging.INFO)

    def test_dict_conversion(self):
        nested_val = NestedProperties(int_val=10)
        py_val = Properties(int_val=10,
                            str_val='testing',