Class: Stax::Cmd::DynamoDB

Inherits:
SubCommand show all
Defined in:
lib/stax/mixin/dynamodb.rb,
lib/stax/mixin/dynamodb/local.rb,
lib/stax/mixin/dynamodb/backup.rb,
lib/stax/mixin/dynamodb/throughput.rb

Constant Summary collapse

COLORS =
{
  CREATING:  :yellow,
  UPDATING:  :yellow,
  DELETING:  :red,
  ACTIVE:    :green,
  DELETED:   :red,
  AVAILABLE: :green,
}

Instance Method Summary collapse

Methods inherited from SubCommand

#info, stax_info, stax_info_tasks

Instance Method Details

#backup(id = nil) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/stax/mixin/dynamodb/backup.rb', line 24

def backup(id = nil)
  name = my.resource(id)
  if options[:create]
    create_backup(name, options[:create])
  else
    list_backups(name)
  end
end

#count(id) ⇒ Object



81
82
83
# File 'lib/stax/mixin/dynamodb.rb', line 81

def count(id)
  puts Aws::DynamoDB.count(table_name: my.resource(id))
end

#gsi(id) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/stax/mixin/dynamodb.rb', line 53

def gsi(id)
  print_table Aws::DynamoDB.gsi(my.resource(id)).map { |i|
    hash  = i.key_schema.find{ |k| k.key_type == 'HASH' }&.attribute_name
    range = i.key_schema.find{ |k| k.key_type == 'RANGE' }&.attribute_name
    [i.index_name, hash, range, i.projection.projection_type, i.index_size_bytes, i.item_count]
  }.sort
end

#keys(id) ⇒ Object



71
72
73
# File 'lib/stax/mixin/dynamodb.rb', line 71

def keys(id)
  print_table Aws::DynamoDB.keys(my.resource(id))
end

#local_createObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/stax/mixin/dynamodb/local.rb', line 64

def local_create
  tables = dynamo_local_tables
  tables.slice!(*options[:tables]) if options[:tables]

  tables.each do |id, value|
    payload = dynamo_payload_from_template(id, value)
    payload = my.dynamo_local_payload_hacks(id, payload) # apply user-supplied hacks
    if options[:payload]
      puts JSON.pretty_generate(payload)
    else
      puts "create table #{id}"
      dynamo_local_create(payload, options[:port])
    end
  end
end

#local_deleteObject



83
84
85
86
87
88
89
90
91
# File 'lib/stax/mixin/dynamodb/local.rb', line 83

def local_delete
  tables = dynamo_local_tables
  tables.slice!(*options[:tables]) if options[:tables]

  tables.each do |id,_value|
    puts "deleting table #{id}"
    client(options[:port]).delete_table(table_name: id)
  end
end

#lsi(id) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/stax/mixin/dynamodb.rb', line 62

def lsi(id)
  print_table Aws::DynamoDB.lsi(my.resource(id)).map { |i|
    hash  = i.key_schema.find{ |k| k.key_type == 'HASH' }&.attribute_name
    range = i.key_schema.find{ |k| k.key_type == 'RANGE' }&.attribute_name
    [i.index_name, hash, range, i.projection.projection_type, i.index_size_bytes, i.item_count]
  }.sort
end

#put(id) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/stax/mixin/dynamodb.rb', line 104

def put(id)
  table = my.resource(id)
  count = 0
  $stdin.each do |line|
    Aws::DynamoDB.put(table_name: table, item: JSON.parse(line))
    print '.' if options[:verbose]
    count += 1
  end
  print "\n" if options[:verbose]
  puts "put #{count} items to #{table}"
end

#query(id, hash_value, range_value = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/stax/mixin/dynamodb.rb', line 86

def query(id, hash_value, range_value = nil)
  name = my.resource(id)
  k = Aws::DynamoDB.keys(name)
  Aws::DynamoDB.query(
    table_name: name,
    expression_attribute_values: {
      ':h' => hash_value,
      ':r' => range_value,
    }.compact,
    key_condition_expression: [
      "#{k[:hash]} = :h",
      range_value ? "#{k[:range]} = :r" : nil,
    ].compact.join(' and '),
  )
end

#restore(arn, table) ⇒ Object



34
35
36
37
# File 'lib/stax/mixin/dynamodb/backup.rb', line 34

def restore(arn, table)
  debug("Creating table #{table} from backup #{arn}")
  Aws::DynamoDB.restore_backup(table, arn)
end

#scan(id) ⇒ Object



76
77
78
# File 'lib/stax/mixin/dynamodb.rb', line 76

def scan(id)
  Aws::DynamoDB.scan(table_name: my.resource(id))
end

#tablesObject



42
43
44
45
46
47
48
49
50
# File 'lib/stax/mixin/dynamodb.rb', line 42

def tables
  debug("Dynamo tables for stack #{my.stack_name}")
  print_table stack_tables.map { |r|
    t = Aws::DynamoDB.table(r.physical_resource_id)
    g = Aws::DynamoDB.global_table(r.physical_resource_id)
    regions = g.nil? ? '-' : g.replication_group.map(&:region_name).sort.join(',')
    [ t.table_name, color(t.table_status, COLORS), t.item_count, t.table_size_bytes, t.creation_date_time, regions ]
  }
end

#throughputObject



57
58
59
60
61
62
63
# File 'lib/stax/mixin/dynamodb/throughput.rb', line 57

def throughput
  if options[:write] || options[:read]
    update_throughput(options[:tables], options[:read], options[:write])
  else
    print_throughput(options[:tables])
  end
end