def Run(self, args):
    """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The created sink with its destination.
    """
    util.CheckSinksCommandArguments(args)

    if not (args.log or args.service or args.log_filter):
      # Attempt to create a project sink with an empty filter.
      if not console_io.PromptContinue(
          'Sink with empty filter matches all entries in the project.'):
        raise exceptions.ToolException('action canceled by user')

    sink_ref = self.context['sink_reference']
    sink_data = {'name': sink_ref.sinksId, 'destination': args.destination,
                 'filter': args.log_filter}

    if args.log:
      result = util.TypedLogSink(self.CreateLogSink(sink_data),
                                 log_name=args.log)
    elif args.service:
      result = util.TypedLogSink(self.CreateLogServiceSink(sink_data),
                                 service_name=args.service)
    else:
      sink_data['outputVersionFormat'] = args.output_version_format
      result = util.TypedLogSink(self.CreateProjectSink(sink_data))
    log.CreatedResource(sink_ref)
    return result
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The specified sink with its destination.
    """
        try:
            if args.log:
                return util.TypedLogSink(self.GetLogSink(), log_name=args.log)
            elif args.service:
                return util.TypedLogSink(self.GetLogServiceSink(),
                                         service_name=args.service)
            else:
                return util.TypedLogSink(self.GetProjectSink())
        except apitools_exceptions.HttpError as error:
            project_sink = not args.log and not args.service
            # Suggest the user to add --log or --log-service flag.
            if project_sink and exceptions.HttpException(
                    error).payload.status_code == 404:
                log.status.Print(
                    ('Project sink was not found. '
                     'Did you forget to add --log or --log-service flag?'))
            raise error
예제 #3
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The specified sink with its destination.
    """
        util.CheckLegacySinksCommandArguments(args)

        sink_ref = util.GetSinkReference(args.sink_name, args.log,
                                         args.service)

        try:
            if args.log:
                return util.TypedLogSink(self.GetLogSink(sink_ref),
                                         log_name=args.log)
            elif args.service:
                return util.TypedLogSink(self.GetLogServiceSink(sink_ref),
                                         service_name=args.service)
            else:
                return util.TypedLogSink(
                    self.GetSink(util.GetParentFromArgs(args), sink_ref))
        except apitools_exceptions.HttpError as error:
            v2_sink = not args.log and not args.service
            # Suggest the user to add --log or --log-service flag.
            if v2_sink and exceptions.HttpException(
                    error).payload.status_code == 404:
                log.status.Print(
                    ('Sink was not found. '
                     'Did you forget to add --log or --log-service flag?'))
            raise error
예제 #4
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The created sink with its destination.
    """
        if not args.log_filter:
            # Attempt to create a sink with an empty filter.
            console_io.PromptContinue(
                'Sink with empty filter matches all entries.',
                cancel_on_no=True)

        sink_ref = resources.REGISTRY.Parse(
            args.sink_name,
            params={'projectsId': properties.VALUES.core.project.GetOrFail},
            collection='logging.projects.sinks')

        sink_data = {
            'name': sink_ref.sinksId,
            'destination': args.destination,
            'filter': args.log_filter
        }

        result = util.TypedLogSink(
            self.CreateSink(util.GetParentFromArgs(args), sink_data))

        log.CreatedResource(sink_ref)
        self._epilog_result_destination = result.destination
        self._writer_identity = result.writer_identity
        return result
예제 #5
0
 def ListLogServiceSinks(self, project, service_name):
     """List log service sinks from the specified service."""
     result = util.GetClientV1().projects_logServices_sinks.List(
         util.GetMessagesV1().LoggingProjectsLogServicesSinksListRequest(
             projectsId=project, logServicesId=service_name))
     for sink in result.sinks:
         yield util.TypedLogSink(sink, service_name=service_name)
예제 #6
0
 def ListSinks(self, parent):
     """List sinks."""
     # Use V2 logging API.
     result = util.GetClient().projects_sinks.List(
         util.GetMessages().LoggingProjectsSinksListRequest(parent=parent))
     for sink in result.sinks:
         yield util.TypedLogSink(sink)
예제 #7
0
 def ListProjectSinks(self, project):
   """List project sinks from the specified project."""
   # Use V2 logging API for project sinks.
   result = util.GetClient().projects_sinks.List(
       util.GetMessages().LoggingProjectsSinksListRequest(
           parent='projects/{0}'.format(project)))
   for sink in result.sinks:
     yield util.TypedLogSink(sink)
예제 #8
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The specified sink with its destination.
    """
        if args.log:
            return util.TypedLogSink(self.GetLogSink(), log_name=args.log)
        elif args.service:
            return util.TypedLogSink(self.GetLogServiceSink(),
                                     service_name=args.service)
        else:
            return util.TypedLogSink(self.GetProjectSink())
