Sha256: e6d5bc665763113e18407ba64995ec5fd3a786e8e341747570a315309a4f1555

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

require_relative 'cloud_watch_logs'

module AwsLogCleaner

  # Responsible for cleaning log groups in AWS.
  class LogGroupCleanerCommands

    def initialize(cloud_watch_logs, api_retriever, log_group_filterer)
      @cloud_watch_logs = cloud_watch_logs
      @api_retriever = api_retriever
      @log_group_filterer = log_group_filterer
    end

    def plan(text)
      result(text)
    end

    def delete(text)
      @cloud_watch_logs.delete_log_groups(log_group_names_containing(text))
      result(text)
    end

    private

    def log_group_names_containing(text)
      if @log_group_names.nil?
        @log_group_names = []
        log_groups =
          @log_group_filterer.filter_by_name_includes(text)

        apis = @api_retriever.retrieve(text)
        apis.each do |api|
          log_groups.concat(
            @log_group_filterer.filter_by_name_includes(api.id.to_s)
          )
        end

        log_groups.each do |log_group|
          @log_group_names.push(log_group.log_group_name)
        end
      end

      @log_group_names
    end

    def result(text)
      to_delete = []
      log_group_names_containing(text).each do |name|
        to_delete.push("(-) #{name}")
      end
      to_delete
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aws_log_cleaner-0.1.7 lib/aws_log_cleaner/log_group_cleaner_commands.rb
aws_log_cleaner-0.1.6 lib/aws_log_cleaner/log_group_cleaner_commands.rb
aws_log_cleaner-0.1.5 lib/aws_log_cleaner/log_group_cleaner_commands.rb