class Dyna::Client

Public Class Methods

new(options = {}) click to toggle source
# File lib/dyna/client.rb, line 6
def initialize(options = {})
  @options = OpenStruct.new(options)
  @options_hash = options
  if @options.use_local
    @options.ddb = Aws::DynamoDB::Client.new(
      endpoint: @options.dynamo_endpoint || 'http://127.0.0.1:8000',
      region: @options.region || 'ap-northeast-1',
      access_key_id: @options.access_key_id || 'dummy',
      secret_access_key: @options.secret_access_key || 'dummy',
    )
  else
    @options.ddb = Aws::DynamoDB::Client.new
    @options.aas = Aws::ApplicationAutoScaling::Client.new
    Exporter.aas(@options.aas)
  end
end

Public Instance Methods

apply(file) click to toggle source
# File lib/dyna/client.rb, line 23
def apply(file)
  walk(file)
end
export(options = {}) { |exported, converter| ... } click to toggle source
# File lib/dyna/client.rb, line 27
def export(options = {})
  exported = Exporter.export(@options.ddb, @options)

  converter = proc do |src|
    DSL.convert(@options.ddb.config.region, src)
  end

  if block_given?
    yield(exported, converter)
  else
    converter.call(exported)
  end
end

Private Instance Methods

load_file(file) click to toggle source
# File lib/dyna/client.rb, line 42
def load_file(file)
  if file.kind_of?(String)
    open(file) do |f|
      parse(f.read, file)
    end
  elsif file.respond_to?(:read)
    parse(file.read, file.path)
  else
    raise TypeError, "can't load #{file}"
  end
end
parse(src, path) click to toggle source
# File lib/dyna/client.rb, line 54
def parse(src, path)
  DSL.define(src, path).result
end
walk(file) click to toggle source
# File lib/dyna/client.rb, line 58
def walk(file)
  dsl = load_file(file)
  dsl_ddbs = dsl.ddbs
  ddb_wrapper = DynamoDBWrapper.new(@options.ddb, @options)

  dsl_ddbs.each do |region, ddb_dsl|
    walk_ddb(ddb_dsl, ddb_wrapper) if @options.ddb.config.region == region
  end

  ddb_wrapper.updated?
end
walk_ddb(ddb_dsl, ddb_wrapper) click to toggle source
# File lib/dyna/client.rb, line 70
def walk_ddb(ddb_dsl, ddb_wrapper)
  table_list_dsl = ddb_dsl.tables.group_by(&:table_name).each_with_object({}) do |(k, v), h|
    h[k] = v.first unless should_skip(k)
  end
  table_list_aws = ddb_wrapper.tables.group_by(&:table_name).each_with_object({}) do |(k, v), h|
    h[k] = v.first unless should_skip(k)
  end

  table_list_dsl.each do |name, table_dsl|
    unless table_list_aws[name]
      result = ddb_wrapper.create(table_dsl)
      if result
        table_list_aws[name] = DynamoDBWrapper::Table.new(
          @options.ddb,
          result.table_description,
          @options,
        )
      end
    end
  end

  table_list_dsl.each do |name, table_dsl|
    table_aws = table_list_aws.delete(name)
    next unless table_aws # only dry-run and should be created
    table_aws.update(table_dsl) unless table_aws.eql?(table_dsl)
  end

  table_list_aws.values.each(&:delete)
end