예제 #9
0
 def ListLogServiceSinks(self, project, service_name):
     """List log service sinks from the specified service."""
     client = self.context['logging_client_v1beta3']
     messages = self.context['logging_messages_v1beta3']
     result = client.projects_logServices_sinks.List(
         messages.LoggingProjectsLogServicesSinksListRequest(
             projectsId=project, logServicesId=service_name))
     for sink in result.sinks:
         yield util.TypedLogSink(sink, service_name=service_name)
예제 #10
0
 def ListProjectSinks(self, project):
     """List project sinks from the specified project."""
     # Use V2 logging API for project sinks.
     client = self.context['logging_client_v2beta1']
     messages = self.context['logging_messages_v2beta1']
     result = client.projects_sinks.List(
         messages.LoggingProjectsSinksListRequest(projectsId=project))
     for sink in result.sinks:
         yield util.TypedLogSink(sink)
 def ListLogSinks(self, project, log_name, limit):
     """List log sinks from the specified log."""
     client = self.context['logging_client_v1beta3']
     messages = self.context['logging_messages_v1beta3']
     result = client.projects_logs_sinks.List(
         messages.LoggingProjectsLogsSinksListRequest(projectsId=project,
                                                      logsId=log_name))
     for sink in result.sinks:
         yield util.TypedLogSink(sink, log_name=log_name)
         limit -= 1
         if not limit:
             return
예제 #12
0
  def Run(self, args):
    """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The created sink with its destination.
    """
    util.CheckSinksCommandArguments(args)

    if not args.unique_writer_identity:
      log.warn(
          '--unique-writer-identity is deprecated and will soon be removed.')

    if not (args.log or args.service or args.log_filter):
      # Attempt to create a sink with an empty filter.
      console_io.PromptContinue(
          'Sink with empty filter matches all entries.', cancel_on_no=True)

    sink_ref = self.context['sink_reference']
    sink_data = {'name': sink_ref.sinksId, 'destination': args.destination,
                 'filter': args.log_filter}

    if args.log:
      result = util.TypedLogSink(self.CreateLogSink(sink_data),
                                 log_name=args.log)
    elif args.service:
      result = util.TypedLogSink(self.CreateLogServiceSink(sink_data),
                                 service_name=args.service)
    else:
      sink_data['outputVersionFormat'] = args.output_version_format
      result = util.TypedLogSink(
          self.CreateSink(util.GetParentFromArgs(args), sink_data,
                          args.unique_writer_identity))
    log.CreatedResource(sink_ref)
    self._epilog_result_destination = result.destination
    self._writer_identity = result.writer_identity
    return result
