Esempio n. 1
0
    def list_resource(self,
                      context,
                      resource_type,
                      method_variant='list',
                      item_list_transform=None,
                      **kwargs):
        """List the contents of the specified resource.

    Args:
      resource_type: [string] The name of the resource to list.
      method_variant: [string] The API method name to invoke.
      item_list_transform: [lambda items] Converts the list of items into
         a result list, or None for the identity.
      kwargs: [kwargs] Additional parameters may be required depending
         on the resource type (such as zone, etc).

    Returns:
      A list of resources.
    """
        return JournalLogger.execute_in_context(
            'List {}'.format(resource_type), lambda: self.__do_list_resource(
                context,
                resource_type,
                method_variant=method_variant,
                item_list_transform=item_list_transform,
                **kwargs))
Esempio n. 2
0
    def invoke_resource(self,
                        context,
                        method,
                        resource_type,
                        resource_id=None,
                        **kwargs):
        """Invoke a method on a resource type or instance.

    Args:
      method: [string] The operation to perform as named under the
          |resource_type| in the discovery document.
      resource_type: [string] The type of the resource instance to operate on.
      resource_id: [string] The id of the resource instance, or None to operate
        on the resource type or collection.
      kwargs: [kwargs] Additional parameters may be required depending
         on the resource type and method.
    """
        return JournalLogger.execute_in_context(
            'Invoke "{method}" {type}'.format(method=method,
                                              type=resource_type),
            lambda: self.__do_invoke_resource(context,
                                              method,
                                              resource_type,
                                              resource_id=resource_id,
                                              **kwargs))
Esempio n. 3
0
    def call_method(self, method, context, *pos_args, **kwargs):
        """Invokes method and returns result.

    This is a wrapper around calling the method that will log the
    call and result.

    Args:
      method: callable method to invoke with *pos_args and **kwargs.
      context: [ExecutionContext]
      pos_args: [list]  positional arguments to pass to method.
      kwargs: [kwargs]to pass to method.
      _citest_log
    Raises:
      Exceptions thrown by method
    Returns:
      result of method
    """
        if hasattr(method, 'im_class'):
            method_name = '{0}.{1}'.format(method.im_class.__name__,
                                           method.im_func.__name__)
        else:
            method_name = str(method)

        return JournalLogger.execute_in_context(
            'Call {0}'.format(method_name), lambda: self.__do_call_method(
                method_name, method, context, *pos_args, **kwargs))
Esempio n. 4
0
  def call_method(self, method, context, *pos_args, **kwargs):
    """Invokes method and returns result.

    This is a wrapper around calling the method that will log the
    call and result.

    Args:
      method: callable method to invoke with *pos_args and **kwargs.
      context: [ExecutionContext]
      pos_args: [list]  positional arguments to pass to method.
      kwargs: [kwargs]to pass to method.
      _citest_log
    Raises:
      Exceptions thrown by method
    Returns:
      result of method
    """
    if hasattr(method, 'im_class'):
      method_name = '{0}.{1}'.format(method.im_class.__name__,
                                     method.im_func.__name__)
    else:
      method_name = str(method)

    return JournalLogger.execute_in_context(
        'Call {0}'.format(method_name),
        lambda: self.__do_call_method(
            method_name, method, context, *pos_args, **kwargs))
Esempio n. 5
0
  def test_context_logging(self):
      offset = len(_journal_file.getvalue())
      logger = JournalLogger('test_journal_logger')
      logger.addHandler(JournalLogHandler(path=None))
      citest_extra = {'foo':'bar'}
      start_time = _journal_clock.last_time

      JournalLogger.execute_in_context(
        'The Test Context',
        lambda: {logger.debug('Test Log Message')},
        **citest_extra)

      expect_sequence = [
        {
          '_title': 'The Test Context',
          '_type': 'JournalContextControl',
          '_timestamp': start_time + 1,
          '_thread': current_thread().ident,
          'control': 'BEGIN',
          'foo': 'bar',
        },
        {
          '_value': 'Test Log Message',
          '_type': 'JournalMessage',
          '_level': logging.DEBUG,
          '_timestamp': start_time + 2,
          '_thread': current_thread().ident,
          'format': 'pre'
        },
        {
          '_type': 'JournalContextControl',
          '_timestamp': start_time + 3,
          '_thread': current_thread().ident,
          'control': 'END'
        }
      ]

      entry_str = _journal_file.getvalue()[offset:]
      input_stream = RecordInputStream(BytesIO(entry_str))
      for expect in expect_sequence:
        json_str = next(input_stream)
        json_dict = json_module.JSONDecoder().decode(json_str)
        self.assertEqual(expect, json_dict)
Esempio n. 6
0
  def invoke_resource(self, context, method, resource_type,
                      resource_id=None, **kwargs):
    """Invoke a method on a resource type or instance.

    Args:
      method: [string] The operation to perform as named under the
          |resource_type| in the discovery document.
      resource_type: [string] The type of the resource instance to operate on.
      resource_id: [string] The id of the resource instance, or None to operate
        on the resource type or collection.
      kwargs: [kwargs] Additional parameters may be required depending
         on the resource type and method.
    """
    return JournalLogger.execute_in_context(
        'Invoke "{method}" {type}'.format(method=method, type=resource_type),
        lambda: self.__do_invoke_resource(
            context, method, resource_type,
            resource_id=resource_id, **kwargs))
Esempio n. 7
0
  def list_resource(self, context, resource_type, method_variant='list',
                    item_list_transform=None, **kwargs):
    """List the contents of the specified resource.

    Args:
      resource_type: [string] The name of the resource to list.
      method_variant: [string] The API method name to invoke.
      item_list_transform: [lambda items] Converts the list of items into
         a result list, or None for the identity.
      kwargs: [kwargs] Additional parameters may be required depending
         on the resource type (such as zone, etc).

    Returns:
      A list of resources.
    """
    return JournalLogger.execute_in_context(
        'List {}'.format(resource_type),
        lambda: self.__do_list_resource(
            context, resource_type, method_variant=method_variant,
            item_list_transform=item_list_transform, **kwargs))