class Dynamocli::Erase

Constants

LOGGER

Attributes

stack_resources[R]
table_name[R]
table_on_aws[R]

Public Class Methods

new(table_name:, with_drift: false) click to toggle source
# File lib/dynamocli/erase.rb, line 12
def initialize(table_name:, with_drift: false)
  @with_drift = with_drift
  @table_name = table_name

  @dynamodb = Aws::DynamoDB::Client.new
  @cloudformation = Aws::CloudFormation::Client.new
  @table_on_aws = Aws::DynamoDB::Table.new(@table_name)

  @stack_resources = @cloudformation.describe_stack_resources(physical_resource_id: @table_name).to_h
rescue Aws::CloudFormation::Errors::ValidationError
  @stack_resources = nil
end

Public Instance Methods

start() click to toggle source
# File lib/dynamocli/erase.rb, line 25
def start
  erase_table
rescue Aws::CloudFormation::Errors::ValidationError,
       Aws::DynamoDB::Errors::ValidationException,
       Aws::DynamoDB::Errors::ResourceNotFoundException => e
  LOGGER.error(e.message)
  exit(42)
end

Private Instance Methods

abort_message() click to toggle source
# File lib/dynamocli/erase.rb, line 60
def abort_message
  "Erase of #{@table_name} table canceled"
end
check_if_user_wants_to_continue() click to toggle source
# File lib/dynamocli/erase.rb, line 46
def check_if_user_wants_to_continue
  LOGGER.warn(
    "#{dynamocli_table.alert_message_before_continue} " \
    "Do you really want to continue?"
  )
  STDOUT.print("(anything other than 'y' will cancel) > ")

  confirmation = STDIN.gets.strip
  return if confirmation == "y"

  LOGGER.info(abort_message)
  exit(0)
end
dynamocli_table() click to toggle source
# File lib/dynamocli/erase.rb, line 64
def dynamocli_table
  @dynamocli_table ||=
    if stack_resources.nil? || with_drift?
      table = Dynamocli::AWS::Table.new(table_name: table_name, table_on_aws: table_on_aws)
      Dynamocli::Table::StandaloneTable.new(table_name: table_name, table: table)
    else
      stack = Dynamocli::AWS::Stack.new(table_name: table_name, table_resource: table_resource)
      Dynamocli::Table::CloudformationTable.new(table_name: table_name, stack: stack)
    end
end
erase_table() click to toggle source
# File lib/dynamocli/erase.rb, line 41
def erase_table
  check_if_user_wants_to_continue
  dynamocli_table.erase
end
table_resource() click to toggle source
# File lib/dynamocli/erase.rb, line 79
def table_resource
  @table_resource ||= stack_resources[:stack_resources].find do |resource|
    resource[:physical_resource_id] == @table_name
  end
end
with_drift?() click to toggle source
# File lib/dynamocli/erase.rb, line 75
def with_drift?
  @with_drift
end