예제 #13
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The created sink with its destination.
    """
        if not args.unique_writer_identity:
            log.warn(
                '--unique-writer-identity is deprecated and will soon be removed.'
            )

        if not args.log_filter:
            # Attempt to create a sink with an empty filter.
            console_io.PromptContinue(
                'Sink with empty filter matches all entries.',
                cancel_on_no=True)

        sink_ref = resources.REGISTRY.Parse(
            args.sink_name, collection='logging.projects.sinks')

        sink_data = {
            'name': sink_ref.sinksId,
            'destination': args.destination,
            'filter': args.log_filter,
            'outputVersionFormat': args.output_version_format
        }

        result = util.TypedLogSink(
            self.CreateSink(util.GetParentFromArgs(args), sink_data,
                            args.unique_writer_identity))

        log.CreatedResource(sink_ref)
        self._epilog_result_destination = result.destination
        self._writer_identity = result.writer_identity
        return result
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The created sink with its destination.
    """
        if not args.log_filter:
            # Attempt to create a sink with an empty filter.
            console_io.PromptContinue(
                'Sink with empty filter matches all entries.',
                cancel_on_no=True)

        if args.include_children and not (args.organization or args.folder):
            log.warn(
                'include-children only has an effect for sinks at the folder '
                'or organization level')

        sink_ref = util.GetSinkReference(args.sink_name, None, None, args)

        sink_data = {
            'name': sink_ref.sinksId,
            'destination': args.destination,
            'filter': args.log_filter,
            'includeChildren': args.include_children
        }

        result = util.TypedLogSink(
            self.CreateSink(util.GetParentFromArgs(args), sink_data))

        log.CreatedResource(sink_ref)
        self._epilog_result_destination = result.destination
        self._writer_identity = result.writer_identity
        return result
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The updated sink with its new destination.
    """
        util.CheckSinksCommandArguments(args)

        # One of the flags is required to update the sink.
        # log_filter can be an empty string, so check explicitly for None.
        if not (args.destination or args.log_filter is not None
                or args.output_version_format):
            raise exceptions.ToolException(
                '[destination], --log-filter or --output-version-format is required'
            )

        # Calling Update on a non-existing sink creates it.
        # We need to make sure it exists, otherwise we would create it.
        if args.log:
            sink = self.GetLogSink()
        elif args.service:
            sink = self.GetLogServiceSink()
        else:
            sink = self.GetProjectSink()

        # Only update fields that were passed to the command.
        if args.destination:
            destination = args.destination
        else:
            destination = sink.destination

        if args.log_filter is not None:
            log_filter = args.log_filter
        else:
            log_filter = sink.filter

        sink_ref = self.context['sink_reference']
        sink_data = {
            'name': sink_ref.sinksId,
            'destination': destination,
            'filter': log_filter
        }

        if args.log:
            result = util.TypedLogSink(self.UpdateLogSink(sink_data),
                                       log_name=args.log)
        elif args.service:
            result = util.TypedLogSink(self.UpdateLogServiceSink(sink_data),
                                       service_name=args.service)
        else:
            if args.output_version_format:
                sink_data['outputVersionFormat'] = args.output_version_format
            else:
                sink_data[
                    'outputVersionFormat'] = sink.outputVersionFormat.name
            result = util.TypedLogSink(self.UpdateProjectSink(sink_data))
        log.UpdatedResource(sink_ref)
        return result
예제 #16
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The updated sink with its new destination.
    """
        if args.output_version_format:
            log.warn(
                '--output-version-format is deprecated and will soon be removed.'
            )

        # One of the flags is required to update the sink.
        # log_filter can be an empty string, so check explicitly for None.
        if not (args.destination or args.log_filter is not None
                or args.output_version_format):
            raise calliope_exceptions.ToolException(
                '[destination], --log-filter or --output-version-format is required'
            )

        sink_ref = resources.REGISTRY.Parse(
            args.sink_name,
            params={'projectsId': properties.VALUES.core.project.GetOrFail},
            collection='logging.projects.sinks')

        # Calling Update on a non-existing sink creates it.
        # We need to make sure it exists, otherwise we would create it.
        try:
            sink = self.GetSink(util.GetParentFromArgs(args), sink_ref)
        except apitools_exceptions.HttpError as error:
            if exceptions.HttpException(error).payload.status_code == 404:
                log.status.Print('Sink was not found.')
            raise error

        # Only update fields that were passed to the command.
        if args.destination:
            destination = args.destination
        else:
            destination = sink.destination

        if args.log_filter is not None:
            log_filter = args.log_filter
        else:
            log_filter = sink.filter

        if args.output_version_format:
            output_format = args.output_version_format
        else:
            output_format = sink.outputVersionFormat.name

        sink_data = {
            'name': sink_ref.sinksId,
            'destination': destination,
            'filter': log_filter,
            'outputVersionFormat': output_format,
            'includeChildren': sink.includeChildren,
            'startTime': sink.startTime,
            'endTime': sink.endTime
        }

        # Check for legacy configuration, and let users decide if they still want
        # to update the sink with new settings.
        if 'cloud-logs@' in sink.writerIdentity:
            console_io.PromptContinue(
                'This update will create a new writerIdentity (service account) for '
                'the sink. In order for the sink to continue working, grant that '
                'service account correct permission on the destination. The service '
                'account will be displayed after a successful update operation.',
                cancel_on_no=True,
                default=False)

        result = util.TypedLogSink(
            self.UpdateSink(util.GetParentFromArgs(args), sink_data))

        log.UpdatedResource(sink_ref, kind='sink')
        util.PrintPermissionInstructions(result.destination,
                                         result.writer_identity)
        return result
예제 #17
0
    def Run(self, args):
        """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The updated sink with its new destination.
    """
        util.CheckSinksCommandArguments(args)

        # One of the flags is required to update the sink.
        # log_filter can be an empty string, so check explicitly for None.
        if not (args.destination or args.log_filter is not None
                or args.output_version_format):
            raise calliope_exceptions.ToolException(
                '[destination], --log-filter or --output-version-format is required'
            )

        # Calling Update on a non-existing sink creates it.
        # We need to make sure it exists, otherwise we would create it.
        try:
            if args.log:
                sink = self.GetLogSink()
            elif args.service:
                sink = self.GetLogServiceSink()
            else:
                sink = self.GetProjectSink()
        except apitools_exceptions.HttpError as error:
            project_sink = not args.log and not args.service
            # Suggest the user to add --log or --log-service flag.
            if project_sink and exceptions.HttpException(
                    error).payload.status_code == 404:
                log.status.Print(
                    ('Project sink was not found. '
                     'Did you forget to add --log or --log-service flag?'))
            raise error

        # Only update fields that were passed to the command.
        if args.destination:
            destination = args.destination
        else:
            destination = sink.destination

        if args.log_filter is not None:
            log_filter = args.log_filter
        else:
            log_filter = sink.filter

        sink_ref = self.context['sink_reference']
        sink_data = {
            'name': sink_ref.sinksId,
            'destination': destination,
            'filter': log_filter
        }

        if args.log:
            result = util.TypedLogSink(self.UpdateLogSink(sink_data),
                                       log_name=args.log)
            kind = 'log sink'
        elif args.service:
            result = util.TypedLogSink(self.UpdateLogServiceSink(sink_data),
                                       service_name=args.service)
            kind = 'service log sink'
        else:
            if args.output_version_format:
                sink_data['outputVersionFormat'] = args.output_version_format
            else:
                sink_data[
                    'outputVersionFormat'] = sink.outputVersionFormat.name
            result = util.TypedLogSink(self.UpdateProjectSink(sink_data))
            kind = 'project sink'
        log.UpdatedResource(sink_ref, kind=kind)
        util.PrintPermissionInstructions(result.destination,
                                         result.writer_identity)
        return result
예제 #18
0
  def Run(self, args):
    """This is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      The updated sink with its new destination.
    """
    if not args.unique_writer_identity:
      log.warn(
          '--unique-writer-identity is deprecated and will soon be removed.')

    # One of the flags is required to update the sink.
    # log_filter can be an empty string, so check explicitly for None.
    if not (args.destination or args.log_filter is not None or
            args.output_version_format):
      raise calliope_exceptions.ToolException(
          '[destination], --log-filter or --output-version-format is required')

    sink_ref = resources.REGISTRY.Parse(
        args.sink_name, collection='logging.projects.sinks')

    # Calling Update on a non-existing sink creates it.
    # We need to make sure it exists, otherwise we would create it.
    try:
      sink = self.GetSink(util.GetParentFromArgs(args), sink_ref)
    except apitools_exceptions.HttpError as error:
      if exceptions.HttpException(error).payload.status_code == 404:
        log.status.Print('Sink was not found.')
      raise error

    # Only update fields that were passed to the command.
    if args.destination:
      destination = args.destination
    else:
      destination = sink.destination

    if args.log_filter is not None:
      log_filter = args.log_filter
    else:
      log_filter = sink.filter

    if args.output_version_format:
      output_format = args.output_version_format
    else:
      output_format = sink.outputVersionFormat.name

    sink_data = {
        'name': sink_ref.sinksId,
        'destination': destination,
        'filter': log_filter,
        'outputVersionFormat': output_format
    }

    result = util.TypedLogSink(
        self.UpdateSink(
            util.GetParentFromArgs(args), sink_data,
            args.unique_writer_identity))

    log.UpdatedResource(sink_ref, kind='sink')
    util.PrintPermissionInstructions(result.destination, result.writer_identity)
    return result